001: /*
002: Copyright (C) 2004 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.hipermail;
034:
035: import java.io.ByteArrayOutputStream;
036: import java.io.IOException;
037:
038: import java.sql.SQLException;
039: import java.sql.PreparedStatement;
040: import java.sql.ResultSet;
041:
042: import javax.mail.MessagingException;
043: import javax.mail.internet.MimeBodyPart;
044:
045: import com.knowgate.debug.DebugFile;
046: import com.knowgate.jdc.JDCConnection;
047: import com.knowgate.misc.Gadgets;
048: import com.knowgate.dataobjs.DB;
049:
050: /**
051: * @author Sergio Montoro Ten
052: * @version 1.0
053: */
054: public class DraftsHelper {
055: public DraftsHelper() {
056: }
057:
058: // ----------------------------------------------------------------------------------------
059:
060: public static DBMimeMessage draftMessage(DBFolder oDraftsFldr,
061: String sMailHost, String sGuWorkArea, String sUserId,
062: String sContentType) throws SQLException,
063: MessagingException, IOException {
064:
065: if (DebugFile.trace) {
066: DebugFile.writeln("Begin DraftsHelper.draftMessage("
067: + sMailHost + "," + sGuWorkArea + "," + sUserId
068: + "," + sContentType + ")");
069: DebugFile.incIdent();
070: }
071:
072: JDCConnection oConn = oDraftsFldr.getConnection();
073:
074: java.sql.PreparedStatement oStmt;
075: java.sql.ResultSet oRSet;
076:
077: com.knowgate.acl.ACLUser oUsr = new com.knowgate.acl.ACLUser();
078: oUsr.load(oConn, new Object[] { sUserId });
079:
080: String sGuMsg = Gadgets.generateUUID();
081: String sGuFldr = oUsr.getMailFolder(oConn, "drafts");
082: String sFrom = oUsr.getStringNull(DB.nm_user, "") + " "
083: + oUsr.getStringNull(DB.tx_surname1, "") + " "
084: + oUsr.getStringNull(DB.tx_surname2, "");
085: String sIdMsg = "<" + sGuMsg + "."
086: + oUsr.getStringNull(DB.tx_nickname, "") + "@"
087: + sMailHost + ">";
088:
089: oConn.setAutoCommit(false);
090:
091: oStmt = oConn.prepareStatement("SELECT MAX(" + DB.pg_message
092: + ") FROM " + DB.k_mime_msgs + " WHERE "
093: + DB.gu_category + "=?");
094: oStmt.setString(1, sGuFldr);
095: oRSet = oStmt.executeQuery();
096: oRSet.next();
097: java.math.BigDecimal oMax = oRSet.getBigDecimal(1);
098: if (oRSet.wasNull())
099: oMax = new java.math.BigDecimal(0);
100: oRSet.close();
101: oStmt.close();
102:
103: oStmt = oConn
104: .prepareStatement("INSERT INTO " + DB.k_mime_msgs
105: + " (" + DB.gu_mimemsg + "," + DB.pg_message
106: + "," + DB.gu_workarea + "," + DB.gu_category
107: + "," + DB.id_type + "," + DB.id_message + ","
108: + DB.tx_subject + "," + DB.tx_email_from + ","
109: + DB.tx_email_reply + "," + DB.nm_from + ","
110: + DB.len_mimemsg + "," + DB.bo_draft + ","
111: + DB.bo_deleted
112: + ") VALUES(?,?,?,?,?,?,?,?,?,?,0,1,0)");
113: oStmt.setString(1, sGuMsg);
114: oStmt.setBigDecimal(2, oMax.add(new java.math.BigDecimal(1)));
115: oStmt.setString(3, sGuWorkArea);
116: oStmt.setString(4, sGuFldr);
117: oStmt.setString(5, "text/" + sContentType + "; charset=utf-8");
118: oStmt.setString(6, sIdMsg);
119: oStmt.setString(7, "");
120: oStmt.setString(8, oUsr.getString(DB.tx_main_email));
121: oStmt.setString(9, oUsr.getString(DB.tx_main_email));
122: oStmt.setString(10, sFrom.trim());
123: oStmt.executeUpdate();
124: oStmt.close();
125:
126: oStmt = oConn.prepareStatement("INSERT INTO " + DB.k_inet_addrs
127: + " (" + DB.gu_mimemsg + "," + DB.id_message + ","
128: + DB.tx_email + "," + DB.tp_recipient + ","
129: + DB.tx_personal + ") VALUES (?,?,?,?,?)");
130: oStmt.setString(1, sGuMsg);
131: oStmt.setString(2, sIdMsg);
132: oStmt.setString(3, oUsr.getString(DB.tx_main_email));
133: oStmt.setString(4, "from");
134: oStmt.setString(5, sFrom);
135: oStmt.executeUpdate();
136: oStmt.close();
137:
138: oConn.commit();
139:
140: if (DebugFile.trace) {
141: DebugFile.decIdent();
142: DebugFile.writeln("End DraftsHelper.draftMessage()");
143: }
144:
145: return oDraftsFldr.getMessageByGuid(sGuMsg);
146:
147: } // draftMessage
148:
149: // ----------------------------------------------------------------------------------------
150:
151: public static DBMimeMessage draftMessageForReply(
152: DBFolder oDraftsFldr, String sMailHost, String sGuWorkArea,
153: String sUserId, DBFolder oOriginalFldr,
154: String sGuOriginalMsg, boolean bReplyAll,
155: String sContentType) throws SQLException,
156: MessagingException, IOException {
157:
158: if (DebugFile.trace) {
159: DebugFile
160: .writeln("Begin DraftsHelper.draftMessageForReply([DBStore],"
161: + sMailHost
162: + ","
163: + sGuWorkArea
164: + ","
165: + sUserId
166: + ",[DBFolder],"
167: + sGuOriginalMsg
168: + ")");
169: DebugFile.incIdent();
170: }
171:
172: PreparedStatement oStmt;
173:
174: JDCConnection oConn = oDraftsFldr.getConnection();
175:
176: DBMimeMessage oDraft = draftMessage(oDraftsFldr, sMailHost,
177: sGuWorkArea, sUserId, sContentType);
178:
179: com.knowgate.acl.ACLUser oUsr = new com.knowgate.acl.ACLUser();
180: oUsr.load(oConn, new Object[] { sUserId });
181:
182: String sIdMsg = "<" + oDraft.getMessageGuid() + "."
183: + oUsr.getStringNull(DB.tx_nickname, "") + "@"
184: + sMailHost + ">";
185:
186: DBMimeMessage oOrMsg = new DBMimeMessage(oOriginalFldr,
187: sGuOriginalMsg);
188: DBInetAddr oFrom = (DBInetAddr) oOrMsg.getFromRecipient();
189:
190: String sText;
191: if (sContentType.equals("html"))
192: sText = oOrMsg.tagBodyHtml();
193: else
194: sText = oOrMsg.tagBodyPlain();
195:
196: MimeBodyPart oText = new MimeBodyPart();
197: oText.setContent(sText, "text/html");
198: java.io.ByteArrayOutputStream oBaStrm = new java.io.ByteArrayOutputStream(
199: sText.length() * 2 + 2);
200: oText.writeTo(oBaStrm);
201:
202: oStmt = oConn.prepareStatement("UPDATE " + DB.k_mime_msgs
203: + " SET " + DB.len_mimemsg + "=?" + "," + DB.tx_subject
204: + "=?," + DB.by_content + "=? WHERE " + DB.gu_mimemsg
205: + "=?");
206: oStmt.setInt(1, oBaStrm.size());
207: oStmt.setString(2, oOrMsg.getSubject());
208: oStmt.setBinaryStream(3, new java.io.ByteArrayInputStream(
209: oBaStrm.toByteArray()), oBaStrm.size());
210: oStmt.setString(4, oDraft.getMessageGuid());
211: oStmt.executeUpdate();
212: oStmt.close();
213:
214: oStmt = oConn.prepareStatement("INSERT INTO " + DB.k_inet_addrs
215: + " (" + DB.gu_mimemsg + "," + DB.id_message + ","
216: + DB.tx_email + "," + DB.tp_recipient + ","
217: + DB.tx_personal + ") (SELECT '"
218: + oDraft.getMessageGuid() + "','" + sIdMsg + "',"
219: + DB.tx_email + ",'to'," + DB.tx_personal + " FROM "
220: + DB.k_inet_addrs + " WHERE " + DB.gu_mimemsg
221: + "=? AND " + DB.tp_recipient + "='from')");
222: oStmt.setString(1, sGuOriginalMsg);
223: oStmt.executeUpdate();
224: oStmt.close();
225:
226: if (bReplyAll) {
227: oStmt = oConn.prepareStatement("INSERT INTO "
228: + DB.k_inet_addrs + " (" + DB.gu_mimemsg + ","
229: + DB.id_message + "," + DB.tx_email + ","
230: + DB.tp_recipient + "," + DB.tx_personal
231: + ") (SELECT '" + oDraft.getMessageGuid() + "','"
232: + sIdMsg + "'," + DB.tx_email + ",'to',"
233: + DB.tx_personal + " FROM " + DB.k_inet_addrs
234: + " WHERE " + DB.gu_mimemsg + "=? AND "
235: + DB.tp_recipient + "='to')");
236: oStmt.setString(1, sGuOriginalMsg);
237: oStmt.executeUpdate();
238: oStmt.close();
239: oStmt = oConn.prepareStatement("INSERT INTO "
240: + DB.k_inet_addrs + " (" + DB.gu_mimemsg + ","
241: + DB.id_message + "," + DB.tx_email + ","
242: + DB.tp_recipient + "," + DB.tx_personal
243: + ") (SELECT '" + oDraft.getMessageGuid() + "','"
244: + sIdMsg + "'," + DB.tx_email + ",'cc',"
245: + DB.tx_personal + " FROM " + DB.k_inet_addrs
246: + " WHERE " + DB.gu_mimemsg + "=? AND "
247: + DB.tp_recipient + "='cc')");
248: oStmt.setString(1, sGuOriginalMsg);
249: oStmt.executeUpdate();
250: oStmt.close();
251: }
252:
253: oConn.commit();
254:
255: if (DebugFile.trace) {
256: DebugFile.decIdent();
257: DebugFile
258: .writeln("End DraftsHelper.draftMessageForReply()");
259: }
260:
261: return oDraft;
262: } // draftMessageForReply
263:
264: // ----------------------------------------------------------------------------------------
265:
266: public static DBMimeMessage draftMessageForForward(
267: DBFolder oDraftsFldr, String sMailHost, String sGuWorkArea,
268: String sUserId, DBFolder oOriginalFldr,
269: String sGuOriginalMsg, String sContentType)
270: throws SQLException, MessagingException, IOException {
271:
272: if (DebugFile.trace) {
273: DebugFile
274: .writeln("Begin DraftsHelper.draftMessageForForward([DBStore],"
275: + sMailHost
276: + ","
277: + sGuWorkArea
278: + ","
279: + sUserId
280: + ",[DBFolder],"
281: + sGuOriginalMsg
282: + "," + sContentType + ")");
283: DebugFile.incIdent();
284: }
285:
286: PreparedStatement oStmt;
287: ResultSet oRSet;
288:
289: JDCConnection oConn = oDraftsFldr.getConnection();
290:
291: DBMimeMessage oDraft = draftMessage(oDraftsFldr, sMailHost,
292: sGuWorkArea, sUserId, sContentType);
293:
294: // Compose message id by concatenating guid, user nickname and mail host
295: com.knowgate.acl.ACLUser oUsr = new com.knowgate.acl.ACLUser();
296: oUsr.load(oConn, new Object[] { sUserId });
297:
298: String sIdMsg = "<" + oDraft.getMessageGuid() + "."
299: + oUsr.getStringNull(DB.tx_nickname, "") + "@"
300: + sMailHost + ">";
301:
302: DBMimeMessage oOrMsg = new DBMimeMessage(oOriginalFldr,
303: sGuOriginalMsg);
304: DBInetAddr oFrom = (DBInetAddr) oOrMsg.getFromRecipient();
305:
306: String sText;
307: if (sContentType.equals("html"))
308: sText = oOrMsg.tagBodyHtml();
309: else
310: sText = oOrMsg.tagBodyPlain();
311:
312: MimeBodyPart oText = new MimeBodyPart();
313: oText.setContent(sText, "text/html");
314: java.io.ByteArrayOutputStream oBaStrm = new java.io.ByteArrayOutputStream(
315: sText.length() * 2 + 2);
316: oText.writeTo(oBaStrm);
317:
318: java.math.BigDecimal oPosition;
319: oStmt = oConn.prepareStatement("SELECT " + DB.nu_position
320: + " FROM " + DB.k_mime_msgs + " WHERE " + DB.gu_mimemsg
321: + "=?");
322: oStmt.setString(1, sGuOriginalMsg);
323: oRSet = oStmt.executeQuery();
324: if (oRSet.next())
325: oPosition = oRSet.getBigDecimal(1);
326: else
327: oPosition = null;
328: oRSet.close();
329: oRSet = null;
330: oStmt.close();
331: oStmt = null;
332:
333: if (com.knowgate.debug.DebugFile.trace)
334: com.knowgate.debug.DebugFile
335: .writeln("Connection.prepareStatement(UPDATE "
336: + DB.k_mime_msgs + " SET " + DB.nu_position
337: + "=?," + DB.len_mimemsg + "=?" + ","
338: + DB.by_content + "=? WHERE "
339: + DB.gu_mimemsg + "='"
340: + oDraft.getMessageGuid() + "')");
341:
342: oStmt = oConn.prepareStatement("UPDATE " + DB.k_mime_msgs
343: + " SET " + DB.nu_position + "=?," + DB.len_mimemsg
344: + "=?," + DB.tx_subject + "=?," + DB.by_content
345: + "=? WHERE " + DB.gu_mimemsg + "=?");
346: oStmt.setBigDecimal(1, oPosition);
347: oStmt.setInt(2, oBaStrm.size());
348: oStmt.setString(3, oOrMsg.getSubject());
349: oStmt.setBinaryStream(4, new java.io.ByteArrayInputStream(
350: oBaStrm.toByteArray()), oBaStrm.size());
351: oStmt.setString(5, oDraft.getMessageGuid());
352: oStmt.executeUpdate();
353: oStmt.close();
354:
355: if (com.knowgate.debug.DebugFile.trace)
356: com.knowgate.debug.DebugFile
357: .writeln("Connection.prepareStatement(INSERT INTO "
358: + DB.k_mime_parts + " (" + DB.gu_mimemsg
359: + "," + DB.id_message + "," + DB.pg_message
360: + "," + DB.nu_offset + "," + DB.id_part
361: + "," + DB.id_content + "," + DB.id_type
362: + "," + DB.id_disposition + ","
363: + DB.id_encoding + "," + DB.len_part + ","
364: + DB.de_part + "," + DB.tx_md5 + ","
365: + DB.file_name + "," + DB.id_compression
366: + "," + DB.by_content + ") (SELECT '"
367: + oDraft.getMessageGuid() + "','" + sIdMsg
368: + "',NULL," + DB.nu_offset + ","
369: + DB.id_part + "," + DB.id_content + ","
370: + DB.id_type + ",'pointer',"
371: + DB.id_encoding + "," + DB.len_part + ","
372: + DB.file_name + "," + DB.tx_md5 + ",'"
373: + oOriginalFldr.getFilePath() + "',"
374: + DB.id_compression + ",NULL FROM "
375: + DB.k_mime_parts + " WHERE "
376: + DB.gu_mimemsg + "='" + sGuOriginalMsg
377: + "'))");
378:
379: oStmt = oConn.prepareStatement("INSERT INTO " + DB.k_mime_parts
380: + " (" + DB.gu_mimemsg + "," + DB.id_message + ","
381: + DB.pg_message + "," + DB.nu_offset + "," + DB.id_part
382: + "," + DB.id_content + "," + DB.id_type + ","
383: + DB.id_disposition + "," + DB.id_encoding + ","
384: + DB.len_part + "," + DB.de_part + "," + DB.tx_md5
385: + "," + DB.file_name + "," + DB.id_compression + ","
386: + DB.by_content + ") (SELECT '"
387: + oDraft.getMessageGuid() + "','" + sIdMsg + "',NULL,"
388: + DB.nu_offset + "," + DB.id_part + "," + DB.id_content
389: + "," + DB.id_type + ",'pointer'," + DB.id_encoding
390: + "," + DB.len_part + "," + DB.file_name + ","
391: + DB.tx_md5 + ",'" + oOriginalFldr.getFilePath() + "',"
392: + DB.id_compression + ",NULL FROM " + DB.k_mime_parts
393: + " WHERE " + DB.gu_mimemsg + "=? AND "
394: + DB.id_disposition + "='attachment')");
395: oStmt.setString(1, sGuOriginalMsg);
396: oStmt.executeUpdate();
397: oStmt.close();
398:
399: oConn.commit();
400:
401: if (DebugFile.trace) {
402: DebugFile.decIdent();
403: DebugFile
404: .writeln("End DraftsHelper.draftMessageForForward()");
405: }
406:
407: return oDraft;
408: } // draftMessageForForward
409:
410: // ----------------------------------------------------------------------------------------
411:
412: /**
413: *
414: * @param oConn JDCConnection
415: * @param iIdDomain int
416: * @param sGuWorkarea String
417: * @param sGuMsg String
418: * @param sIdMsg String
419: * @param sTxMailFrom String
420: * @param sTxMailReply String
421: * @param sNmFrom String
422: * @param sTxSubject String
423: * @param sContentType String
424: * @param sBody String
425: * @param aTo String[]
426: * @param aCc String[]
427: * @param aBcc String[]
428: * @throws UnsupportedDataTypeException
429: * @throws SQLException
430: * @throws MessagingException
431: * @throws IOException
432: */
433: public static void draftUpdate(JDCConnection oConn, int iIdDomain,
434: String sGuWorkarea, String sGuMsg, String sIdMsg,
435: String sTxMailFrom, String sTxMailReply, String sNmFrom,
436: String sTxSubject, String sContentType, String sBody,
437: String[] aTo, String[] aCc, String[] aBcc)
438: throws SQLException, MessagingException, IOException {
439:
440: if (DebugFile.trace) {
441: DebugFile
442: .writeln("Begin DraftsHelper.draftUpdate([JDCConnection],"
443: + String.valueOf(iIdDomain)
444: + ","
445: + sGuWorkarea
446: + ","
447: + sGuMsg
448: + ","
449: + sIdMsg
450: + ","
451: + sTxMailFrom
452: + ","
453: + sTxMailReply
454: + ","
455: + sNmFrom
456: + ","
457: + sTxSubject
458: + ","
459: + sContentType
460: + ","
461: + Gadgets.left(sBody, 100)
462: + ", String[], String[], String[])");
463: DebugFile.incIdent();
464: }
465:
466: String sAddr;
467: String sSQL = "UPDATE " + DB.k_mime_msgs + " SET "
468: + DB.bo_draft + "=1," + DB.tx_email_from + "=?,"
469: + DB.tx_email_reply + "=?," + DB.nm_from + "=?,"
470: + DB.tx_subject + "=?," + DB.id_type + "=?,"
471: + DB.by_content + "=? WHERE " + DB.gu_mimemsg + "=?";
472:
473: if (DebugFile.trace)
474: DebugFile.writeln("Connection.prepareStatement(" + sSQL
475: + ")");
476:
477: PreparedStatement oStmt = oConn.prepareStatement(sSQL);
478:
479: oStmt.setString(1, sTxMailFrom);
480: oStmt.setString(2, sTxMailReply);
481: oStmt.setString(3, sNmFrom);
482: oStmt.setString(4, sTxSubject);
483: oStmt.setString(5, "text/" + sContentType + "; charset=utf-8");
484: MimeBodyPart oBody = new MimeBodyPart();
485: if (DebugFile.trace)
486: DebugFile.writeln("MimeBodyPart.setContent(\""
487: + Gadgets.left(sBody, 255) + "\",\"text/"
488: + sContentType + "; charset=utf-8\")");
489: if (sContentType.toLowerCase().startsWith("text/"))
490: oBody.setContent(sBody, sContentType + "; charset=utf-8");
491: else
492: oBody.setContent(sBody, "text/" + sContentType
493: + "; charset=utf-8");
494: ByteArrayOutputStream oBodyStrm = new ByteArrayOutputStream(
495: (sBody.length() * 2) + 2);
496: oBody.writeTo(oBodyStrm);
497: oStmt.setBytes(6, oBodyStrm.toByteArray());
498: oStmt.setString(7, sGuMsg);
499: if (DebugFile.trace)
500: DebugFile.writeln("PreparedStatement.executeUpdate()");
501: oStmt.executeUpdate();
502: oStmt.close();
503:
504: RecipientsHelper.clearRecipientsForMessage(oConn, sGuMsg);
505:
506: if (aTo != null) {
507: if (DebugFile.trace)
508: DebugFile.writeln("to recipients count is "
509: + String.valueOf(aTo.length));
510: for (int t = 0; t < aTo.length; t++) {
511: sAddr = aTo[t].trim();
512: if (sAddr.length() > 0) {
513: DBInetAddr.write(oConn, iIdDomain, sGuWorkarea,
514: sGuMsg, sIdMsg, aTo[t].trim(), "to", null);
515: }
516: } // next
517: } // fi
518: if (aCc != null) {
519: if (DebugFile.trace)
520: DebugFile.writeln("cc recipients count is "
521: + String.valueOf(aCc.length));
522: for (int t = 0; t < aCc.length; t++) {
523: sAddr = aCc[t].trim();
524: if (sAddr.length() > 0) {
525: DBInetAddr.write(oConn, iIdDomain, sGuWorkarea,
526: sGuMsg, sIdMsg, aCc[t].trim(), "cc", null);
527: }
528: } // next
529: } // fi
530: if (aBcc != null) {
531: if (DebugFile.trace)
532: DebugFile.writeln("bcc recipients count is "
533: + String.valueOf(aBcc.length));
534: for (int t = 0; t < aBcc.length; t++) {
535: sAddr = aBcc[t].trim();
536: if (sAddr.length() > 0) {
537: DBInetAddr
538: .write(oConn, iIdDomain, sGuWorkarea,
539: sGuMsg, sIdMsg, aBcc[t].trim(),
540: "bcc", null);
541: }
542: } // next
543: } // fi
544: if (DebugFile.trace) {
545: DebugFile.decIdent();
546: DebugFile.writeln("End DraftsHelper.draftUpdate");
547: }
548: } // draftUpdate
549: }
|