Subversion Repositories SmartDukaan

Rev

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