Subversion Repositories SmartDukaan

Rev

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