Subversion Repositories SmartDukaan

Rev

Rev 22350 | Rev 23242 | 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);
105
	        }
106
		}catch (Exception e) {
107
			e.printStackTrace();
108
			throw e;
109
		}
110
    	return index();
111
    }
2674 vikas 112
 
3499 mandeep.dh 113
    public String update() throws Exception {
114
        userId = Long.parseLong(id);
115
        userContextServiceClient = new UserClient().getClient();
116
        userContextServiceClient.increaseTrustLevel(userId, Double.parseDouble(trustLevelDelta));
117
        return index();
118
    }
119
 
11890 kshitij.so 120
    public String addPrivateDealUser() throws Exception{
121
        if (canViewBulkOrderEnquiry()){
122
            userContextServiceClient = new UserClient().getClient();
123
            userContextServiceClient.addPrivateDealUser(userId);
124
        }
125
        return index();
126
    }
22350 ashik.ali 127
 
128
    public String updateAddress() throws Exception{
129
    	userId = Long.parseLong(id);
130
    	userContextServiceClient = new UserClient().getClient();
131
        user = userContextServiceClient.getUserById(userId);
132
        primaryAddress = userContextServiceClient.getAddressById(user.getDefaultAddressId());
133
        primaryAddress.setLine1(line1);
134
        primaryAddress.setLine2(line2);
135
        primaryAddress.setCity(city);
136
        primaryAddress.setPin(pinCode);
137
        primaryAddress.setState(state);
138
        userContextServiceClient.updateAddress(primaryAddress);
139
    	return index();
140
    }
11890 kshitij.so 141
 
142
    public String changeStatusOfPrivateDealUser() throws Exception{
143
        if (canViewBulkOrderEnquiry()){
144
            userContextServiceClient = new UserClient().getClient();
145
            userContextServiceClient.changePrivateDealUserStatus(userId, Boolean.valueOf(isPrivateDealUserActive));
146
        }
147
        return index();
148
    }
149
 
150
    public String resetPrivateDealUserPassword() throws Exception{
151
        log.info("Inside reset password"+userId);
152
        if (canViewBulkOrderEnquiry()){
153
            userContextServiceClient = new UserClient().getClient();
154
            user = userContextServiceClient.getUserById(userId);
155
            String newPassword = generateNewPassword();
156
            String encryptedPassword =   desEncrypter.encrypt(newPassword);
157
            if(userContextServiceClient.forgotPassword(user.getEmail(), encryptedPassword)){
158
                if(mailNewPassword(user.getEmail(), newPassword)){
159
                    ;
160
                }else{
161
                    throw new UserCommunicationException();
162
                }
163
            }else{
164
                throw new UserCommunicationException();
165
            }
166
        }
167
        return index();
168
    }
169
 
170
    private boolean mailNewPassword(String emailId, String newPassword) {
171
        List<String> toList = new ArrayList<String>();
172
        toList.add(emailId);
173
 
174
        try {
175
            HelperClient helperClient = new HelperClient("helper_service_server","helper_service_server_port");
176
            helperClient.getClient().saveUserEmailForSending(toList, "help@saholic.com", "Password reset request", "Your new password is: " + newPassword, "", "ForgotPassword", new ArrayList<String>(), new ArrayList<String>(), 1);
177
        } catch (Exception e) {
178
            log.error("Unexpected error while mailing the new password");
179
            return false;
180
        }
181
        return true;
182
    }
183
 
184
    private static String generateNewPassword() {
185
        char[] buf = new char[LENGTH];
186
        for (int i = 0; i < buf.length; i++) {
187
            buf[i] = chars.charAt(random.nextInt(chars.length()));
188
        }
189
        return new String(buf);
190
    }
191
 
2674 vikas 192
    public void setUserId(String userId) {
193
        try {
194
            this.userId = Long.parseLong(userId);
195
        }
196
        catch (NumberFormatException e) {
197
            log.error(e);
198
        }
199
    }
200
 
3499 mandeep.dh 201
    public boolean isTrustLevelEditable() {
3701 mandeep.dh 202
        return SecurityUtils.getSubject().hasRole("TeamLead") && SecurityUtils.getSubject().hasRole("Outbound");
3499 mandeep.dh 203
    }
204
 
2674 vikas 205
    public Long getUserId() {
206
        return userId;
207
    }
11890 kshitij.so 208
 
2674 vikas 209
    public User getUser() {
210
        return user;
211
    }
212
 
213
    public List<Address> getUserAddresses() {
214
        return userAddresses;
215
    }
216
 
22350 ashik.ali 217
    public Address getPrimaryAddress() {
218
        return primaryAddress;
2674 vikas 219
    }
3499 mandeep.dh 220
 
221
    public String getTrustLevelDelta() {
222
        return trustLevelDelta;
223
    }
224
 
225
    public void setTrustLevelDelta(String trustLevelDelta) {
226
        this.trustLevelDelta = trustLevelDelta;
227
    }
228
 
229
    public String getId() {
230
        return id;
231
    }
232
 
233
    public void setId(String id) {
234
        this.id = id;
235
    }
11890 kshitij.so 236
 
237
    public void setPrivateDealUser(PrivateDealUser privateDealUser) {
238
        this.privateDealUser = privateDealUser;
239
    }
240
 
241
    public PrivateDealUser getPrivateDealUser() {
242
        return privateDealUser;
243
    }
244
 
245
    public void setIsPrivateDealUserActive(String isPrivateDealUserActive) {
246
        this.isPrivateDealUserActive = isPrivateDealUserActive;
247
    }
248
 
249
    public String getIsPrivateDealUserActive() {
250
        return isPrivateDealUserActive;
251
    }
21934 amit.gupta 252
 
253
	public void setCounter(Counter counter) {
254
		this.counter = counter;
255
	}
256
 
257
	public Counter getCounter() {
258
		return counter;
259
	}
260
 
261
	public boolean isDocumentVerified() {
262
		return documentVerified;
263
	}
264
 
265
	public void setDocumentVerified(boolean documentVerified) {
266
		this.documentVerified = documentVerified;
267
	}
268
 
269
	public String getGstin() {
270
		return gstin;
271
	}
272
 
273
	public void setGstin(String gstin) {
274
		this.gstin = gstin;
275
	}
276
 
22350 ashik.ali 277
	public String getLine1() {
278
		log.info("GET LINE1 CALLED");
279
		return line1;
280
	}
281
	public void setLine1(String line1) {
282
		this.line1 = line1;
283
	}
284
	public String getLine2() {
285
		return line2;
286
	}
287
	public void setLine2(String line2) {
288
		this.line2 = line2;
289
	}
290
	public String getCity() {
291
		return city;
292
	}
293
	public void setCity(String city) {
294
		this.city = city;
295
	}
296
	public String getPinCode() {
297
		return pinCode;
298
	}
299
	public void setPinCode(String pinCode) {
300
		this.pinCode = pinCode;
301
	}
302
	public String getState() {
303
		return state;
304
	}
305
	public void setState(String state) {
306
		this.state = state;
307
	}
21934 amit.gupta 308
 
2674 vikas 309
}