| 5407 |
amar.kumar |
1 |
package in.shop2020.crm.util;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.crm.ActivityType;
|
|
|
4 |
import in.shop2020.crm.TicketCategory;
|
|
|
5 |
import in.shop2020.crm.TicketPriority;
|
|
|
6 |
import in.shop2020.crm.TicketStatus;
|
|
|
7 |
import in.shop2020.crm.domain.Activity;
|
|
|
8 |
import in.shop2020.crm.domain.Ticket;
|
|
|
9 |
import in.shop2020.crm.handler.ActivityHandler;
|
|
|
10 |
import in.shop2020.crm.handler.TicketHandler;
|
|
|
11 |
import in.shop2020.model.v1.user.User;
|
|
|
12 |
import in.shop2020.model.v1.user.UserCommunication;
|
|
|
13 |
import in.shop2020.model.v1.user.UserCommunicationException;
|
|
|
14 |
import in.shop2020.model.v1.user.UserContextException;
|
|
|
15 |
import in.shop2020.thrift.clients.UserClient;
|
|
|
16 |
|
|
|
17 |
import java.util.List;
|
|
|
18 |
|
|
|
19 |
import org.apache.commons.logging.Log;
|
|
|
20 |
import org.apache.commons.logging.LogFactory;
|
|
|
21 |
import org.apache.thrift.TException;
|
|
|
22 |
import org.springframework.context.ApplicationContext;
|
|
|
23 |
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
|
|
24 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
25 |
|
|
|
26 |
public class UserCommunicationProcessorTask {
|
|
|
27 |
|
|
|
28 |
private static Log log = LogFactory.getLog(UserCommunicationProcessorTask.class);
|
|
|
29 |
private TicketHandler ticketHandler;
|
|
|
30 |
private ActivityHandler activityHandler;
|
|
|
31 |
private static List<UserCommunication> userCommunications;
|
|
|
32 |
|
|
|
33 |
private static final long ADMIN_AGENT_ID = 1;
|
| 6667 |
amar.kumar |
34 |
private static final long DEF_ASSIGNEE_FOR_ORDER_CANCEL = 34; //Assigning to Suraj
|
| 5407 |
amar.kumar |
35 |
//TODO Above field should be configurable
|
|
|
36 |
|
|
|
37 |
|
|
|
38 |
public UserCommunicationProcessorTask()
|
|
|
39 |
{
|
|
|
40 |
ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
|
|
|
41 |
ticketHandler = context.getBean(TicketHandler.class);
|
|
|
42 |
activityHandler = context.getBean(ActivityHandler.class);
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
public static void main(String[] args) {
|
|
|
46 |
UserCommunicationProcessorTask newTask = new UserCommunicationProcessorTask();
|
|
|
47 |
in.shop2020.model.v1.user.UserContextService.Client client;
|
|
|
48 |
try {
|
|
|
49 |
client = new UserClient().getClient();
|
|
|
50 |
userCommunications = client.getAllUserCommunications();
|
|
|
51 |
} catch(UserCommunicationException e ) {
|
|
|
52 |
log.fatal("Not able to remove UserCommunication" + e);
|
|
|
53 |
System.exit(1);
|
|
|
54 |
} catch(TException ex) {
|
|
|
55 |
log.fatal("Not able to remove UserCommunication" + ex);
|
|
|
56 |
System.exit(1);
|
|
|
57 |
}
|
|
|
58 |
if (userCommunications != null && !userCommunications.isEmpty()) {
|
|
|
59 |
log.info("Fetched " + userCommunications.size() + " UserCommunications");
|
|
|
60 |
for (UserCommunication userCommunication : userCommunications) {
|
| 5435 |
amar.kumar |
61 |
if(newTask.processUserCommunication(userCommunication)) {
|
|
|
62 |
try {
|
|
|
63 |
client = new UserClient().getClient();
|
|
|
64 |
client.removeUserCommunication(userCommunication.getId());
|
|
|
65 |
} catch(UserCommunicationException e) {
|
|
|
66 |
log.error("Not able to remove UserCommunication" + e);
|
|
|
67 |
} catch(TException ex) {
|
|
|
68 |
log.error("Not able to remove UserCommunication" + ex);
|
|
|
69 |
}
|
| 5407 |
amar.kumar |
70 |
}
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
}
|
|
|
74 |
|
| 5435 |
amar.kumar |
75 |
private boolean processUserCommunication(UserCommunication userCommunication) {
|
| 5407 |
amar.kumar |
76 |
try{
|
|
|
77 |
log.info("Processing userCommunication : " + userCommunication.getId());
|
|
|
78 |
|
|
|
79 |
Ticket ticket = new Ticket();
|
| 6176 |
amit.gupta |
80 |
|
| 5407 |
amar.kumar |
81 |
ticket.setProductName(userCommunication.getProductName());
|
|
|
82 |
TicketCategory tktCategory = TicketCategory.findByValue
|
|
|
83 |
((int)userCommunication.getCommunicationType().getValue());
|
|
|
84 |
if(tktCategory != null) {
|
|
|
85 |
ticket.setCategory(tktCategory);
|
|
|
86 |
} else {
|
|
|
87 |
ticket.setCategory(TicketCategory.OTHER);
|
|
|
88 |
}
|
| 6176 |
amit.gupta |
89 |
if(!ticket.getCategory().equals(TicketCategory.RECHARGE_RELATED)) {
|
|
|
90 |
ticket.setAirwayBillNo(userCommunication.getAirwaybillNo());
|
|
|
91 |
ticket.setDescription("From: " + userCommunication.getReplyTo() +
|
|
|
92 |
"\n\nSubject: " + userCommunication.getSubject() +
|
|
|
93 |
"\n\nBody: " + userCommunication.getMessage());
|
|
|
94 |
} else {
|
|
|
95 |
ticket.setDescription("From: " + userCommunication.getReplyTo() +
|
|
|
96 |
"\n\nSubject: " + userCommunication.getSubject() +
|
|
|
97 |
"\n\nBody: " + userCommunication.getMessage() +
|
|
|
98 |
"\n\nDevice Number:" + userCommunication.getAirwaybillNo());
|
|
|
99 |
}
|
| 5407 |
amar.kumar |
100 |
if(userCommunication.getCommunicationType().getValue() == 2) {
|
|
|
101 |
ticket.setAssigneeId(DEF_ASSIGNEE_FOR_ORDER_CANCEL);
|
|
|
102 |
}
|
|
|
103 |
ticket.setCustomerEmailId(userCommunication.getReplyTo());
|
|
|
104 |
ticket.setOrderId(userCommunication.getOrderId());
|
|
|
105 |
ticket.setCreatorId(ADMIN_AGENT_ID);
|
|
|
106 |
ticket.setPriority(TicketPriority.MEDIUM);
|
|
|
107 |
ticket.setStatus(TicketStatus.OPEN);
|
|
|
108 |
|
|
|
109 |
log.info("Creating activity!");
|
|
|
110 |
Activity activity = new Activity();
|
|
|
111 |
activity.setCreatorId(ADMIN_AGENT_ID);
|
|
|
112 |
activity.setDescription("Ticket created");
|
|
|
113 |
activity.setTicketCategory(ticket.getCategory());
|
|
|
114 |
activity.setTicketDescription(ticket.getDescription());
|
|
|
115 |
activity.setCustomerEmailId(ticket.getCustomerEmailId());
|
|
|
116 |
activity.setTicketPriority(ticket.getPriority());
|
|
|
117 |
activity.setTicketStatus(ticket.getStatus());
|
|
|
118 |
activity.setType(ActivityType.RECEIVED_EMAIL_FROM_CUSTOMER);
|
|
|
119 |
|
|
|
120 |
if (userCommunication.getUserId() > 0) {
|
|
|
121 |
activity.setCustomerId(userCommunication.getUserId());
|
|
|
122 |
ticket.setCustomerId(userCommunication.getUserId());
|
|
|
123 |
} else {
|
|
|
124 |
try {
|
|
|
125 |
in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient()
|
|
|
126 |
.getClient();
|
|
|
127 |
User user = userClient.getUserByEmail(userCommunication.getReplyTo());
|
|
|
128 |
if (user != null && user.getUserId() != -1) {
|
|
|
129 |
log.info("Found registered user for: " + userCommunication.getReplyTo());
|
|
|
130 |
ticket.setCustomerId(user.getUserId());
|
|
|
131 |
activity.setCustomerId(user.getUserId());
|
|
|
132 |
} else {
|
|
|
133 |
log.info("Setting customer email id to: " + userCommunication.getReplyTo());
|
|
|
134 |
ticket.setCustomerEmailId(userCommunication.getReplyTo());
|
|
|
135 |
}
|
|
|
136 |
} catch(UserContextException ex) {
|
|
|
137 |
log.error("Exception while getting user By email " +
|
|
|
138 |
"or getting email from userCommunication"+ex);
|
|
|
139 |
} catch (TException e) {
|
|
|
140 |
log.error("Exception while getting user By email " +
|
|
|
141 |
"or getting email from userCommunication"+e);
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
log.info("Creating ticket!");
|
|
|
145 |
createTicket(ticket, activity);
|
|
|
146 |
} catch(Exception e) {
|
|
|
147 |
log.error("Exception while creating ticket for commId : " +
|
|
|
148 |
userCommunication.getId() + "\n" + e);
|
| 5435 |
amar.kumar |
149 |
return false;
|
| 5407 |
amar.kumar |
150 |
}
|
| 5435 |
amar.kumar |
151 |
return true;
|
| 5407 |
amar.kumar |
152 |
}
|
|
|
153 |
|
|
|
154 |
@Transactional
|
|
|
155 |
private void createTicket(Ticket ticket, Activity activity) {
|
|
|
156 |
ticketHandler.insertTicket(ticket);
|
|
|
157 |
activity.setTicketId(ticket.getId());
|
|
|
158 |
activityHandler.insertActivity(activity);
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
}
|