Subversion Repositories SmartDukaan

Rev

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