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.notification;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsUser;
032import org.opencms.i18n.CmsMessages;
033import org.opencms.main.OpenCms;
034import org.opencms.report.I_CmsReport;
035
036import java.util.Iterator;
037import java.util.List;
038
039/**
040 * Class to send a notification to an OpenCms user with a summary of warnings and
041 * errors occurred while publishing the project.<p>
042 *
043 * @since 6.5.3
044 */
045public class CmsPublishNotification extends A_CmsNotification {
046
047    /** The path to the xml content with the subject, header and footer of the notification e-mail.<p> */
048    public static final String NOTIFICATION_CONTENT = "notification/publish-notification";
049
050    /** The report containing the errors and warnings to put into the notification. */
051    private I_CmsReport m_report;
052
053    /**
054     * Creates a new CmsPublishNotification.<p>
055     *
056     * @param cms the cms object to use
057     * @param receiver the notification receiver
058     * @param report the report to write the output to
059     */
060    public CmsPublishNotification(CmsObject cms, CmsUser receiver, I_CmsReport report) {
061
062        super(cms, receiver);
063        m_report = report;
064    }
065
066    /**
067     * @see org.opencms.notification.A_CmsNotification#generateHtmlMsg()
068     */
069    @Override
070    protected String generateHtmlMsg() {
071
072        StringBuffer buffer = new StringBuffer();
073
074        CmsMessages messages = Messages.get().getBundle(getLocale());
075
076        // add warnings to the notification
077        if (m_report.hasWarning()) {
078            buffer.append("<b>");
079            buffer.append(messages.key(Messages.GUI_PUBLISH_WARNING_HEADER_0));
080            buffer.append("</b><br/>\n");
081            appendList(buffer, m_report.getWarnings());
082            buffer.append("<br/>\n");
083        }
084
085        // add errors to the notification
086        if (m_report.hasError()) {
087            buffer.append("<b>");
088            buffer.append(messages.key(Messages.GUI_PUBLISH_ERROR_HEADER_0));
089            buffer.append("</b><br/>\n");
090            appendList(buffer, m_report.getErrors());
091            buffer.append("<br/>\n");
092        }
093
094        return buffer.toString();
095    }
096
097    /**
098     * @see org.opencms.notification.A_CmsNotification#getNotificationContent()
099     */
100    @Override
101    protected String getNotificationContent() {
102
103        return OpenCms.getSystemInfo().getConfigFilePath(m_cms, NOTIFICATION_CONTENT);
104    }
105
106    /**
107     * Appends the contents of a list to the buffer with every entry in a new line.<p>
108     *
109     * @param buffer The buffer were the entries of the list will be appended.
110     * @param list The list with the entries to append to the buffer.
111     */
112    private void appendList(StringBuffer buffer, List<Object> list) {
113
114        Iterator<Object> iter = list.iterator();
115        while (iter.hasNext()) {
116            Object entry = iter.next();
117            buffer.append(entry).append("<br/>\n");
118        }
119    }
120
121}