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, 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.notification;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.CmsUser;
033import org.opencms.file.types.CmsResourceTypeJsp;
034import org.opencms.file.types.CmsResourceTypePlain;
035import org.opencms.file.types.CmsResourceTypeXmlPage;
036import org.opencms.i18n.CmsLocaleManager;
037import org.opencms.i18n.CmsMessages;
038import org.opencms.loader.CmsLoaderException;
039import org.opencms.main.CmsException;
040import org.opencms.main.CmsLog;
041import org.opencms.main.OpenCms;
042import org.opencms.util.CmsDateUtil;
043import org.opencms.util.CmsRequestUtil;
044import org.opencms.util.CmsUUID;
045import org.opencms.workplace.CmsDialog;
046import org.opencms.workplace.CmsWorkplace;
047
048import java.util.ArrayList;
049import java.util.Calendar;
050import java.util.Collections;
051import java.util.GregorianCalendar;
052import java.util.HashMap;
053import java.util.Iterator;
054import java.util.List;
055import java.util.Map;
056import java.util.TimeZone;
057
058import org.apache.commons.logging.Log;
059
060/**
061 * The E-Mail to be written to responsibles of resources.<p>
062 */
063public class CmsContentNotification extends A_CmsNotification {
064
065    /** The path to the xml content with the subject, header and footer of the notification e-mail.<p> */
066    public static final String NOTIFICATION_CONTENT = "notification/notification";
067
068    /** The log object for this class. */
069    private static final Log LOG = CmsLog.getLog(CmsContentNotification.class);
070
071    /** The message bundle initialized with the locale of the reciever. */
072    private CmsMessages m_messages;
073
074    /**  The resources the responsible will be notified of, a list of CmsNotificationCauses. */
075    private List<CmsExtendedNotificationCause> m_notificationCauses;
076
077    /** The receiver of the notification. */
078    private CmsUser m_responsible;
079
080    /** Server name and opencms context. */
081    private String m_serverAndContext = OpenCms.getSiteManager().getWorkplaceServer()
082        + OpenCms.getSystemInfo().getOpenCmsContext();
083
084    /** Uri of the workplace folder. */
085    private String m_uriWorkplace = m_serverAndContext + CmsWorkplace.VFS_PATH_WORKPLACE;
086
087    /** Uri of the workplace jsp. */
088    private String m_uriWorkplaceJsp = m_serverAndContext + CmsWorkplace.JSP_WORKPLACE_URI;
089
090    /**
091     * Creates a new CmsContentNotification.<p>
092     *
093     * @param responsible the user that will be notified
094     * @param cms the cms object to use
095     */
096    CmsContentNotification(CmsUser responsible, CmsObject cms) {
097
098        super(cms, responsible);
099        m_responsible = responsible;
100    }
101
102    /**
103     * Returns true, if there exists an editor for a specific resource.<p>
104     *
105     * @param resource the resource to check if there exists an editor
106     *
107     * @return true if there exists an editor for the resource
108     */
109    public static boolean existsEditor(CmsResource resource) {
110
111        int plainId;
112        try {
113            plainId = OpenCms.getResourceManager().getResourceType(
114                CmsResourceTypePlain.getStaticTypeName()).getTypeId();
115        } catch (CmsLoaderException e) {
116            // this should really never happen
117            plainId = CmsResourceTypePlain.getStaticTypeId();
118        }
119        if ((CmsResourceTypeJsp.isJsp(resource))
120            || (resource.getTypeId() == plainId)
121            || CmsResourceTypeXmlPage.isXmlPage(resource)) {
122            return true;
123        }
124        return false;
125    }
126
127    /**
128     * Returns the responsible.<p>
129     *
130     * @return the responsible
131     */
132    public CmsUser getResponsible() {
133
134        return m_responsible;
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     */
142    @Override
143    protected String generateHtmlMsg() {
144
145        // set the messages
146        m_messages = Messages.get().getBundle(getLocale());
147
148        StringBuffer htmlMsg = new StringBuffer();
149        htmlMsg.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">");
150        htmlMsg.append("<tr><td colspan=\"5\"><br/>");
151
152        GregorianCalendar tomorrow = new GregorianCalendar(TimeZone.getDefault(), CmsLocaleManager.getDefaultLocale());
153        tomorrow.add(Calendar.DAY_OF_YEAR, 1);
154        List<CmsExtendedNotificationCause> outdatedResources = new ArrayList<CmsExtendedNotificationCause>();
155        List<CmsExtendedNotificationCause> resourcesNextDay = new ArrayList<CmsExtendedNotificationCause>();
156        List<CmsExtendedNotificationCause> resourcesNextWeek = new ArrayList<CmsExtendedNotificationCause>();
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<CmsExtendedNotificationCause> notificationCauses = m_notificationCauses.iterator();
161        while (notificationCauses.hasNext()) {
162            CmsExtendedNotificationCause notificationCause = 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(
177            htmlMsg,
178            outdatedResources,
179            m_messages.key(
180                Messages.GUI_FILES_NOT_UPDATED_1,
181                String.valueOf(OpenCms.getSystemInfo().getNotificationTime())));
182
183        htmlMsg.append("</td></tr></table>");
184        String result = htmlMsg.toString();
185        return result;
186    }
187
188    /**
189     * Returns a list of CmsNotificationResourceInfos of the resources that will occur in the notification.<p>
190     *
191     * @return a list of CmsNotificationResourceInfos of the resources that will occur in the notification
192     */
193    protected List<CmsExtendedNotificationCause> getNotificationCauses() {
194
195        return m_notificationCauses;
196    }
197
198    /**
199     * @see org.opencms.notification.A_CmsNotification#getNotificationContent()
200     */
201    @Override
202    protected String getNotificationContent() {
203
204        return OpenCms.getSystemInfo().getConfigFilePath(m_cms, NOTIFICATION_CONTENT);
205    }
206
207    /**
208     * Sets the resources.<p>
209     *
210     * @param resources a list of CmsNotificationResourceInfo's
211     */
212    protected void setNotificationCauses(List<CmsExtendedNotificationCause> resources) {
213
214        m_notificationCauses = resources;
215    }
216
217    /**
218     * Appends a link to confirm a resource, so that the responsible will not be notified any more.<p>
219     *
220     * @param buf the StringBuffer to append the html code to
221     * @param notificationCause the information for specific resource
222     */
223    private void appendConfirmLink(StringBuffer buf, CmsExtendedNotificationCause notificationCause) {
224
225        Map<String, String[]> params = new HashMap<String, String[]>();
226        buf.append("<td>");
227        try {
228            String resourcePath = notificationCause.getResource().getRootPath();
229            String siteRoot = OpenCms.getSiteManager().getSiteRoot(resourcePath);
230            resourcePath = resourcePath.substring(siteRoot.length());
231            buf.append("[<a href=\"");
232            StringBuffer wpStartUri = new StringBuffer(m_uriWorkplace);
233            wpStartUri.append("commons/confirm_content_notification.jsp?userId=");
234            wpStartUri.append(m_responsible.getId());
235            wpStartUri.append("&cause=");
236            wpStartUri.append(notificationCause.getCause());
237            wpStartUri.append("&resource=");
238            wpStartUri.append(resourcePath);
239            params.put(CmsWorkplace.PARAM_WP_START, new String[] {wpStartUri.toString()});
240            params.put(
241                CmsWorkplace.PARAM_WP_EXPLORER_RESOURCE,
242                new String[] {CmsResource.getParentFolder(resourcePath)});
243            params.put(CmsWorkplace.PARAM_WP_SITE, new String[] {siteRoot});
244            CmsUUID projectId = getCmsObject().readProject(OpenCms.getSystemInfo().getNotificationProject()).getUuid();
245            params.put(CmsWorkplace.PARAM_WP_PROJECT, new String[] {String.valueOf(projectId)});
246            buf.append(CmsRequestUtil.appendParameters(m_uriWorkplaceJsp, params, true));
247            buf.append("\">");
248            buf.append(m_messages.key(Messages.GUI_CONFIRM_0));
249            buf.append("</a>]");
250        } catch (CmsException e) {
251            if (LOG.isInfoEnabled()) {
252                LOG.info(e.getLocalizedMessage(), e);
253            }
254        }
255        buf.append("</td>");
256    }
257
258    /**
259     * Appends a link to edit the resource to a StringBuffer.<p>
260     *
261     * @param buf the StringBuffer to append the html code to.
262     * @param notificationCause the information for specific resource.
263     */
264    private void appendEditLink(StringBuffer buf, CmsExtendedNotificationCause notificationCause) {
265
266        buf.append("<td>");
267        if (existsEditor(notificationCause.getResource())) {
268            try {
269                String resourcePath = notificationCause.getResource().getRootPath();
270                String siteRoot = OpenCms.getSiteManager().getSiteRoot(resourcePath);
271                resourcePath = resourcePath.substring(siteRoot.length());
272                Map<String, String[]> params = new HashMap<String, String[]>();
273                CmsUUID projectId = getCmsObject().readProject(
274                    OpenCms.getSystemInfo().getNotificationProject()).getUuid();
275                params.put(CmsWorkplace.PARAM_WP_PROJECT, new String[] {String.valueOf(projectId)});
276                params.put(
277                    CmsWorkplace.PARAM_WP_EXPLORER_RESOURCE,
278                    new String[] {CmsResource.getParentFolder(resourcePath)});
279                params.put(CmsWorkplace.PARAM_WP_SITE, new String[] {siteRoot});
280                params.put(CmsDialog.PARAM_RESOURCE, new String[] {resourcePath});
281                buf.append("[<a href=\"");
282                buf.append(CmsRequestUtil.appendParameters(m_uriWorkplace + "editors/editor.jsp", params, false));
283                buf.append("\">");
284                buf.append(m_messages.key(Messages.GUI_EDIT_0));
285                buf.append("</a>]");
286            } catch (CmsException e) {
287                if (LOG.isInfoEnabled()) {
288                    LOG.info(e.getLocalizedMessage(), e);
289                }
290            }
291        }
292        buf.append("</td>");
293    }
294
295    /**
296     * Appends a link to edit the notification settings of a resource to a StringBuffer.<p>
297     *
298     * @param buf the StringBuffer to append the html code to.
299     * @param notificationCause the information for specific resource.
300     */
301    private void appendModifyLink(StringBuffer buf, CmsExtendedNotificationCause notificationCause) {
302
303        Map<String, String[]> params = new HashMap<String, String[]>();
304        buf.append("<td>");
305        try {
306            buf.append("[<a href=\"");
307            String resourcePath = notificationCause.getResource().getRootPath();
308            String siteRoot = OpenCms.getSiteManager().getSiteRoot(resourcePath);
309            resourcePath = resourcePath.substring(siteRoot.length());
310            StringBuffer wpStartUri = new StringBuffer(m_uriWorkplace);
311            wpStartUri.append("commons/availability.jsp?resource=");
312            wpStartUri.append(resourcePath);
313            params.put(
314                CmsWorkplace.PARAM_WP_EXPLORER_RESOURCE,
315                new String[] {CmsResource.getParentFolder(resourcePath)});
316            params.put(CmsWorkplace.PARAM_WP_START, new String[] {wpStartUri.toString()});
317            params.put(CmsWorkplace.PARAM_WP_SITE, new String[] {siteRoot});
318            CmsUUID projectId = getCmsObject().readProject(OpenCms.getSystemInfo().getNotificationProject()).getUuid();
319            params.put(CmsWorkplace.PARAM_WP_PROJECT, new String[] {String.valueOf(projectId)});
320            buf.append(CmsRequestUtil.appendParameters(m_uriWorkplaceJsp, params, true));
321            buf.append("\">");
322            buf.append(m_messages.key(Messages.GUI_MODIFY_0));
323            buf.append("</a>]");
324        } catch (CmsException e) {
325            if (LOG.isInfoEnabled()) {
326                LOG.info(e.getLocalizedMessage(), e);
327            }
328        }
329        buf.append("</td>");
330    }
331
332    /**
333     * Appends a table showing a set of resources, and the cause of the notification.<p>
334     *
335     * @param htmlMsg html the StringBuffer to append the html code to
336     * @param notificationCauseList the list of notification causes
337     * @param header the title of the resource list
338     */
339    private void appendResourceList(
340        StringBuffer htmlMsg,
341        List<CmsExtendedNotificationCause> notificationCauseList,
342        String header) {
343
344        if (!notificationCauseList.isEmpty()) {
345            htmlMsg.append("<tr><td colspan=\"5\"><br/><p style=\"margin-top:20px;margin-bottom:10px;\"><b>");
346            htmlMsg.append(header);
347            htmlMsg.append(
348                "</b></p></td></tr><tr class=\"trow1\"><td><div style=\"padding-top:2px;padding-bottom:2px;\">");
349            htmlMsg.append(m_messages.key(Messages.GUI_RESOURCE_0));
350            htmlMsg.append("</div></td><td><div style=\"padding-top:2px;padding-bottom:2px;padding-left:10px;\">");
351            htmlMsg.append(m_messages.key(Messages.GUI_SITE_0));
352            htmlMsg.append("</div></td><td><div style=\"padding-top:2px;padding-bottom:2px;padding-left:10px;\">");
353            htmlMsg.append(m_messages.key(Messages.GUI_ISSUE_0));
354            htmlMsg.append("</div></td><td colspan=\"2\"/></tr>");
355            Iterator<CmsExtendedNotificationCause> notificationCauses = notificationCauseList.iterator();
356            for (int i = 0; notificationCauses.hasNext(); i++) {
357                CmsExtendedNotificationCause notificationCause = notificationCauses.next();
358                htmlMsg.append(buildNotificationListItem(notificationCause, (i % 2) + 2));
359            }
360        }
361    }
362
363    /**
364     * Returns a string representation of this resource info.<p>
365     *
366     * @param notificationCause the notification cause
367     * @param row the row number
368     *
369     * @return a string representation of this resource info
370     */
371    private String buildNotificationListItem(CmsExtendedNotificationCause notificationCause, int row) {
372
373        StringBuffer result = new StringBuffer("<tr class=\"trow");
374        result.append(row);
375        result.append("\"><td width=\"100%\">");
376        String resourcePath = notificationCause.getResource().getRootPath();
377        String siteRoot = OpenCms.getSiteManager().getSiteRoot(resourcePath);
378        resourcePath = resourcePath.substring(siteRoot.length());
379        // append link, if page is available
380        if ((notificationCause.getResource().getDateReleased() < System.currentTimeMillis())
381            && (notificationCause.getResource().getDateExpired() > System.currentTimeMillis())) {
382
383            Map<String, String[]> params = new HashMap<String, String[]>();
384            params.put(CmsWorkplace.PARAM_WP_SITE, new String[] {siteRoot});
385            params.put(CmsDialog.PARAM_RESOURCE, new String[] {resourcePath});
386            result.append("<a href=\"");
387            result.append(
388                CmsRequestUtil.appendParameters(m_uriWorkplace + "commons/displayresource.jsp", params, false));
389            result.append("\">");
390            result.append(resourcePath);
391            result.append("</a>");
392        } else {
393            result.append(resourcePath);
394        }
395        result.append("</td><td><div style=\"white-space:nowrap;padding-left:10px;padding-right:10px;\">");
396        result.append(siteRoot);
397        result.append("</td><td><div style=\"white-space:nowrap;padding-left:10px;padding-right:10px;\">");
398        if (notificationCause.getCause() == CmsExtendedNotificationCause.RESOURCE_EXPIRES) {
399            result.append(m_messages.key(Messages.GUI_EXPIRES_AT_1, new Object[] {notificationCause.getDate()}));
400            result.append("</div></td>");
401            appendConfirmLink(result, notificationCause);
402            appendModifyLink(result, notificationCause);
403        } else if (notificationCause.getCause() == CmsExtendedNotificationCause.RESOURCE_RELEASE) {
404            result.append(m_messages.key(Messages.GUI_RELEASE_AT_1, new Object[] {notificationCause.getDate()}));
405            result.append("</div></td>");
406            appendConfirmLink(result, notificationCause);
407            appendModifyLink(result, notificationCause);
408        } else if (notificationCause.getCause() == CmsExtendedNotificationCause.RESOURCE_UPDATE_REQUIRED) {
409            result.append(m_messages.key(Messages.GUI_UPDATE_REQUIRED_1, new Object[] {notificationCause.getDate()}));
410            result.append("</div></td>");
411            appendConfirmLink(result, notificationCause);
412            appendEditLink(result, notificationCause);
413        } else {
414            result.append(
415                m_messages.key(
416                    Messages.GUI_UNCHANGED_SINCE_1,
417                    new Object[] {Integer.valueOf(CmsDateUtil.getDaysPassedSince(notificationCause.getDate()))}));
418            result.append("</div></td>");
419            appendConfirmLink(result, notificationCause);
420            appendEditLink(result, notificationCause);
421        }
422
423        result.append("</tr>");
424
425        return result.toString();
426    }
427}