1 56 package org.opencrx.mail.workflow; 57 58 import java.io.ByteArrayOutputStream ; 59 import java.util.Date ; 60 import java.util.Iterator ; 61 import java.util.Map ; 62 63 import javax.activation.DataHandler ; 64 import javax.activation.DataSource ; 65 import javax.jmi.reflect.RefObject; 66 import javax.mail.BodyPart ; 67 import javax.mail.Message ; 68 import javax.mail.Multipart ; 69 import javax.mail.Part ; 70 import javax.mail.Session ; 71 import javax.mail.Message.RecipientType; 72 import javax.mail.internet.InternetAddress ; 73 import javax.mail.internet.MimeBodyPart ; 74 import javax.mail.internet.MimeMessage ; 75 import javax.mail.internet.MimeMultipart ; 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 protected String setContent( 89 Message message, 90 Session session, 91 RefPackage_1_0 rootPkg, 92 Path targetIdentity, 93 Path wfProcessInstanceIdentity, 94 UserHome userHome, 95 Map params 96 ) throws ServiceException { 97 String text = null; 98 try { 99 text = this.getText( 100 message, 101 rootPkg, 102 targetIdentity, 103 wfProcessInstanceIdentity, 104 userHome, 105 params 106 ); 107 Multipart multipart = new MimeMultipart (); 109 BodyPart bodyPart = new MimeBodyPart (); 110 bodyPart.setText(text); 111 multipart.addBodyPart(bodyPart); 112 113 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 (); 119 bodyPart.setFileName( 120 emailActivity.getMessageSubject() + ".msg" 121 ); 122 MimeMessage nestedMessage = new MimeMessage (session); 124 nestedMessage.setHeader( 125 "X-Mailer", 126 "openCRX SendMail" 127 ); 128 nestedMessage.setSubject( 130 emailActivity.getMessageSubject() 131 ); 132 Multipart nestedMultipart = new MimeMultipart (); 134 BodyPart nestedBodyPart = new MimeBodyPart (); 135 nestedBodyPart.setText( 136 emailActivity.getMessageBody() 137 ); 138 nestedMultipart.addBodyPart(nestedBodyPart); 139 if(emailActivity.getSender() != null) { 141 nestedMessage.setFrom( 142 new InternetAddress ( 143 emailActivity.getSender().getEmailAddress() 144 ) 145 ); 146 } 147 nestedMessage.setSentDate( 149 new Date () 150 ); 151 for(Iterator 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 ( 168 recipient.getParty().getEmailAddress() 169 ) 170 ); 171 } 172 } 173 for(Iterator i = emailActivity.getMedia().iterator(); i.hasNext(); ) { 175 nestedBodyPart = new MimeBodyPart (); 176 Media media = (Media)i.next(); 177 nestedBodyPart.setFileName( 178 media.getContentName() 179 ); 180 ByteArrayOutputStream os = new ByteArrayOutputStream (); 181 media.getContent(os, 0); 182 nestedBodyPart.setDisposition( 183 Part.ATTACHMENT 184 ); 185 DataSource dataSource = new ByteArrayDataSource( 186 os.toByteArray(), 187 media.getContentMimeType() 188 ); 189 nestedBodyPart.setDataHandler( 190 new DataHandler (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 e) { 207 throw new ServiceException(e); 208 } 209 return text; 210 } 211 212 216 } 217 218 | Popular Tags |