Subversion Repositories SmartDukaan

Rev

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

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

import in.shop2020.datalogger.EventType;
import in.shop2020.model.v1.user.Cart;
import in.shop2020.model.v1.user.User;
import in.shop2020.serving.utils.DesEncrypter;
import in.shop2020.serving.utils.UserMessage;
import in.shop2020.thrift.clients.UserClient;
import in.shop2020.utils.DataLogger;

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

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.InterceptorRef;
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 = "redirect", location = "${redirectUrl}", type = "redirect")
})

public class LoginController extends BaseController {

        /**
         * 
         */
        private static final long serialVersionUID = 5390035354379263121L;

        private static Logger log = Logger.getLogger(Class.class);
        private DesEncrypter desEncrypter = new DesEncrypter("saholic");
        private String loginResult = "0";
        private String redirectUrl = "/";

        public LoginController() {
                super();
        }
        @Actions({
                @Action(value="login", interceptorRefs={@InterceptorRef("myDefault")}),
                @Action(value="login-mini", interceptorRefs={@InterceptorRef("myDefault")})
        })
        public String index() throws SecurityException, IOException {
                if(userinfo.isLoggedIn()){
                return "success";
        }
                return "index";
        }

        public String create() throws SecurityException, Exception {
                if (loginUser()) {
                        log.info("Will redirect the user to:" + redirectUrl);
            return "redirect";
                } else {
                        addActionError(UserMessage.USER_AUTHENTICATION_FAILURE);
            DataLogger.logData(EventType.LOGIN_FAILED, getSessionId(), userinfo.getUserId(), this.request.getParameter("email"));
                        return "login";
                }
        }

        private boolean loginUser() {
                try {
                        String email, password;

                        email = this.request.getParameter("email");
                        password = this.request.getParameter("password");

                        if (email == null || password == null) {
                                return false;
                        }

                        String encryptedPassword = desEncrypter.encrypt(password);
                        
                        UserClient userContextServiceClient = new UserClient();
                        in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
                        User user = userClient.authenticateUser(email, encryptedPassword);
                        userClient.setUserAsLoggedIn(user.getUserId(),(new Date()).getTime());
                        String pincode = userClient.getDefaultPincode(user.getUserId());
                        
                        // 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.
                        if(userinfo.getUserId() != -1){
                                userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
                                
                                List<Long> items = userClient.getBrowseHistoryItems(userinfo.getUserId());
                                if(items != null){
                                        for(Long itemId: items){
                                                userClient.updateBrowseHistory(user.getUserId(), itemId);
                                        }
                                }
                                
                                items = userClient.getMyResearchItems(userinfo.getUserId());
                                if(items != null){
                                        for(Long itemId: items){
                                                userClient.updateMyResearch(user.getUserId(), itemId);
                                        }
                                }
                        }
                        
                        userinfo.setUserId(user.getUserId());
                        userinfo.setEmail(email);
                        userinfo.setLoggedIn(true);
                        userinfo.setPincode(pincode);
                        userinfo.setCartId(user.getActiveCartId());
                        Cart cart = userClient.getCart(user.getActiveCartId());
                        userinfo.setTotalItems(cart.getLinesSize());
                        userinfo.setTotalAmount(cart.getTotalPrice());
                        log.info(userinfo);
                        String src = user.getSource();
                        if (src == null) {
                            src = "";
                        }
                        DataLogger.logData(EventType.LOGIN_SUCCESS, getSessionId(), userinfo.getUserId(),
                    email, src);
                return true;
                } catch (Exception e) {
                        log.error(UserMessage.USER_AUTHENTICATION_FAILURE, e);
                        return false;
                }
        }

        public String authenticateUser() {
                String email, password;

                email = this.request.getParameter("email");
                password = this.request.getParameter("password");

                if (email == null || password == null) {
                        loginResult = "0";
                        return "result";
                }

                String encryptedPassword = desEncrypter.encrypt(password);
                try{
                        UserClient userContextServiceClient = new UserClient();
                        in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
                        userClient.authenticateUser(email, encryptedPassword);
                }catch (Exception e) {
                        loginResult = "0";
                        return "result";
                }
                loginResult = "1";
                return "result";
        }
        
        public String getLoginResult() {
                return loginResult;
        }
        
        public String getRedirectUrl() {
                return redirectUrl;
        }

        public void setRedirectUrl(String redirectUrl) {
                this.redirectUrl = redirectUrl;
        }
}