Subversion Repositories SmartDukaan

Rev

Rev 4086 | Rev 21934 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2674 vikas 1
package in.shop2020.serving.controllers;
2
 
3
import in.shop2020.model.v1.user.Address;
11890 kshitij.so 4
import in.shop2020.model.v1.user.PrivateDealUser;
2674 vikas 5
import in.shop2020.model.v1.user.User;
11890 kshitij.so 6
import in.shop2020.model.v1.user.UserCommunicationException;
7
import in.shop2020.model.v1.user.UserContextService.Client;
8
import in.shop2020.util.DesEncrypter;
9
import in.shop2020.thrift.clients.HelperClient;
3128 rajveer 10
import in.shop2020.thrift.clients.UserClient;
2674 vikas 11
 
11890 kshitij.so 12
import java.util.ArrayList;
2674 vikas 13
import java.util.List;
11890 kshitij.so 14
import java.util.Random;
2674 vikas 15
 
16
import org.apache.log4j.Logger;
3499 mandeep.dh 17
import org.apache.shiro.SecurityUtils;
11890 kshitij.so 18
import org.apache.thrift.TException;
19
import org.apache.thrift.transport.TTransportException;
2674 vikas 20
 
21
/**
22
 * @author vikas
23
 *
24
 */
25
@SuppressWarnings("serial")
26
public class UserInfoController extends BaseController {
27
    private static Logger log = Logger.getLogger(Class.class);
3499 mandeep.dh 28
    private String id;
2674 vikas 29
    private long userId;
30
    private User user;
31
    private List<Address> userAddresses;
32
    private Address primaryAdddress;
3499 mandeep.dh 33
    private String trustLevelDelta;
11890 kshitij.so 34
    private PrivateDealUser privateDealUser;
35
    private String isPrivateDealUserActive;
36
    private static final String chars = "abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
37
    private static final Random random = new Random();
38
    private static final int LENGTH = 10;
2674 vikas 39
 
11890 kshitij.so 40
 
41
    private in.shop2020.util.DesEncrypter desEncrypter = new in.shop2020.util.DesEncrypter("saholic");
42
 
2674 vikas 43
    public String index() throws Exception {
11890 kshitij.so 44
        UserClient userServiceClient = new UserClient();
3128 rajveer 45
        in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
2674 vikas 46
        user = userClient.getUserById(userId);
47
        userAddresses = user.getAddresses();
4086 mandeep.dh 48
 
49
        //
50
        // For cases where Default Address is NULL in db, we get 0 as long here
51
        // Skipping such cases to avoid exception. Ideally UserContextService should simply
52
        // return null in such cases.
53
        //
54
        if (user.getDefaultAddressId() != 0) {
55
            primaryAdddress = userClient.getAddressById(user.getDefaultAddressId());
56
        }
57
 
11890 kshitij.so 58
        setPrivateDealUser(userClient.getPrivateDealUser(userId));
59
 
3499 mandeep.dh 60
        return INDEX;
2674 vikas 61
    }
62
 
3499 mandeep.dh 63
    public String update() throws Exception {
64
        userId = Long.parseLong(id);
65
        userContextServiceClient = new UserClient().getClient();
66
        userContextServiceClient.increaseTrustLevel(userId, Double.parseDouble(trustLevelDelta));
67
        return index();
68
    }
69
 
11890 kshitij.so 70
    public String addPrivateDealUser() throws Exception{
71
        if (canViewBulkOrderEnquiry()){
72
            userContextServiceClient = new UserClient().getClient();
73
            userContextServiceClient.addPrivateDealUser(userId);
74
        }
75
        return index();
76
    }
77
 
78
    public String changeStatusOfPrivateDealUser() throws Exception{
79
        if (canViewBulkOrderEnquiry()){
80
            userContextServiceClient = new UserClient().getClient();
81
            userContextServiceClient.changePrivateDealUserStatus(userId, Boolean.valueOf(isPrivateDealUserActive));
82
        }
83
        return index();
84
    }
85
 
86
    public String resetPrivateDealUserPassword() throws Exception{
87
        log.info("Inside reset password"+userId);
88
        if (canViewBulkOrderEnquiry()){
89
            userContextServiceClient = new UserClient().getClient();
90
            user = userContextServiceClient.getUserById(userId);
91
            String newPassword = generateNewPassword();
92
            String encryptedPassword =   desEncrypter.encrypt(newPassword);
93
            if(userContextServiceClient.forgotPassword(user.getEmail(), encryptedPassword)){
94
                if(mailNewPassword(user.getEmail(), newPassword)){
95
                    ;
96
                }else{
97
                    throw new UserCommunicationException();
98
                }
99
            }else{
100
                throw new UserCommunicationException();
101
            }
102
        }
103
        return index();
104
    }
105
 
106
    private boolean mailNewPassword(String emailId, String newPassword) {
107
        List<String> toList = new ArrayList<String>();
108
        toList.add(emailId);
109
 
110
        try {
111
            HelperClient helperClient = new HelperClient("helper_service_server","helper_service_server_port");
112
            helperClient.getClient().saveUserEmailForSending(toList, "help@saholic.com", "Password reset request", "Your new password is: " + newPassword, "", "ForgotPassword", new ArrayList<String>(), new ArrayList<String>(), 1);
113
        } catch (Exception e) {
114
            log.error("Unexpected error while mailing the new password");
115
            return false;
116
        }
117
        return true;
118
    }
119
 
120
    private static String generateNewPassword() {
121
        char[] buf = new char[LENGTH];
122
        for (int i = 0; i < buf.length; i++) {
123
            buf[i] = chars.charAt(random.nextInt(chars.length()));
124
        }
125
        return new String(buf);
126
    }
127
 
2674 vikas 128
    public void setUserId(String userId) {
129
        try {
130
            this.userId = Long.parseLong(userId);
131
        }
132
        catch (NumberFormatException e) {
133
            log.error(e);
134
        }
135
    }
136
 
3499 mandeep.dh 137
    public boolean isTrustLevelEditable() {
3701 mandeep.dh 138
        return SecurityUtils.getSubject().hasRole("TeamLead") && SecurityUtils.getSubject().hasRole("Outbound");
3499 mandeep.dh 139
    }
140
 
2674 vikas 141
    public Long getUserId() {
142
        return userId;
143
    }
11890 kshitij.so 144
 
2674 vikas 145
    public User getUser() {
146
        return user;
147
    }
148
 
149
    public List<Address> getUserAddresses() {
150
        return userAddresses;
151
    }
152
 
153
    public Address getPrimaryAdddress() {
154
        return primaryAdddress;
155
    }
3499 mandeep.dh 156
 
157
    public String getTrustLevelDelta() {
158
        return trustLevelDelta;
159
    }
160
 
161
    public void setTrustLevelDelta(String trustLevelDelta) {
162
        this.trustLevelDelta = trustLevelDelta;
163
    }
164
 
165
    public String getId() {
166
        return id;
167
    }
168
 
169
    public void setId(String id) {
170
        this.id = id;
171
    }
11890 kshitij.so 172
 
173
    public void setPrivateDealUser(PrivateDealUser privateDealUser) {
174
        this.privateDealUser = privateDealUser;
175
    }
176
 
177
    public PrivateDealUser getPrivateDealUser() {
178
        return privateDealUser;
179
    }
180
 
181
    public void setIsPrivateDealUserActive(String isPrivateDealUserActive) {
182
        this.isPrivateDealUserActive = isPrivateDealUserActive;
183
    }
184
 
185
    public String getIsPrivateDealUserActive() {
186
        return isPrivateDealUserActive;
187
    }
2674 vikas 188
}