Subversion Repositories SmartDukaan

Rev

Rev 16244 | Rev 17081 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3339 mandeep.dh 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
6
import in.shop2020.crm.Activity;
7
import in.shop2020.crm.ActivityType;
8
import in.shop2020.crm.Agent;
3390 mandeep.dh 9
import in.shop2020.crm.SearchFilter;
4142 mandeep.dh 10
import in.shop2020.crm.TicketCategory;
3405 mandeep.dh 11
import in.shop2020.model.v1.user.User;
12
import in.shop2020.model.v1.user.UserContextException;
3390 mandeep.dh 13
import in.shop2020.serving.auth.CRMAuthorizingRealm;
14
import in.shop2020.thrift.clients.CRMClient;
3405 mandeep.dh 15
import in.shop2020.thrift.clients.UserClient;
3339 mandeep.dh 16
import in.shop2020.util.CRMConstants;
17079 manish.sha 17
import in.shop2020.util.SendGrid;
18
import in.shop2020.util.SendGridException;
5203 amar.kumar 19
import in.shop2020.util.CRMConstants.ActivityCommonDescriptionMatrix;
16208 manish.sha 20
import in.shop2020.util.CRMConstants.ActivityPMCommonDescriptionMatrix;
3339 mandeep.dh 21
 
3397 mandeep.dh 22
import java.text.ParseException;
23
import java.util.ArrayList;
3390 mandeep.dh 24
import java.util.Collections;
3339 mandeep.dh 25
import java.util.List;
26
 
16092 amit.gupta 27
import org.apache.commons.lang.StringEscapeUtils;
3339 mandeep.dh 28
import org.apache.thrift.TException;
29
 
30
/**
31
 * @author mandeep
32
 * 
33
 */
34
public class ActivityController extends BaseController {
35
    /**
36
     * 
37
     */
38
    private static final long serialVersionUID = 1L;
39
    private List<Activity>    activities;
3390 mandeep.dh 40
    private String            activityId;
3339 mandeep.dh 41
    private String            description;
5203 amar.kumar 42
    private String			  commonDescription;
3339 mandeep.dh 43
    private String            type;
4142 mandeep.dh 44
    private String            category;
3339 mandeep.dh 45
    private String            customerMobileNumber;
46
    private String            creatorId;
3397 mandeep.dh 47
    private String[]          agentIds;
48
    private String            startTimestamp;
49
    private String            endTimestamp;
3405 mandeep.dh 50
    private String            customerEmailId;
51
    private String            customerName;
52
    private String            userId;
14898 manish.sha 53
    private String			  notShowPmTickets;
3339 mandeep.dh 54
 
55
    public String index() {
56
        try {
3390 mandeep.dh 57
            SearchFilter searchFilter = new SearchFilter();
16208 manish.sha 58
            if(isProfitMandiAgent()){
59
            	searchFilter.setNotShowPmTickets(false);
16244 manish.sha 60
            	searchFilter.setTicketCategoryList(profitMandiTicketCategoryList);
61
 
62
            }else if(isSaholicAndProfitMandiAllowed()){
63
 
16208 manish.sha 64
            }else{
65
            	if(notShowPmTickets!=null && !notShowPmTickets.isEmpty() && "yes".equalsIgnoreCase(notShowPmTickets)){
66
	            	searchFilter.setNotShowPmTickets(true);
16244 manish.sha 67
	            	searchFilter.setTicketCategoryList(profitMandiTicketCategoryList);
16208 manish.sha 68
	            }
14898 manish.sha 69
            }
3405 mandeep.dh 70
            if (activityId != null && !activityId.isEmpty()) {
71
                searchFilter.setActivityId(Long.parseLong(activityId));
72
            } else if (userId != null && !userId.isEmpty()) {
73
                searchFilter.setCustomerId(Long.parseLong(userId));
74
            } else if (creatorId != null && !creatorId.isEmpty()) {
75
                searchFilter.setActivityCreatorIds(Collections
76
                        .singletonList(Long.parseLong(creatorId)));
3390 mandeep.dh 77
                searchFilter.setIsActivityRead(false);
3405 mandeep.dh 78
            } else if (agentIds != null && agentIds.length != 0) {
3397 mandeep.dh 79
                searchFilter.setActivityCreatorIds(new ArrayList<Long>());
80
                for (String agentId : agentIds) {
3405 mandeep.dh 81
                    searchFilter.getActivityCreatorIds().add(
82
                            CRMAuthorizingRealm.getAgent(agentId).getId());
3397 mandeep.dh 83
                }
84
 
4142 mandeep.dh 85
                if (type != null && !type.isEmpty()) {
86
                    searchFilter.setActivityType(ActivityType.valueOf(type));
87
                }
88
 
89
                if (category != null && !category.isEmpty()) {
90
                    searchFilter.setTicketCategory(TicketCategory.valueOf(category));
91
                }
7595 kshitij.so 92
 
93
                //Start:- Added By Manish Sharma for Searching Tickets/Activities using  Mobile No. on 21-Jun-2013
94
                if(customerMobileNumber!=null && !customerMobileNumber.isEmpty()){
95
                	searchFilter.setCustomerMobileNumber(customerMobileNumber);
96
                }
97
                //End:- Added By Manish Sharma for Searching Tickets/Activities using  Mobile No. on 21-Jun-2013
4142 mandeep.dh 98
 
3397 mandeep.dh 99
                try {
100
                    if (startTimestamp != null && !startTimestamp.isEmpty()) {
3405 mandeep.dh 101
                        searchFilter.setStartTimestamp(SDF
102
                                .parse(startTimestamp).getTime());
3397 mandeep.dh 103
                    }
104
 
105
                    if (endTimestamp != null && !endTimestamp.isEmpty()) {
3405 mandeep.dh 106
                        searchFilter.setEndTimestamp(SDF.parse(endTimestamp)
107
                                .getTime());
3397 mandeep.dh 108
                    }
3405 mandeep.dh 109
                } catch (ParseException e) {
3397 mandeep.dh 110
                    log.error("Error parsing timestamps", e);
111
                }
3405 mandeep.dh 112
            } else {
113
                searchFilter.setActivityCreatorIds(Collections
114
                        .singletonList(CRMAuthorizingRealm.getAgent(
115
                                currentAgentEmailId).getId()));
3397 mandeep.dh 116
            }
117
 
3405 mandeep.dh 118
            crmServiceClient = new CRMClient().getClient();
3397 mandeep.dh 119
            activities = crmServiceClient.getActivities(searchFilter);
3339 mandeep.dh 120
        } catch (TException e) {
121
            log.error("Error while retrieving activities for "
122
                    + currentAgentEmailId, e);
3405 mandeep.dh 123
        } catch (NumberFormatException e) {
124
            log.error("Error parsing userId: " + userId, e);
3339 mandeep.dh 125
        }
126
 
127
        return INDEX;
128
    }
129
 
3405 mandeep.dh 130
    public String editNew() {
131
        return EDIT_NEW;
132
    }
133
 
3339 mandeep.dh 134
    public String create() throws Exception {
135
        Activity activity = new Activity();
5203 amar.kumar 136
        if(commonDescription!=null && !commonDescription.isEmpty()){
137
        	if(description!=null) {
138
        		description = commonDescription + "  " +description;
139
        	} else {
140
        		description = commonDescription;
141
        	}
142
        }
3339 mandeep.dh 143
        activity.setDescription(description);
144
        activity.setType(ActivityType.valueOf(type));
3405 mandeep.dh 145
        activity.setCreatorId(CRMAuthorizingRealm.getAgent(currentAgentEmailId)
146
                .getId());
3339 mandeep.dh 147
 
3405 mandeep.dh 148
        if (userId != null && !userId.isEmpty()) {
149
            activity.setCustomerId(Long.parseLong(userId));
5224 amar.kumar 150
            if(customerMobileNumber != null
151
                        && !customerMobileNumber.isEmpty()) {
152
            	activity.setCustomerMobileNumber(customerMobileNumber);
153
            }
3405 mandeep.dh 154
        } else {
155
            User user = null;
156
            userContextServiceClient = new UserClient().getClient();
157
 
158
            try {
159
                if (customerName != null && !customerName.isEmpty()) {
160
                    activity.setCustomerName(customerName);
161
                }
162
 
163
                if (customerEmailId != null && !customerEmailId.isEmpty()) {
164
                    activity.setCustomerEmailId(customerEmailId);
165
                    user = userContextServiceClient
166
                            .getUserByEmail(customerEmailId);
167
                }
168
 
169
                if ((user == null || user.getUserId() == -1)
170
                        && customerMobileNumber != null
171
                        && !customerMobileNumber.isEmpty()) {
172
                    activity.setCustomerMobileNumber(customerMobileNumber);
173
                    user = userContextServiceClient.getUserByMobileNumber(Long
174
                            .parseLong(customerMobileNumber));
175
                }
176
            } catch (UserContextException e) {
177
                log.error("Could not fetch user for: " + customerEmailId + " "
178
                        + customerMobileNumber + " " + customerName, e);
179
            }
180
 
181
            if (user != null && user.getUserId() != -1) {
182
                activity.setCustomerId(user.getUserId());
183
            }
3339 mandeep.dh 184
        }
14898 manish.sha 185
 
3339 mandeep.dh 186
 
3405 mandeep.dh 187
        crmServiceClient = new CRMClient().getClient();
188
        activityId = String.valueOf(crmServiceClient.insertActivity(activity));
17079 manish.sha 189
        try{
190
	        if (ActivityType.RECEIVED_CALL_FROM_CUSTOMER.equals(ActivityType.valueOf(type))) {
191
	        	String addTo[] = new String[]{"manish.sharma@shop2020.in","amit.sirohi@shop2020.in"};
192
	        	if(commonDescription!=null && !commonDescription.isEmpty()){
193
	        		commonDescription = "Received Call From Customer";
194
	        	}
195
	        	sendMailToManagers(commonDescription, addTo, description);
196
	        }
197
        }
198
	    catch(Exception e){
199
	    	e.printStackTrace();
200
	    }
3339 mandeep.dh 201
 
202
        return index();
203
    }
17079 manish.sha 204
 
205
    private void sendMailToManagers(String subject, String[] addTo, String body) throws SendGridException{
206
    	SendGrid sendgrid = new SendGrid("profitmandi", "pma20aug");
207
		SendGrid.Email email = new SendGrid.Email();
208
		email.setFrom(CRMConstants.PROFIT_MANDI_EMAIL_SENDER);
209
		email.setFromName("ProfitMandi");
210
		email.addTo(addTo);
211
		email.addBcc("backup@saholic.com");
212
		email.setSubject(subject);
213
		email.setHtml(body);
214
		log.info("Mail Body:- "+body);
215
		sendgrid.send(email);
216
		log.info("Mail sent to user:- "+addTo);
217
    }
3339 mandeep.dh 218
 
3405 mandeep.dh 219
    public String markAsRead() {
3390 mandeep.dh 220
        try {
3405 mandeep.dh 221
            crmServiceClient = new CRMClient().getClient();
222
            crmServiceClient.markAsRead(Long.parseLong(activityId),
223
                    CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId());
3390 mandeep.dh 224
        } catch (NumberFormatException e) {
225
            log.error("Could not parse activity id from" + activityId, e);
226
        } catch (TException e) {
3405 mandeep.dh 227
            log.error("Could not mark the activityId: " + activityId
228
                    + " as read", e);
3390 mandeep.dh 229
        }
230
 
3405 mandeep.dh 231
        return index();
3390 mandeep.dh 232
    }
233
 
3711 mandeep.dh 234
    public String getUnreadActivities() {
235
        try {
236
            SearchFilter searchFilter = new SearchFilter();
237
            searchFilter.setIsActivityRead(false);
238
            searchFilter.setActivityCreatorIds(Collections.singletonList(CRMConstants.ADMIN_AGENT_ID));
239
 
14906 manish.sha 240
            if(notShowPmTickets!=null && !notShowPmTickets.isEmpty() && "yes".equalsIgnoreCase(notShowPmTickets)){
241
            	searchFilter.setNotShowPmTickets(true);
14911 manish.sha 242
            	searchFilter.setTicketCategoryList(new ArrayList<TicketCategory>());
243
                searchFilter.getTicketCategoryList().add(TicketCategory.PROFITMANDI_CASHBACK);
244
                searchFilter.getTicketCategoryList().add(TicketCategory.PROFITMANDI_FEEDBACK);
245
                searchFilter.getTicketCategoryList().add(TicketCategory.PROFITMANDI_ORDER_NOT_SEEN);
246
                searchFilter.getTicketCategoryList().add(TicketCategory.PROFITMANDI_OTHER);
247
                searchFilter.getTicketCategoryList().add(TicketCategory.PROFITMANDI_RECHARGE_ISSUE);
14903 manish.sha 248
            }
249
 
3711 mandeep.dh 250
            searchFilter.setTicketAssigneeIds(Collections
251
                    .singletonList(CRMAuthorizingRealm.getAgent(
252
                            currentAgentEmailId).getId()));
253
 
254
            crmServiceClient = new CRMClient().getClient();
255
            activities = crmServiceClient.getActivities(searchFilter);
256
        } catch (TException e) {
257
            log.error("Could not get agentId for: " + currentAgentEmailId, e);
258
        }
259
 
260
        return INDEX;
261
    }
262
 
3339 mandeep.dh 263
    public ActivityType[] getActivityTypes() {
264
        return ActivityType.values();
265
    }
266
 
267
    public Agent getAgent(long agentId) throws TException {
3390 mandeep.dh 268
        return CRMAuthorizingRealm.getAgent(agentId);
3339 mandeep.dh 269
    }
270
 
5203 amar.kumar 271
    public ActivityCommonDescriptionMatrix[] getActivityCommonDescriptionMatrix () {
272
        return CRMConstants.ActivityCommonDescriptionMatrix.values();
273
    }
274
 
16208 manish.sha 275
    public ActivityPMCommonDescriptionMatrix[] getPMActivityCommonDescriptionMatrix(){
276
    	return CRMConstants.ActivityPMCommonDescriptionMatrix.values();
277
    }
278
 
3339 mandeep.dh 279
    public String getDescription() {
280
        return description;
281
    }
282
 
283
    public void setDescription(String description) {
284
        this.description = description;
285
    }
286
 
5203 amar.kumar 287
    public String getCommonDescription() {
288
		return commonDescription;
289
	}
290
 
291
	public void setCommonDescription(String commonDescription) {
292
		this.commonDescription = commonDescription;
293
	}
294
 
295
	public String getType() {
3339 mandeep.dh 296
        return type;
297
    }
298
 
299
    public void setType(String type) {
300
        this.type = type;
301
    }
302
 
303
    public String getCustomerMobileNumber() {
304
        return customerMobileNumber;
305
    }
306
 
307
    public void setCustomerMobileNumber(String customerMobileNumber) {
308
        this.customerMobileNumber = customerMobileNumber;
309
    }
310
 
311
    public List<Activity> getActivities() {
312
        return activities;
313
    }
314
 
315
    public String getCreatorId() {
316
        return creatorId;
317
    }
318
 
319
    public void setCreatorId(String creatorId) {
320
        this.creatorId = creatorId;
321
    }
3390 mandeep.dh 322
 
323
    public String getActivityId() {
324
        return activityId;
325
    }
326
 
327
    public void setActivityId(String activityId) {
328
        this.activityId = activityId;
329
    }
3397 mandeep.dh 330
 
331
    public String[] getAgentIds() {
332
        return agentIds;
333
    }
334
 
335
    public void setAgentIds(String[] agentIds) {
336
        this.agentIds = agentIds;
337
    }
338
 
339
    public String getStartTimestamp() {
340
        return startTimestamp;
341
    }
342
 
343
    public void setStartTimestamp(String startTimestamp) {
344
        this.startTimestamp = startTimestamp;
345
    }
346
 
347
    public String getEndTimestamp() {
348
        return endTimestamp;
349
    }
350
 
351
    public void setEndTimestamp(String endTimestamp) {
352
        this.endTimestamp = endTimestamp;
353
    }
354
 
355
    public void setActivities(List<Activity> activities) {
356
        this.activities = activities;
357
    }
3405 mandeep.dh 358
 
359
    public String getCustomerEmailId() {
360
        return customerEmailId;
361
    }
362
 
363
    public void setCustomerEmailId(String customerEmailId) {
364
        this.customerEmailId = customerEmailId;
365
    }
366
 
367
    public String getUserId() {
368
        return userId;
369
    }
370
 
371
    public void setUserId(String userId) {
372
        this.userId = userId;
373
    }
374
 
375
    public User getUser(Long userId) {
376
        User user = null;
377
 
378
        try {
379
            userContextServiceClient = new UserClient().getClient();
380
            user = userContextServiceClient.getUserById(userId);
381
        } catch (UserContextException e) {
382
            String errorString = "Could not fetch user for " + userId;
383
            log.error(errorString, e);
384
            addActionError(errorString);
385
        } catch (TException e) {
386
            String errorString = "Could not create client";
387
            log.error(errorString, e);
388
            addActionError(errorString);
389
        }
390
 
391
        return user;
392
    }
393
 
394
    public String getCustomerName() {
395
        return customerName;
396
    }
397
 
398
    public void setCustomerName(String customerName) {
399
        this.customerName = customerName;
400
    }
4142 mandeep.dh 401
 
402
    public String getCategory() {
403
        return category;
404
    }
405
 
406
    public void setCategory(String category) {
407
        this.category = category;
408
    }
14898 manish.sha 409
 
410
	public String getNotShowPmTickets() {
411
		return notShowPmTickets;
412
	}
413
 
14904 manish.sha 414
	public void setNotShowPmTickets(String notShowPmTickets) {
14898 manish.sha 415
		this.notShowPmTickets = notShowPmTickets;
416
	}
16092 amit.gupta 417
 
418
	public String safeHtml(String htmlString){
419
		try{
420
			return StringEscapeUtils.escapeHtml(htmlString);
421
		}catch (Exception e){
422
			return "This ticket need to be ignored";
423
		}
424
	}
3339 mandeep.dh 425
}