Subversion Repositories SmartDukaan

Rev

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