001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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.workflow;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsProject;
032import org.opencms.file.CmsProperty;
033import org.opencms.file.CmsPropertyDefinition;
034import org.opencms.file.CmsResource;
035import org.opencms.file.CmsUser;
036import org.opencms.main.CmsException;
037import org.opencms.notification.A_CmsNotification;
038import org.opencms.util.CmsStringUtil;
039
040import java.util.List;
041
042import org.apache.commons.mail.EmailException;
043
044/**
045 * Notification class for the workflow 'release' action.<p>
046 */
047public class CmsWorkflowNotification extends A_CmsNotification {
048
049    /** The admin CMS context. */
050    private CmsObject m_adminCms;
051
052    /** The publish link. */
053    private String m_link;
054
055    /** The path of the notification XML content. */
056    private String m_notificationContent;
057
058    /** The workflow project. */
059    private CmsProject m_project;
060
061    /** The released resources. */
062    private List<CmsResource> m_resources;
063
064    /** The user's cms context. */
065    @SuppressWarnings("unused")
066    private CmsObject m_userCms;
067
068    /**
069     * Creates a new workflow notification mail object.<p>
070     *
071     * @param adminCms the admin CMS context
072     * @param userCms the user CMS context
073     * @param receiver the mail recipient
074     * @param notificationContent the file from which to read the notification configuration
075     * @param project the workflow project
076     * @param resources the workflow resources
077     * @param link the link used for publishing the resources
078     *
079     * @throws EmailException if an email error occurs
080     */
081    public CmsWorkflowNotification(
082        CmsObject adminCms,
083        CmsObject userCms,
084        CmsUser receiver,
085        String notificationContent,
086        CmsProject project,
087        List<CmsResource> resources,
088        String link)
089        throws EmailException {
090
091        super(userCms, receiver);
092        m_notificationContent = notificationContent;
093        m_adminCms = adminCms;
094        m_userCms = userCms;
095        m_project = project;
096        m_resources = resources;
097        m_link = link;
098        String userAddress = userCms.getRequestContext().getCurrentUser().getEmail();
099        if (!CmsStringUtil.isEmptyOrWhitespaceOnly(userAddress)) {
100            setFrom(userAddress);
101        }
102        addMacro("release.project", m_project.getName());
103    }
104
105    /**
106     * Gets the fields which should be displayed for a single resource.<p>
107     *
108     * @param resource the resource for which we should fetch the fields
109     *
110     * @return a string array containing the information for the given resource
111     */
112    public String[] getResourceInfo(CmsResource resource) {
113
114        String rootPath = resource.getRootPath();
115        String title = "-";
116
117        try {
118            CmsProperty titleProp = m_adminCms.readPropertyObject(
119                resource,
120                CmsPropertyDefinition.PROPERTY_TITLE,
121                false);
122            if (!titleProp.isNullProperty()) {
123                title = titleProp.getValue();
124            }
125        } catch (CmsException e) {
126            // ignore
127        }
128        return new String[] {rootPath, title};
129    }
130
131    /**
132     * Gets the resource info headers.<p>
133     *
134     * @return the resource info headers
135     */
136    public String[] getResourceInfoHeaders() {
137
138        return new String[] {"Resource", "Title"};
139    }
140
141    /**
142     * @see org.opencms.notification.A_CmsNotification#generateHtmlMsg()
143     */
144    @Override
145    protected String generateHtmlMsg() {
146
147        StringBuffer buffer = new StringBuffer();
148
149        //---------PUBLISH LINK-----------------------------------------
150        buffer.append("    <div class=\"publish_link\">");
151        buffer.append(getMessage(Messages.GUI_MAIL_PUBLISH_LINK_1, m_link));
152        buffer.append("</div>\r\n");
153
154        //----------RESOURCE TABLE-------------------------------------
155        buffer.append("    <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"resource_table\">\r\n");
156        String[] tableHeaders = getResourceInfoHeaders();
157        buffer.append("      <tr>\r\n");
158        boolean first = true;
159        for (String header : tableHeaders) {
160            if (first) {
161                buffer.append("<th align=\"left\" class=\"rescol\">");
162                first = false;
163            } else {
164                buffer.append("<th align=\"left\" class=\"titlecol\">");
165            }
166            buffer.append(header);
167            buffer.append("</th>");
168        }
169        buffer.append("</tr>\n");
170
171        first = true;
172        for (CmsResource resource : m_resources) {
173            String[] resourceInfos = getResourceInfo(resource);
174            buffer.append("      <tr>\r\n");
175            for (String resourceInfo : resourceInfos) {
176                if (first) {
177                    buffer.append("<td class=\"rescol\">");
178                    first = false;
179                } else {
180                    buffer.append("<td class=\"titlecol\">");
181                    first = true;
182                }
183                buffer.append(resourceInfo);
184                buffer.append("</td>");
185            }
186            buffer.append("</tr>\n");
187        }
188        buffer.append("</table>");
189
190        //---------PUBLISH LINK-----------------------------------------
191        buffer.append("    <div class=\"publish_link\">");
192        buffer.append(getMessage(Messages.GUI_MAIL_PUBLISH_LINK_1, m_link));
193        buffer.append("</div>\r\n");
194
195        return buffer.toString();
196    }
197
198    /**
199     * Gets a message from the message bundle.<p>
200     *
201     * @param key the message key
202     * @param args the message parameters
203     *
204     * @return the message from the message bundle
205     */
206    protected String getMessage(String key, String... args) {
207
208        return Messages.get().getBundle(getLocale()).key(key, args);
209    }
210
211    /**
212     * @see org.opencms.notification.A_CmsNotification#getNotificationContent()
213     */
214    @Override
215    protected String getNotificationContent() {
216
217        return m_notificationContent;
218    }
219}