Subversion Repositories SmartDukaan

Rev

Rev 555 | Blame | Last modification | View Log | RSS feed

package in.shop2020.serving.controllers;

import in.shop2020.model.v1.order.Order;
import in.shop2020.serving.controllers.BaseController;
import in.shop2020.serving.pages.PageContentKeys;
import in.shop2020.serving.pages.PageEnum;
import in.shop2020.serving.pages.PageManager;
import in.shop2020.serving.services.PageLoaderHandler;
import in.shop2020.thrift.clients.TransactionServiceClient;
import in.shop2020.thrift.clients.UserContextServiceClient;

import java.util.*;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.thrift.TException;

@Results({
        @Result(name="redirect", location="${url}", type="redirect")
})
public class OrderController extends BaseController implements ParameterAware {
        
        private static final long serialVersionUID = 1L;
        
        private PageManager pageManager = null;
        private Map<String, String[]> reqparams;
        private String redirectUrl;
        private static Log log = LogFactory.getLog(OrderController.class);
        private String id;
        private Map<String,String> htmlSnippets;

        private String message;
        
        public OrderController(){
                super();
                pageManager = PageManager.getPageManager();
        }
        
    // GET /order/ orderid
    public String show() {
        log.info("id=" + id);
        if(!userinfo.isLoggedIn()){
                this.redirectUrl = "login";
                return "redirect";
        }
        
        Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
        
        params.put(PageContentKeys.ORDER_ID, id);
        params.put(PageContentKeys.USER_ID, new Long(userinfo.getUserId()).toString());
        params.put(PageContentKeys.CART_ID, new Long(userinfo.getCartId()).toString());
        params.put(PageContentKeys.USER_NAME, userinfo.getNameOfUser());
        params.put(PageContentKeys.ITEM_COUNT, new Long(userinfo.getTotalItems()).toString());
        
        htmlSnippets = pageManager.getPageContents(PageEnum.ORDER_DETAILS_PAGE, params);
        return "show";
    }

    // POST /order/
    public String create(){
        long addressId = Long.parseLong(this.request.getParameter("addressid"));
        long currentCartId = userinfo.getCartId();
        List<Order> orders = null;
                try {
                        UserContextServiceClient userServiceClient = new UserContextServiceClient();
                        in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
                        
                        userClient.addAddressToCart(currentCartId, addressId);
                        Long txn_id = userClient.commitCart(currentCartId);
                        
                        TransactionServiceClient tsc = new TransactionServiceClient();
                        orders = tsc.getClient().getOrdersForTransaction(txn_id);
                        
                        long newCartId = userClient.getUserById(userinfo.getUserId()).getActiveCartId();
                        userinfo.setCartId(newCartId);
                        userinfo.setTotalItems(0);
                } catch (TException e) {
                        e.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        
                this.message = "Your order numbers are as below:";
                
                for(Order order: orders){
                        this.message = this.message  + "<br> <a href=\"./order/" + order.getId() + "\">" + order.getId() + "</a>";
                }
                
                PageLoaderHandler pageLoader = new PageLoaderHandler();
                htmlSnippets = new HashMap<String, String>();
                htmlSnippets.put("HEADER", pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getNameOfUser()));
                htmlSnippets.put("MAIN_MENU", pageLoader.getMainMenuHtml());
                htmlSnippets.put("SEARCH_BAR", pageLoader.getSearchBarHtml(userinfo.getTotalItems(), 10000));
                htmlSnippets.put("CUSTOMER_SERVICE", pageLoader.getCustomerServiceHtml());
                htmlSnippets.put("FOOTER",pageLoader.getFooterHtml());
                
                return "success";
    }
    
        public String getId(){
                return id;
        }
        
        public void setId(String id){
                this.id = id;
        }
        
    @Override
        public void setParameters(Map<String, String[]> reqmap) {
                log.info("setParameters:" + reqmap);
                
                this.reqparams = reqmap;
        }
         
    public String getHeaderSnippet(){
                return htmlSnippets.get("HEADER");
        }
        
        public String getMainMenuSnippet(){
                return htmlSnippets.get("MAIN_MENU");
        }
        
        public String getSearchBarSnippet(){
                return htmlSnippets.get("SEARCH_BAR");
        }
                        
        
        public String getCustomerServiceSnippet(){
                return htmlSnippets.get("CUSTOMER_SERVICE");
        }
        
        public String getMyaccountHeaderSnippet(){
                return htmlSnippets.get("MYACCOUNT_HEADER");
        }
        
        public String getOrderDetailsSnippet(){
                return htmlSnippets.get("ORDER_DETAILS");
        }
        
        public String getMyResearchSnippet(){
                return htmlSnippets.get("MY_RESEARCH");
        }
                                
        public String getFooterSnippet(){
                return htmlSnippets.get("FOOTER");
        }
        
        public String getJsFileSnippet(){
                return htmlSnippets.get("JS_FILES");
        }
        
        public String getCssFileSnippet(){
                return htmlSnippets.get("CSS_FILES");
        }
        
        public String getMessage(){
                return this.message;
        }

}