Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
3137 mandeep.dh 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
3339 mandeep.dh 6
import in.shop2020.crm.Activity;
7
import in.shop2020.crm.ActivityType;
3137 mandeep.dh 8
import in.shop2020.crm.Agent;
3390 mandeep.dh 9
import in.shop2020.crm.SearchFilter;
3137 mandeep.dh 10
import in.shop2020.crm.Ticket;
3339 mandeep.dh 11
import in.shop2020.crm.TicketCategory;
12
import in.shop2020.crm.TicketPriority;
3137 mandeep.dh 13
import in.shop2020.crm.TicketStatus;
14
import in.shop2020.model.v1.user.User;
15
import in.shop2020.model.v1.user.UserContextException;
3390 mandeep.dh 16
import in.shop2020.serving.auth.CRMAuthorizingRealm;
17
import in.shop2020.thrift.clients.CRMClient;
3405 mandeep.dh 18
import in.shop2020.thrift.clients.HelperClient;
3390 mandeep.dh 19
import in.shop2020.thrift.clients.UserClient;
3405 mandeep.dh 20
import in.shop2020.util.CRMConstants;
21
import in.shop2020.utils.HelperService.Client;
22
import in.shop2020.utils.HelperServiceException;
3137 mandeep.dh 23
 
3397 mandeep.dh 24
import java.text.ParseException;
3137 mandeep.dh 25
import java.util.ArrayList;
3390 mandeep.dh 26
import java.util.Collections;
3405 mandeep.dh 27
import java.util.Date;
3137 mandeep.dh 28
import java.util.List;
29
 
3405 mandeep.dh 30
import org.apache.shiro.SecurityUtils;
3137 mandeep.dh 31
import org.apache.thrift.TException;
32
 
33
/**
34
 * @author mandeep
35
 * 
36
 */
37
public class TicketsController extends BaseController {
38
 
39
    /**
40
     * 
41
     */
42
    private static final long serialVersionUID = 1L;
43
 
44
    List<Ticket>              tickets          = new ArrayList<Ticket>();
3405 mandeep.dh 45
    private String            customerEmailId;
3339 mandeep.dh 46
    private String            description;
47
    private String            assigneeEmailId;
48
    private String            priority;
49
    private String            category;
50
    private String            orderId;
3397 mandeep.dh 51
    private String[]          agentIds;
52
    private String            startTimestamp;
53
    private String            endTimestamp;
3137 mandeep.dh 54
 
3405 mandeep.dh 55
    private String            userId;
56
    private String            id;
57
    private String            activityDescription;
58
    private String            status;
59
    private String            activityType;
60
    private Ticket            ticket;
61
    private List<Activity>    activities;
62
    private String            subject;
63
    private String            body;
64
    private String            customerName;
65
    private String            customerMobileNumber;
66
 
67
    public String index() {
68
        try {
69
            if (id != null && !id.isEmpty()) {
70
                SearchFilter searchFilter = new SearchFilter();
71
                crmServiceClient = new CRMClient().getClient();
72
                searchFilter.setTicketId(Long.parseLong(id));
73
                tickets = crmServiceClient.getTickets(searchFilter);
74
            }
75
        } catch (TException e) {
76
            log.error("Error while getting tickets", e);
77
            return EXCEPTION;
78
        }
79
 
80
        return INDEX;
81
    }
82
 
83
    public String edit() {
84
        try {
85
            long ticketId = Long.parseLong(id);
86
            SearchFilter searchFilter = new SearchFilter();
87
            searchFilter.setTicketId(ticketId);
88
            crmServiceClient = new CRMClient().getClient();
89
            ticket = crmServiceClient.getTickets(searchFilter).get(0);
90
            activities = crmServiceClient.getActivities(searchFilter);
91
            subject = createSubjectString(ticket);
92
 
93
            if (ticket.isSetCustomerId()) {
94
                userId = String.valueOf(ticket.getCustomerId());
95
            }
96
 
97
            customerEmailId = ticket.getCustomerEmailId();
98
        } catch (TException e) {
99
            log.error("Error while loading edit page", e);
100
            return EXCEPTION;
101
        }
102
 
103
        return EDIT;
104
    }
105
 
106
    private String createSubjectString(Ticket ticket) {
107
        return CRMConstants.CRM_SUBJECT_PREFIX_FOR_TICKET_ID + ticket.getId()
108
                + " " + ticket.getCategory().name();
109
    }
110
 
3339 mandeep.dh 111
    public String create() {
112
        try {
3390 mandeep.dh 113
            long creatorId = CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId();
3339 mandeep.dh 114
            Ticket ticket = new Ticket();
115
            ticket.setDescription(description);
116
            ticket.setCreatorId(creatorId);
117
            ticket.setStatus(TicketStatus.OPEN);
118
            ticket.setPriority(TicketPriority.valueOf(priority));
119
            ticket.setCategory(TicketCategory.valueOf(category));
120
 
121
            Activity activity = new Activity();
122
            activity.setDescription("Creating Ticket");
123
            activity.setType(ActivityType.OTHER);
124
            activity.setTicketPriority(TicketPriority.valueOf(priority));
125
            activity.setTicketStatus(TicketStatus.OPEN);
126
            activity.setCreatorId(creatorId);
127
            activity.setTicketCategory(TicketCategory.valueOf(category));
128
            activity.setTicketDescription(description);
129
 
130
            if (orderId != null && !orderId.isEmpty()) {
131
                ticket.setOrderId(Long.parseLong(orderId));
132
            }
133
 
3405 mandeep.dh 134
            if (userId != null && !userId.isEmpty()) {
135
                ticket.setCustomerId(Long.parseLong(userId));
136
                activity.setCustomerId(Long.parseLong(userId));
137
            }
138
            else {
3390 mandeep.dh 139
                User user = null;
140
                userContextServiceClient = new UserClient().getClient();
141
                try {
3405 mandeep.dh 142
                    if (customerName != null && !customerName.isEmpty()) {
143
                        ticket.setCustomerName(customerName);
144
                        activity.setCustomerName(customerName);
145
                    }
146
 
147
                    if (customerEmailId != null && !customerEmailId.isEmpty()) {
148
                        ticket.setCustomerEmailId(customerEmailId);
149
                        activity.setCustomerEmailId(customerEmailId);
150
                        user = userContextServiceClient.getUserByEmail(customerEmailId);
151
                    }
152
 
153
                    if ((user == null || user.getUserId() == -1) && customerMobileNumber != null && !customerMobileNumber.isEmpty()) {
154
                        ticket.setCustomerMobileNumber(customerMobileNumber);
155
                        activity.setCustomerMobileNumber(customerMobileNumber);
156
                        user = userContextServiceClient.getUserByMobileNumber(Long.parseLong(customerMobileNumber));
157
                    }
3390 mandeep.dh 158
                } catch (UserContextException e) {
3405 mandeep.dh 159
                    log.error("Could not fetch user for: " + customerEmailId + " "
160
                            + customerMobileNumber + " " + customerName, e);
3390 mandeep.dh 161
                }
3405 mandeep.dh 162
 
3390 mandeep.dh 163
                if (user != null && user.getUserId() != -1) {
164
                    ticket.setCustomerId(user.getUserId());
165
                    activity.setCustomerId(user.getUserId());
166
                }
3339 mandeep.dh 167
            }
168
 
169
            // handling null values appropriately
170
            if (assigneeEmailId != null && !assigneeEmailId.isEmpty()) {
3390 mandeep.dh 171
                long assigneeId = CRMAuthorizingRealm.getAgent(assigneeEmailId).getId();
3339 mandeep.dh 172
                ticket.setAssigneeId(assigneeId);
173
                activity.setTicketAssigneeId(assigneeId);
174
            }
175
 
3390 mandeep.dh 176
            crmServiceClient         = new CRMClient().getClient();
3405 mandeep.dh 177
            id = String.valueOf(crmServiceClient.insertTicket(ticket, activity));
3339 mandeep.dh 178
        } catch (TException e) {
179
            log.error("Error while creating ticket", e);
180
            return EXCEPTION;
181
        }
182
 
3405 mandeep.dh 183
        return index();
3339 mandeep.dh 184
    }
185
 
3405 mandeep.dh 186
    public boolean isAssigneeEditable() {
187
        return SecurityUtils.getSubject().hasRole("TeamLead");
188
    }
189
 
3397 mandeep.dh 190
    public String searchTickets() throws ParseException {
3137 mandeep.dh 191
        try {
3397 mandeep.dh 192
            SearchFilter searchFilter = new SearchFilter();
3405 mandeep.dh 193
            if (userId != null && !userId.isEmpty()) {
194
                searchFilter.setCustomerId(Long.parseLong(userId));
195
            }
196
 
3397 mandeep.dh 197
            if (agentIds != null && agentIds.length != 0) {
198
                searchFilter.setTicketAssigneeIds(new ArrayList<Long>());
199
                for (String agentId : agentIds) {
200
                    searchFilter.getTicketAssigneeIds().add(CRMAuthorizingRealm.getAgent(agentId).getId());
201
                }
202
            }
3137 mandeep.dh 203
 
3397 mandeep.dh 204
            if (startTimestamp != null && !startTimestamp.isEmpty()) {
205
                searchFilter.setStartTimestamp(SDF.parse(startTimestamp).getTime());
206
            }
207
 
208
            if (endTimestamp != null && !endTimestamp.isEmpty()) {
209
                searchFilter.setEndTimestamp(SDF.parse(endTimestamp).getTime());
210
            }
211
 
3390 mandeep.dh 212
            crmServiceClient         = new CRMClient().getClient();
3405 mandeep.dh 213
            tickets = crmServiceClient.getTickets(searchFilter);
3137 mandeep.dh 214
        } catch (TException e) {
215
            String errorString = "Error getting tickets for "
216
                    + currentAgentEmailId;
217
            log.error(errorString, e);
218
            addActionError(errorString);
219
        }
220
 
3405 mandeep.dh 221
        return index();
3137 mandeep.dh 222
    }
223
 
3405 mandeep.dh 224
    /**
225
     * Returns tickets assigned to the current logged in agent
226
     *
227
     * @return
228
     */
3397 mandeep.dh 229
    public String getMyOpenTickets() {
3137 mandeep.dh 230
        try {
3390 mandeep.dh 231
            Agent agent = CRMAuthorizingRealm.getAgent(currentAgentEmailId);
232
 
233
            SearchFilter searchFilter = new SearchFilter();
234
            searchFilter.setTicketAssigneeIds(Collections.singletonList(agent.getId()));
235
            crmServiceClient         = new CRMClient().getClient();
3397 mandeep.dh 236
            for (Ticket ticket : crmServiceClient.getTickets(searchFilter)) {
237
                if (TicketStatus.OPEN.equals(ticket.getStatus())) {
238
                    tickets.add(ticket);
239
                }
240
            }
3137 mandeep.dh 241
        } catch (TException e) {
3339 mandeep.dh 242
            String errorString = "Error getting tickets for "
243
                    + currentAgentEmailId;
3137 mandeep.dh 244
            log.error(errorString, e);
245
            addActionError(errorString);
246
        }
247
 
3405 mandeep.dh 248
        return index();
3137 mandeep.dh 249
    }
250
 
251
    public String getUnassignedTickets() {
252
        try {
3390 mandeep.dh 253
            crmServiceClient         = new CRMClient().getClient();
3137 mandeep.dh 254
            tickets = crmServiceClient.getUnassignedTickets();
255
        } catch (TException e) {
3339 mandeep.dh 256
            String errorString = "Error getting tickets for "
257
                    + currentAgentEmailId;
3137 mandeep.dh 258
            log.error(errorString, e);
259
            addActionError(errorString);
260
        }
261
 
3405 mandeep.dh 262
        return index();
3137 mandeep.dh 263
    }
264
 
3405 mandeep.dh 265
    public String update() {
266
        try {
267
            long creatorId = CRMAuthorizingRealm.getAgent(currentAgentEmailId)
268
                    .getId();
269
 
270
            // Only subject and close date are editable fields for a ticket
271
            Ticket ticket = new Ticket();
272
            ticket.setId(Long.parseLong(id));
273
            ticket.setDescription(description);
274
            ticket.setCategory(TicketCategory.valueOf(category));
275
            ticket.setPriority(TicketPriority.valueOf(priority));
276
            ticket.setStatus(TicketStatus.valueOf(status));
277
 
278
            // Update when a ticket is closed!
279
            if (TicketStatus.CLOSED.name().equals(status)) {
280
                ticket.setCloseDate(new Date().getTime());
281
            }
282
 
283
            if (activityDescription == null || activityDescription.isEmpty()) {
284
                activityDescription = "Updating ticket fields";
285
            }
286
 
287
            Activity activity = new Activity();
288
            activity.setDescription(activityDescription);
289
            activity.setType(ActivityType.valueOf(activityType));
290
            activity.setTicketPriority(TicketPriority.valueOf(priority));
291
            activity.setTicketStatus(TicketStatus.valueOf(status));
292
            activity.setCreatorId(creatorId);
293
            activity.setTicketCategory(TicketCategory.valueOf(category));
294
            activity.setTicketDescription(description);
295
 
296
            if (userId != null && !userId.isEmpty()) {
297
                activity.setCustomerId(Long.parseLong(userId));
298
            }
299
 
300
            log.info(activityType);
301
            if (ActivityType.SEND_EMAIL_TO_CUSTOMER.equals(activity.getType())) {
302
                log.info("Sending mail");
303
                Client helperClient = new HelperClient().getClient();
304
                activity.setUserEmailId(helperClient.saveUserEmailForSending(
305
                        customerEmailId, CRMConstants.CRM_EMAIL_SENDOR, subject,
306
                        "<pre>" + body + "</pre>", null, CRMConstants.CRM_EMAIL_TYPE));
307
 
308
                // We change activityType to OTHER when pop up box for email
309
                // closes
310
                activity.setDescription("Subject: " + subject + "\n\n"
311
                        + "Body: " + body);
312
            }
313
 
314
            // handling null values appropriately
315
            if (assigneeEmailId != null && !assigneeEmailId.isEmpty()) {
316
                long assigneeId = CRMAuthorizingRealm.getAgent(
317
                        assigneeEmailId).getId();
318
                ticket.setAssigneeId(assigneeId);
319
                activity.setTicketAssigneeId(assigneeId);
320
            }
321
 
322
            User user = null;
323
            userContextServiceClient = new UserClient().getClient();
324
            try {
325
                if (customerName != null && !customerName.isEmpty()) {
326
                    ticket.setCustomerName(customerName);
327
                    activity.setCustomerName(customerName);
328
                }
329
 
330
                if (customerEmailId != null && !customerEmailId.isEmpty()) {
331
                    ticket.setCustomerEmailId(customerEmailId);
332
                    activity.setCustomerEmailId(customerEmailId);
333
                    user = userContextServiceClient.getUserByEmail(customerEmailId);
334
                }
335
 
336
                if ((user == null || user.getUserId() == -1) && customerMobileNumber != null && !customerMobileNumber.isEmpty()) {
337
                    ticket.setCustomerMobileNumber(customerMobileNumber);
338
                    activity.setCustomerMobileNumber(customerMobileNumber);
339
                    user = userContextServiceClient.getUserByMobileNumber(Long.parseLong(customerMobileNumber));
340
                }
341
            } catch (UserContextException e) {
342
                log.error("Could not fetch user for: " + customerEmailId + " "
343
                        + customerMobileNumber + " " + customerName, e);
344
            }
345
 
346
            if (user != null && user.getUserId() != -1) {
347
                ticket.setCustomerId(user.getUserId());
348
                activity.setCustomerId(user.getUserId());
349
            }
350
 
351
            crmServiceClient = new CRMClient().getClient();
352
            crmServiceClient.updateTicket(ticket, activity);
353
        } catch (TException e) {
354
            log.error("Error while updating ticket", e);
355
            return EXCEPTION;
356
        } catch (HelperServiceException hse) {
357
            log.error("Error while sending mail", hse);
358
            return EXCEPTION;
359
        }
360
 
361
        return index();
362
    }
363
 
3137 mandeep.dh 364
    public User getUser(Long userId) {
365
        User user = null;
366
 
367
        try {
3390 mandeep.dh 368
            userContextServiceClient = new UserClient().getClient();
3137 mandeep.dh 369
            user = userContextServiceClient.getUserById(userId);
370
        } catch (UserContextException e) {
371
            String errorString = "Could not fetch user for " + userId;
372
            log.error(errorString, e);
373
            addActionError(errorString);
374
        } catch (TException e) {
375
            String errorString = "Could not create client";
376
            log.error(errorString, e);
377
            addActionError(errorString);
378
        }
379
 
380
        return user;
381
    }
382
 
3405 mandeep.dh 383
    public ActivityType[] getActivityTypes() {
384
        return ActivityType.values();
385
    }
386
 
387
    public TicketStatus[] getTicketStatuses() {
388
        return TicketStatus.values();
389
    }
390
 
3390 mandeep.dh 391
    public Agent getAgent(long agentId) throws TException {
392
        return CRMAuthorizingRealm.getAgent(agentId);
3137 mandeep.dh 393
    }
394
 
3339 mandeep.dh 395
    public List<Agent> getAllAgents() {
3390 mandeep.dh 396
        return CRMAuthorizingRealm.getAgents();
3339 mandeep.dh 397
    }
398
 
399
    public TicketCategory[] getTicketCategories() {
400
        return TicketCategory.values();
401
    }
402
 
403
    public TicketPriority[] getTicketPriorities() {
404
        return TicketPriority.values();
405
    }
406
 
3137 mandeep.dh 407
    public List<Ticket> getTickets() {
408
        return tickets;
409
    }
410
 
411
    public void setTickets(List<Ticket> tickets) {
412
        this.tickets = tickets;
413
    }
3339 mandeep.dh 414
 
3405 mandeep.dh 415
    public String getCustomerEmailId() {
416
        return customerEmailId;
3339 mandeep.dh 417
    }
418
 
3405 mandeep.dh 419
    public void setCustomerEmailId(String customerEmailId) {
420
        this.customerEmailId = customerEmailId;
3339 mandeep.dh 421
    }
422
 
423
    public String getDescription() {
424
        return description;
425
    }
426
 
427
    public void setDescription(String description) {
428
        this.description = description;
429
    }
430
 
431
    public String getAssigneeEmailId() {
432
        return assigneeEmailId;
433
    }
434
 
435
    public void setAssigneeEmailId(String assigneeEmailId) {
436
        this.assigneeEmailId = assigneeEmailId;
437
    }
438
 
439
    public String getPriority() {
440
        return priority;
441
    }
442
 
443
    public void setPriority(String priority) {
444
        this.priority = priority;
445
    }
446
 
447
    public String getCategory() {
448
        return category;
449
    }
450
 
451
    public void setCategory(String category) {
452
        this.category = category;
453
    }
454
 
455
    public String getOrderId() {
456
        return orderId;
457
    }
458
 
459
    public void setOrderId(String orderId) {
460
        this.orderId = orderId;
461
    }
3397 mandeep.dh 462
 
463
    public String[] getAgentIds() {
464
        return agentIds;
465
    }
466
 
467
    public void setAgentIds(String[] agentIds) {
468
        this.agentIds = agentIds;
469
    }
470
 
471
    public String getStartTimestamp() {
472
        return startTimestamp;
473
    }
474
 
475
    public void setStartTimestamp(String startTimestamp) {
476
        this.startTimestamp = startTimestamp;
477
    }
478
 
479
    public String getEndTimestamp() {
480
        return endTimestamp;
481
    }
482
 
483
    public void setEndTimestamp(String endTimestamp) {
484
        this.endTimestamp = endTimestamp;
485
    }
3405 mandeep.dh 486
 
487
    public String getUserId() {
488
        return userId;
489
    }
490
 
491
    public void setUserId(String userId) {
492
        this.userId = userId;
493
    }
494
 
495
    public String getId() {
496
        return id;
497
    }
498
 
499
    public void setId(String id) {
500
        this.id = id;
501
    }
502
 
503
    public String getActivityDescription() {
504
        return activityDescription;
505
    }
506
 
507
    public void setActivityDescription(String activityDescription) {
508
        this.activityDescription = activityDescription;
509
    }
510
 
511
    public String getStatus() {
512
        return status;
513
    }
514
 
515
    public void setStatus(String status) {
516
        this.status = status;
517
    }
518
 
519
    public String getActivityType() {
520
        return activityType;
521
    }
522
 
523
    public void setActivityType(String activityType) {
524
        this.activityType = activityType;
525
    }
526
 
527
    public Ticket getTicket() {
528
        return ticket;
529
    }
530
 
531
    public void setTicket(Ticket ticket) {
532
        this.ticket = ticket;
533
    }
534
 
535
    public List<Activity> getActivities() {
536
        return activities;
537
    }
538
 
539
    public void setActivities(List<Activity> activities) {
540
        this.activities = activities;
541
    }
542
 
543
    public String getSubject() {
544
        return subject;
545
    }
546
 
547
    public void setSubject(String subject) {
548
        this.subject = subject;
549
    }
550
 
551
    public String getBody() {
552
        return body;
553
    }
554
 
555
    public void setBody(String body) {
556
        this.body = body;
557
    }
558
 
559
    public String getCustomerName() {
560
        return customerName;
561
    }
562
 
563
    public void setCustomerName(String customerName) {
564
        this.customerName = customerName;
565
    }
566
 
567
    public String getCustomerMobileNumber() {
568
        return customerMobileNumber;
569
    }
570
 
571
    public void setCustomerMobileNumber(String customerMobileNumber) {
572
        this.customerMobileNumber = customerMobileNumber;
573
    }
3137 mandeep.dh 574
}