Subversion Repositories SmartDukaan

Rev

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

Rev 4854 Rev 11890
Line 20... Line 20...
20
import java.io.File;
20
import java.io.File;
21
import java.io.FileOutputStream;
21
import java.io.FileOutputStream;
22
import java.io.IOException;
22
import java.io.IOException;
23
import java.io.InputStream;
23
import java.io.InputStream;
24
import java.text.ParseException;
24
import java.text.ParseException;
-
 
25
import java.util.Calendar;
25
import java.util.Date;
26
import java.util.Date;
-
 
27
import java.util.GregorianCalendar;
26
import java.util.List;
28
import java.util.List;
27
import java.util.regex.Matcher;
29
import java.util.regex.Matcher;
28
import java.util.regex.Pattern;
30
import java.util.regex.Pattern;
29
 
31
 
30
import javax.mail.BodyPart;
32
import javax.mail.BodyPart;
Line 46... Line 48...
46
 */
48
 */
47
public class CRMEmailProcessor {
49
public class CRMEmailProcessor {
48
    private static final int DESCRIPTION_MAX_WIDTH = 1900;
50
    private static final int DESCRIPTION_MAX_WIDTH = 1900;
49
    private static final String MAILOR_DAEMON_EMAIL_ID = "mailer-daemon@googlemail.com";
51
    private static final String MAILOR_DAEMON_EMAIL_ID = "mailer-daemon@googlemail.com";
50
    private static final Log log                   = LogFactory
52
    private static final Log log                   = LogFactory
51
                                                           .getLog(CRMEmailProcessor.class);
53
    .getLog(CRMEmailProcessor.class);
52
    private Client           client;
54
    private Client           client;
-
 
55
    private String emailType="";
53
 
56
 
54
    public void processEmail(Message message) throws Exception {
57
    public void processEmail(Message message) throws Exception {
55
        // Ignoring mails from Mailor daemon
58
        // Ignoring mails from Mailor daemon
56
        if (MAILOR_DAEMON_EMAIL_ID.equals(parseEmailId(message.getFrom()[0].toString()))) {
59
        if (MAILOR_DAEMON_EMAIL_ID.equals(parseEmailId(message.getFrom()[0].toString()))) {
57
            return;
60
            return;
Line 82... Line 85...
82
        }
85
        }
83
        else {
86
        else {
84
            return extractTicketId(subject);
87
            return extractTicketId(subject);
85
        }
88
        }
86
    }
89
    }
87
    
90
 
88
    private Long extractTicketId (String stringToSearch){
91
    private Long extractTicketId (String stringToSearch){
89
        Pattern p = Pattern.compile("(?s).*"
92
        Pattern p = Pattern.compile("(?s).*"
90
                + CRMConstants.CRM_SUBJECT_PREFIX_FOR_TICKET_ID
93
                + CRMConstants.CRM_SUBJECT_PREFIX_FOR_TICKET_ID
91
                + "(\\d+).*$");
94
                + "(\\d+).*$");
92
        Matcher m = p.matcher(stringToSearch);
95
        Matcher m = p.matcher(stringToSearch);
Line 137... Line 140...
137
            activity.setDescription(convertMessageToText(message));
140
            activity.setDescription(convertMessageToText(message));
138
 
141
 
139
            if (TicketStatus.CLOSED.equals(ticket.getStatus())) {
142
            if (TicketStatus.CLOSED.equals(ticket.getStatus())) {
140
                ticket.setStatus(TicketStatus.REOPEN);
143
                ticket.setStatus(TicketStatus.REOPEN);
141
                if(CRMAuthorizingRealm.getAgent(ticket.getAssigneeId()) == null){
144
                if(CRMAuthorizingRealm.getAgent(ticket.getAssigneeId()) == null){
142
                	ticket.setAssigneeId(1);
145
                    ticket.setAssigneeId(1);
143
                	activity.setTicketAssigneeId(1);
146
                    activity.setTicketAssigneeId(1);
144
                }
147
                }
145
                
-
 
-
 
148
 
146
            }
149
            }
147
 
150
 
148
            activity.setTicketStatus(ticket.getStatus());
151
            activity.setTicketStatus(ticket.getStatus());
149
 
152
 
150
            client = new CRMClient().getClient();
153
            client = new CRMClient().getClient();
Line 155... Line 158...
155
            throw e;
158
            throw e;
156
        }
159
        }
157
    }
160
    }
158
 
161
 
159
    private void createTicket(Message message) throws MessagingException,
162
    private void createTicket(Message message) throws MessagingException,
160
            IOException, TException, UserContextException {
163
    IOException, TException, UserContextException {
161
        String description = convertMessageToText(message);
164
        String description = convertMessageToText(message);
162
        Ticket ticket = new Ticket();
165
        Ticket ticket = new Ticket();
163
        ticket.setCreatorId(CRMConstants.ADMIN_AGENT_ID);
166
        ticket.setCreatorId(CRMConstants.ADMIN_AGENT_ID);
-
 
167
        if (getEmailType().equalsIgnoreCase("BulkEmail")){
-
 
168
            ticket.setCategory(TicketCategory.Bulk_Order_ENQUIRY);
-
 
169
        }
-
 
170
        else{
164
        ticket.setCategory(TicketCategory.OTHER);
171
            ticket.setCategory(TicketCategory.OTHER);
-
 
172
        }
165
        String customerEmailId = parseEmailId(message.getFrom()[0].toString());
173
        String customerEmailId = parseEmailId(message.getFrom()[0].toString());
166
 
174
 
167
        in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient()
175
        in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient()
168
                .getClient();
176
        .getClient();
169
        User user = userClient.getUserByEmail(customerEmailId);
177
        User user = userClient.getUserByEmail(customerEmailId);
170
 
178
 
171
        if (user == null || user.getUserId() == -1) {
179
        if (user == null || user.getUserId() == -1) {
172
            ticket.setCustomerEmailId(customerEmailId);
180
            ticket.setCustomerEmailId(customerEmailId);
173
        } else {
181
        } else {
Line 205... Line 213...
205
 
213
 
206
        return address;
214
        return address;
207
    }
215
    }
208
 
216
 
209
    private String convertMessageToText(Message message)
217
    private String convertMessageToText(Message message)
210
            throws MessagingException, IOException {
218
    throws MessagingException, IOException {
211
        String messageContent = getText(message);
219
        String messageContent = getText(message);
212
 
220
 
213
        String content = "From: " + message.getFrom()[0].toString()
221
        String content = "From: " + message.getFrom()[0].toString()
214
                + "\n\nSubject: " + message.getSubject() + "\n\nBody: "
222
        + "\n\nSubject: " + message.getSubject() + "\n\nBody: "
215
                + messageContent;
223
        + messageContent;
216
 
224
 
217
        if (content.length() > CRMConstants.DESCRIPTION_MAX_WIDTH) {
225
        if (content.length() > CRMConstants.DESCRIPTION_MAX_WIDTH) {
218
            content = content.substring(0, CRMConstants.DESCRIPTION_MAX_WIDTH);
226
            content = content.substring(0, CRMConstants.DESCRIPTION_MAX_WIDTH);
219
            content += "\n\nTHIS TEXT IS TRUNCATED. PLEASE VISIT INBOX TO SEE COMPLETE DETAILS.";
227
            content += "\n\nTHIS TEXT IS TRUNCATED. PLEASE VISIT INBOX TO SEE COMPLETE DETAILS.";
220
        }
228
        }
Line 278... Line 286...
278
                if(!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {
286
                if(!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {
279
                    continue; // we want attachments only
287
                    continue; // we want attachments only
280
                }
288
                }
281
 
289
 
282
                String fileName = bodyPart.getFileName();
290
                String fileName = bodyPart.getFileName();
-
 
291
                fileName = fileName+"_"+getTimeInMilliseconds();
283
                InputStream is = bodyPart.getInputStream();
292
                InputStream is = bodyPart.getInputStream();
284
                File f = new File(CRMConstants.ATTACHMENTS_ARCHIVE_DIR + fileName);
293
                File f = new File(CRMConstants.ATTACHMENTS_ARCHIVE_DIR + fileName);
285
                FileOutputStream fos = new FileOutputStream(f);
294
                FileOutputStream fos = new FileOutputStream(f);
286
                byte[] buf = new byte[4096];
295
                byte[] buf = new byte[4096];
287
                int bytesRead;
296
                int bytesRead;
Line 300... Line 309...
300
            }//end for loop
309
            }//end for loop
301
        }//end if
310
        }//end if
302
 
311
 
303
        return listOfFiles.isEmpty() ? null : listOfFiles;
312
        return listOfFiles.isEmpty() ? null : listOfFiles;
304
    }//end getAttachment
313
    }//end getAttachment
-
 
314
    
-
 
315
    public String getTimeInMilliseconds(){
-
 
316
        Calendar cal=GregorianCalendar.getInstance();
-
 
317
        return String.valueOf(cal.getTimeInMillis());
-
 
318
    }
-
 
319
 
-
 
320
    public void setEmailType(String emailType) {
-
 
321
        this.emailType = emailType;
-
 
322
    }
-
 
323
 
-
 
324
    public String getEmailType() {
-
 
325
        return emailType;
-
 
326
    }
305
}//end Class
327
}//end Class
306
328