Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

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


import in.shop2020.model.v1.user.Phone;
import in.shop2020.model.v1.user.PhoneType;
import in.shop2020.model.v1.user.Sex;
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.serving.utils.Utils;
import in.shop2020.thrift.clients.UserContextServiceClient;

import java.io.IOException;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
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;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ModelDriven;

/**
 * 
 * @author rajveer
 *
 */

@Results({
    @Result(name="success", type="redirectAction", 
                params = {"actionName" , "home"}),
    @Result(name="failure", type="redirectAction", 
                params = {"actionName" , "login"})
})
public class LoginController extends BaseController{
                  
        /**
         * 
         */
        private static Log log = LogFactory.getLog(LoginController.class);
        
        private Map<String,String> htmlSnippets = new HashMap<String, String>();
        
        public LoginController(){
                super();
        }


    public HttpHeaders index() throws SecurityException, IOException {
        htmlSnippets.put("LOGIN_HEADER", pageLoader.getLoginHeaderHtml());
                htmlSnippets.put("LOGIN_FORM", pageLoader.getLoginFormHtml());
                return new DefaultHttpHeaders("index");
    }

    public String create() throws SecurityException, Exception {
        
        if(loginUser()){
                this.request.getSession().getAttribute("REDIRECT_URL"); 
                return "success";
        }
        else
                return "failure";
    }
    
    public boolean loginUser(){
        try{
                String email, password;
                
                email = this.request.getParameter("email"); 
                password = this.request.getParameter("password");
                
                UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
                        in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
                        User user = userClient.authenticateUser(email, password);
                        userClient.setUserAsLoggedIn(user.getUserId(), (new Date()).getTime());
        
                        userinfo.setUserId(user.getUserId());
                        userinfo.setNameOfUser(user.getName());
                        userinfo.setEmail(email);
                        userinfo.setLoggedIn(true);
                        
                        // TODO: setTotalItems shouldn't be a method on userinfo. This allows
                        // for potentially updating the item count wrongly. The method setCartId
                        // should update the item count as well. Also, there can be a method
                        // called refreshItemCount() that automatically updates the number of
                        // items currently in the cart.
                        userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
                        userinfo.setCartId(user.getActiveCartId());
                        userinfo.setTotalItems(Utils.getNumberOfItemsInCart(userinfo.getCartId()));
                        return true;
        }catch(Exception e){
                log.error("Wrong username or password.");
                return false;
        }
    }

        public Map<String,String> getHtmlSnippets(){
                System.out.println(" getHtmlSnippets  is called");
                return htmlSnippets;
        }

        /*
        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 getLoginHeaderSnippet(){
                return htmlSnippets.get("LOGIN_HEADER");
        }
        
        public String getLoginFormSnippet(){
                return htmlSnippets.get("LOGIN_FORM");
        }

        /*
        public String getRecommendationsSnippet(){
                return htmlSnippets.get("RECOMMENDATIONS");
        }

        public String getBrowseHistorySnippet(){
                return htmlSnippets.get("BROWSE_HISTORY");
        }
                        
        public String getFooterSnippet(){
                return htmlSnippets.get("FOOTER");
        }
         */
        public String getJsFileSnippet(){
                return htmlSnippets.get("JS_FILES");
        }
        
        public String getCssFileSnippet(){
                return htmlSnippets.get("CSS_FILES");
        }

}