Subversion Repositories SmartDukaan

Rev

Rev 3339 | Rev 3368 | 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.util;
5
 
6
import in.shop2020.crm.Activity;
7
import in.shop2020.crm.ActivityType;
8
import in.shop2020.crm.CRMService.Client;
9
import in.shop2020.crm.Ticket;
10
import in.shop2020.crm.TicketCategory;
11
import in.shop2020.crm.TicketPriority;
12
import in.shop2020.crm.TicketStatus;
13
import in.shop2020.model.v1.user.User;
14
import in.shop2020.model.v1.user.UserContextException;
15
import in.shop2020.thrift.clients.CRMClient;
16
import in.shop2020.thrift.clients.UserClient;
17
 
18
import java.io.IOException;
19
import java.text.ParseException;
20
import java.util.Date;
21
import java.util.regex.Matcher;
22
import java.util.regex.Pattern;
23
 
24
import javax.mail.Message;
25
import javax.mail.MessagingException;
26
import javax.mail.internet.MimeMultipart;
27
 
28
import org.apache.commons.logging.Log;
29
import org.apache.commons.logging.LogFactory;
30
import org.apache.thrift.TException;
31
import org.apache.thrift.transport.TTransportException;
32
 
33
/**
34
 * @author mandeep
35
 * 
36
 */
37
public class CRMEmailProcessor {
38
    private static final int DESCRIPTION_MAX_WIDTH = 2000;
39
    private static final Log log                   = LogFactory
40
                                                           .getLog(CRMEmailProcessor.class);
41
    private Client           client;
42
 
43
    public CRMEmailProcessor() {
44
        try {
45
            client = new CRMClient().getClient();
46
        } catch (TTransportException e) {
47
            log.error("Could not create client", e);
48
        }
49
    }
50
 
51
    public void processEmail(Message message) throws MessagingException,
52
            IOException, TException, UserContextException {
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 {
83
        return new Date(client.getLastEmailProcessedTimestamp());
84
    }
85
 
86
    public void updateLastProcessedTimestamp(Date date) throws TException {
3340 mandeep.dh 87
        client = new CRMClient().getClient();
3339 mandeep.dh 88
        client.updateLastEmailProcessedTimestamp(date.getTime());
89
    }
90
 
91
    private void updateTicket(Long ticketId, Message message) {
92
        try {
93
            Ticket ticket = client.getTicket(ticketId);
94
            Activity activity = new Activity();
95
            activity.setTicketId(ticketId);
96
            activity.setTicketAssigneeId(ticket.getAssigneeId());
97
            activity.setTicketCategory(ticket.getCategory());
98
            activity.setTicketDescription(ticket.getDescription());
99
            activity.setTicketPriority(ticket.getPriority());
100
            activity.setTicketStatus(ticket.getStatus());
101
            activity.setType(ActivityType.RECEIVED_EMAIL_FROM_CUSTOMER);
102
            activity.setCreatorId(CRMConstants.ADMIN_AGENT_ID);
103
 
104
            if (ticket.isSetCustomerId()) {
105
                activity.setCustomerId(ticket.getCustomerId());
106
            }
107
 
108
            activity.setDescription(convertMessageToText(message));
109
            client.updateTicket(ticket, activity);
110
        } catch (TTransportException e) {
111
            log.error("Could not update ticket " + ticketId + " with mail "
112
                    + message, e);
113
        } catch (TException e) {
114
            log.error("Could not update ticket " + ticketId + " with mail "
115
                    + message, e);
116
        } catch (MessagingException e) {
117
            log.error("Could not update ticket " + ticketId + " with mail "
118
                    + message, e);
119
        } catch (IOException e) {
120
            log.error("Could not update ticket " + ticketId + " with mail "
121
                    + message, e);
122
        }
123
    }
124
 
125
    private void createTicket(Message message) throws MessagingException,
126
            IOException, TException, UserContextException {
127
        String description = convertMessageToText(message);
128
        Ticket ticket = new Ticket();
129
        ticket.setCreatorId(CRMConstants.ADMIN_AGENT_ID);
130
        ticket.setCategory(TicketCategory.OTHER);
131
        String customerEmailId = parseEmailId(message.getFrom()[0].toString());
132
 
133
        in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient()
134
                .getClient();
135
        User user = userClient.getUserByEmail(customerEmailId);
136
 
137
        if (user == null || user.getUserId() == -1) {
138
            ticket.setCustomerEmailId(customerEmailId);
139
        } else {
140
            ticket.setCustomerId(user.getUserId());
141
        }
142
 
143
        ticket.setDescription(description);
144
        ticket.setPriority(TicketPriority.MEDIUM);
145
        ticket.setStatus(TicketStatus.OPEN);
146
 
147
        log.info("Creating activity!");
148
        Activity activity = new Activity();
149
        activity.setDescription(description);
150
        activity.setTicketCategory(ticket.getCategory());
151
        activity.setTicketDescription(ticket.getDescription());
152
        activity.setTicketPriority(ticket.getPriority());
153
        activity.setTicketStatus(ticket.getStatus());
154
        activity.setType(ActivityType.RECEIVED_EMAIL_FROM_CUSTOMER);
155
        activity.setCreatorId(CRMConstants.ADMIN_AGENT_ID);
156
 
157
        client.insertTicket(ticket, activity);
158
    }
159
 
160
    // parsing email Id from strings like
161
    // Pankaj Jain <ponkoj@hotmail.com>
162
    private String parseEmailId(String address) {
163
        Pattern p = Pattern.compile("^.*<(.+)>.*$");
164
        Matcher m = p.matcher(address);
165
        if (m.matches()) {
166
            address = m.group(1);
167
        }
168
 
169
        return address;
170
    }
171
 
172
    private String convertMessageToText(Message message)
173
            throws MessagingException, IOException {
174
        String messageContent = "";
175
 
176
        if (message.getContent() instanceof String) {
177
            messageContent = message.getContent().toString();
178
        } else if (message.getContent() instanceof MimeMultipart) {
179
            MimeMultipart mimeMultipart = (MimeMultipart) message.getContent();
180
            if (mimeMultipart.getCount() > 0) {
181
                messageContent = mimeMultipart.getBodyPart(0).getContent()
182
                        .toString();
183
            }
184
        } else {
185
            log.error("Could not read message content: " + message.getContent());
186
        }
187
 
188
        String content = "From: " + message.getFrom()[0].toString() + "\n\nSubject: "
189
                + message.getSubject() + "\n\nBody: " + messageContent;
190
        if (content.length() > DESCRIPTION_MAX_WIDTH) {
191
            content = content.substring(0, DESCRIPTION_MAX_WIDTH);
192
        }
193
 
194
        return content;
195
    }
196
}