KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ====================================================================
3  * Project: opencrx, http://www.opencrx.org/
4  * Name: $Id: SendMailWorkflow.java,v 1.2 2006/03/31 22:48:17 wfro Exp $
5  * Description: PrintConsole workflow
6  * Revision: $Revision: 1.2 $
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.ArrayList JavaDoc;
60 import java.util.Iterator JavaDoc;
61 import java.util.List JavaDoc;
62 import java.util.Map JavaDoc;
63
64 import javax.activation.DataHandler JavaDoc;
65 import javax.activation.DataSource JavaDoc;
66 import javax.jmi.reflect.RefObject;
67 import javax.mail.Address JavaDoc;
68 import javax.mail.BodyPart JavaDoc;
69 import javax.mail.Message JavaDoc;
70 import javax.mail.Multipart JavaDoc;
71 import javax.mail.Part JavaDoc;
72 import javax.mail.Session JavaDoc;
73 import javax.mail.Message.RecipientType;
74 import javax.mail.internet.InternetAddress JavaDoc;
75 import javax.mail.internet.MimeBodyPart JavaDoc;
76 import javax.mail.internet.MimeMultipart JavaDoc;
77
78 import org.opencrx.kernel.activity1.cci.EMailRecipient;
79 import org.opencrx.kernel.generic.cci.Media;
80 import org.opencrx.kernel.home1.cci.EMailAccount;
81 import org.opencrx.kernel.home1.cci.UserHome;
82 import org.openmdx.base.accessor.jmi.cci.RefPackage_1_0;
83 import org.openmdx.base.exception.ServiceException;
84 import org.openmdx.compatibility.base.naming.Path;
85
86 public class SendMailWorkflow
87     extends AbstractMailingWorkflow {
88
89     //-----------------------------------------------------------------------
90
protected String JavaDoc getSubject(
91         RefPackage_1_0 rootPkg,
92         Path targetIdentity,
93         UserHome userHome,
94         Map JavaDoc params
95     ) throws ServiceException {
96         String JavaDoc subject = null;
97         try {
98             RefObject targetObject = rootPkg.refObject(targetIdentity.toXri());
99             if(targetObject instanceof org.opencrx.kernel.activity1.cci.EMail) {
100                 org.opencrx.kernel.activity1.cci.EMail emailActivity =
101                     (org.opencrx.kernel.activity1.cci.EMail)targetObject;
102                 subject = emailActivity.getMessageSubject();
103             }
104         }
105         catch(Exception JavaDoc e) {
106             throw new ServiceException(e);
107         }
108         return subject;
109     }
110     
111     //-----------------------------------------------------------------------
112
protected Address JavaDoc[] setRecipients(
113         Message JavaDoc message,
114         RefPackage_1_0 rootPkg,
115         Path targetIdentity,
116         EMailAccount eMailAccount
117     ) throws ServiceException {
118         List JavaDoc recipients = new ArrayList JavaDoc();
119         try {
120             RefObject targetObject = rootPkg.refObject(targetIdentity.toXri());
121             if(targetObject instanceof org.opencrx.kernel.activity1.cci.EMail) {
122                 org.opencrx.kernel.activity1.cci.EMail emailActivity =
123                     (org.opencrx.kernel.activity1.cci.EMail)targetObject;
124                 if(emailActivity.getSender() != null) {
125                     Address JavaDoc sender = new InternetAddress JavaDoc(
126                         emailActivity.getSender().getEmailAddress()
127                     );
128                     message.setFrom(sender);
129                 }
130                 for(Iterator JavaDoc i = emailActivity.getEmailRecipient().iterator(); i.hasNext(); ) {
131                     EMailRecipient recipient = (EMailRecipient)i.next();
132                     RecipientType recipientType = null;
133                     if(recipient.getPartyType() == PARTY_TYPE_TO) {
134                         recipientType = RecipientType.TO;
135                     }
136                     else if(recipient.getPartyType() == PARTY_TYPE_CC) {
137                         recipientType = RecipientType.CC;
138                     }
139                     else if(recipient.getPartyType() == PARTY_TYPE_BCC) {
140                         recipientType = RecipientType.BCC;
141                     }
142                     if(recipientType != null) {
143                         Address JavaDoc to = new InternetAddress JavaDoc(
144                             recipient.getParty().getEmailAddress()
145                         );
146                         recipients.add(to);
147                         message.addRecipient(
148                             recipientType,
149                             to
150                         );
151                     }
152                 }
153             }
154         }
155         catch(Exception JavaDoc e) {
156             throw new ServiceException(e);
157         }
158         return (Address JavaDoc[])recipients.toArray(new Address JavaDoc[recipients.size()]);
159     }
160     
161     //-----------------------------------------------------------------------
162
protected String JavaDoc setContent(
163         Message JavaDoc message,
164         Session JavaDoc session,
165         RefPackage_1_0 rootPkg,
166         Path targetIdentity,
167         Path wfProcessInstanceIdentity,
168         UserHome userHome,
169         Map JavaDoc params
170     ) throws ServiceException {
171         String JavaDoc text = null;
172         try {
173             RefObject targetObject = rootPkg.refObject(targetIdentity.toXri());
174             if(targetObject instanceof org.opencrx.kernel.activity1.cci.EMail) {
175                 org.opencrx.kernel.activity1.cci.EMail emailActivity =
176                     (org.opencrx.kernel.activity1.cci.EMail)targetObject;
177                 Multipart JavaDoc multipart = new MimeMultipart JavaDoc();
178                 BodyPart JavaDoc messageBodyPart = new MimeBodyPart JavaDoc();
179                 messageBodyPart.setText(
180                     text = emailActivity.getMessageBody()
181                 );
182                 multipart.addBodyPart(messageBodyPart);
183                 for(Iterator JavaDoc i = emailActivity.getMedia().iterator(); i.hasNext(); ) {
184                     messageBodyPart = new MimeBodyPart JavaDoc();
185                     Media media = (Media)i.next();
186                     messageBodyPart.setFileName(
187                         media.getContentName()
188                     );
189                     ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
190                     media.getContent(os, 0);
191                     messageBodyPart.setDisposition(
192                         Part.ATTACHMENT
193                     );
194                     DataSource JavaDoc dataSource = new ByteArrayDataSource(
195                         os.toByteArray(),
196                         media.getContentMimeType()
197                     );
198                     messageBodyPart.setDataHandler(
199                         new DataHandler JavaDoc(dataSource)
200                     );
201                     multipart.addBodyPart(messageBodyPart);
202                 }
203                 message.setContent(multipart);
204             }
205         }
206         catch(Exception JavaDoc e) {
207             throw new ServiceException(e);
208         }
209         return text;
210     }
211     
212 }
213
214 //--- End of File -----------------------------------------------------------
215
Popular Tags