Subversion Repositories SmartDukaan

Rev

Rev 4086 | Rev 21963 | 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.PrivateDealUser;
import in.shop2020.model.v1.user.User;
import in.shop2020.model.v1.user.UserCommunicationException;
import in.shop2020.model.v1.user.UserContextService.Client;
import in.shop2020.util.DesEncrypter;
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.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;

/**
 * @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 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));

        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;
    }
}