Subversion Repositories SmartDukaan

Rev

Rev 1034 | Rev 1625 | 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.Sex;
import in.shop2020.model.v1.user.User;
import in.shop2020.serving.utils.DesEncrypter;
import in.shop2020.serving.utils.Utils;
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="failure", type="redirectAction", 
                params = {"actionName" , "register"})
                
})
public class RegisterController extends BaseController{
                
        private static final long serialVersionUID = 1L;
        private static Logger log = Logger.getLogger(Class.class);
        private DesEncrypter desEncrypter = new DesEncrypter("saholic");
        
        public RegisterController(){
                super();
        }


    public String index() throws SecurityException, IOException {
        if(userinfo.isLoggedIn()){
                return "success";
        }
        htmlSnippets.put("REGISTRATION_HEADER",pageLoader.getRegistrationHeaderHtml());
        htmlSnippets.put("REGISTRATION_FORM",pageLoader.getRegistrationFormHtml());
        return "index";
    }

    public String create() throws SecurityException, Exception {
        
        if(registerUser())
                return "success";
        else
                return "failure";
    }
    
    public boolean registerUser() throws Exception{
        String email, password, userName, mobileNumber, communicationEmail, sex, dateOfBirth;
        boolean isValid = true;
        userName =  this.request.getParameter("nameOfUser");
        email = this.request.getParameter("email"); 
        password = this.request.getParameter("txtPassword");
        
        mobileNumber = this.request.getParameter("mobileNumber");
        communicationEmail = this.request.getParameter("communicationEmail");
        
        dateOfBirth = this.request.getParameter("dateOfBirth");
        sex =  this.request.getParameter("sex");
        
        if(userName == null ){
                addActionError("Please enter name of user.");
                isValid = false;
        }
        if(email == null ){
                addActionError("Please enter valid email.");
                isValid = false;
        }
        if(password == null ){
                addActionError("Please enter password.");
                isValid = false;
        }
        if(communicationEmail == null ){
                addActionError("Please enter Communication email.");
                isValid = false;
        }
        
        if(!isValid){
                return isValid;
        }
        
                UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
                in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();

                if(userClient.userExists(email)){
                        addActionError("User already exists with this email id.");
                        return false;
                }

                User user = new User();
                user.setName(userName);
                user.setEmail(email);
                String encryptedPassword = desEncrypter.encrypt(password);
                user.setPassword(encryptedPassword);
                user.setCommunicationEmail(communicationEmail);
                user.setMobileNumber(mobileNumber);
                user.setDateOfBirth(dateOfBirth);

                
                user.setSex(Sex.WONT_SAY);
                if (sex != null) {
                        try {
                                user.setSex(Sex.findByValue(Integer.parseInt(sex)));
                        } catch (NumberFormatException nfe) {
                                log.error("Unusual value for sex. Leaving it marked as won't say.");
                        }
                }


                user = userClient.createUser(user);
                long userId = user.getUserId();
                userClient.setUserAsLoggedIn(userId, (new Date()).getTime());
                String pincode = userClient.getDefaultPincode(user.getUserId());
                
                userinfo.setUserId(userId);
                userinfo.setNameOfUser(userName);
                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(userinfo.getCartId()).getLinesSize();
                userinfo.setTotalItems(totalItems);

                return true;
    }

        public String getRegistrationHeaderSnippet(){
                return htmlSnippets.get("REGISTRATION_HEADER");
        }
        
        public String getRegistrationFormSnippet(){
                return htmlSnippets.get("REGISTRATION_FORM");
        }

}