KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > newsletter > CmsNewsletter


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/newsletter/CmsNewsletter.java,v $
3  * Date : $Date: 2006/07/20 13:06:52 $
4  * Version: $Revision: 1.3 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.newsletter;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.CmsPropertyDefinition;
36 import org.opencms.file.CmsResource;
37 import org.opencms.mail.CmsHtmlMail;
38 import org.opencms.mail.CmsSimpleMail;
39 import org.opencms.mail.CmsVfsDataSource;
40 import org.opencms.main.CmsException;
41 import org.opencms.util.CmsMacroResolver;
42 import org.opencms.util.CmsStringUtil;
43
44 import java.text.DateFormat JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.util.Date JavaDoc;
47 import java.util.Iterator JavaDoc;
48 import java.util.List JavaDoc;
49
50 import javax.mail.MessagingException JavaDoc;
51
52 import org.apache.commons.mail.Email;
53
54 /**
55  * Basic implementation of the interface {@link I_CmsNewsletter}.<p>
56  *
57  * @author Jan Baudisch
58  */

59 public class CmsNewsletter implements I_CmsNewsletter {
60
61     /** The attachments, a list of {@link org.opencms.file.CmsResource} objects.<p> */
62     private List JavaDoc m_attachments;
63
64     /** The contents, a list of {@link CmsNewsletterContent} objects.<p> */
65     private List JavaDoc m_contents;
66
67     /** The subject of the newsletter.<p> */
68     private String JavaDoc m_subject;
69
70     /** Creates a new newsletter instance.<p> */
71     public CmsNewsletter() {
72
73         m_contents = new ArrayList JavaDoc();
74         m_attachments = new ArrayList JavaDoc();
75     }
76
77     /**
78      *
79      * @see org.opencms.newsletter.I_CmsNewsletter#addAttachment(org.opencms.file.CmsObject, org.opencms.file.CmsResource)
80      */

81     public void addAttachment(CmsObject cms, CmsResource resource) {
82
83         m_attachments.add(resource);
84     }
85
86     /**
87      *
88      * @see org.opencms.newsletter.I_CmsNewsletter#addContent(org.opencms.newsletter.I_CmsNewsletterContent)
89      */

90     public void addContent(I_CmsNewsletterContent content) {
91
92         m_contents.add(content);
93     }
94
95     /**
96      * Returns the e-mail for the newsletter.<p>
97      *
98      * @param recipient the recipient to whom the newsletter is sent
99      * @param cms the CmsObject
100      *
101      * @return the e-mail for the newsletter
102      *
103      * @throws CmsException if something goes wrong
104      * @throws MessagingException if there is an error attaching resources
105      */

106     public Email getEmail(CmsObject cms, I_CmsNewsletterRecipient recipient) throws MessagingException JavaDoc, CmsException {
107
108         StringBuffer JavaDoc htmlMsg = new StringBuffer JavaDoc(1024);
109         StringBuffer JavaDoc txtMsg = new StringBuffer JavaDoc(1024);
110         Iterator JavaDoc contents = m_contents.iterator();
111         while (contents.hasNext()) {
112             I_CmsNewsletterContent content = (I_CmsNewsletterContent)contents.next();
113             if (recipient.isSubscriber(content)) {
114                 if (content.getType().equals(CmsNewsletterContentType.TYPE_HTML)) {
115                     htmlMsg.append(content.getContent());
116                 } else {
117                     txtMsg.append(content.getContent());
118                 }
119             }
120         }
121         Email email;
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 JavaDoc attachments = m_attachments.iterator();
127             while (attachments.hasNext()) {
128                 CmsResource resource = (CmsResource)attachments.next();
129                 // set the description of the attachment either to the property description, if it is set, or
130
// to the property title
131
String JavaDoc description = "";
132                 String JavaDoc propertyDescription = cms.readPropertyObject(
133                     cms.getSitePath(resource),
134                     CmsPropertyDefinition.PROPERTY_DESCRIPTION,
135                     true).getValue();
136                 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(propertyDescription)) {
137                     description = propertyDescription;
138                 } else {
139                     String JavaDoc propertyTitle = cms.readPropertyObject(
140                         cms.getSitePath(resource),
141                         CmsPropertyDefinition.PROPERTY_TITLE,
142                         true).getValue();
143                     description = propertyTitle;
144                 }
145                 htmlMail.attach(new CmsVfsDataSource(cms, resource), resource.getName(), description);
146             }
147             htmlMail.setTextMsg(replaceMacros(txtMsg.toString(), recipient));
148             email = htmlMail;
149         } else {
150             // only text content, return text mail
151
CmsSimpleMail textMail = new CmsSimpleMail();
152             textMail.setMsg(replaceMacros(txtMsg.toString(), recipient));
153             email = textMail;
154         }
155         email.addTo(recipient.getEmail());
156         email.setSubject(m_subject);
157         return email;
158     }
159
160     /**
161      *
162      * @see org.opencms.newsletter.I_CmsNewsletter#setSubject(java.lang.String)
163      */

164     public void setSubject(String JavaDoc subject) {
165
166         m_subject = subject;
167     }
168
169     /**
170      * Replaces the macros in the given message.<p>
171      *
172      * @param msg the message in which the macros are replaced
173      * @param recipient the recipient in the message
174      *
175      * @return the message with the macros replaced
176      */

177     private String JavaDoc replaceMacros(String JavaDoc msg, I_CmsNewsletterRecipient recipient) {
178
179         CmsMacroResolver resolver = CmsMacroResolver.newInstance();
180         resolver.addMacro(MACRO_USER_FIRSTNAME, recipient.getFirstname());
181         resolver.addMacro(MACRO_USER_LASTNAME, recipient.getLastname());
182         resolver.addMacro(MACRO_USER_FULLNAME, recipient.getFullName());
183         resolver.addMacro(MACRO_USER_EMAIL, recipient.getEmail());
184         resolver.addMacro(
185             MACRO_SEND_DATE,
186             DateFormat.getDateTimeInstance().format(new Date JavaDoc(System.currentTimeMillis())));
187         return resolver.resolveMacros(msg);
188     }
189 }
Popular Tags