Subversion Repositories SmartDukaan

Rev

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