1 56 package org.opencrx.mail.workflow; 57 58 import java.io.ByteArrayOutputStream ; 59 import java.util.ArrayList ; 60 import java.util.Iterator ; 61 import java.util.List ; 62 import java.util.Map ; 63 64 import javax.activation.DataHandler ; 65 import javax.activation.DataSource ; 66 import javax.jmi.reflect.RefObject; 67 import javax.mail.Address ; 68 import javax.mail.BodyPart ; 69 import javax.mail.Message ; 70 import javax.mail.Multipart ; 71 import javax.mail.Part ; 72 import javax.mail.Session ; 73 import javax.mail.Message.RecipientType; 74 import javax.mail.internet.InternetAddress ; 75 import javax.mail.internet.MimeBodyPart ; 76 import javax.mail.internet.MimeMultipart ; 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 protected String getSubject( 91 RefPackage_1_0 rootPkg, 92 Path targetIdentity, 93 UserHome userHome, 94 Map params 95 ) throws ServiceException { 96 String 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 e) { 106 throw new ServiceException(e); 107 } 108 return subject; 109 } 110 111 protected Address [] setRecipients( 113 Message message, 114 RefPackage_1_0 rootPkg, 115 Path targetIdentity, 116 EMailAccount eMailAccount 117 ) throws ServiceException { 118 List recipients = new ArrayList (); 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 sender = new InternetAddress ( 126 emailActivity.getSender().getEmailAddress() 127 ); 128 message.setFrom(sender); 129 } 130 for(Iterator 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 to = new InternetAddress ( 144 recipient.getParty().getEmailAddress() 145 ); 146 recipients.add(to); 147 message.addRecipient( 148 recipientType, 149 to 150 ); 151 } 152 } 153 } 154 } 155 catch(Exception e) { 156 throw new ServiceException(e); 157 } 158 return (Address [])recipients.toArray(new Address [recipients.size()]); 159 } 160 161 protected String setContent( 163 Message message, 164 Session session, 165 RefPackage_1_0 rootPkg, 166 Path targetIdentity, 167 Path wfProcessInstanceIdentity, 168 UserHome userHome, 169 Map params 170 ) throws ServiceException { 171 String 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 multipart = new MimeMultipart (); 178 BodyPart messageBodyPart = new MimeBodyPart (); 179 messageBodyPart.setText( 180 text = emailActivity.getMessageBody() 181 ); 182 multipart.addBodyPart(messageBodyPart); 183 for(Iterator i = emailActivity.getMedia().iterator(); i.hasNext(); ) { 184 messageBodyPart = new MimeBodyPart (); 185 Media media = (Media)i.next(); 186 messageBodyPart.setFileName( 187 media.getContentName() 188 ); 189 ByteArrayOutputStream os = new ByteArrayOutputStream (); 190 media.getContent(os, 0); 191 messageBodyPart.setDisposition( 192 Part.ATTACHMENT 193 ); 194 DataSource dataSource = new ByteArrayDataSource( 195 os.toByteArray(), 196 media.getContentMimeType() 197 ); 198 messageBodyPart.setDataHandler( 199 new DataHandler (dataSource) 200 ); 201 multipart.addBodyPart(messageBodyPart); 202 } 203 message.setContent(multipart); 204 } 205 } 206 catch(Exception e) { 207 throw new ServiceException(e); 208 } 209 return text; 210 } 211 212 } 213 214 | Popular Tags |