001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.newsletter;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsPropertyDefinition;
032import org.opencms.file.CmsResource;
033import org.opencms.mail.CmsHtmlMail;
034import org.opencms.mail.CmsSimpleMail;
035import org.opencms.mail.CmsVfsDataSource;
036import org.opencms.mail.Messages;
037import org.opencms.main.CmsException;
038import org.opencms.main.CmsLog;
039import org.opencms.util.CmsMacroResolver;
040import org.opencms.util.CmsStringUtil;
041
042import java.text.DateFormat;
043import java.util.ArrayList;
044import java.util.Date;
045import java.util.Iterator;
046import java.util.List;
047
048import org.apache.commons.logging.Log;
049import org.apache.commons.mail.Email;
050import org.apache.commons.mail.EmailException;
051
052/**
053 * Basic implementation of the interface {@link I_CmsNewsletter}.
054 * <p>
055 */
056public class CmsNewsletter implements I_CmsNewsletter {
057
058    /** The log object for this class. */
059    private static final Log LOG = CmsLog.getLog(CmsNewsletter.class);
060
061    /** The attachments, a list of {@link org.opencms.file.CmsResource} objects. */
062    private List<CmsResource> m_attachments;
063
064    /** The contents, a list of {@link I_CmsNewsletterContent} objects. */
065    private List<I_CmsNewsletterContent> m_contents;
066
067    /** The subject of the newsletter. */
068    private String m_subject;
069
070    /**
071     * Creates a new newsletter instance.<p>
072     */
073    public CmsNewsletter() {
074
075        m_contents = new ArrayList<I_CmsNewsletterContent>();
076        m_attachments = new ArrayList<CmsResource>();
077    }
078
079    /**
080     * @see org.opencms.newsletter.I_CmsNewsletter#addAttachment(org.opencms.file.CmsObject, org.opencms.file.CmsResource)
081     */
082    public void addAttachment(CmsObject cms, CmsResource resource) {
083
084        m_attachments.add(resource);
085    }
086
087    /**
088     * @see org.opencms.newsletter.I_CmsNewsletter#addContent(org.opencms.newsletter.I_CmsNewsletterContent)
089     */
090    public void addContent(I_CmsNewsletterContent content) {
091
092        m_contents.add(content);
093    }
094
095    /**
096     * Returns the e-mail for the newsletter.<p>
097     *
098     * @param recipient the recipient to whom the newsletter is sent
099     * @param cms the CmsObject
100     *
101     * @return the e-mail for the newsletter
102     *
103     * @throws CmsException if something goes wrong
104     */
105    public Email getEmail(CmsObject cms, I_CmsNewsletterRecipient recipient) throws CmsException {
106
107        StringBuffer htmlMsg = new StringBuffer(1024);
108        StringBuffer txtMsg = new StringBuffer(1024);
109        Iterator<I_CmsNewsletterContent> contents = m_contents.iterator();
110        while (contents.hasNext()) {
111            I_CmsNewsletterContent content = contents.next();
112            if (recipient.isSubscriber(content)) {
113                if (content.getType().equals(CmsNewsletterContentType.TYPE_HTML)) {
114                    htmlMsg.append(content.getContent());
115                } else {
116                    txtMsg.append(content.getContent());
117                }
118            }
119        }
120        Email email = null;
121        try {
122            if ((htmlMsg.length() > 0) || !m_attachments.isEmpty()) {
123                // we need to create a HTML mail
124                CmsHtmlMail htmlMail = new CmsHtmlMail();
125                htmlMail.setHtmlMsg(replaceMacros(htmlMsg.toString(), recipient));
126                Iterator<CmsResource> attachments = m_attachments.iterator();
127                while (attachments.hasNext()) {
128                    CmsResource resource = attachments.next();
129                    // set the description of the attachment either to the
130                    // property description, if it is set, or
131                    // to the property title
132                    String description = "";
133                    String propertyDescription = cms.readPropertyObject(
134                        cms.getSitePath(resource),
135                        CmsPropertyDefinition.PROPERTY_DESCRIPTION,
136                        true).getValue();
137                    if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(propertyDescription)) {
138                        description = propertyDescription;
139                    } else {
140                        String propertyTitle = cms.readPropertyObject(
141                            cms.getSitePath(resource),
142                            CmsPropertyDefinition.PROPERTY_TITLE,
143                            true).getValue();
144                        description = propertyTitle;
145                    }
146                    htmlMail.attach(new CmsVfsDataSource(cms, resource), resource.getName(), description);
147                }
148                htmlMail.setTextMsg(replaceMacros(txtMsg.toString(), recipient));
149                email = htmlMail;
150            } else {
151                // only text content, return text mail
152                CmsSimpleMail textMail = new CmsSimpleMail();
153                textMail.setMsg(replaceMacros(txtMsg.toString(), recipient));
154                email = textMail;
155            }
156            email.addTo(recipient.getEmail());
157            email.setSubject(m_subject);
158        } catch (EmailException e) {
159            LOG.error(Messages.get().getBundle().key(Messages.LOG_COMPOSE_MAIL_ERR_0), e);
160        }
161        return email;
162    }
163
164    /**
165     * @see org.opencms.newsletter.I_CmsNewsletter#setSubject(java.lang.String)
166     */
167    public void setSubject(String subject) {
168
169        m_subject = subject;
170    }
171
172    /**
173     * Replaces the macros in the given message.<p>
174     *
175     * @param msg the message in which the macros are replaced
176     * @param recipient the recipient in the message
177     *
178     * @return the message with the macros replaced
179     */
180    private String replaceMacros(String msg, I_CmsNewsletterRecipient recipient) {
181
182        CmsMacroResolver resolver = CmsMacroResolver.newInstance();
183        resolver.addMacro(MACRO_USER_FIRSTNAME, recipient.getFirstname());
184        resolver.addMacro(MACRO_USER_LASTNAME, recipient.getLastname());
185        resolver.addMacro(MACRO_USER_FULLNAME, recipient.getFullName());
186        resolver.addMacro(MACRO_USER_EMAIL, recipient.getEmail());
187        resolver.addMacro(
188            MACRO_SEND_DATE,
189            DateFormat.getDateTimeInstance().format(new Date(System.currentTimeMillis())));
190        return resolver.resolveMacros(msg);
191    }
192}