Java tutorial
/******************************************************************************* * Copyright (c) 2009 David Harrison. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl-3.0.html * * Contributors: * David Harrison - initial API and implementation ******************************************************************************/ package com.sfs.whichdoctor.beans; import java.util.ArrayList; import java.util.Collection; import java.util.StringTokenizer; import java.util.TreeMap; import org.apache.commons.lang.StringUtils; import com.sfs.beans.UserBean; import com.sfs.beans.PrivilegesBean; import com.sfs.whichdoctor.formatter.email.EmailFormatter; /** * The Class BulkEmailBean. * * @author David Harrison 7th January 2009 */ public class BulkEmailBean extends WhichDoctorBean { /** The Constant serialVersionUID. */ private static final long serialVersionUID = 1L; /** The Constant MAX_PRIORITY. */ private static final int MAX_PRIORITY = 5; /** The Constant DEFAULT_PRIORITY. */ private static final int DEFAULT_PRIORITY = 3; /** The email recipients. */ private Collection<EmailRecipientBean> emailRecipients; /** The ordered email recipients. */ private Collection<EmailRecipientBean> orderedEmailRecipients; /** The email address. */ private String emailAddress; /** The sender name. */ private String senderName; /** The subject. */ private String subject; /** The message. */ private String message; /** The priority. */ private int priority; /** The append contact. */ private boolean appendContact; /** The append logo. */ private boolean appendLogo; /** The append privacy. */ private boolean appendPrivacy; /** The email class. */ private String emailClass; /** The email type. */ private String emailType; /** The additional recipients. */ private String additionalRecipients; /** The sent count. */ private int sentCount; /** The failed count. */ private int failedCount; /** The pending count. */ private int pendingCount; /** The people sent count. */ private int peopleSentCount; /** The people pending count. */ private int peoplePendingCount; /** The people failed count. */ private int peopleFailedCount; /** The organisations sent count. */ private int organisationsSentCount; /** The organisations pending count. */ private int organisationsPendingCount; /** The organisations failed count. */ private int organisationsFailedCount; /** The send copy flag. */ private boolean sendCopy; /** The deliver out of hours flag. */ private boolean deliverOutOfHours; /** * Sets the email recipients. * * @param emailRecipientsRef the new email recipients */ public final void setEmailRecipients(final Collection<EmailRecipientBean> emailRecipientsRef) { this.emailRecipients = emailRecipientsRef; // Calculate the recipient totals by iterating through the collection for (EmailRecipientBean recipient : this.getEmailRecipients()) { if (recipient.getPersonGUID() > 0) { if (recipient.getSent()) { this.peopleSentCount++; } if (recipient.getPending()) { this.peoplePendingCount++; } if (recipient.getFailed()) { this.peopleFailedCount++; } } if (recipient.getOrganisationGUID() > 0) { if (recipient.getSent()) { this.organisationsSentCount++; } if (recipient.getPending()) { this.organisationsPendingCount++; } if (recipient.getFailed()) { this.organisationsFailedCount++; } } } } /** * Gets the email recipients. * * @return the email recipients */ public final Collection<EmailRecipientBean> getEmailRecipients() { if (this.emailRecipients == null) { this.emailRecipients = new ArrayList<EmailRecipientBean>(); } return this.emailRecipients; } /** * Gets the ordered email recipients. * * @return the ordered email recipients */ public final Collection<EmailRecipientBean> getOrderedEmailRecipients() { if (this.orderedEmailRecipients == null) { this.orderedEmailRecipients = new ArrayList<EmailRecipientBean>(); TreeMap<String, EmailRecipientBean> orderMap = new TreeMap<String, EmailRecipientBean>(); for (EmailRecipientBean recipient : getEmailRecipients()) { if (recipient != null) { if (StringUtils.isNotBlank(recipient.getOrder())) { orderMap.put(recipient.getOrder(), recipient); } } } for (String key : orderMap.keySet()) { EmailRecipientBean recipient = orderMap.get(key); this.orderedEmailRecipients.add(recipient); } } return this.orderedEmailRecipients; } /** * Sets the email address. * * @param emailAddressVal the new email address */ public final void setEmailAddress(final String emailAddressVal) { this.emailAddress = emailAddressVal; } /** * Gets the email address. * * @return the email address */ public final String getEmailAddress() { if (this.emailAddress == null) { this.emailAddress = ""; } return this.emailAddress; } /** * Sets the sender name. * * @param senderNameVal the new sender name */ public final void setSenderName(final String senderNameVal) { this.senderName = senderNameVal; } /** * Gets the sender name. * * @return the sender name */ public final String getSenderName() { if (this.senderName == null) { this.senderName = ""; } return this.senderName; } /** * Sets the subject. * * @param subjectVal the new subject */ public final void setSubject(final String subjectVal) { this.subject = subjectVal; } /** * Gets the subject. * * @return the subject */ public final String getSubject() { if (this.subject == null) { this.subject = ""; } return this.subject; } /** * Sets the message. * * @param messageVal the new message */ public final void setMessage(final String messageVal) { this.message = messageVal; } /** * Gets the message. * * @return the message */ public final String getMessage() { if (this.message == null) { this.message = ""; } return this.message; } /** * Sets the priority. * * @param priorityVal the new priority */ public final void setPriority(final int priorityVal) { // If the priority is less than 1 or more than 5 set to default (3) if (priorityVal < 1 || priorityVal > MAX_PRIORITY) { this.priority = DEFAULT_PRIORITY; } else { this.priority = priorityVal; } } /** * Gets the priority. * * @return the priority */ public final int getPriority() { if (this.priority < 1 || this.priority > MAX_PRIORITY) { this.priority = DEFAULT_PRIORITY; } return this.priority; } /** * Sets the append contact. * * @param appendContactVal the new append contact */ public final void setAppendContact(final boolean appendContactVal) { this.appendContact = appendContactVal; } /** * Gets the append contact. * * @return the append contact */ public final boolean getAppendContact() { return this.appendContact; } /** * Sets the append logo. * * @param appendLogoVal the new append logo */ public final void setAppendLogo(final boolean appendLogoVal) { this.appendLogo = appendLogoVal; } /** * Gets the append logo. * * @return the append logo */ public final boolean getAppendLogo() { return this.appendLogo; } /** * Sets the append privacy. * * @param appendPrivacyVal the new append privacy */ public final void setAppendPrivacy(final boolean appendPrivacyVal) { this.appendPrivacy = appendPrivacyVal; } /** * Gets the append privacy. * * @return the append privacy */ public final boolean getAppendPrivacy() { return this.appendPrivacy; } /** * Gets the append description. * * @return the append description */ public final String getAppendDescription() { final StringBuffer description = new StringBuffer(); if (this.appendContact) { description.append("contact details"); } if (this.appendLogo) { if (description.length() > 0) { if (this.appendPrivacy) { description.append(", "); } else { description.append(" and "); } } description.append("logo"); } if (this.appendPrivacy) { if (description.length() > 0) { description.append(" and "); } description.append("privacy statement"); } if (description.length() > 0) { description.insert(0, "Appended "); description.append("."); } if (this.getDeliverOutOfHours()) { if (description.length() > 0) { description.append(" "); } description.append("Delivered outside of office hours."); } return description.toString(); } /** * Sets the email class. * * @param emailClassVal the new email class */ public final void setEmailClass(final String emailClassVal) { this.emailClass = emailClassVal; } /** * Gets the email class. * * @return the email class */ public final String getEmailClass() { if (this.emailClass == null) { this.emailClass = ""; } return this.emailClass; } /** * Sets the email type. * * @param emailTypeVal the new email type */ public final void setEmailType(final String emailTypeVal) { this.emailType = emailTypeVal; } /** * Gets the email type. * * @return the email type */ public final String getEmailType() { if (this.emailType == null) { this.emailType = ""; } return this.emailType; } /** * Sets the additional recipients. * * @param additionalRecipientsVal the new additional recipients */ public final void setAdditionalRecipients(final String additionalRecipientsVal) { this.additionalRecipients = additionalRecipientsVal; } /** * Gets the additional recipients. * * @return the additional recipients */ public final String getAdditionalRecipients() { if (this.additionalRecipients == null) { this.additionalRecipients = ""; } return this.additionalRecipients.trim(); } /** * Gets the additional recipients as a list. * * @return the additional recipients list */ public final Collection<String> getAdditionalRecipientsList() { Collection<String> list = new ArrayList<String>(); if (StringUtils.isNotBlank(this.getAdditionalRecipients())) { StringTokenizer tk = new StringTokenizer(this.getAdditionalRecipients(), ","); while (tk.hasMoreTokens()) { String address = tk.nextToken(); if (StringUtils.isNotBlank(address)) { if (address.indexOf("@") > 1) { list.add(address); } } } } return list; } /** * Sets the sent count. * * @param sentCountVal the new sent count */ public final void setSentCount(final int sentCountVal) { this.sentCount = sentCountVal; } /** * Gets the sent count. * * @return the sent count */ public final int getSentCount() { return this.sentCount; } /** * Sets the failed count. * * @param sentCountVal the new failed count */ public final void setFailedCount(final int sentCountVal) { this.failedCount = sentCountVal; } /** * Gets the failed count. * * @return the failed count */ public final int getFailedCount() { return this.failedCount; } /** * Sets the pending count. * * @param pendingCountVal the new pending count */ public final void setPendingCount(final int pendingCountVal) { this.pendingCount = pendingCountVal; } /** * Gets the pending count. * * @return the pending count */ public final int getPendingCount() { return this.pendingCount; } /** * Gets the total count. * * @return the total count */ public final int getTotalCount() { return getSentCount() + getFailedCount() + getPendingCount(); } /** * Sets the people sent count. * * @param peopleSentCountVal the new people sent count */ public final void setPeopleSentCount(final int peopleSentCountVal) { this.peopleSentCount = peopleSentCountVal; } /** * Gets the people sent count. * * @return the people sent count */ public final int getPeopleSentCount() { return this.peopleSentCount; } /** * Sets the people pending count. * * @param peoplePendingCountVal the new people pending count */ public final void setPeoplePendingCount(final int peoplePendingCountVal) { this.peoplePendingCount = peoplePendingCountVal; } /** * Gets the people pending count. * * @return the people pending count */ public final int getPeoplePendingCount() { return this.peoplePendingCount; } /** * Sets the people failed count. * * @param peopleFailedCountVal the new people failed count */ public final void setPeopleFailedCount(final int peopleFailedCountVal) { this.peopleFailedCount = peopleFailedCountVal; } /** * Gets the people failed count. * * @return the people failed count */ public final int getPeopleFailedCount() { return this.peopleFailedCount; } /** * Sets the organisations sent count. * * @param organisationsSentCountVal the new organisations sent count */ public final void setOrganisationsSentCount(final int organisationsSentCountVal) { this.organisationsSentCount = organisationsSentCountVal; } /** * Gets the organisations sent count. * * @return the organisations sent count */ public final int getOrganisationsSentCount() { return this.organisationsSentCount; } /** * Sets the organisations pending count. * * @param organisationsPendingCountVal the new organisations pending count */ public final void setOrganisationsPendingCount(final int organisationsPendingCountVal) { this.organisationsPendingCount = organisationsPendingCountVal; } /** * Gets the organisations pending count. * * @return the organisations pending count */ public final int getOrganisationsPendingCount() { return this.organisationsPendingCount; } /** * Sets the organisations failed count. * * @param organisationsFailedCountVal the new organisations failed count */ public final void setOrganisationsFailedCount(final int organisationsFailedCountVal) { this.organisationsFailedCount = organisationsFailedCountVal; } /** * Gets the organisations failed count. * * @return the organisations failed count */ public final int getOrganisationsFailedCount() { return this.organisationsFailedCount; } /** * Sets the send copy flag. * * @param sendCopyVal the new send copy */ public final void setSendCopy(final boolean sendCopyVal) { this.sendCopy = sendCopyVal; } /** * Gets the send copy flag. * * @return the send copy */ public final boolean getSendCopy() { return this.sendCopy; } /** * Sets the deliver out of hours flag. * * @param deliverOutOfHoursVal the new deliver out of hours value */ public final void setDeliverOutOfHours(final boolean deliverOutOfHoursVal) { this.deliverOutOfHours = deliverOutOfHoursVal; } /** * Gets the deliver out of hours flag. * * @return the deliver out of hours flag */ public final boolean getDeliverOutOfHours() { return this.deliverOutOfHours; } /** * Gets the sent list. * * @return the sent list */ public final Collection<EmailRecipientBean> getSentList() { Collection<EmailRecipientBean> sent = new ArrayList<EmailRecipientBean>(); for (EmailRecipientBean recipient : this.getOrderedEmailRecipients()) { if (recipient.getSent()) { sent.add(recipient); } } return sent; } /** * Gets the failed list. * * @return the failed list */ public final Collection<EmailRecipientBean> getFailedList() { Collection<EmailRecipientBean> failed = new ArrayList<EmailRecipientBean>(); for (EmailRecipientBean recipient : this.getOrderedEmailRecipients()) { if (recipient.getFailed()) { failed.add(recipient); } } return failed; } /** * Gets the pending list. * * @return the pending list */ public final Collection<EmailRecipientBean> getPendingList() { Collection<EmailRecipientBean> pending = new ArrayList<EmailRecipientBean>(); for (EmailRecipientBean recipient : this.getOrderedEmailRecipients()) { if (recipient.getPending()) { pending.add(recipient); } } return pending; } /** * Sets the from address. * * @param emailAddress the from email address * @param user the user * @param privileges the privileges */ public final void setFrom(final String emailAddress, final UserBean user, final PrivilegesBean privileges) { Collection<String> addresses = privileges.getEmailAddresses(user); boolean parseAddress = false; if (StringUtils.isNotBlank(emailAddress)) { for (String address : addresses) { if (StringUtils.equalsIgnoreCase(address.trim(), emailAddress.trim())) { // The address is in the allowed group for this user parseAddress = true; } } } if (parseAddress) { if (emailAddress.indexOf("<") > 0) { String name = emailAddress.substring(0, emailAddress.indexOf("<")); this.setSenderName(name.trim()); String address = emailAddress.substring(emailAddress.indexOf("<") + 1); if (address.indexOf(">") > 0) { address = address.substring(0, address.indexOf(">")); } this.setEmailAddress(address); } else { this.setSenderName(emailAddress); this.setEmailAddress(emailAddress); } } if (StringUtils.isBlank(this.getEmailAddress())) { // If blank set to default this.setSenderName(user.getPreferredName() + " " + user.getLastName()); this.setEmailAddress(user.getEmail()); } } /** * Generate an HTML message based on the bulk email details. * * @param preferences the preferences * @param includeWrapper the include wrapper * @param embedImage the embed image * @param person the person * @param organisation the organisation * @param emailFormatters the email formatters * @return the html message */ public final String getHtmlMessage(final PreferencesBean preferences, final boolean includeWrapper, final boolean embedImage, final PersonBean person, final OrganisationBean organisation, final Collection<EmailFormatter> emailFormatters) { final StringBuffer html = new StringBuffer(); String message = this.getMessage(); if (person != null || organisation != null) { for (EmailFormatter formatter : emailFormatters) { message = StringUtils.replace(message, formatter.getKey(), formatter.getReplacement(person, organisation)); } } if (includeWrapper) { html.append("<html><head><title>"); html.append(this.getSubject()); html.append("</title>"); if (StringUtils.isNotBlank(preferences.getOption("email", "stylesheet"))) { final String css = StringUtils.replace(preferences.getOption("email", "stylesheet"), "\n", ""); html.append("<style type=\"text/css\">"); html.append(css.trim()); html.append("</style>"); } html.append("</head><body>"); } html.append("<div id=\"message\">"); html.append(message); html.append("</div>"); html.append(this.getFooter(preferences, embedImage)); if (includeWrapper) { html.append("</body></html>"); } return html.toString(); } /** * Gets the footer. * * @param preferences the preferences * @param embedImage the embed image * * @return the footer */ public final String getFooter(final PreferencesBean preferences, final boolean embedImage) { final StringBuffer ftr = new StringBuffer(); if (preferences != null) { final String contactMessage = getContactMessage(preferences); if (StringUtils.isNotBlank(contactMessage) && this.getAppendContact()) { // Append the organisation contact details ftr.append("<div class=\"email-footer-contact\">"); ftr.append(contactMessage); ftr.append("</div>"); } if (StringUtils.isNotBlank(preferences.getOption("email", "logo")) && this.getAppendLogo()) { // Append the organisation logo ftr.append("<div class=\"email-footer-logo\">"); ftr.append("<img src=\""); if (embedImage) { // Include an embedded mimemessage reference ftr.append("cid:whichdoctorLogo"); } else { // Just include a standard img http reference ftr.append(preferences.getOption("email", "logo")); } ftr.append("\" alt=\""); ftr.append(preferences.getOption("system", "companyabbrev")); ftr.append(" logo\" />"); ftr.append("</div>"); } if (StringUtils.isNotBlank(preferences.getOption("email", "privacy")) && this.getAppendPrivacy()) { // Append the organisation privacy statement ftr.append("<div class=\"email-footer-privacy \">"); ftr.append(preferences.getOption("email", "privacy")); ftr.append("</div>"); } } return ftr.toString(); } /** * Gets the contact message. * * @param preferences the preferences * * @return the contact message */ private String getContactMessage(final PreferencesBean preferences) { final StringBuffer address = new StringBuffer(); final StringBuffer contact = new StringBuffer(); if (preferences != null) { if (StringUtils.isNotBlank(preferences.getOption("system", "companyname"))) { address.append(preferences.getOption("system", "companyname")); } if (StringUtils.isNotBlank(preferences.getOption("contact", "physical"))) { if (address.length() > 0) { address.append("<br />"); } address.append(preferences.getOption("contact", "physical")); } if (StringUtils.isNotBlank(preferences.getOption("contact", "postal"))) { if (address.length() > 0) { address.append("<br />"); } address.append(preferences.getOption("contact", "postal")); } if (address.length() > 0) { address.insert(0, "<p>"); address.append("</p>"); } if (StringUtils.isNotBlank(preferences.getOption("contact", "telephone"))) { contact.append("<span style=\"padding-right: 30px;\">"); contact.append(preferences.getOption("contact", "telephone")); contact.append("</span>"); } if (StringUtils.isNotBlank(preferences.getOption("contact", "fax"))) { contact.append("<span style=\"padding-right: 30px;\">"); contact.append(preferences.getOption("contact", "fax")); contact.append("</span>"); } if (StringUtils.isNotBlank(preferences.getOption("contact", "web"))) { contact.append("<span>"); contact.append(preferences.getOption("contact", "web")); contact.append("</span>"); } if (contact.length() > 0) { contact.insert(0, "<p>"); contact.append("</p>"); } } return address.toString() + contact.toString(); } }