Subversion Repositories SmartDukaan

Rev

Rev 23242 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.serving.controllers;

import in.shop2020.model.v1.order.TransactionService.retrieveHotspotRechargeInvoice_result;
import in.shop2020.model.v1.user.Address;
import in.shop2020.model.v1.user.Counter;
import in.shop2020.model.v1.user.CounterVerificationType;
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.Date;
import java.util.List;
import java.util.Random;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.velocity.app.event.implement.ReportInvalidReferences;

import com.google.gson.Gson;

/**
 * @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 primaryAddress;
    private String trustLevelDelta;
    private PrivateDealUser privateDealUser;
    private boolean documentVerified=false;
    private String gstin = "Unregistered";
    private Counter counter= null;
    
    private String line1;
    private String line2;
    private String city;
    private String pinCode;
    private String state;

        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 {
        log.info("GET INDEX CALLED");
        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) {
            primaryAddress = userClient.getAddressById(user.getDefaultAddressId());
            log.info("primaryAddressState "+primaryAddress.getState());
        }

        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 verifyRetailer() throws Exception{
        try{
                        UserClient userServiceClient = new UserClient();
                in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
                Counter counter = userClient.getCounterByUserId(userId);
                if(counter.getId()!=-1) {
                        counter.setGstin(gstin);
                        counter.setDocumentVerified(true);
                        userClient.updateCounter(counter);
                }else{
                        counter = new Counter();
                        long currentTime = new Date().getTime();
                        counter.setGstin(gstin);
                        counter.setAddedOn(currentTime);
                        counter.setVerifiedOn(currentTime);
                        counter.setVerificationType(CounterVerificationType.OFFLINE);
                        counter.setDocumentVerified(true);
                        userClient.registerCounter(counter, userId);
                }
                }catch (Exception e) {
                        e.printStackTrace();
                        throw e;
                }
        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 updateAddress() throws Exception{
        userId = Long.parseLong(id);
        userContextServiceClient = new UserClient().getClient();
        user = userContextServiceClient.getUserById(userId);
        primaryAddress = userContextServiceClient.getAddressById(user.getDefaultAddressId());
        primaryAddress.setLine1(line1);
        primaryAddress.setLine2(line2);
        primaryAddress.setCity(city);
        primaryAddress.setPin(pinCode);
        primaryAddress.setState(state);
        userContextServiceClient.updateAddress(primaryAddress);
        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 getPrimaryAddress() {
        return primaryAddress;
    }

    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;
        }
    
        public String getLine1() {
                log.info("GET LINE1 CALLED");
                return line1;
        }
        public void setLine1(String line1) {
                this.line1 = line1;
        }
        public String getLine2() {
                return line2;
        }
        public void setLine2(String line2) {
                this.line2 = line2;
        }
        public String getCity() {
                return city;
        }
        public void setCity(String city) {
                this.city = city;
        }
        public String getPinCode() {
                return pinCode;
        }
        public void setPinCode(String pinCode) {
                this.pinCode = pinCode;
        }
        public String getState() {
                return state;
        }
        public void setState(String state) {
                this.state = state;
        }
    
}