Subversion Repositories SmartDukaan

Rev

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

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


import in.shop2020.model.v1.user.User;
import in.shop2020.serving.utils.Utils;
import in.shop2020.thrift.clients.UserContextServiceClient;

import java.io.IOException;
import java.util.Date;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

/**
 * 
 * @author rajveer
 *
 */

@Results({
    @Result(name="success", type="redirectAction", 
                params = {"actionName" , "home"}),
    @Result(name="failure", type="redirectAction", 
                params = {"actionName" , "login"}),
    @Result(name="redirect", location="${url}", type="redirect")
})

public class LoginController extends BaseController{
                  

        private static Log log = LogFactory.getLog(LoginController.class);
        
        private String redirectUrl;
        
        public LoginController(){
                super();
        }


    public String index() throws SecurityException, IOException {
        htmlSnippets.put("LOGIN_HEADER", pageLoader.getLoginHeaderHtml());
                return "index";
    }

    public String create() throws SecurityException, Exception {
        
        if(loginUser()){
                if(getUrl()!=null){
                        redirectUrl = getUrl();
                        resetRedirectUrl();
                        return "redirect";
                }
                return "success";
        }
        else{
                addActionError("Either email or password is wrong.");
                return "failure";
        }
    }
    
    private boolean loginUser(){
        try{
                String email, password;
                
                email = this.request.getParameter("email"); 
                password = this.request.getParameter("password");
                
                if(email == null || password == null){
                        return false;
                }
                
                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;
        }
    }

    String getUrl(){
        return this.redirectUrl;
    }
    
        public String getLoginHeaderSnippet(){
                return htmlSnippets.get("LOGIN_HEADER");
        }

}