Subversion Repositories SmartDukaan

Rev

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