Subversion Repositories SmartDukaan

Rev

Rev 1034 | Rev 1623 | 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.User;
import in.shop2020.serving.utils.DesEncrypter;
import in.shop2020.thrift.clients.UserContextServiceClient;

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

import org.apache.log4j.Logger;
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 redirectUrl = null;

        public LoginController() {
                super();
        }

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

        public String create() throws SecurityException, Exception {
                if (loginUser()) {
                        log.info(redirectUrl);
                        return "redirect";
                } else {
                        addActionError("Either email or password is wrong.");
                        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);
                        
                        UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
                        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());
                        userinfo.setUserId(user.getUserId());
                        userinfo.setNameOfUser(user.getName());
                        userinfo.setEmail(email);
                        userinfo.setLoggedIn(true);
                        userinfo.setPincode(pincode);

                        // 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());
                        int totalItems = userClient.getCart(user.getActiveCartId())
                                        .getLinesSize();
                        userinfo.setTotalItems(totalItems);

                        return true;
                } catch (Exception e) {
                        log.error("Wrong username or password.");
                        return false;
                }
        }

        public String getRedirectUrl() {
                return redirectUrl;
        }

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

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

}