KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencrx > mail > workflow > ExportMailWorkflow


1 /*
2  * ====================================================================
3  * Project: opencrx, http://www.opencrx.org/
4  * Name: $Id: ExportMailWorkflow.java,v 1.4 2006/03/31 22:48:17 wfro Exp $
5  * Description: PrintConsole workflow
6  * Revision: $Revision: 1.4 $
7  * Owner: CRIXP AG, Switzerland, http://www.crixp.com
8  * Date: $Date: 2006/03/31 22:48:17 $
9  * ====================================================================
10  *
11  * This software is published under the BSD license
12  * as listed below.
13  *
14  * Copyright (c) 2004, CRIXP Corp., Switzerland
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  *
21  * * Redistributions of source code must retain the above copyright
22  * notice, this list of conditions and the following disclaimer.
23  *
24  * * Redistributions in binary form must reproduce the above copyright
25  * notice, this list of conditions and the following disclaimer in
26  * the documentation and/or other materials provided with the
27  * distribution.
28  *
29  * * Neither the name of CRIXP Corp. nor the names of the contributors
30  * to openCRX may be used to endorse or promote products derived
31  * from this software without specific prior written permission
32  *
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
35  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
36  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
39  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
41  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
43  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46  * POSSIBILITY OF SUCH DAMAGE.
47  *
48  * ------------------
49  *
50  * This product includes software developed by the Apache Software
51  * Foundation (http://www.apache.org/).
52  *
53  * This product includes software developed by contributors to
54  * openMDX (http://www.openmdx.org/)
55  */

56 package org.opencrx.mail.workflow;
57
58 import java.io.ByteArrayOutputStream JavaDoc;
59 import java.util.Date JavaDoc;
60 import java.util.Iterator JavaDoc;
61 import java.util.Map JavaDoc;
62
63 import javax.activation.DataHandler JavaDoc;
64 import javax.activation.DataSource JavaDoc;
65 import javax.jmi.reflect.RefObject;
66 import javax.mail.BodyPart JavaDoc;
67 import javax.mail.Message JavaDoc;
68 import javax.mail.Multipart JavaDoc;
69 import javax.mail.Part JavaDoc;
70 import javax.mail.Session JavaDoc;
71 import javax.mail.Message.RecipientType;
72 import javax.mail.internet.InternetAddress JavaDoc;
73 import javax.mail.internet.MimeBodyPart JavaDoc;
74 import javax.mail.internet.MimeMessage JavaDoc;
75 import javax.mail.internet.MimeMultipart JavaDoc;
76
77 import org.opencrx.kernel.activity1.cci.EMailRecipient;
78 import org.opencrx.kernel.generic.cci.Media;
79 import org.opencrx.kernel.home1.cci.UserHome;
80 import org.openmdx.base.accessor.jmi.cci.RefPackage_1_0;
81 import org.openmdx.base.exception.ServiceException;
82 import org.openmdx.compatibility.base.naming.Path;
83
84 public class ExportMailWorkflow
85     extends AbstractMailingWorkflow {
86
87     //-----------------------------------------------------------------------
88
protected String JavaDoc setContent(
89         Message JavaDoc message,
90         Session JavaDoc session,
91         RefPackage_1_0 rootPkg,
92         Path targetIdentity,
93         Path wfProcessInstanceIdentity,
94         UserHome userHome,
95         Map JavaDoc params
96     ) throws ServiceException {
97         String JavaDoc text = null;
98         try {
99             text = this.getText(
100                 message,
101                 rootPkg,
102                 targetIdentity,
103                 wfProcessInstanceIdentity,
104                 userHome,
105                 params
106             );
107             // message text
108
Multipart JavaDoc multipart = new MimeMultipart JavaDoc();
109             BodyPart JavaDoc bodyPart = new MimeBodyPart JavaDoc();
110             bodyPart.setText(text);
111             multipart.addBodyPart(bodyPart);
112
113             // Add EMail activity as nested message
114
RefObject targetObject = rootPkg.refObject(targetIdentity.toXri());
115             if(targetObject instanceof org.opencrx.kernel.activity1.cci.EMail) {
116                 org.opencrx.kernel.activity1.cci.EMail emailActivity =
117                     (org.opencrx.kernel.activity1.cci.EMail)targetObject;
118                 bodyPart = new MimeBodyPart JavaDoc();
119                 bodyPart.setFileName(
120                     emailActivity.getMessageSubject() + ".msg"
121                 );
122                 // nested message
123
MimeMessage JavaDoc nestedMessage = new MimeMessage JavaDoc(session);
124                 nestedMessage.setHeader(
125                     "X-Mailer",
126                     "openCRX SendMail"
127                 );
128                 // nested message subject
129
nestedMessage.setSubject(
130                     emailActivity.getMessageSubject()
131                 );
132                 // nested message body
133
Multipart JavaDoc nestedMultipart = new MimeMultipart JavaDoc();
134                 BodyPart JavaDoc nestedBodyPart = new MimeBodyPart JavaDoc();
135                 nestedBodyPart.setText(
136                     emailActivity.getMessageBody()
137                 );
138                 nestedMultipart.addBodyPart(nestedBodyPart);
139                 // nested message sender
140
if(emailActivity.getSender() != null) {
141                     nestedMessage.setFrom(
142                         new InternetAddress JavaDoc(
143                             emailActivity.getSender().getEmailAddress()
144                         )
145                     );
146                 }
147                 // nested message sent date
148
nestedMessage.setSentDate(
149                     new Date JavaDoc()
150                 );
151                 // nested message recipients
152
for(Iterator JavaDoc i = emailActivity.getEmailRecipient().iterator(); i.hasNext(); ) {
153                     EMailRecipient recipient = (EMailRecipient)i.next();
154                     RecipientType recipientType = null;
155                     if(recipient.getPartyType() == PARTY_TYPE_TO) {
156                         recipientType = RecipientType.TO;
157                     }
158                     else if(recipient.getPartyType() == PARTY_TYPE_CC) {
159                         recipientType = RecipientType.CC;
160                     }
161                     else if(recipient.getPartyType() == PARTY_TYPE_BCC) {
162                         recipientType = RecipientType.BCC;
163                     }
164                     if(recipientType != null) {
165                         nestedMessage.addRecipient(
166                             recipientType,
167                             new InternetAddress JavaDoc(
168                                 recipient.getParty().getEmailAddress()
169                             )
170                         );
171                     }
172                 }
173                 // nested message media attachments
174
for(Iterator JavaDoc i = emailActivity.getMedia().iterator(); i.hasNext(); ) {
175                     nestedBodyPart = new MimeBodyPart JavaDoc();
176                     Media media = (Media)i.next();
177                     nestedBodyPart.setFileName(
178                         media.getContentName()
179                     );
180                     ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
181                     media.getContent(os, 0);
182                     nestedBodyPart.setDisposition(
183                         Part.ATTACHMENT
184                     );
185                     DataSource JavaDoc dataSource = new ByteArrayDataSource(
186                         os.toByteArray(),
187                         media.getContentMimeType()
188                     );
189                     nestedBodyPart.setDataHandler(
190                         new DataHandler JavaDoc(dataSource)
191                     );
192                     nestedMultipart.addBodyPart(nestedBodyPart);
193                 }
194                 nestedMessage.setContent(nestedMultipart);
195                 bodyPart.setDisposition(
196                     Part.ATTACHMENT
197                 );
198                 bodyPart.setContent(
199                     nestedMessage,
200                     "message/rfc822"
201                 );
202                 multipart.addBodyPart(bodyPart);
203             }
204             message.setContent(multipart);
205         }
206         catch(Exception JavaDoc e) {
207             throw new ServiceException(e);
208         }
209         return text;
210     }
211         
212     //-----------------------------------------------------------------------
213
// Members
214
//-----------------------------------------------------------------------
215

216 }
217
218 //--- End of File -----------------------------------------------------------
219
Popular Tags