Subversion Repositories SmartDukaan

Rev

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

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

import in.shop2020.model.v1.user.User;
import in.shop2020.serving.interceptors.LoginInterceptor;
import in.shop2020.serving.utils.DesEncrypter;
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.log4j.Logger;
import org.apache.struts2.convention.annotation.Result;

/**
 * 
 * @author rajveer
 * 
 */

@Result(name = "redirect", location = "${url}", 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;

        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()) {
                        redirectUrl = (String) this.session.getAttribute(LoginInterceptor.REDIRECT_URL);
                        if (redirectUrl == null) {
                                redirectUrl = "";
                        }
                        log.info(redirectUrl);
                        resetRedirectUrl();
                        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 getUrl() {
                return this.redirectUrl;
        }

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

}