Subversion Repositories SmartDukaan

Rev

Rev 555 | Rev 620 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/**
 * 
 */
package in.shop2020.serving.controllers;

import in.shop2020.model.v1.user.Address;
import in.shop2020.model.v1.user.User;
import in.shop2020.model.v1.user.UserState;
import in.shop2020.serving.pages.PageContentKeys;
import in.shop2020.serving.pages.PageEnum;
import in.shop2020.serving.pages.PageManager;
import in.shop2020.thrift.clients.UserContextServiceClient;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

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.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

/**
 * @author rajveer
 *
 */

public class CompletedordersController extends BaseController 
        implements ParameterAware {

        
        /**
         * 
         */
        private static Log log = LogFactory.getLog(CompletedordersController.class);
        
        private Map<String,String> htmlSnippets;
        /**
         * 
         */
        
        
        private Map<String, String[]> reqparams;
        
        private String id;
        
        private int action;
        
        
        public CompletedordersController() {
                super();
        }
        
    // POST /myaccount
    public String create() {
        log.info("CompletedordersController.create");
        log.info("action type is " + this.reqparams.get("action"));
        
        action = Integer.parseInt(this.reqparams.get("action")[0]);
        
        
        try {
                        if(addUpdateUserDetails(action)){       
                                return "success";
                        }else{
                                return "failure";
                        }
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return "failure";
    }

    
    // GET /test
    public HttpHeaders index() throws UnsupportedEncodingException {
        log.info("this.request=" + this.request);
        
        String userId = "";
        
        if(this.userinfo.isLoggedIn()){
                userId = (new Long(this.userinfo.getUserId())).toString();
                        Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
                        params.put(PageContentKeys.CUSTOMER_ID, userId);
                        params.put(PageContentKeys.ITEM_COUNT, userinfo.getTotalItems()+"");
                        htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.COMPLETED_ORDERS_PAGE, params);
                        
                        return new DefaultHttpHeaders("index").disableCaching();
        
        }else{
                return new DefaultHttpHeaders("login").disableCaching();
        }
        
    }
    
    public HttpHeaders show(){
        log.info("this.id=" + this.id);
        log.info("this.id=" + userinfo.getTotalItems());
                Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
                params.put(PageContentKeys.CUSTOMER_ID, id);
                params.put(PageContentKeys.ITEM_COUNT, userinfo.getTotalItems()+"");
                htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.MY_ACCOUNT_PAGE, params);
                
        return new DefaultHttpHeaders("index").disableCaching();
    }
    
    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 getMyaccountDetailsSnippet(){
                return htmlSnippets.get("MYACCOUNT_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");
        }
        
    /**
     * 
     * @param id
     */
    public void setId(String id) {
        this.id = id;
    }

    public boolean logoutUser(int userId) throws Exception{
                UserContextServiceClient userContextServiceClient = null;
                in.shop2020.model.v1.user.UserContextService.Client userClient = null;

                userContextServiceClient = new UserContextServiceClient();
                userClient = userContextServiceClient.getClient();
                
                userClient.setUserAsLoggedOut(userId, (new Date()).getTime());
                
        return true;
    }
    
    public boolean addUpdateUserDetails(int action) throws Exception{
        
                UserContextServiceClient userContextServiceClient = null;
                in.shop2020.model.v1.user.UserContextService.Client userClient = null;

                userContextServiceClient = new UserContextServiceClient();
                userClient = userContextServiceClient.getClient();

                switch (action) {
                case 1:
                        String email = this.reqparams.get("email")[0];
                        String password = this.reqparams.get("password")[0];
                        
                        if(userClient.userExists(email)){
                                return false;
                        }else{
                                User user = new User();
                                user.setPassword(password);
                                user.setEmail(email);
                                                        
                                user = userClient.createUser(user);
                                long userId = user.getUserId();
                                userClient.setUserAsLoggedIn(userId, (new Date()).getTime());
                                
                                this.userinfo.setUserId(userId);
                                this.userinfo.setEmail(email);
                                this.userinfo.setLoggedIn(true);
                                
                                return true;
                        }

                        //add address
                case 2:
                        long userId = Long.parseLong(this.reqparams.get("user_id")[0]);
                        String name = this.reqparams.get("name")[0];
                        String line1 = this.reqparams.get("line1")[0];
                        String line2 = this.reqparams.get("line2")[0];
                        String landmark = this.reqparams.get("landmark")[0];
                        String city = this.reqparams.get("city")[0];
                        String state = this.reqparams.get("state")[0];
                        String pin = this.reqparams.get("pin")[0];
                        String country = this.reqparams.get("country")[0];
                        String phone = this.reqparams.get("phone")[0];
        
                        Address address = new Address();
                        address.setName(name);
                        address.setLine1(line1);
                        address.setLine2(line2);
                        address.setLandmark(landmark);
                        address.setCity(city);
                        address.setState(state);
                        address.setPin(pin);
                        address.setCountry(country);
                        address.setPhone(phone);
                        
                        if(userClient.addAddressForUser(userId, address, false)>0){
                                return true;
                        }else{
                                return false;
                        }
                        
                case 3:
                        
                        break;
        
                default:
                        break;
                }
                return false;
    }


    
    /**
     * 
     */
//    @Override
//      public void setServletRequest(HttpServletRequest request) {
//              this.request = request;
//      }

    
        @Override
        public void setParameters(Map<String, String[]> reqmap) {
                log.info("setParameters:" + reqmap);
                
                this.reqparams = reqmap;
        }
        
        
    public String getMyAccountSnippets(){
        return htmlSnippets.get("My_ACCOUNT");
    }

    public String getMyOrdersSnippets(){
        return htmlSnippets.get("My_ORDERS");
    }

}