KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > notification > A_CmsNotification


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/notification/A_CmsNotification.java,v $
3  * Date : $Date: 2006/11/29 16:31:26 $
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.notification;
33
34 import org.opencms.db.CmsUserSettings;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsUser;
37 import org.opencms.mail.CmsHtmlMail;
38 import org.opencms.main.CmsException;
39 import org.opencms.main.CmsLog;
40 import org.opencms.main.OpenCms;
41 import org.opencms.util.CmsMacroResolver;
42 import org.opencms.util.CmsStringUtil;
43 import org.opencms.xml.content.CmsXmlContent;
44 import org.opencms.xml.content.CmsXmlContentFactory;
45
46 import java.util.List JavaDoc;
47 import java.util.Locale JavaDoc;
48
49 import javax.mail.MessagingException JavaDoc;
50
51 import org.apache.commons.logging.Log;
52
53 /**
54  * Abstract class to create a notfication which will be send as a html mail to
55  * a user in OpenCms.
56  *
57  * @author Peter Bonrad
58  *
59  * @version $Revision: 1.3 $
60  *
61  * @since 6.5.3
62  */

63 public abstract class A_CmsNotification extends CmsHtmlMail {
64
65     /** The log object for this class. */
66     private static final Log LOG = CmsLog.getLog(A_CmsNotification.class);
67
68     /** The CmsObject. */
69     private CmsObject m_cms;
70
71     /** The locale of the reciever of the content notification. */
72     private Locale JavaDoc m_locale;
73
74     /** The macro resolver used. */
75     private CmsMacroResolver m_macroResolver;
76
77     /** The xml-content to read subject, header and footer of the notification. */
78     private CmsXmlContent m_mailContent;
79
80     /** The receiver of the notification. */
81     private CmsUser m_receiver;
82
83     /**
84      * Creates a new A_CmsNotification.<p>
85      *
86      * @param cms the cms object to use
87      * @param receiver the cms user who should receive the message
88      */

89     public A_CmsNotification(CmsObject cms, CmsUser receiver) {
90
91         m_cms = cms;
92         m_receiver = receiver;
93
94         m_macroResolver = new CmsMacroResolver();
95     }
96
97     /**
98      * Adds a new macro to the used macro resolver. Macros are used for the xml
99      * content file.
100      *
101      * @param key The key of the macro.
102      * @param value The value of the macro.
103      */

104     public void addMacro(String JavaDoc key, String JavaDoc value) {
105
106         m_macroResolver.addMacro(key, value);
107     }
108
109     /**
110      * Returns the CmsObject.<p>
111      *
112      * @return the CmsObject
113      */

114     public CmsObject getCmsObject() {
115
116         return m_cms;
117     }
118
119     /**
120      * Returns the locale.<p>
121      *
122      * @return the locale
123      */

124     public Locale JavaDoc getLocale() {
125
126         return m_locale;
127     }
128
129     /**
130      * Returns the receiver.<p>
131      *
132      * @return the receiver
133      */

134     public CmsUser getReceiver() {
135
136         return m_receiver;
137     }
138
139     /**
140      * @see org.opencms.mail.CmsSimpleMail#send()
141      */

142     public void send() throws MessagingException JavaDoc {
143
144         try {
145             // check if user is valid and has a mail address specified
146
if (CmsStringUtil.isEmpty(m_receiver.getEmail())) {
147                 LOG.error(Messages.get().getBundle().key(Messages.LOG_NOTIFICATION_NO_ADDRESS_1, m_receiver.getName()));
148                 return;
149             }
150
151             if (LOG.isInfoEnabled()) {
152                 LOG.info(Messages.get().getBundle().key(Messages.LOG_NOTIFICATION_SEND_1, m_receiver.getEmail()));
153             }
154
155             // read resource with subject, header and footer
156
m_mailContent = CmsXmlContentFactory.unmarshal(m_cms, m_cms.readFile(getNotificationContent()));
157
158             // detect locale
159
List JavaDoc locales = m_mailContent.getLocales();
160             Locale JavaDoc userLocale = new CmsUserSettings(m_receiver).getLocale();
161             if (locales.contains(userLocale)) {
162                 // mail is localized in the user locale, use that
163
m_locale = userLocale;
164             } else if (locales.contains(OpenCms.getWorkplaceManager().getDefaultLocale())) {
165                 // mail is localized in the system default locale, use that
166
m_locale = OpenCms.getWorkplaceManager().getDefaultLocale();
167             } else {
168                 // use any localization
169
m_locale = (Locale JavaDoc)locales.get(0);
170             }
171
172             // define macro resolver
173
m_macroResolver.addMacro("firstname", m_receiver.getFirstname());
174             m_macroResolver.addMacro("lastname", m_receiver.getLastname());
175             m_macroResolver.addMacro("project", m_cms.getRequestContext().currentProject().getName());
176
177             StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
178
179             // append html header
180
appendHtmlHeader(msg);
181
182             // append header from xmlcontent
183
msg.append(CmsMacroResolver.resolveMacros(
184                 m_mailContent.getStringValue(m_cms, "Header", m_locale),
185                 m_macroResolver));
186
187             // append body
188
msg.append("\n<br/><br/>\n");
189             msg.append(generateHtmlMsg());
190             msg.append("\n<br/><br/>\n");
191
192             // append footer from xmlcontent
193
msg.append(CmsMacroResolver.resolveMacros(
194                 m_mailContent.getStringValue(m_cms, "Footer", m_locale),
195                 m_macroResolver));
196
197             // append html footer
198
appenHtmlFooter(msg);
199
200             addTo(m_receiver.getEmail(), m_receiver.getFirstname() + ' ' + m_receiver.getLastname());
201             setSubject(CmsMacroResolver.resolveMacros(
202                 m_mailContent.getStringValue(m_cms, "Subject", m_locale),
203                 m_macroResolver));
204             setHtmlMsg(msg.toString());
205
206             // send mail
207
super.send();
208         } catch (CmsException e) {
209             LOG.error(Messages.get().getBundle().key(Messages.LOG_NOTIFICATION_SEND_ERROR_0), e);
210         }
211     }
212
213     /**
214      * Append the html-code to start a html mail message to the given buffer.<p>
215      *
216      * @param buffer The StringBuffer to add the html code to.
217      */

218     protected void appendHtmlHeader(StringBuffer JavaDoc buffer) {
219
220         buffer.append("<html><head><style type=\"text/css\">\n");
221         buffer.append("<!--\n");
222         buffer.append("body { font-family: Verdana, Arial, Helvetica, sans-serif; background-color:#ffefdb; }\n");
223         buffer.append("a { color:#b22222; text-decoration:none; }\n");
224         buffer.append("table { white-space: nowrap; font-size: x-small; }\n");
225         buffer.append("tr.trow1 { background-color: #cdc0b0; }\n");
226         buffer.append("tr.trow2 { background-color: #eedfcc; }\n");
227         buffer.append("tr.trow3 { background-color: #ffefdb; }\n");
228         buffer.append("--></style>\n");
229         buffer.append("</head><body><span style='font-size:8.0pt;'>");
230     }
231
232     /**
233      * Append the html-code to finish a html mail message to the given buffer.
234      *
235      * @param buffer The StringBuffer to add the html code to.
236      */

237     protected void appenHtmlFooter(StringBuffer JavaDoc buffer) {
238
239         buffer.append("</span></body></html>");
240     }
241
242     /**
243      * Overwrite the method to generate the message body of the notification. This
244      * text is placed between the header and the footer of the defined xmlcontent
245      * and the required html code is added.<p>
246      *
247      * @return The text to be inserted in the notification.
248      */

249     protected abstract String JavaDoc generateHtmlMsg();
250
251     /**
252      * Overwrite the method to return the path to the xmlcontent, where the subject,
253      * the header and the footer are defined.<p>
254      *
255      * @return The path to the xmlcontent file.
256      */

257     protected abstract String JavaDoc getNotificationContent();
258 }
259
Popular Tags