Subversion Repositories SmartDukaan

Rev

Rev 4206 | Rev 4793 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4206 Rev 4241
Line 13... Line 13...
13
import in.shop2020.crm.TicketStatus;
13
import in.shop2020.crm.TicketStatus;
14
import in.shop2020.model.v1.user.User;
14
import in.shop2020.model.v1.user.User;
15
import in.shop2020.model.v1.user.UserContextException;
15
import in.shop2020.model.v1.user.UserContextException;
16
import in.shop2020.thrift.clients.CRMClient;
16
import in.shop2020.thrift.clients.CRMClient;
17
import in.shop2020.thrift.clients.UserClient;
17
import in.shop2020.thrift.clients.UserClient;
18
 
-
 
-
 
18
import in.shop2020.util.CRMConstants;
-
 
19
import java.io.File;
-
 
20
import java.io.FileOutputStream;
19
import java.io.IOException;
21
import java.io.IOException;
-
 
22
import java.io.InputStream;
20
import java.text.ParseException;
23
import java.text.ParseException;
21
import java.util.Date;
24
import java.util.Date;
22
import java.util.List;
25
import java.util.List;
23
import java.util.regex.Matcher;
26
import java.util.regex.Matcher;
24
import java.util.regex.Pattern;
27
import java.util.regex.Pattern;
25
 
28
 
-
 
29
import javax.mail.BodyPart;
26
import javax.mail.Message;
30
import javax.mail.Message;
27
import javax.mail.MessagingException;
31
import javax.mail.MessagingException;
28
import javax.mail.Multipart;
32
import javax.mail.Multipart;
29
import javax.mail.Part;
33
import javax.mail.Part;
30
 
34
 
Line 38... Line 42...
38
/**
42
/**
39
 * @author mandeep
43
 * @author mandeep
40
 * 
44
 * 
41
 */
45
 */
42
public class CRMEmailProcessor {
46
public class CRMEmailProcessor {
-
 
47
    private static final int DESCRIPTION_MAX_WIDTH = 1900;
43
    private static final String MAILOR_DAEMON_EMAIL_ID = "mailer-daemon@googlemail.com";
48
    private static final String MAILOR_DAEMON_EMAIL_ID = "mailer-daemon@googlemail.com";
44
    private static final Log log                   = LogFactory
49
    private static final Log log                   = LogFactory
45
                                                           .getLog(CRMEmailProcessor.class);
50
                                                           .getLog(CRMEmailProcessor.class);
46
    private Client           client;
51
    private Client           client;
47
 
52
 
Line 65... Line 70...
65
            createTicket(message);
70
            createTicket(message);
66
        }
71
        }
67
    }
72
    }
68
 
73
 
69
    // Parses regex like ^.*Saholic#(\d+).*
74
    // Parses regex like ^.*Saholic#(\d+).*
70
    private Long parseTicketId(Message message) throws MessagingException {
75
    private Long parseTicketId(Message message) throws MessagingException, IOException {
71
        Long ticketId = null;
-
 
72
        String subject = message.getSubject();
76
        String subject = message.getSubject();
73
        if (subject != null) {
77
        if (subject == null) {
74
            Pattern p = Pattern.compile("^.*"
-
 
75
                    + CRMConstants.CRM_SUBJECT_PREFIX_FOR_TICKET_ID
-
 
76
                    + "(\\d+).*$");
-
 
77
            Matcher m = p.matcher(subject);
-
 
78
            if (m.matches()) {
-
 
79
                ticketId = Long.parseLong(m.group(1));
-
 
80
            }
78
            return null;
81
        }
79
        }
-
 
80
        else if (subject.contains("Undelivered Mail")) {
-
 
81
            return extractTicketId(convertMessageToText(message));
-
 
82
        }
-
 
83
        else {
-
 
84
            return extractTicketId(subject);
-
 
85
        }
-
 
86
    }
82
 
87
    
-
 
88
    private Long extractTicketId (String stringToSearch){
-
 
89
        Pattern p = Pattern.compile("(?s).*"
-
 
90
                + CRMConstants.CRM_SUBJECT_PREFIX_FOR_TICKET_ID
-
 
91
                + "(\\d+).*$");
-
 
92
        Matcher m = p.matcher(stringToSearch);
-
 
93
        if (m.matches()) {
-
 
94
            return Long.parseLong(m.group(1));
-
 
95
        }
83
        return ticketId;
96
        return null;
84
    }
97
    }
85
 
98
 
86
    public Date getLastProcessedTimestamp() throws ParseException, TException {
99
    public Date getLastProcessedTimestamp() throws ParseException, TException {
87
        client = new CRMClient().getClient();
100
        client = new CRMClient().getClient();
88
        return new Date(client.getLastEmailProcessedTimestamp());
101
        return new Date(client.getLastEmailProcessedTimestamp());
Line 118... Line 131...
118
 
131
 
119
            if (ticket.isSetCustomerId()) {
132
            if (ticket.isSetCustomerId()) {
120
                activity.setCustomerId(ticket.getCustomerId());
133
                activity.setCustomerId(ticket.getCustomerId());
121
            }
134
            }
122
 
135
 
-
 
136
            activity.setAttachments(getAttachment(message));
123
            activity.setDescription(convertMessageToText(message));
137
            activity.setDescription(convertMessageToText(message));
124
 
138
 
125
            if (TicketStatus.CLOSED.equals(ticket.getStatus())) {
139
            if (TicketStatus.CLOSED.equals(ticket.getStatus())) {
126
                ticket.setStatus(TicketStatus.REOPEN);
140
                ticket.setStatus(TicketStatus.REOPEN);
127
            }
141
            }
Line 168... Line 182...
168
        ticket.setStatus(TicketStatus.OPEN);
182
        ticket.setStatus(TicketStatus.OPEN);
169
 
183
 
170
        log.info("Creating activity!");
184
        log.info("Creating activity!");
171
        Activity activity = new Activity();
185
        Activity activity = new Activity();
172
        activity.setDescription(description);
186
        activity.setDescription(description);
-
 
187
        activity.setAttachments(getAttachment(message));
173
        activity.setTicketCategory(ticket.getCategory());
188
        activity.setTicketCategory(ticket.getCategory());
174
        activity.setTicketDescription(ticket.getDescription());
189
        activity.setTicketDescription(ticket.getDescription());
175
        activity.setTicketPriority(ticket.getPriority());
190
        activity.setTicketPriority(ticket.getPriority());
176
        activity.setTicketStatus(ticket.getStatus());
191
        activity.setTicketStatus(ticket.getStatus());
177
        activity.setType(ActivityType.RECEIVED_EMAIL_FROM_CUSTOMER);
192
        activity.setType(ActivityType.RECEIVED_EMAIL_FROM_CUSTOMER);
Line 252... Line 267...
252
            }
267
            }
253
        }
268
        }
254
 
269
 
255
        return null;
270
        return null;
256
    }
271
    }
-
 
272
 
-
 
273
    private String getAttachment(Part p) throws MessagingException, IOException {
-
 
274
        String listOfFiles = "";
257
}
275
 
-
 
276
        if (p.isMimeType("multipart/*")) {
-
 
277
            Multipart multipart = (Multipart) p.getContent();
-
 
278
            for (int x = 0; x < multipart.getCount(); x++) {
-
 
279
                BodyPart bodyPart = multipart.getBodyPart(x);
-
 
280
 
-
 
281
                if(!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {
-
 
282
                    continue; // we want attachments only
-
 
283
                }
-
 
284
 
-
 
285
                String fileName = bodyPart.getFileName();
-
 
286
                InputStream is = bodyPart.getInputStream();
-
 
287
                File f = new File(CRMConstants.ATTACHMENTS_ARCHIVE_DIR + fileName);
-
 
288
                FileOutputStream fos = new FileOutputStream(f);
-
 
289
                byte[] buf = new byte[4096];
-
 
290
                int bytesRead;
-
 
291
 
-
 
292
                while((bytesRead = is.read(buf)) != -1) {
-
 
293
                    fos.write(buf, 0, bytesRead);
-
 
294
                }
-
 
295
 
-
 
296
                fos.close();
-
 
297
                listOfFiles += fileName + ";";
-
 
298
 
-
 
299
                if (listOfFiles.length() > DESCRIPTION_MAX_WIDTH) {
-
 
300
                    listOfFiles = listOfFiles.substring(0, DESCRIPTION_MAX_WIDTH);
-
 
301
                    listOfFiles += "\n\nTHIS TEXT IS TRUNCATED. PLEASE VISIT INBOX TO SEE COMPLETE DETAILS.";
-
 
302
                }//end of if
-
 
303
            }//end for loop
-
 
304
        }//end if
-
 
305
 
-
 
306
        return listOfFiles.isEmpty() ? null : listOfFiles;
-
 
307
    }//end getAttachment
-
 
308
}//end Class
258
309