Subversion Repositories SmartDukaan

Rev

Rev 3499 | Rev 3546 | 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
 
3422 mandeep.dh 212
            if (id != null && !id.isEmpty()) {
213
                searchFilter.setTicketId(Long.parseLong(id));
214
            }
215
 
3499 mandeep.dh 216
            if (status != null && !status.isEmpty()) {
217
                searchFilter.setTicketStatus(TicketStatus.valueOf(status));
218
            }
219
 
220
            if (category != null && !category.isEmpty()) {
221
                searchFilter.setTicketCategory(TicketCategory.valueOf(category));
222
            }
223
 
3390 mandeep.dh 224
            crmServiceClient         = new CRMClient().getClient();
3405 mandeep.dh 225
            tickets = crmServiceClient.getTickets(searchFilter);
3137 mandeep.dh 226
        } catch (TException e) {
227
            String errorString = "Error getting tickets for "
228
                    + currentAgentEmailId;
229
            log.error(errorString, e);
230
            addActionError(errorString);
231
        }
232
 
3405 mandeep.dh 233
        return index();
3137 mandeep.dh 234
    }
235
 
3405 mandeep.dh 236
    /**
237
     * Returns tickets assigned to the current logged in agent
238
     *
239
     * @return
240
     */
3397 mandeep.dh 241
    public String getMyOpenTickets() {
3137 mandeep.dh 242
        try {
3390 mandeep.dh 243
            Agent agent = CRMAuthorizingRealm.getAgent(currentAgentEmailId);
244
 
245
            SearchFilter searchFilter = new SearchFilter();
246
            searchFilter.setTicketAssigneeIds(Collections.singletonList(agent.getId()));
247
            crmServiceClient         = new CRMClient().getClient();
3397 mandeep.dh 248
            for (Ticket ticket : crmServiceClient.getTickets(searchFilter)) {
249
                if (TicketStatus.OPEN.equals(ticket.getStatus())) {
250
                    tickets.add(ticket);
251
                }
252
            }
3137 mandeep.dh 253
        } catch (TException e) {
3339 mandeep.dh 254
            String errorString = "Error getting tickets for "
255
                    + currentAgentEmailId;
3137 mandeep.dh 256
            log.error(errorString, e);
257
            addActionError(errorString);
258
        }
259
 
3405 mandeep.dh 260
        return index();
3137 mandeep.dh 261
    }
262
 
263
    public String getUnassignedTickets() {
264
        try {
3390 mandeep.dh 265
            crmServiceClient         = new CRMClient().getClient();
3137 mandeep.dh 266
            tickets = crmServiceClient.getUnassignedTickets();
267
        } catch (TException e) {
3339 mandeep.dh 268
            String errorString = "Error getting tickets for "
269
                    + currentAgentEmailId;
3137 mandeep.dh 270
            log.error(errorString, e);
271
            addActionError(errorString);
272
        }
273
 
3405 mandeep.dh 274
        return index();
3137 mandeep.dh 275
    }
276
 
3405 mandeep.dh 277
    public String update() {
278
        try {
279
            long creatorId = CRMAuthorizingRealm.getAgent(currentAgentEmailId)
280
                    .getId();
281
 
282
            // Only subject and close date are editable fields for a ticket
283
            Ticket ticket = new Ticket();
284
            ticket.setId(Long.parseLong(id));
285
            ticket.setDescription(description);
286
            ticket.setCategory(TicketCategory.valueOf(category));
287
            ticket.setPriority(TicketPriority.valueOf(priority));
288
            ticket.setStatus(TicketStatus.valueOf(status));
289
 
290
            // Update when a ticket is closed!
291
            if (TicketStatus.CLOSED.name().equals(status)) {
292
                ticket.setCloseDate(new Date().getTime());
293
            }
294
 
295
            if (activityDescription == null || activityDescription.isEmpty()) {
296
                activityDescription = "Updating ticket fields";
297
            }
298
 
299
            Activity activity = new Activity();
300
            activity.setDescription(activityDescription);
301
            activity.setType(ActivityType.valueOf(activityType));
302
            activity.setTicketPriority(TicketPriority.valueOf(priority));
303
            activity.setTicketStatus(TicketStatus.valueOf(status));
304
            activity.setCreatorId(creatorId);
305
            activity.setTicketCategory(TicketCategory.valueOf(category));
306
            activity.setTicketDescription(description);
307
 
308
            if (userId != null && !userId.isEmpty()) {
309
                activity.setCustomerId(Long.parseLong(userId));
310
            }
311
 
312
            log.info(activityType);
313
            if (ActivityType.SEND_EMAIL_TO_CUSTOMER.equals(activity.getType())) {
314
                log.info("Sending mail");
315
                Client helperClient = new HelperClient().getClient();
316
                activity.setUserEmailId(helperClient.saveUserEmailForSending(
317
                        customerEmailId, CRMConstants.CRM_EMAIL_SENDOR, subject,
3519 mandeep.dh 318
                        body, null, CRMConstants.CRM_EMAIL_TYPE));
3405 mandeep.dh 319
 
320
                // We change activityType to OTHER when pop up box for email
321
                // closes
322
                activity.setDescription("Subject: " + subject + "\n\n"
323
                        + "Body: " + body);
324
            }
325
 
326
            // handling null values appropriately
327
            if (assigneeEmailId != null && !assigneeEmailId.isEmpty()) {
328
                long assigneeId = CRMAuthorizingRealm.getAgent(
329
                        assigneeEmailId).getId();
330
                ticket.setAssigneeId(assigneeId);
331
                activity.setTicketAssigneeId(assigneeId);
332
            }
333
 
334
            User user = null;
335
            userContextServiceClient = new UserClient().getClient();
336
            try {
337
                if (customerName != null && !customerName.isEmpty()) {
338
                    ticket.setCustomerName(customerName);
339
                    activity.setCustomerName(customerName);
340
                }
341
 
342
                if (customerEmailId != null && !customerEmailId.isEmpty()) {
343
                    ticket.setCustomerEmailId(customerEmailId);
344
                    activity.setCustomerEmailId(customerEmailId);
345
                    user = userContextServiceClient.getUserByEmail(customerEmailId);
346
                }
347
 
348
                if ((user == null || user.getUserId() == -1) && customerMobileNumber != null && !customerMobileNumber.isEmpty()) {
349
                    ticket.setCustomerMobileNumber(customerMobileNumber);
350
                    activity.setCustomerMobileNumber(customerMobileNumber);
351
                    user = userContextServiceClient.getUserByMobileNumber(Long.parseLong(customerMobileNumber));
352
                }
353
            } catch (UserContextException e) {
354
                log.error("Could not fetch user for: " + customerEmailId + " "
355
                        + customerMobileNumber + " " + customerName, e);
356
            }
357
 
358
            if (user != null && user.getUserId() != -1) {
359
                ticket.setCustomerId(user.getUserId());
360
                activity.setCustomerId(user.getUserId());
361
            }
362
 
363
            crmServiceClient = new CRMClient().getClient();
364
            crmServiceClient.updateTicket(ticket, activity);
365
        } catch (TException e) {
366
            log.error("Error while updating ticket", e);
367
            return EXCEPTION;
368
        } catch (HelperServiceException hse) {
369
            log.error("Error while sending mail", hse);
370
            return EXCEPTION;
371
        }
372
 
373
        return index();
374
    }
375
 
3137 mandeep.dh 376
    public User getUser(Long userId) {
377
        User user = null;
378
 
379
        try {
3390 mandeep.dh 380
            userContextServiceClient = new UserClient().getClient();
3137 mandeep.dh 381
            user = userContextServiceClient.getUserById(userId);
382
        } catch (UserContextException e) {
383
            String errorString = "Could not fetch user for " + userId;
384
            log.error(errorString, e);
385
            addActionError(errorString);
386
        } catch (TException e) {
387
            String errorString = "Could not create client";
388
            log.error(errorString, e);
389
            addActionError(errorString);
390
        }
391
 
392
        return user;
393
    }
394
 
3405 mandeep.dh 395
    public ActivityType[] getActivityTypes() {
396
        return ActivityType.values();
397
    }
398
 
399
    public TicketStatus[] getTicketStatuses() {
400
        return TicketStatus.values();
401
    }
402
 
3390 mandeep.dh 403
    public Agent getAgent(long agentId) throws TException {
404
        return CRMAuthorizingRealm.getAgent(agentId);
3137 mandeep.dh 405
    }
406
 
3339 mandeep.dh 407
    public List<Agent> getAllAgents() {
3390 mandeep.dh 408
        return CRMAuthorizingRealm.getAgents();
3339 mandeep.dh 409
    }
410
 
411
    public TicketCategory[] getTicketCategories() {
412
        return TicketCategory.values();
413
    }
414
 
415
    public TicketPriority[] getTicketPriorities() {
416
        return TicketPriority.values();
417
    }
418
 
3137 mandeep.dh 419
    public List<Ticket> getTickets() {
420
        return tickets;
421
    }
422
 
423
    public void setTickets(List<Ticket> tickets) {
424
        this.tickets = tickets;
425
    }
3339 mandeep.dh 426
 
3405 mandeep.dh 427
    public String getCustomerEmailId() {
428
        return customerEmailId;
3339 mandeep.dh 429
    }
430
 
3405 mandeep.dh 431
    public void setCustomerEmailId(String customerEmailId) {
432
        this.customerEmailId = customerEmailId;
3339 mandeep.dh 433
    }
434
 
435
    public String getDescription() {
436
        return description;
437
    }
438
 
439
    public void setDescription(String description) {
440
        this.description = description;
441
    }
442
 
443
    public String getAssigneeEmailId() {
444
        return assigneeEmailId;
445
    }
446
 
447
    public void setAssigneeEmailId(String assigneeEmailId) {
448
        this.assigneeEmailId = assigneeEmailId;
449
    }
450
 
451
    public String getPriority() {
452
        return priority;
453
    }
454
 
455
    public void setPriority(String priority) {
456
        this.priority = priority;
457
    }
458
 
459
    public String getCategory() {
460
        return category;
461
    }
462
 
463
    public void setCategory(String category) {
464
        this.category = category;
465
    }
466
 
467
    public String getOrderId() {
468
        return orderId;
469
    }
470
 
471
    public void setOrderId(String orderId) {
472
        this.orderId = orderId;
473
    }
3397 mandeep.dh 474
 
475
    public String[] getAgentIds() {
476
        return agentIds;
477
    }
478
 
479
    public void setAgentIds(String[] agentIds) {
480
        this.agentIds = agentIds;
481
    }
482
 
483
    public String getStartTimestamp() {
484
        return startTimestamp;
485
    }
486
 
487
    public void setStartTimestamp(String startTimestamp) {
488
        this.startTimestamp = startTimestamp;
489
    }
490
 
491
    public String getEndTimestamp() {
492
        return endTimestamp;
493
    }
494
 
495
    public void setEndTimestamp(String endTimestamp) {
496
        this.endTimestamp = endTimestamp;
497
    }
3405 mandeep.dh 498
 
499
    public String getUserId() {
500
        return userId;
501
    }
502
 
503
    public void setUserId(String userId) {
504
        this.userId = userId;
505
    }
506
 
507
    public String getId() {
508
        return id;
509
    }
510
 
511
    public void setId(String id) {
512
        this.id = id;
513
    }
514
 
515
    public String getActivityDescription() {
516
        return activityDescription;
517
    }
518
 
519
    public void setActivityDescription(String activityDescription) {
520
        this.activityDescription = activityDescription;
521
    }
522
 
523
    public String getStatus() {
524
        return status;
525
    }
526
 
527
    public void setStatus(String status) {
528
        this.status = status;
529
    }
530
 
531
    public String getActivityType() {
532
        return activityType;
533
    }
534
 
535
    public void setActivityType(String activityType) {
536
        this.activityType = activityType;
537
    }
538
 
539
    public Ticket getTicket() {
540
        return ticket;
541
    }
542
 
543
    public void setTicket(Ticket ticket) {
544
        this.ticket = ticket;
545
    }
546
 
547
    public List<Activity> getActivities() {
548
        return activities;
549
    }
550
 
551
    public void setActivities(List<Activity> activities) {
552
        this.activities = activities;
553
    }
554
 
555
    public String getSubject() {
556
        return subject;
557
    }
558
 
559
    public void setSubject(String subject) {
560
        this.subject = subject;
561
    }
562
 
563
    public String getBody() {
564
        return body;
565
    }
566
 
567
    public void setBody(String body) {
568
        this.body = body;
569
    }
570
 
571
    public String getCustomerName() {
572
        return customerName;
573
    }
574
 
575
    public void setCustomerName(String customerName) {
576
        this.customerName = customerName;
577
    }
578
 
579
    public String getCustomerMobileNumber() {
580
        return customerMobileNumber;
581
    }
582
 
583
    public void setCustomerMobileNumber(String customerMobileNumber) {
584
        this.customerMobileNumber = customerMobileNumber;
585
    }
3137 mandeep.dh 586
}