Subversion Repositories SmartDukaan

Rev

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

Rev 3340 Rev 3368
Line 21... Line 21...
21
import java.util.regex.Matcher;
21
import java.util.regex.Matcher;
22
import java.util.regex.Pattern;
22
import java.util.regex.Pattern;
23
 
23
 
24
import javax.mail.Message;
24
import javax.mail.Message;
25
import javax.mail.MessagingException;
25
import javax.mail.MessagingException;
26
import javax.mail.internet.MimeMultipart;
26
import javax.mail.Multipart;
-
 
27
import javax.mail.Part;
27
 
28
 
28
import org.apache.commons.logging.Log;
29
import org.apache.commons.logging.Log;
29
import org.apache.commons.logging.LogFactory;
30
import org.apache.commons.logging.LogFactory;
30
import org.apache.thrift.TException;
31
import org.apache.thrift.TException;
31
import org.apache.thrift.transport.TTransportException;
32
import org.apache.thrift.transport.TTransportException;
Line 33... Line 34...
33
/**
34
/**
34
 * @author mandeep
35
 * @author mandeep
35
 * 
36
 * 
36
 */
37
 */
37
public class CRMEmailProcessor {
38
public class CRMEmailProcessor {
38
    private static final int DESCRIPTION_MAX_WIDTH = 2000;
39
    private static final int DESCRIPTION_MAX_WIDTH = 1900;
39
    private static final Log log                   = LogFactory
40
    private static final Log log                   = LogFactory
40
                                                           .getLog(CRMEmailProcessor.class);
41
                                                           .getLog(CRMEmailProcessor.class);
41
    private Client           client;
42
    private Client           client;
42
 
43
 
43
    public CRMEmailProcessor() {
44
    public CRMEmailProcessor() {
Line 104... Line 105...
104
            if (ticket.isSetCustomerId()) {
105
            if (ticket.isSetCustomerId()) {
105
                activity.setCustomerId(ticket.getCustomerId());
106
                activity.setCustomerId(ticket.getCustomerId());
106
            }
107
            }
107
 
108
 
108
            activity.setDescription(convertMessageToText(message));
109
            activity.setDescription(convertMessageToText(message));
-
 
110
 
-
 
111
            if (TicketStatus.CLOSED.equals(ticket.getStatus())) {
-
 
112
                ticket.setStatus(TicketStatus.REOPEN);
-
 
113
            }
-
 
114
 
109
            client.updateTicket(ticket, activity);
115
            client.updateTicket(ticket, activity);
110
        } catch (TTransportException e) {
116
        } catch (TTransportException e) {
111
            log.error("Could not update ticket " + ticketId + " with mail "
117
            log.error("Could not update ticket " + ticketId + " with mail "
112
                    + message, e);
118
                    + message, e);
113
        } catch (TException e) {
119
        } catch (TException e) {
Line 156... Line 162...
156
 
162
 
157
        client.insertTicket(ticket, activity);
163
        client.insertTicket(ticket, activity);
158
    }
164
    }
159
 
165
 
160
    // parsing email Id from strings like
166
    // parsing email Id from strings like
161
    // Pankaj Jain <ponkoj@hotmail.com>
167
    // "Pankaj Jain <ponkoj@hotmail.com>"
162
    private String parseEmailId(String address) {
168
    private String parseEmailId(String address) {
163
        Pattern p = Pattern.compile("^.*<(.+)>.*$");
169
        Pattern p = Pattern.compile("^.*<(.+)>.*$");
164
        Matcher m = p.matcher(address);
170
        Matcher m = p.matcher(address);
165
        if (m.matches()) {
171
        if (m.matches()) {
166
            address = m.group(1);
172
            address = m.group(1);
Line 169... Line 175...
169
        return address;
175
        return address;
170
    }
176
    }
171
 
177
 
172
    private String convertMessageToText(Message message)
178
    private String convertMessageToText(Message message)
173
            throws MessagingException, IOException {
179
            throws MessagingException, IOException {
174
        String messageContent = "";
180
        String messageContent = getText(message);
-
 
181
        System.out.println(messageContent);
175
 
182
 
176
        if (message.getContent() instanceof String) {
-
 
177
            messageContent = message.getContent().toString();
183
        String content = "From: " + message.getFrom()[0].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()
184
                + "\n\nSubject: " + message.getSubject() + "\n\nBody: "
182
                        .toString();
185
                + messageContent;
183
            }
-
 
184
        } else {
-
 
185
            log.error("Could not read message content: " + message.getContent());
-
 
186
        }
-
 
187
 
186
 
188
        String content = "From: " + message.getFrom()[0].toString() + "\n\nSubject: "
-
 
189
                + message.getSubject() + "\n\nBody: " + messageContent;
-
 
190
        if (content.length() > DESCRIPTION_MAX_WIDTH) {
187
        if (content.length() > DESCRIPTION_MAX_WIDTH) {
191
            content = content.substring(0, DESCRIPTION_MAX_WIDTH);
188
            content = content.substring(0, DESCRIPTION_MAX_WIDTH);
-
 
189
            content += "\n\nTHIS TEXT IS TRUNCATED. PLEASE VISIT INBOX TO SEE COMPLETE DETAILS.";
192
        }
190
        }
193
 
191
 
194
        return content;
192
        return content;
195
    }
193
    }
-
 
194
 
-
 
195
    /**
-
 
196
     * Return the primary text content of the message.
-
 
197
     */
-
 
198
    private String getText(Part p) throws MessagingException, IOException {
-
 
199
        if (p.isMimeType("text/*")) {
-
 
200
            String s = (String) p.getContent();
-
 
201
            return s;
-
 
202
        }
-
 
203
 
-
 
204
        if (p.isMimeType("multipart/alternative")) {
-
 
205
            // prefer plain text over html text
-
 
206
            Multipart mp = (Multipart) p.getContent();
-
 
207
            String text = null;
-
 
208
            for (int i = 0; i < mp.getCount(); i++) {
-
 
209
                Part bp = mp.getBodyPart(i);
-
 
210
                if (bp.isMimeType("text/html")) {
-
 
211
                    if (text == null)
-
 
212
                        text = getText(bp);
-
 
213
                    continue;
-
 
214
                } else if (bp.isMimeType("text/plain")) {
-
 
215
                    String s = getText(bp);
-
 
216
                    if (s != null)
-
 
217
                        return s;
-
 
218
                } else {
-
 
219
                    return getText(bp);
-
 
220
                }
-
 
221
            }
-
 
222
            return text;
-
 
223
        } else if (p.isMimeType("multipart/*")) {
-
 
224
            Multipart mp = (Multipart) p.getContent();
-
 
225
            for (int i = 0; i < mp.getCount(); i++) {
-
 
226
                String s = getText(mp.getBodyPart(i));
-
 
227
                if (s != null)
-
 
228
                    return s;
-
 
229
            }
-
 
230
        }
-
 
231
 
-
 
232
        return null;
-
 
233
    }
196
}
234
}