Subversion Repositories SmartDukaan

Rev

Rev 17081 | Rev 17197 | 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 {
17082 manish.sha 139
    	String addTo[] = new String[]{"amit.sirohi@shop2020.in","rajneesh.arora@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));
17079 manish.sha 194
        try{
195
	        if (ActivityType.RECEIVED_CALL_FROM_CUSTOMER.equals(ActivityType.valueOf(type))) {
17082 manish.sha 196
 
17081 manish.sha 197
	        	if(commonDescription==null || commonDescription.isEmpty()){
198
	        		commonDescription = "Received Call From Customer || Email Id- ";
199
	        		if (customerEmailId != null && !customerEmailId.isEmpty()) {
200
	        			commonDescription = commonDescription+customerEmailId;
201
	        		}
202
	        		if(customerMobileNumber != null
203
	                        && !customerMobileNumber.isEmpty()) {
204
	        			commonDescription = commonDescription+" & Mobile No:- "+customerMobileNumber;
205
	        		}
206
	        	}else{
207
	        		if (customerEmailId != null && !customerEmailId.isEmpty()) {
208
	        			commonDescription = commonDescription+" || Email Id:- "+customerEmailId;
209
	        		}
210
	        		if(customerMobileNumber != null
211
	                        && !customerMobileNumber.isEmpty()) {
212
	        			commonDescription = commonDescription+" & Mobile No:- "+customerMobileNumber;
213
	        		}
17079 manish.sha 214
	        	}
17082 manish.sha 215
	        	sendMailtoManagersUsingGmailUtils(commonDescription, addTo, description);
17079 manish.sha 216
	        }
217
        }
218
	    catch(Exception e){
219
	    	e.printStackTrace();
17082 manish.sha 220
	    	try{
221
	    		sendMailToManagers(commonDescription, addTo, description);
222
	    	}
223
	    	catch(Exception e1){
224
	    		e1.printStackTrace();
225
	    	}
17079 manish.sha 226
	    }
3339 mandeep.dh 227
 
228
        return index();
229
    }
17079 manish.sha 230
 
231
    private void sendMailToManagers(String subject, String[] addTo, String body) throws SendGridException{
232
    	SendGrid sendgrid = new SendGrid("profitmandi", "pma20aug");
233
		SendGrid.Email email = new SendGrid.Email();
234
		email.setFrom(CRMConstants.PROFIT_MANDI_EMAIL_SENDER);
235
		email.setFromName("ProfitMandi");
236
		email.addTo(addTo);
237
		email.addBcc("backup@saholic.com");
238
		email.setSubject(subject);
239
		email.setHtml(body);
240
		log.info("Mail Body:- "+body);
241
		sendgrid.send(email);
242
		log.info("Mail sent to user:- "+addTo);
243
    }
17082 manish.sha 244
 
245
    public void sendMailtoManagersUsingGmailUtils(String subject, String[] addTo, String body) throws MessagingException{
246
    	GmailUtils mailer = new GmailUtils();
247
		mailer.sendSSLMessage(addTo, subject, body, "build-staging@shop2020.in", "shop2020", new ArrayList<File>());
248
    }
3339 mandeep.dh 249
 
3405 mandeep.dh 250
    public String markAsRead() {
3390 mandeep.dh 251
        try {
3405 mandeep.dh 252
            crmServiceClient = new CRMClient().getClient();
253
            crmServiceClient.markAsRead(Long.parseLong(activityId),
254
                    CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId());
3390 mandeep.dh 255
        } catch (NumberFormatException e) {
256
            log.error("Could not parse activity id from" + activityId, e);
257
        } catch (TException e) {
3405 mandeep.dh 258
            log.error("Could not mark the activityId: " + activityId
259
                    + " as read", e);
3390 mandeep.dh 260
        }
261
 
3405 mandeep.dh 262
        return index();
3390 mandeep.dh 263
    }
264
 
3711 mandeep.dh 265
    public String getUnreadActivities() {
266
        try {
267
            SearchFilter searchFilter = new SearchFilter();
268
            searchFilter.setIsActivityRead(false);
269
            searchFilter.setActivityCreatorIds(Collections.singletonList(CRMConstants.ADMIN_AGENT_ID));
270
 
14906 manish.sha 271
            if(notShowPmTickets!=null && !notShowPmTickets.isEmpty() && "yes".equalsIgnoreCase(notShowPmTickets)){
272
            	searchFilter.setNotShowPmTickets(true);
14911 manish.sha 273
            	searchFilter.setTicketCategoryList(new ArrayList<TicketCategory>());
274
                searchFilter.getTicketCategoryList().add(TicketCategory.PROFITMANDI_CASHBACK);
275
                searchFilter.getTicketCategoryList().add(TicketCategory.PROFITMANDI_FEEDBACK);
276
                searchFilter.getTicketCategoryList().add(TicketCategory.PROFITMANDI_ORDER_NOT_SEEN);
277
                searchFilter.getTicketCategoryList().add(TicketCategory.PROFITMANDI_OTHER);
278
                searchFilter.getTicketCategoryList().add(TicketCategory.PROFITMANDI_RECHARGE_ISSUE);
14903 manish.sha 279
            }
280
 
3711 mandeep.dh 281
            searchFilter.setTicketAssigneeIds(Collections
282
                    .singletonList(CRMAuthorizingRealm.getAgent(
283
                            currentAgentEmailId).getId()));
284
 
285
            crmServiceClient = new CRMClient().getClient();
286
            activities = crmServiceClient.getActivities(searchFilter);
287
        } catch (TException e) {
288
            log.error("Could not get agentId for: " + currentAgentEmailId, e);
289
        }
290
 
291
        return INDEX;
292
    }
293
 
3339 mandeep.dh 294
    public ActivityType[] getActivityTypes() {
295
        return ActivityType.values();
296
    }
297
 
298
    public Agent getAgent(long agentId) throws TException {
3390 mandeep.dh 299
        return CRMAuthorizingRealm.getAgent(agentId);
3339 mandeep.dh 300
    }
301
 
5203 amar.kumar 302
    public ActivityCommonDescriptionMatrix[] getActivityCommonDescriptionMatrix () {
303
        return CRMConstants.ActivityCommonDescriptionMatrix.values();
304
    }
305
 
16208 manish.sha 306
    public ActivityPMCommonDescriptionMatrix[] getPMActivityCommonDescriptionMatrix(){
307
    	return CRMConstants.ActivityPMCommonDescriptionMatrix.values();
308
    }
309
 
3339 mandeep.dh 310
    public String getDescription() {
311
        return description;
312
    }
313
 
314
    public void setDescription(String description) {
315
        this.description = description;
316
    }
317
 
5203 amar.kumar 318
    public String getCommonDescription() {
319
		return commonDescription;
320
	}
321
 
322
	public void setCommonDescription(String commonDescription) {
323
		this.commonDescription = commonDescription;
324
	}
325
 
326
	public String getType() {
3339 mandeep.dh 327
        return type;
328
    }
329
 
330
    public void setType(String type) {
331
        this.type = type;
332
    }
333
 
334
    public String getCustomerMobileNumber() {
335
        return customerMobileNumber;
336
    }
337
 
338
    public void setCustomerMobileNumber(String customerMobileNumber) {
339
        this.customerMobileNumber = customerMobileNumber;
340
    }
341
 
342
    public List<Activity> getActivities() {
343
        return activities;
344
    }
345
 
346
    public String getCreatorId() {
347
        return creatorId;
348
    }
349
 
350
    public void setCreatorId(String creatorId) {
351
        this.creatorId = creatorId;
352
    }
3390 mandeep.dh 353
 
354
    public String getActivityId() {
355
        return activityId;
356
    }
357
 
358
    public void setActivityId(String activityId) {
359
        this.activityId = activityId;
360
    }
3397 mandeep.dh 361
 
362
    public String[] getAgentIds() {
363
        return agentIds;
364
    }
365
 
366
    public void setAgentIds(String[] agentIds) {
367
        this.agentIds = agentIds;
368
    }
369
 
370
    public String getStartTimestamp() {
371
        return startTimestamp;
372
    }
373
 
374
    public void setStartTimestamp(String startTimestamp) {
375
        this.startTimestamp = startTimestamp;
376
    }
377
 
378
    public String getEndTimestamp() {
379
        return endTimestamp;
380
    }
381
 
382
    public void setEndTimestamp(String endTimestamp) {
383
        this.endTimestamp = endTimestamp;
384
    }
385
 
386
    public void setActivities(List<Activity> activities) {
387
        this.activities = activities;
388
    }
3405 mandeep.dh 389
 
390
    public String getCustomerEmailId() {
391
        return customerEmailId;
392
    }
393
 
394
    public void setCustomerEmailId(String customerEmailId) {
395
        this.customerEmailId = customerEmailId;
396
    }
397
 
398
    public String getUserId() {
399
        return userId;
400
    }
401
 
402
    public void setUserId(String userId) {
403
        this.userId = userId;
404
    }
405
 
406
    public User getUser(Long userId) {
407
        User user = null;
408
 
409
        try {
410
            userContextServiceClient = new UserClient().getClient();
411
            user = userContextServiceClient.getUserById(userId);
412
        } catch (UserContextException e) {
413
            String errorString = "Could not fetch user for " + userId;
414
            log.error(errorString, e);
415
            addActionError(errorString);
416
        } catch (TException e) {
417
            String errorString = "Could not create client";
418
            log.error(errorString, e);
419
            addActionError(errorString);
420
        }
421
 
422
        return user;
423
    }
424
 
425
    public String getCustomerName() {
426
        return customerName;
427
    }
428
 
429
    public void setCustomerName(String customerName) {
430
        this.customerName = customerName;
431
    }
4142 mandeep.dh 432
 
433
    public String getCategory() {
434
        return category;
435
    }
436
 
437
    public void setCategory(String category) {
438
        this.category = category;
439
    }
14898 manish.sha 440
 
441
	public String getNotShowPmTickets() {
442
		return notShowPmTickets;
443
	}
444
 
14904 manish.sha 445
	public void setNotShowPmTickets(String notShowPmTickets) {
14898 manish.sha 446
		this.notShowPmTickets = notShowPmTickets;
447
	}
16092 amit.gupta 448
 
449
	public String safeHtml(String htmlString){
450
		try{
451
			return StringEscapeUtils.escapeHtml(htmlString);
452
		}catch (Exception e){
453
			return "This ticket need to be ignored";
454
		}
455
	}
3339 mandeep.dh 456
}