| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kcb.service.impl; |
| 17 | |
|
| 18 | |
import javax.mail.internet.AddressException; |
| 19 | |
import javax.mail.internet.InternetAddress; |
| 20 | |
|
| 21 | |
import org.apache.commons.lang.StringUtils; |
| 22 | |
import org.apache.log4j.Logger; |
| 23 | |
import org.kuali.rice.kcb.bo.Message; |
| 24 | |
import org.kuali.rice.kcb.bo.MessageDelivery; |
| 25 | |
import org.kuali.rice.kcb.service.EmailService; |
| 26 | |
import org.kuali.rice.ken.util.NotificationConstants; |
| 27 | |
import org.kuali.rice.kew.mail.EmailBody; |
| 28 | |
import org.kuali.rice.kew.mail.EmailFrom; |
| 29 | |
import org.kuali.rice.kew.mail.EmailSubject; |
| 30 | |
import org.kuali.rice.kew.mail.EmailTo; |
| 31 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
| 32 | |
import org.springframework.beans.factory.annotation.Required; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | 0 | public class EmailServiceImpl implements EmailService { |
| 40 | |
|
| 41 | 0 | private static Logger LOG = Logger.getLogger(EmailServiceImpl.class); |
| 42 | |
|
| 43 | |
private static final String FORMAT_TEXT_HTML = "text/html"; |
| 44 | |
private static final String FORMAT_TEXT_PLAIN = "text/plain"; |
| 45 | |
|
| 46 | |
|
| 47 | |
private String weburl; |
| 48 | 0 | private String defaultSender = "kcb@localhost"; |
| 49 | |
|
| 50 | 0 | private final String DETAILACTION = "DetailView.form"; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
@Required |
| 57 | |
public void setWeburl(String weburl) { |
| 58 | 0 | this.weburl = weburl; |
| 59 | 0 | } |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
public void setDefaultSender(String defaultSender) { |
| 67 | 0 | this.defaultSender = defaultSender; |
| 68 | 0 | } |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
public Long sendEmail(MessageDelivery messageDelivery, String recipientEmailAddress, String emailFormat) throws Exception { |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | 0 | Message message = messageDelivery.getMessage(); |
| 79 | 0 | String channelName = message.getChannel(); |
| 80 | |
|
| 81 | 0 | String producer = message.getProducer(); |
| 82 | 0 | String sender = defaultSender; |
| 83 | 0 | if (producer != null) { |
| 84 | |
try { |
| 85 | 0 | InternetAddress[] addresses = InternetAddress.parse(producer, false); |
| 86 | 0 | if (addresses.length > 0) { |
| 87 | 0 | sender = addresses[0].toString(); |
| 88 | |
} |
| 89 | 0 | } catch (AddressException ae) { |
| 90 | |
|
| 91 | 0 | } |
| 92 | |
} |
| 93 | |
|
| 94 | 0 | String title = messageDelivery.getMessage().getTitle(); |
| 95 | 0 | String subject = (channelName == null ? "" : channelName + " ") + (!StringUtils.isBlank(title) ? " - " + title : ""); |
| 96 | |
|
| 97 | 0 | String format = FORMAT_TEXT_PLAIN; |
| 98 | 0 | String linebreak = "\n\n"; |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | 0 | String link = weburl +"/"+ DETAILACTION +"?" |
| 104 | |
+ NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.MSG_DELIVERY_ID +"="+ messageDelivery.getId(); |
| 105 | |
|
| 106 | 0 | if (emailFormat == null || emailFormat.equals("text")) { |
| 107 | |
|
| 108 | |
} else { |
| 109 | 0 | format = FORMAT_TEXT_HTML; |
| 110 | 0 | link = "<a href='"+ link +"'>Notification Detail</a>"; |
| 111 | 0 | linebreak = "<br /><br />"; |
| 112 | |
} |
| 113 | |
|
| 114 | 0 | LOG.debug("link: "+link); |
| 115 | |
|
| 116 | |
|
| 117 | 0 | StringBuffer sb = new StringBuffer(); |
| 118 | 0 | sb.append("You have a new notification. Click the link below to review its details."); |
| 119 | 0 | sb.append(linebreak); |
| 120 | 0 | sb.append(linebreak); |
| 121 | 0 | sb.append(link); |
| 122 | 0 | String content = sb.toString(); |
| 123 | |
|
| 124 | 0 | LOG.debug("subject: "+subject); |
| 125 | 0 | LOG.debug("sender: "+sender); |
| 126 | 0 | LOG.debug("recipient: "+recipientEmailAddress); |
| 127 | 0 | LOG.debug("content: "+content); |
| 128 | |
|
| 129 | |
|
| 130 | 0 | sendEmail(content, subject, sender, recipientEmailAddress, format); |
| 131 | |
|
| 132 | 0 | return null; |
| 133 | |
} |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
protected void sendEmail(String message, String subject, String from, String sendTo, String format) { |
| 144 | |
|
| 145 | 0 | KEWServiceLocator.getEmailService().sendEmail( |
| 146 | |
new EmailFrom(from), |
| 147 | |
new EmailTo(sendTo), |
| 148 | |
new EmailSubject(subject), |
| 149 | |
new EmailBody(message), |
| 150 | |
!FORMAT_TEXT_PLAIN.equals(format)); |
| 151 | |
|
| 152 | 0 | } |
| 153 | |
} |