Subversion Repositories SmartDukaan

Rev

Rev 3340 | Go to most recent revision | Details | 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 {
87
        client.updateLastEmailProcessedTimestamp(date.getTime());
88
    }
89
 
90
    private void updateTicket(Long ticketId, Message message) {
91
        try {
92
            Ticket ticket = client.getTicket(ticketId);
93
            Activity activity = new Activity();
94
            activity.setTicketId(ticketId);
95
            activity.setTicketAssigneeId(ticket.getAssigneeId());
96
            activity.setTicketCategory(ticket.getCategory());
97
            activity.setTicketDescription(ticket.getDescription());
98
            activity.setTicketPriority(ticket.getPriority());
99
            activity.setTicketStatus(ticket.getStatus());
100
            activity.setType(ActivityType.RECEIVED_EMAIL_FROM_CUSTOMER);
101
            activity.setCreatorId(CRMConstants.ADMIN_AGENT_ID);
102
 
103
            if (ticket.isSetCustomerId()) {
104
                activity.setCustomerId(ticket.getCustomerId());
105
            }
106
 
107
            activity.setDescription(convertMessageToText(message));
108
            client.updateTicket(ticket, activity);
109
        } catch (TTransportException e) {
110
            log.error("Could not update ticket " + ticketId + " with mail "
111
                    + message, e);
112
        } catch (TException e) {
113
            log.error("Could not update ticket " + ticketId + " with mail "
114
                    + message, e);
115
        } catch (MessagingException e) {
116
            log.error("Could not update ticket " + ticketId + " with mail "
117
                    + message, e);
118
        } catch (IOException e) {
119
            log.error("Could not update ticket " + ticketId + " with mail "
120
                    + message, e);
121
        }
122
    }
123
 
124
    private void createTicket(Message message) throws MessagingException,
125
            IOException, TException, UserContextException {
126
        String description = convertMessageToText(message);
127
        Ticket ticket = new Ticket();
128
        ticket.setCreatorId(CRMConstants.ADMIN_AGENT_ID);
129
        ticket.setCategory(TicketCategory.OTHER);
130
        String customerEmailId = parseEmailId(message.getFrom()[0].toString());
131
 
132
        in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient()
133
                .getClient();
134
        User user = userClient.getUserByEmail(customerEmailId);
135
 
136
        if (user == null || user.getUserId() == -1) {
137
            ticket.setCustomerEmailId(customerEmailId);
138
        } else {
139
            ticket.setCustomerId(user.getUserId());
140
        }
141
 
142
        ticket.setDescription(description);
143
        ticket.setPriority(TicketPriority.MEDIUM);
144
        ticket.setStatus(TicketStatus.OPEN);
145
 
146
        log.info("Creating activity!");
147
        Activity activity = new Activity();
148
        activity.setDescription(description);
149
        activity.setTicketCategory(ticket.getCategory());
150
        activity.setTicketDescription(ticket.getDescription());
151
        activity.setTicketPriority(ticket.getPriority());
152
        activity.setTicketStatus(ticket.getStatus());
153
        activity.setType(ActivityType.RECEIVED_EMAIL_FROM_CUSTOMER);
154
        activity.setCreatorId(CRMConstants.ADMIN_AGENT_ID);
155
 
156
        client.insertTicket(ticket, activity);
157
    }
158
 
159
    // parsing email Id from strings like
160
    // Pankaj Jain <ponkoj@hotmail.com>
161
    private String parseEmailId(String address) {
162
        Pattern p = Pattern.compile("^.*<(.+)>.*$");
163
        Matcher m = p.matcher(address);
164
        if (m.matches()) {
165
            address = m.group(1);
166
        }
167
 
168
        return address;
169
    }
170
 
171
    private String convertMessageToText(Message message)
172
            throws MessagingException, IOException {
173
        String messageContent = "";
174
 
175
        if (message.getContent() instanceof String) {
176
            messageContent = message.getContent().toString();
177
        } else if (message.getContent() instanceof MimeMultipart) {
178
            MimeMultipart mimeMultipart = (MimeMultipart) message.getContent();
179
            if (mimeMultipart.getCount() > 0) {
180
                messageContent = mimeMultipart.getBodyPart(0).getContent()
181
                        .toString();
182
            }
183
        } else {
184
            log.error("Could not read message content: " + message.getContent());
185
        }
186
 
187
        String content = "From: " + message.getFrom()[0].toString() + "\n\nSubject: "
188
                + message.getSubject() + "\n\nBody: " + messageContent;
189
        if (content.length() > DESCRIPTION_MAX_WIDTH) {
190
            content = content.substring(0, DESCRIPTION_MAX_WIDTH);
191
        }
192
 
193
        return content;
194
    }
195
}