Subversion Repositories SmartDukaan

Rev

Rev 4086 | Rev 21934 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4086 Rev 11890
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
2
 
2
 
3
import in.shop2020.model.v1.user.Address;
3
import in.shop2020.model.v1.user.Address;
-
 
4
import in.shop2020.model.v1.user.PrivateDealUser;
4
import in.shop2020.model.v1.user.User;
5
import in.shop2020.model.v1.user.User;
-
 
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;
5
import in.shop2020.thrift.clients.UserClient;
10
import in.shop2020.thrift.clients.UserClient;
6
 
11
 
-
 
12
import java.util.ArrayList;
7
import java.util.List;
13
import java.util.List;
-
 
14
import java.util.Random;
8
 
15
 
9
import org.apache.log4j.Logger;
16
import org.apache.log4j.Logger;
10
import org.apache.shiro.SecurityUtils;
17
import org.apache.shiro.SecurityUtils;
-
 
18
import org.apache.thrift.TException;
-
 
19
import org.apache.thrift.transport.TTransportException;
11
 
20
 
12
/**
21
/**
13
 * @author vikas
22
 * @author vikas
14
 *
23
 *
15
 */
24
 */
Line 20... Line 29...
20
    private long userId;
29
    private long userId;
21
    private User user;
30
    private User user;
22
    private List<Address> userAddresses;
31
    private List<Address> userAddresses;
23
    private Address primaryAdddress;
32
    private Address primaryAdddress;
24
    private String trustLevelDelta;
33
    private String trustLevelDelta;
-
 
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;
-
 
39
 
-
 
40
 
-
 
41
    private in.shop2020.util.DesEncrypter desEncrypter = new in.shop2020.util.DesEncrypter("saholic");
25
 
42
 
26
    public String index() throws Exception {
43
    public String index() throws Exception {
27
    	UserClient userServiceClient = new UserClient();
44
        UserClient userServiceClient = new UserClient();
28
        in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
45
        in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
29
        user = userClient.getUserById(userId);
46
        user = userClient.getUserById(userId);
30
        userAddresses = user.getAddresses();
47
        userAddresses = user.getAddresses();
31
 
48
 
32
        //
49
        //
Line 36... Line 53...
36
        //
53
        //
37
        if (user.getDefaultAddressId() != 0) {
54
        if (user.getDefaultAddressId() != 0) {
38
            primaryAdddress = userClient.getAddressById(user.getDefaultAddressId());
55
            primaryAdddress = userClient.getAddressById(user.getDefaultAddressId());
39
        }
56
        }
40
 
57
 
-
 
58
        setPrivateDealUser(userClient.getPrivateDealUser(userId));
-
 
59
 
41
        return INDEX;
60
        return INDEX;
42
    }
61
    }
43
 
62
 
44
    public String update() throws Exception {
63
    public String update() throws Exception {
45
        userId = Long.parseLong(id);
64
        userId = Long.parseLong(id);
46
        userContextServiceClient = new UserClient().getClient();
65
        userContextServiceClient = new UserClient().getClient();
47
        userContextServiceClient.increaseTrustLevel(userId, Double.parseDouble(trustLevelDelta));
66
        userContextServiceClient.increaseTrustLevel(userId, Double.parseDouble(trustLevelDelta));
48
        return index();
67
        return index();
49
    }
68
    }
50
 
69
 
-
 
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
 
51
    public void setUserId(String userId) {
128
    public void setUserId(String userId) {
52
        try {
129
        try {
53
            this.userId = Long.parseLong(userId);
130
            this.userId = Long.parseLong(userId);
54
        }
131
        }
55
        catch (NumberFormatException e) {
132
        catch (NumberFormatException e) {
Line 62... Line 139...
62
    }
139
    }
63
 
140
 
64
    public Long getUserId() {
141
    public Long getUserId() {
65
        return userId;
142
        return userId;
66
    }
143
    }
67
    
144
 
68
    public User getUser() {
145
    public User getUser() {
69
        return user;
146
        return user;
70
    }
147
    }
71
 
148
 
72
    public List<Address> getUserAddresses() {
149
    public List<Address> getUserAddresses() {
Line 90... Line 167...
90
    }
167
    }
91
 
168
 
92
    public void setId(String id) {
169
    public void setId(String id) {
93
        this.id = id;
170
        this.id = id;
94
    }
171
    }
-
 
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
    }
95
}
188
}
96
189