KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/notification/CmsContentNotification.java,v $
3  * Date : $Date: 2006/10/04 07:35:21 $
4  * Version: $Revision: 1.3 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2002 - 2004 Alkacon Software (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, 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.file.CmsObject;
35 import org.opencms.file.CmsResource;
36 import org.opencms.file.CmsUser;
37 import org.opencms.file.types.CmsResourceTypeJsp;
38 import org.opencms.file.types.CmsResourceTypePlain;
39 import org.opencms.file.types.CmsResourceTypeXmlPage;
40 import org.opencms.i18n.CmsLocaleManager;
41 import org.opencms.i18n.CmsMessages;
42 import org.opencms.main.CmsException;
43 import org.opencms.main.CmsLog;
44 import org.opencms.main.OpenCms;
45 import org.opencms.site.CmsSiteManager;
46 import org.opencms.util.CmsDateUtil;
47 import org.opencms.util.CmsRequestUtil;
48 import org.opencms.workplace.CmsDialog;
49 import org.opencms.workplace.CmsFrameset;
50 import org.opencms.workplace.CmsWorkplace;
51
52 import java.util.ArrayList JavaDoc;
53 import java.util.Calendar JavaDoc;
54 import java.util.Collections JavaDoc;
55 import java.util.GregorianCalendar JavaDoc;
56 import java.util.HashMap JavaDoc;
57 import java.util.Iterator JavaDoc;
58 import java.util.List JavaDoc;
59 import java.util.Map JavaDoc;
60 import java.util.TimeZone JavaDoc;
61
62 import org.apache.commons.logging.Log;
63
64 /**
65  * The E-Mail to be written to responsibles of resources.<p>
66  *
67  * @author Jan Baudisch
68  * @author Peter Bonrad
69  */

70 public class CmsContentNotification extends A_CmsNotification {
71
72     /** The path to the xml content with the subject, header and footer of the notification e-mail.<p> */
73     public static final String JavaDoc NOTIFICATION_CONTENT = "/system/workplace/admin/notification/notification";
74
75     /** The log object for this class. */
76     private static final Log LOG = CmsLog.getLog(CmsContentNotification.class);
77
78     /** The message bundle initialized with the locale of the reciever. */
79     private CmsMessages m_messages;
80
81     /** The resources the responsible will be notified of, a list of CmsNotificationCauses. */
82     private List JavaDoc m_notificationCauses;
83
84     /** The receiver of the notification. */
85     private CmsUser m_responsible;
86
87     /** Server name and opencms context. */
88     private String JavaDoc m_serverAndContext = OpenCms.getSiteManager().getWorkplaceServer()
89         + OpenCms.getSystemInfo().getOpenCmsContext();
90
91     /** Uri of the workplace folder. */
92     private String JavaDoc m_uriWorkplace = m_serverAndContext + CmsWorkplace.VFS_PATH_WORKPLACE;
93
94     /** Uri of the workplace jsp. */
95     private String JavaDoc m_uriWorkplaceJsp = m_serverAndContext + CmsFrameset.JSP_WORKPLACE_URI;
96
97     /**
98      * Creates a new CmsContentNotification.<p>
99      *
100      * @param responsible the user that will be notified
101      * @param cms the cms object to use
102      */

103     CmsContentNotification(CmsUser responsible, CmsObject cms) {
104
105         super(cms, responsible);
106         m_responsible = responsible;
107     }
108
109     /**
110      * Returns true, if there exists an editor for a specific resource.<p>
111      *
112      * @param resource the resource to check if there exists an editor
113      *
114      * @return true if there exists an editor for the resource
115      */

116     public static boolean existsEditor(CmsResource resource) {
117
118         if ((resource.getTypeId() == CmsResourceTypeJsp.getStaticTypeId())
119             || (resource.getTypeId() == CmsResourceTypePlain.getStaticTypeId())
120             || (resource.getTypeId() == CmsResourceTypeXmlPage.getStaticTypeId())) {
121             return true;
122         }
123         return false;
124     }
125
126     /**
127      * Returns the responsible.<p>
128      *
129      * @return the responsible
130      */

131     public CmsUser getResponsible() {
132
133         return m_responsible;
134     }
135
136
137     /**
138      * Creates the mail to be sent to the responsible user.<p>
139      *
140      * @return the mail to be sent to the responsible user
141      * @throws CmsException if something goes wrong
142      */

143     protected String JavaDoc generateHtmlMsg() {
144
145         // set the messages
146
m_messages = Messages.get().getBundle(getLocale());
147
148         StringBuffer JavaDoc htmlMsg = new StringBuffer JavaDoc();
149         htmlMsg.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">");
150         htmlMsg.append("<tr><td colspan=\"5\"><br/>");
151
152         GregorianCalendar JavaDoc tomorrow = new GregorianCalendar JavaDoc(TimeZone.getDefault(), CmsLocaleManager.getDefaultLocale());
153         tomorrow.add(Calendar.DAY_OF_YEAR, 1);
154         List JavaDoc outdatedResources = new ArrayList JavaDoc();
155         List JavaDoc resourcesNextDay = new ArrayList JavaDoc();
156         List JavaDoc resourcesNextWeek = new ArrayList JavaDoc();
157
158         // split all resources into three lists: the resources that expire, will be released or get outdated
159
// within the next 24h, within the next week and the resources unchanged since a long time
160
Iterator JavaDoc notificationCauses = m_notificationCauses.iterator();
161         while (notificationCauses.hasNext()) {
162             CmsExtendedNotificationCause notificationCause = (CmsExtendedNotificationCause)notificationCauses.next();
163             if (notificationCause.getCause() == CmsExtendedNotificationCause.RESOURCE_OUTDATED) {
164                 outdatedResources.add(notificationCause);
165             } else if (notificationCause.getDate().before(tomorrow.getTime())) {
166                 resourcesNextDay.add(notificationCause);
167             } else {
168                 resourcesNextWeek.add(notificationCause);
169             }
170         }
171         Collections.sort(resourcesNextDay);
172         Collections.sort(resourcesNextWeek);
173         Collections.sort(outdatedResources);
174         appendResourceList(htmlMsg, resourcesNextDay, m_messages.key(Messages.GUI_WITHIN_NEXT_DAY_0));
175         appendResourceList(htmlMsg, resourcesNextWeek, m_messages.key(Messages.GUI_WITHIN_NEXT_WEEK_0));
176         appendResourceList(htmlMsg, outdatedResources, m_messages.key(
177             Messages.GUI_FILES_NOT_UPDATED_1,
178             String.valueOf(OpenCms.getSystemInfo().getNotificationTime())));
179
180         htmlMsg.append("</td></tr></table>");
181         String JavaDoc result = htmlMsg.toString();
182         return result;
183     }
184
185     /**
186      * Returns a list of CmsNotificationResourceInfos of the resources that will occur in the notification.<p>
187      *
188      * @return a list of CmsNotificationResourceInfos of the resources that will occur in the notification
189      */

190     protected List JavaDoc getNotificationCauses() {
191
192         return m_notificationCauses;
193     }
194
195     /**
196      * @see org.opencms.notification.A_CmsNotification#getNotificationContent()
197      */

198     protected String JavaDoc getNotificationContent() {
199
200         return NOTIFICATION_CONTENT;
201     }
202
203     /**
204      * Sets the resources.<p>
205      *
206      * @param resources a list of CmsNotificationResourceInfo's
207      */

208     protected void setNotificationCauses(List JavaDoc resources) {
209
210         m_notificationCauses = resources;
211     }
212
213     /**
214      * Appends a link to confirm a resource, so that the responsible will not be notified any more.<p>
215      *
216      * @param html the StringBuffer to append the html code to
217      * @param notificationCause the information for specific resource
218      */

219     private void appendConfirmLink(StringBuffer JavaDoc html, CmsExtendedNotificationCause notificationCause) {
220
221         Map JavaDoc params = new HashMap JavaDoc();
222         html.append("<td>");
223         try {
224             String JavaDoc resourcePath = notificationCause.getResource().getRootPath();
225             String JavaDoc siteRoot = CmsSiteManager.getSiteRoot(resourcePath);
226             resourcePath = resourcePath.substring(siteRoot.length());
227             html.append("[<a HREF=\"");
228             StringBuffer JavaDoc wpStartUri = new StringBuffer JavaDoc(m_uriWorkplace);
229             wpStartUri.append("commons/confirm_content_notification.jsp?userId=");
230             wpStartUri.append(m_responsible.getId());
231             wpStartUri.append("&cause=");
232             wpStartUri.append(notificationCause.getCause());
233             wpStartUri.append("&resource=");
234             wpStartUri.append(resourcePath);
235             params.put(CmsFrameset.PARAM_WP_START, wpStartUri.toString());
236             params.put(CmsWorkplace.PARAM_WP_EXPLORER_RESOURCE, CmsResource.getParentFolder(resourcePath));
237             params.put(CmsWorkplace.PARAM_WP_SITE, siteRoot);
238             int projectId = getCmsObject().readProject(OpenCms.getSystemInfo().getNotificationProject()).getId();
239             params.put(CmsWorkplace.PARAM_WP_PROJECT, String.valueOf(projectId));
240             html.append(CmsRequestUtil.appendParameters(m_uriWorkplaceJsp, params, true));
241             html.append("\">");
242             html.append(m_messages.key(Messages.GUI_CONFIRM_0));
243             html.append("</a>]");
244         } catch (CmsException e) {
245             if (LOG.isInfoEnabled()) {
246                 LOG.info(e);
247             }
248         }
249         html.append("</td>");
250     }
251
252     /**
253      * Appends a link to edit the resource to a StringBuffer.<p>
254      *
255      * @param html the StringBuffer to append the html code to.
256      * @param notificationCause the information for specific resource.
257      */

258     private void appendEditLink(StringBuffer JavaDoc html, CmsExtendedNotificationCause notificationCause) {
259
260         html.append("<td>");
261         if (existsEditor(notificationCause.getResource())) {
262             try {
263                 String JavaDoc resourcePath = notificationCause.getResource().getRootPath();
264                 String JavaDoc siteRoot = CmsSiteManager.getSiteRoot(resourcePath);
265                 resourcePath = resourcePath.substring(siteRoot.length());
266                 Map JavaDoc params = new HashMap JavaDoc();
267                 int projectId = getCmsObject().readProject(OpenCms.getSystemInfo().getNotificationProject()).getId();
268                 params.put(CmsWorkplace.PARAM_WP_PROJECT, String.valueOf(projectId));
269                 params.put(CmsWorkplace.PARAM_WP_EXPLORER_RESOURCE, CmsResource.getParentFolder(resourcePath));
270                 params.put(CmsWorkplace.PARAM_WP_SITE, siteRoot);
271                 params.put(CmsDialog.PARAM_RESOURCE, resourcePath);
272                 html.append("[<a HREF=\"");
273                 html.append(CmsRequestUtil.appendParameters(m_uriWorkplace + "editors/editor.jsp", params, false));
274                 html.append("\">");
275                 html.append(m_messages.key(Messages.GUI_EDIT_0));
276                 html.append("</a>]");
277             } catch (CmsException e) {
278                 if (LOG.isInfoEnabled()) {
279                     LOG.info(e);
280                 }
281             }
282         }
283         html.append("</td>");
284     }
285
286     /**
287      * Appends a link to edit the notification settings of a resource to a StringBuffer.<p>
288      *
289      * @param html the StringBuffer to append the html code to.
290      * @param notificationCause the information for specific resource.
291      */

292     private void appendModifyLink(StringBuffer JavaDoc html, CmsExtendedNotificationCause notificationCause) {
293
294         Map JavaDoc params = new HashMap JavaDoc();
295         html.append("<td>");
296         try {
297             html.append("[<a HREF=\"");
298             String JavaDoc resourcePath = notificationCause.getResource().getRootPath();
299             String JavaDoc siteRoot = CmsSiteManager.getSiteRoot(resourcePath);
300             resourcePath = resourcePath.substring(siteRoot.length());
301             StringBuffer JavaDoc wpStartUri = new StringBuffer JavaDoc(m_uriWorkplace);
302             wpStartUri.append("commons/availability.jsp?resource=");
303             wpStartUri.append(resourcePath);
304             params.put(CmsWorkplace.PARAM_WP_EXPLORER_RESOURCE, CmsResource.getParentFolder(resourcePath));
305             params.put(CmsFrameset.PARAM_WP_START, wpStartUri.toString());
306             params.put(CmsWorkplace.PARAM_WP_SITE, siteRoot);
307             int projectId = getCmsObject().readProject(OpenCms.getSystemInfo().getNotificationProject()).getId();
308             params.put(CmsWorkplace.PARAM_WP_PROJECT, String.valueOf(projectId));
309             html.append(CmsRequestUtil.appendParameters(m_uriWorkplaceJsp, params, true));
310             html.append("\">");
311             html.append(m_messages.key(Messages.GUI_MODIFY_0));
312             html.append("</a>]");
313         } catch (CmsException e) {
314             if (LOG.isInfoEnabled()) {
315                 LOG.info(e);
316             }
317         }
318         html.append("</td>");
319     }
320
321     /**
322      * Appends a table showing a set of resources, and the cause of the notification.<p>
323      *
324      * @param htmlMsg html the StringBuffer to append the html code to
325      * @param notificationCauseList the list of notification causes
326      * @param header the title of the resource list
327      */

328     private void appendResourceList(StringBuffer JavaDoc htmlMsg, List JavaDoc notificationCauseList, String JavaDoc header) {
329
330         if (!notificationCauseList.isEmpty()) {
331             htmlMsg.append("<tr><td colspan=\"5\"><br/><p style=\"margin-top:20px;margin-bottom:10px;\"><b>");
332             htmlMsg.append(header);
333             htmlMsg.append("</b></p></td></tr><tr class=\"trow1\"><td><div style=\"padding-top:2px;padding-bottom:2px;\">");
334             htmlMsg.append(m_messages.key(Messages.GUI_RESOURCE_0));
335             htmlMsg.append("</div></td><td><div style=\"padding-top:2px;padding-bottom:2px;padding-left:10px;\">");
336             htmlMsg.append(m_messages.key(Messages.GUI_SITE_0));
337             htmlMsg.append("</div></td><td><div style=\"padding-top:2px;padding-bottom:2px;padding-left:10px;\">");
338             htmlMsg.append(m_messages.key(Messages.GUI_ISSUE_0));
339             htmlMsg.append("</div></td><td colspan=\"2\"/></tr>");
340             Iterator JavaDoc notificationCauses = notificationCauseList.iterator();
341             for (int i = 0; notificationCauses.hasNext(); i++) {
342                 CmsExtendedNotificationCause notificationCause = (CmsExtendedNotificationCause)notificationCauses.next();
343                 htmlMsg.append(buildNotificationListItem(notificationCause, (i % 2) + 2));
344             }
345         }
346     }
347
348     /**
349      * Returns a string representation of this resource info.<p>
350      *
351      * @return a string representation of this resource info
352      */

353     private String JavaDoc buildNotificationListItem(CmsExtendedNotificationCause notificationCause, int row) {
354
355         StringBuffer JavaDoc result = new StringBuffer JavaDoc("<tr class=\"trow");
356         result.append(row);
357         result.append("\"><td width=\"100%\">");
358         String JavaDoc resourcePath = notificationCause.getResource().getRootPath();
359         String JavaDoc siteRoot = CmsSiteManager.getSiteRoot(resourcePath);
360         resourcePath = resourcePath.substring(siteRoot.length());
361         // append link, if page is available
362
if (notificationCause.getResource().getDateReleased() < System.currentTimeMillis()
363             && notificationCause.getResource().getDateExpired() > System.currentTimeMillis()) {
364
365             Map JavaDoc params = new HashMap JavaDoc();
366             params.put(CmsWorkplace.PARAM_WP_SITE, siteRoot);
367             params.put(CmsDialog.PARAM_RESOURCE, resourcePath);
368             result.append("<a HREF=\"");
369             result.append(CmsRequestUtil.appendParameters(m_uriWorkplace + "commons/displayresource.jsp", params, false));
370             result.append("\">");
371             result.append(resourcePath);
372             result.append("</a>");
373         } else {
374             result.append(resourcePath);
375         }
376         result.append("</td><td><div style=\"white-space:nowrap;padding-left:10px;padding-right:10px;\">");
377         result.append(siteRoot);
378         result.append("</td><td><div style=\"white-space:nowrap;padding-left:10px;padding-right:10px;\">");
379         if (notificationCause.getCause() == CmsExtendedNotificationCause.RESOURCE_EXPIRES) {
380             result.append(m_messages.key(Messages.GUI_EXPIRES_AT_1, new Object JavaDoc[] {notificationCause.getDate()}));
381             result.append("</div></td>");
382             appendConfirmLink(result, notificationCause);
383             appendModifyLink(result, notificationCause);
384         } else if (notificationCause.getCause() == CmsExtendedNotificationCause.RESOURCE_RELEASE) {
385             result.append(m_messages.key(Messages.GUI_RELEASE_AT_1, new Object JavaDoc[] {notificationCause.getDate()}));
386             result.append("</div></td>");
387             appendConfirmLink(result, notificationCause);
388             appendModifyLink(result, notificationCause);
389         } else if (notificationCause.getCause() == CmsExtendedNotificationCause.RESOURCE_UPDATE_REQUIRED) {
390             result.append(m_messages.key(Messages.GUI_UPDATE_REQUIRED_1, new Object JavaDoc[] {notificationCause.getDate()}));
391             result.append("</div></td>");
392             appendConfirmLink(result, notificationCause);
393             appendEditLink(result, notificationCause);
394         } else {
395             result.append(m_messages.key(Messages.GUI_UNCHANGED_SINCE_1, new Object JavaDoc[] {new Integer JavaDoc(
396                 CmsDateUtil.getDaysPassedSince(notificationCause.getDate()))}));
397             result.append("</div></td>");
398             appendConfirmLink(result, notificationCause);
399             appendEditLink(result, notificationCause);
400         }
401
402         result.append("</tr>");
403
404         return result.toString();
405     }
406 }
Popular Tags