Subversion Repositories SmartDukaan

Rev

Rev 21934 | Rev 22363 | 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.Address;
import in.shop2020.model.v1.user.Counter;
import in.shop2020.model.v1.user.PrivateDealUser;
import in.shop2020.model.v1.user.User;
import in.shop2020.model.v1.user.UserCommunicationException;
import in.shop2020.thrift.clients.HelperClient;
import in.shop2020.thrift.clients.UserClient;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;

/**
 * @author vikas
 *
 */
@SuppressWarnings("serial")
public class UserInfoController extends BaseController {
    private static Logger log = Logger.getLogger(Class.class);
    private String id;
    private long userId;
    private User user;
    private List<Address> userAddresses;
    private Address primaryAdddress;
    private String trustLevelDelta;
    private PrivateDealUser privateDealUser;
    private boolean documentVerified=false;
    private String gstin = "Unregistered";
    private Counter counter= null;
    

        private String isPrivateDealUserActive;
    private static final String chars = "abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private static final Random random = new Random();
    private static final int LENGTH = 10;


    private in.shop2020.util.DesEncrypter desEncrypter = new in.shop2020.util.DesEncrypter("saholic");

    public String index() throws Exception {
        UserClient userServiceClient = new UserClient();
        in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
        user = userClient.getUserById(userId);
        userAddresses = user.getAddresses();

        //
        // For cases where Default Address is NULL in db, we get 0 as long here
        // Skipping such cases to avoid exception. Ideally UserContextService should simply
        // return null in such cases.
        //
        if (user.getDefaultAddressId() != 0) {
            primaryAdddress = userClient.getAddressById(user.getDefaultAddressId());
        }

        setPrivateDealUser(userClient.getPrivateDealUser(userId));
        Counter c = userClient.getCounterByUserId(userId);
        if(c.getId()!=0) {
                this.counter = c;
                if(StringUtils.isNotEmpty(c.getGstin())) {
                        this.gstin = c.getGstin();
                }
                this.documentVerified = c.isDocumentVerified();
                
        }

        return INDEX;
    }

    public String update() throws Exception {
        userId = Long.parseLong(id);
        userContextServiceClient = new UserClient().getClient();
        userContextServiceClient.increaseTrustLevel(userId, Double.parseDouble(trustLevelDelta));
        return index();
    }

    public String addPrivateDealUser() throws Exception{
        if (canViewBulkOrderEnquiry()){
            userContextServiceClient = new UserClient().getClient();
            userContextServiceClient.addPrivateDealUser(userId);
        }
        return index();
    }

    public String changeStatusOfPrivateDealUser() throws Exception{
        if (canViewBulkOrderEnquiry()){
            userContextServiceClient = new UserClient().getClient();
            userContextServiceClient.changePrivateDealUserStatus(userId, Boolean.valueOf(isPrivateDealUserActive));
        }
        return index();
    }

    public String resetPrivateDealUserPassword() throws Exception{
        log.info("Inside reset password"+userId);
        if (canViewBulkOrderEnquiry()){
            userContextServiceClient = new UserClient().getClient();
            user = userContextServiceClient.getUserById(userId);
            String newPassword = generateNewPassword();
            String encryptedPassword =   desEncrypter.encrypt(newPassword);
            if(userContextServiceClient.forgotPassword(user.getEmail(), encryptedPassword)){
                if(mailNewPassword(user.getEmail(), newPassword)){
                    ;
                }else{
                    throw new UserCommunicationException();
                }
            }else{
                throw new UserCommunicationException();
            }
        }
        return index();
    }

    private boolean mailNewPassword(String emailId, String newPassword) {
        List<String> toList = new ArrayList<String>();
        toList.add(emailId);

        try {
            HelperClient helperClient = new HelperClient("helper_service_server","helper_service_server_port");
            helperClient.getClient().saveUserEmailForSending(toList, "help@saholic.com", "Password reset request", "Your new password is: " + newPassword, "", "ForgotPassword", new ArrayList<String>(), new ArrayList<String>(), 1);
        } catch (Exception e) {
            log.error("Unexpected error while mailing the new password");
            return false;
        }
        return true;
    }

    private static String generateNewPassword() {
        char[] buf = new char[LENGTH];
        for (int i = 0; i < buf.length; i++) {
            buf[i] = chars.charAt(random.nextInt(chars.length()));
        }
        return new String(buf);
    }

    public void setUserId(String userId) {
        try {
            this.userId = Long.parseLong(userId);
        }
        catch (NumberFormatException e) {
            log.error(e);
        }
    }

    public boolean isTrustLevelEditable() {
        return SecurityUtils.getSubject().hasRole("TeamLead") && SecurityUtils.getSubject().hasRole("Outbound");
    }

    public Long getUserId() {
        return userId;
    }

    public User getUser() {
        return user;
    }

    public List<Address> getUserAddresses() {
        return userAddresses;
    }

    public Address getPrimaryAdddress() {
        return primaryAdddress;
    }

    public String getTrustLevelDelta() {
        return trustLevelDelta;
    }

    public void setTrustLevelDelta(String trustLevelDelta) {
        this.trustLevelDelta = trustLevelDelta;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setPrivateDealUser(PrivateDealUser privateDealUser) {
        this.privateDealUser = privateDealUser;
    }

    public PrivateDealUser getPrivateDealUser() {
        return privateDealUser;
    }

    public void setIsPrivateDealUserActive(String isPrivateDealUserActive) {
        this.isPrivateDealUserActive = isPrivateDealUserActive;
    }

    public String getIsPrivateDealUserActive() {
        return isPrivateDealUserActive;
    }

        public void setCounter(Counter counter) {
                this.counter = counter;
        }

        public Counter getCounter() {
                return counter;
        }
        
        public boolean isDocumentVerified() {
                return documentVerified;
        }

        public void setDocumentVerified(boolean documentVerified) {
                this.documentVerified = documentVerified;
        }

        public String getGstin() {
                return gstin;
        }

        public void setGstin(String gstin) {
                this.gstin = gstin;
        }
    
    
}