org.opencms.notification.CmsContentNotification Maven / Gradle / Ivy
Show all versions of opencms-core Show documentation
/*
* This library is part of OpenCms -
* the Open Source Content Management System
*
* Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.notification;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsUser;
import org.opencms.file.types.CmsResourceTypeJsp;
import org.opencms.file.types.CmsResourceTypePlain;
import org.opencms.file.types.CmsResourceTypeXmlPage;
import org.opencms.i18n.CmsLocaleManager;
import org.opencms.i18n.CmsMessages;
import org.opencms.loader.CmsLoaderException;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.util.CmsDateUtil;
import org.opencms.util.CmsRequestUtil;
import org.opencms.util.CmsUUID;
import org.opencms.workplace.CmsDialog;
import org.opencms.workplace.CmsWorkplace;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import org.apache.commons.logging.Log;
/**
* The E-Mail to be written to responsibles of resources.
*/
public class CmsContentNotification extends A_CmsNotification {
/** The path to the xml content with the subject, header and footer of the notification e-mail.
*/
public static final String NOTIFICATION_CONTENT = "notification/notification";
/** The log object for this class. */
private static final Log LOG = CmsLog.getLog(CmsContentNotification.class);
/** The message bundle initialized with the locale of the reciever. */
private CmsMessages m_messages;
/** The resources the responsible will be notified of, a list of CmsNotificationCauses. */
private List m_notificationCauses;
/** The receiver of the notification. */
private CmsUser m_responsible;
/** Server name and opencms context. */
private String m_serverAndContext = OpenCms.getSiteManager().getWorkplaceServer()
+ OpenCms.getSystemInfo().getOpenCmsContext();
/** Uri of the workplace folder. */
private String m_uriWorkplace = m_serverAndContext + CmsWorkplace.VFS_PATH_WORKPLACE;
/** Uri of the workplace jsp. */
private String m_uriWorkplaceJsp = m_serverAndContext + CmsWorkplace.JSP_WORKPLACE_URI;
/**
* Creates a new CmsContentNotification.
*
* @param responsible the user that will be notified
* @param cms the cms object to use
*/
CmsContentNotification(CmsUser responsible, CmsObject cms) {
super(cms, responsible);
m_responsible = responsible;
}
/**
* Returns true, if there exists an editor for a specific resource.
*
* @param resource the resource to check if there exists an editor
*
* @return true if there exists an editor for the resource
*/
public static boolean existsEditor(CmsResource resource) {
int plainId;
try {
plainId = OpenCms.getResourceManager().getResourceType(
CmsResourceTypePlain.getStaticTypeName()).getTypeId();
} catch (CmsLoaderException e) {
// this should really never happen
plainId = CmsResourceTypePlain.getStaticTypeId();
}
if ((CmsResourceTypeJsp.isJsp(resource))
|| (resource.getTypeId() == plainId)
|| CmsResourceTypeXmlPage.isXmlPage(resource)) {
return true;
}
return false;
}
/**
* Returns the responsible.
*
* @return the responsible
*/
public CmsUser getResponsible() {
return m_responsible;
}
/**
* Creates the mail to be sent to the responsible user.
*
* @return the mail to be sent to the responsible user
*/
@Override
protected String generateHtmlMsg() {
// set the messages
m_messages = Messages.get().getBundle(getLocale());
StringBuffer htmlMsg = new StringBuffer();
htmlMsg.append("
");
htmlMsg.append("
");
GregorianCalendar tomorrow = new GregorianCalendar(TimeZone.getDefault(), CmsLocaleManager.getDefaultLocale());
tomorrow.add(Calendar.DAY_OF_YEAR, 1);
List outdatedResources = new ArrayList();
List resourcesNextDay = new ArrayList();
List resourcesNextWeek = new ArrayList();
// split all resources into three lists: the resources that expire, will be released or get outdated
// within the next 24h, within the next week and the resources unchanged since a long time
Iterator notificationCauses = m_notificationCauses.iterator();
while (notificationCauses.hasNext()) {
CmsExtendedNotificationCause notificationCause = notificationCauses.next();
if (notificationCause.getCause() == CmsExtendedNotificationCause.RESOURCE_OUTDATED) {
outdatedResources.add(notificationCause);
} else if (notificationCause.getDate().before(tomorrow.getTime())) {
resourcesNextDay.add(notificationCause);
} else {
resourcesNextWeek.add(notificationCause);
}
}
Collections.sort(resourcesNextDay);
Collections.sort(resourcesNextWeek);
Collections.sort(outdatedResources);
appendResourceList(htmlMsg, resourcesNextDay, m_messages.key(Messages.GUI_WITHIN_NEXT_DAY_0));
appendResourceList(htmlMsg, resourcesNextWeek, m_messages.key(Messages.GUI_WITHIN_NEXT_WEEK_0));
appendResourceList(
htmlMsg,
outdatedResources,
m_messages.key(
Messages.GUI_FILES_NOT_UPDATED_1,
String.valueOf(OpenCms.getSystemInfo().getNotificationTime())));
htmlMsg.append("
");
String result = htmlMsg.toString();
return result;
}
/**
* Returns a list of CmsNotificationResourceInfos of the resources that will occur in the notification.
*
* @return a list of CmsNotificationResourceInfos of the resources that will occur in the notification
*/
protected List getNotificationCauses() {
return m_notificationCauses;
}
/**
* @see org.opencms.notification.A_CmsNotification#getNotificationContent()
*/
@Override
protected String getNotificationContent() {
return OpenCms.getSystemInfo().getConfigFilePath(m_cms, NOTIFICATION_CONTENT);
}
/**
* Sets the resources.
*
* @param resources a list of CmsNotificationResourceInfo's
*/
protected void setNotificationCauses(List resources) {
m_notificationCauses = resources;
}
/**
* Appends a link to confirm a resource, so that the responsible will not be notified any more.
*
* @param buf the StringBuffer to append the html code to
* @param notificationCause the information for specific resource
*/
private void appendConfirmLink(StringBuffer buf, CmsExtendedNotificationCause notificationCause) {
Map params = new HashMap();
buf.append("");
try {
String resourcePath = notificationCause.getResource().getRootPath();
String siteRoot = OpenCms.getSiteManager().getSiteRoot(resourcePath);
resourcePath = resourcePath.substring(siteRoot.length());
buf.append("[");
buf.append(m_messages.key(Messages.GUI_CONFIRM_0));
buf.append("]");
} catch (CmsException e) {
if (LOG.isInfoEnabled()) {
LOG.info(e.getLocalizedMessage(), e);
}
}
buf.append(" ");
}
/**
* Appends a link to edit the resource to a StringBuffer.
*
* @param buf the StringBuffer to append the html code to.
* @param notificationCause the information for specific resource.
*/
private void appendEditLink(StringBuffer buf, CmsExtendedNotificationCause notificationCause) {
buf.append("
");
if (existsEditor(notificationCause.getResource())) {
try {
String resourcePath = notificationCause.getResource().getRootPath();
String siteRoot = OpenCms.getSiteManager().getSiteRoot(resourcePath);
resourcePath = resourcePath.substring(siteRoot.length());
Map params = new HashMap();
CmsUUID projectId = getCmsObject().readProject(
OpenCms.getSystemInfo().getNotificationProject()).getUuid();
params.put(CmsWorkplace.PARAM_WP_PROJECT, new String[] {String.valueOf(projectId)});
params.put(
CmsWorkplace.PARAM_WP_EXPLORER_RESOURCE,
new String[] {CmsResource.getParentFolder(resourcePath)});
params.put(CmsWorkplace.PARAM_WP_SITE, new String[] {siteRoot});
params.put(CmsDialog.PARAM_RESOURCE, new String[] {resourcePath});
buf.append("[");
buf.append(m_messages.key(Messages.GUI_EDIT_0));
buf.append("]");
} catch (CmsException e) {
if (LOG.isInfoEnabled()) {
LOG.info(e.getLocalizedMessage(), e);
}
}
}
buf.append(" ");
}
/**
* Appends a link to edit the notification settings of a resource to a StringBuffer.
*
* @param buf the StringBuffer to append the html code to.
* @param notificationCause the information for specific resource.
*/
private void appendModifyLink(StringBuffer buf, CmsExtendedNotificationCause notificationCause) {
Map params = new HashMap();
buf.append("");
try {
buf.append("[");
buf.append(m_messages.key(Messages.GUI_MODIFY_0));
buf.append("]");
} catch (CmsException e) {
if (LOG.isInfoEnabled()) {
LOG.info(e.getLocalizedMessage(), e);
}
}
buf.append(" ");
}
/**
* Appends a table showing a set of resources, and the cause of the notification.
*
* @param htmlMsg html the StringBuffer to append the html code to
* @param notificationCauseList the list of notification causes
* @param header the title of the resource list
*/
private void appendResourceList(
StringBuffer htmlMsg,
List notificationCauseList,
String header) {
if (!notificationCauseList.isEmpty()) {
htmlMsg.append("
");
htmlMsg.append(header);
htmlMsg.append(
"
");
htmlMsg.append(m_messages.key(Messages.GUI_RESOURCE_0));
htmlMsg.append(" ");
htmlMsg.append(m_messages.key(Messages.GUI_SITE_0));
htmlMsg.append(" ");
htmlMsg.append(m_messages.key(Messages.GUI_ISSUE_0));
htmlMsg.append(" ");
Iterator notificationCauses = notificationCauseList.iterator();
for (int i = 0; notificationCauses.hasNext(); i++) {
CmsExtendedNotificationCause notificationCause = notificationCauses.next();
htmlMsg.append(buildNotificationListItem(notificationCause, (i % 2) + 2));
}
}
}
/**
* Returns a string representation of this resource info.
*
* @param notificationCause the notification cause
* @param row the row number
*
* @return a string representation of this resource info
*/
private String buildNotificationListItem(CmsExtendedNotificationCause notificationCause, int row) {
StringBuffer result = new StringBuffer("
");
String resourcePath = notificationCause.getResource().getRootPath();
String siteRoot = OpenCms.getSiteManager().getSiteRoot(resourcePath);
resourcePath = resourcePath.substring(siteRoot.length());
// append link, if page is available
if ((notificationCause.getResource().getDateReleased() < System.currentTimeMillis())
&& (notificationCause.getResource().getDateExpired() > System.currentTimeMillis())) {
Map params = new HashMap();
params.put(CmsWorkplace.PARAM_WP_SITE, new String[] {siteRoot});
params.put(CmsDialog.PARAM_RESOURCE, new String[] {resourcePath});
result.append("");
result.append(resourcePath);
result.append("");
} else {
result.append(resourcePath);
}
result.append(" ");
result.append(siteRoot);
result.append(" ");
if (notificationCause.getCause() == CmsExtendedNotificationCause.RESOURCE_EXPIRES) {
result.append(m_messages.key(Messages.GUI_EXPIRES_AT_1, new Object[] {notificationCause.getDate()}));
result.append(" ");
appendConfirmLink(result, notificationCause);
appendModifyLink(result, notificationCause);
} else if (notificationCause.getCause() == CmsExtendedNotificationCause.RESOURCE_RELEASE) {
result.append(m_messages.key(Messages.GUI_RELEASE_AT_1, new Object[] {notificationCause.getDate()}));
result.append("");
appendConfirmLink(result, notificationCause);
appendModifyLink(result, notificationCause);
} else if (notificationCause.getCause() == CmsExtendedNotificationCause.RESOURCE_UPDATE_REQUIRED) {
result.append(m_messages.key(Messages.GUI_UPDATE_REQUIRED_1, new Object[] {notificationCause.getDate()}));
result.append("");
appendConfirmLink(result, notificationCause);
appendEditLink(result, notificationCause);
} else {
result.append(
m_messages.key(
Messages.GUI_UNCHANGED_SINCE_1,
new Object[] {new Integer(CmsDateUtil.getDaysPassedSince(notificationCause.getDate()))}));
result.append("");
appendConfirmLink(result, notificationCause);
appendEditLink(result, notificationCause);
}
result.append(" ");
return result.toString();
}
}