| 3339 |
mandeep.dh |
1 |
/**
|
|
|
2 |
*
|
|
|
3 |
*/
|
|
|
4 |
package in.shop2020.util;
|
|
|
5 |
|
|
|
6 |
import in.shop2020.crm.Activity;
|
|
|
7 |
import in.shop2020.crm.ActivityType;
|
|
|
8 |
import in.shop2020.crm.CRMService.Client;
|
| 3390 |
mandeep.dh |
9 |
import in.shop2020.crm.SearchFilter;
|
| 3339 |
mandeep.dh |
10 |
import in.shop2020.crm.Ticket;
|
|
|
11 |
import in.shop2020.crm.TicketCategory;
|
|
|
12 |
import in.shop2020.crm.TicketPriority;
|
|
|
13 |
import in.shop2020.crm.TicketStatus;
|
|
|
14 |
import in.shop2020.model.v1.user.User;
|
|
|
15 |
import in.shop2020.model.v1.user.UserContextException;
|
|
|
16 |
import in.shop2020.thrift.clients.CRMClient;
|
|
|
17 |
import in.shop2020.thrift.clients.UserClient;
|
|
|
18 |
|
|
|
19 |
import java.io.IOException;
|
|
|
20 |
import java.text.ParseException;
|
|
|
21 |
import java.util.Date;
|
|
|
22 |
import java.util.regex.Matcher;
|
|
|
23 |
import java.util.regex.Pattern;
|
|
|
24 |
|
|
|
25 |
import javax.mail.Message;
|
|
|
26 |
import javax.mail.MessagingException;
|
| 3368 |
mandeep.dh |
27 |
import javax.mail.Multipart;
|
|
|
28 |
import javax.mail.Part;
|
| 3339 |
mandeep.dh |
29 |
|
|
|
30 |
import org.apache.commons.logging.Log;
|
|
|
31 |
import org.apache.commons.logging.LogFactory;
|
|
|
32 |
import org.apache.thrift.TException;
|
|
|
33 |
import org.apache.thrift.transport.TTransportException;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* @author mandeep
|
|
|
37 |
*
|
|
|
38 |
*/
|
|
|
39 |
public class CRMEmailProcessor {
|
| 3368 |
mandeep.dh |
40 |
private static final int DESCRIPTION_MAX_WIDTH = 1900;
|
| 3369 |
mandeep.dh |
41 |
private static final String MAILOR_DAEMON_EMAIL_ID = "mailer-daemon@googlemail.com";
|
| 3339 |
mandeep.dh |
42 |
private static final Log log = LogFactory
|
|
|
43 |
.getLog(CRMEmailProcessor.class);
|
|
|
44 |
private Client client;
|
|
|
45 |
|
|
|
46 |
public void processEmail(Message message) throws MessagingException,
|
|
|
47 |
IOException, TException, UserContextException {
|
| 3369 |
mandeep.dh |
48 |
// Ignoring mails from Mailor daemon
|
|
|
49 |
if (MAILOR_DAEMON_EMAIL_ID.equals(parseEmailId(message.getFrom()[0].toString()))) {
|
|
|
50 |
return;
|
|
|
51 |
}
|
|
|
52 |
|
| 3339 |
mandeep.dh |
53 |
Long ticketId = parseTicketId(message);
|
|
|
54 |
|
|
|
55 |
if (ticketId != null) {
|
|
|
56 |
log.info("Response for Ticket: " + ticketId + ": " + message);
|
|
|
57 |
updateTicket(ticketId, message);
|
|
|
58 |
} else {
|
|
|
59 |
log.info("Mail not recognized as CRM ticket response with subject: "
|
|
|
60 |
+ message.getSubject());
|
|
|
61 |
log.info("Creating ticket for the same");
|
|
|
62 |
createTicket(message);
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
private Long parseTicketId(Message message) throws MessagingException {
|
|
|
67 |
Long ticketId = null;
|
|
|
68 |
String subject = message.getSubject();
|
|
|
69 |
if (subject != null) {
|
|
|
70 |
Pattern p = Pattern.compile("^.*"
|
|
|
71 |
+ CRMConstants.CRM_SUBJECT_PREFIX_FOR_TICKET_ID
|
|
|
72 |
+ "(\\d+).*$");
|
|
|
73 |
Matcher m = p.matcher(subject);
|
|
|
74 |
if (m.matches()) {
|
|
|
75 |
ticketId = Long.parseLong(m.group(1));
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
return ticketId;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
public Date getLastProcessedTimestamp() throws ParseException, TException {
|
| 3373 |
mandeep.dh |
83 |
client = new CRMClient().getClient();
|
| 3339 |
mandeep.dh |
84 |
return new Date(client.getLastEmailProcessedTimestamp());
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
public void updateLastProcessedTimestamp(Date date) throws TException {
|
| 3340 |
mandeep.dh |
88 |
client = new CRMClient().getClient();
|
| 3339 |
mandeep.dh |
89 |
client.updateLastEmailProcessedTimestamp(date.getTime());
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
private void updateTicket(Long ticketId, Message message) {
|
|
|
93 |
try {
|
| 3390 |
mandeep.dh |
94 |
SearchFilter searchFilter = new SearchFilter();
|
|
|
95 |
searchFilter.setTicketId(ticketId);
|
| 3373 |
mandeep.dh |
96 |
client = new CRMClient().getClient();
|
| 3390 |
mandeep.dh |
97 |
Ticket ticket = client.getTickets(searchFilter).get(0);
|
| 3339 |
mandeep.dh |
98 |
Activity activity = new Activity();
|
|
|
99 |
activity.setTicketId(ticketId);
|
|
|
100 |
activity.setTicketAssigneeId(ticket.getAssigneeId());
|
|
|
101 |
activity.setTicketCategory(ticket.getCategory());
|
|
|
102 |
activity.setTicketDescription(ticket.getDescription());
|
|
|
103 |
activity.setTicketPriority(ticket.getPriority());
|
|
|
104 |
activity.setTicketStatus(ticket.getStatus());
|
|
|
105 |
activity.setType(ActivityType.RECEIVED_EMAIL_FROM_CUSTOMER);
|
|
|
106 |
activity.setCreatorId(CRMConstants.ADMIN_AGENT_ID);
|
| 3398 |
mandeep.dh |
107 |
activity.setIsRead(false);
|
| 3339 |
mandeep.dh |
108 |
|
|
|
109 |
if (ticket.isSetCustomerId()) {
|
|
|
110 |
activity.setCustomerId(ticket.getCustomerId());
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
activity.setDescription(convertMessageToText(message));
|
| 3368 |
mandeep.dh |
114 |
|
|
|
115 |
if (TicketStatus.CLOSED.equals(ticket.getStatus())) {
|
|
|
116 |
ticket.setStatus(TicketStatus.REOPEN);
|
|
|
117 |
}
|
|
|
118 |
|
| 3373 |
mandeep.dh |
119 |
client = new CRMClient().getClient();
|
| 3339 |
mandeep.dh |
120 |
client.updateTicket(ticket, activity);
|
|
|
121 |
} catch (TTransportException e) {
|
|
|
122 |
log.error("Could not update ticket " + ticketId + " with mail "
|
|
|
123 |
+ message, e);
|
|
|
124 |
} catch (TException e) {
|
|
|
125 |
log.error("Could not update ticket " + ticketId + " with mail "
|
|
|
126 |
+ message, e);
|
|
|
127 |
} catch (MessagingException e) {
|
|
|
128 |
log.error("Could not update ticket " + ticketId + " with mail "
|
|
|
129 |
+ message, e);
|
|
|
130 |
} catch (IOException e) {
|
|
|
131 |
log.error("Could not update ticket " + ticketId + " with mail "
|
|
|
132 |
+ message, e);
|
|
|
133 |
}
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
private void createTicket(Message message) throws MessagingException,
|
|
|
137 |
IOException, TException, UserContextException {
|
|
|
138 |
String description = convertMessageToText(message);
|
|
|
139 |
Ticket ticket = new Ticket();
|
|
|
140 |
ticket.setCreatorId(CRMConstants.ADMIN_AGENT_ID);
|
|
|
141 |
ticket.setCategory(TicketCategory.OTHER);
|
|
|
142 |
String customerEmailId = parseEmailId(message.getFrom()[0].toString());
|
|
|
143 |
|
|
|
144 |
in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient()
|
|
|
145 |
.getClient();
|
|
|
146 |
User user = userClient.getUserByEmail(customerEmailId);
|
|
|
147 |
|
|
|
148 |
if (user == null || user.getUserId() == -1) {
|
|
|
149 |
ticket.setCustomerEmailId(customerEmailId);
|
|
|
150 |
} else {
|
|
|
151 |
ticket.setCustomerId(user.getUserId());
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
ticket.setDescription(description);
|
|
|
155 |
ticket.setPriority(TicketPriority.MEDIUM);
|
|
|
156 |
ticket.setStatus(TicketStatus.OPEN);
|
|
|
157 |
|
|
|
158 |
log.info("Creating activity!");
|
|
|
159 |
Activity activity = new Activity();
|
|
|
160 |
activity.setDescription(description);
|
|
|
161 |
activity.setTicketCategory(ticket.getCategory());
|
|
|
162 |
activity.setTicketDescription(ticket.getDescription());
|
|
|
163 |
activity.setTicketPriority(ticket.getPriority());
|
|
|
164 |
activity.setTicketStatus(ticket.getStatus());
|
|
|
165 |
activity.setType(ActivityType.RECEIVED_EMAIL_FROM_CUSTOMER);
|
|
|
166 |
activity.setCreatorId(CRMConstants.ADMIN_AGENT_ID);
|
| 3398 |
mandeep.dh |
167 |
activity.setIsRead(false);
|
| 3339 |
mandeep.dh |
168 |
|
| 3373 |
mandeep.dh |
169 |
client = new CRMClient().getClient();
|
| 3339 |
mandeep.dh |
170 |
client.insertTicket(ticket, activity);
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
// parsing email Id from strings like
|
| 3368 |
mandeep.dh |
174 |
// "Pankaj Jain <ponkoj@hotmail.com>"
|
| 3339 |
mandeep.dh |
175 |
private String parseEmailId(String address) {
|
|
|
176 |
Pattern p = Pattern.compile("^.*<(.+)>.*$");
|
|
|
177 |
Matcher m = p.matcher(address);
|
|
|
178 |
if (m.matches()) {
|
|
|
179 |
address = m.group(1);
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
return address;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
private String convertMessageToText(Message message)
|
|
|
186 |
throws MessagingException, IOException {
|
| 3368 |
mandeep.dh |
187 |
String messageContent = getText(message);
|
| 3339 |
mandeep.dh |
188 |
|
| 3368 |
mandeep.dh |
189 |
String content = "From: " + message.getFrom()[0].toString()
|
|
|
190 |
+ "\n\nSubject: " + message.getSubject() + "\n\nBody: "
|
|
|
191 |
+ messageContent;
|
| 3339 |
mandeep.dh |
192 |
|
|
|
193 |
if (content.length() > DESCRIPTION_MAX_WIDTH) {
|
|
|
194 |
content = content.substring(0, DESCRIPTION_MAX_WIDTH);
|
| 3368 |
mandeep.dh |
195 |
content += "\n\nTHIS TEXT IS TRUNCATED. PLEASE VISIT INBOX TO SEE COMPLETE DETAILS.";
|
| 3339 |
mandeep.dh |
196 |
}
|
|
|
197 |
|
|
|
198 |
return content;
|
|
|
199 |
}
|
| 3368 |
mandeep.dh |
200 |
|
|
|
201 |
/**
|
|
|
202 |
* Return the primary text content of the message.
|
|
|
203 |
*/
|
|
|
204 |
private String getText(Part p) throws MessagingException, IOException {
|
|
|
205 |
if (p.isMimeType("text/*")) {
|
|
|
206 |
String s = (String) p.getContent();
|
|
|
207 |
return s;
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
if (p.isMimeType("multipart/alternative")) {
|
|
|
211 |
// prefer plain text over html text
|
|
|
212 |
Multipart mp = (Multipart) p.getContent();
|
|
|
213 |
String text = null;
|
|
|
214 |
for (int i = 0; i < mp.getCount(); i++) {
|
|
|
215 |
Part bp = mp.getBodyPart(i);
|
|
|
216 |
if (bp.isMimeType("text/html")) {
|
|
|
217 |
if (text == null)
|
|
|
218 |
text = getText(bp);
|
|
|
219 |
continue;
|
|
|
220 |
} else if (bp.isMimeType("text/plain")) {
|
|
|
221 |
String s = getText(bp);
|
|
|
222 |
if (s != null)
|
|
|
223 |
return s;
|
|
|
224 |
} else {
|
|
|
225 |
return getText(bp);
|
|
|
226 |
}
|
|
|
227 |
}
|
|
|
228 |
return text;
|
|
|
229 |
} else if (p.isMimeType("multipart/*")) {
|
|
|
230 |
Multipart mp = (Multipart) p.getContent();
|
|
|
231 |
for (int i = 0; i < mp.getCount(); i++) {
|
|
|
232 |
String s = getText(mp.getBodyPart(i));
|
|
|
233 |
if (s != null)
|
|
|
234 |
return s;
|
|
|
235 |
}
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
return null;
|
|
|
239 |
}
|
| 3339 |
mandeep.dh |
240 |
}
|