📄 cmscontentnotification.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/notification/CmsContentNotification.java,v $
* Date : $Date: 2006/03/27 14:52:46 $
* Version: $Revision: 1.2 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2002 - 2004 Alkacon Software (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.db.CmsUserSettings;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
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.mail.CmsHtmlMail;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.site.CmsSiteManager;
import org.opencms.util.CmsDateUtil;
import org.opencms.util.CmsMacroResolver;
import org.opencms.util.CmsRequestUtil;
import org.opencms.workplace.CmsDialog;
import org.opencms.workplace.CmsFrameset;
import org.opencms.workplace.CmsWorkplace;
import org.opencms.xml.content.CmsXmlContent;
import org.opencms.xml.content.CmsXmlContentFactory;
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.Locale;
import java.util.Map;
import java.util.TimeZone;
import javax.mail.MessagingException;
import org.apache.commons.logging.Log;
/**
* The E-Mail to be written to responsibles of resources.<p>
*
* @author Jan Baudisch
*/
public class CmsContentNotification extends CmsHtmlMail {
/** The path to the xml content with the subject, header and footer of the notification e-mail.<p> */
public static final String NOTIFICATION_CONTENT = "/system/workplace/admin/notification/notification";
/** The log object for this class. */
private static final Log LOG = CmsLog.getLog(CmsContentNotification.class);
/** The CmsObject. */
private CmsObject m_cms;
/** The locale of the reciever of the content notification. */
private Locale m_locale;
/** The xml-content to read subject, header and footer of the notification. */
private CmsXmlContent m_mailContent;
/** 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 + CmsFrameset.JSP_WORKPLACE_URI;
/**
* Creates a new CmsContentNotification.<p>
*
* @param responsible the user that will be notified
* @param cms the cms object to use
*/
CmsContentNotification(CmsUser responsible, CmsObject cms) {
m_responsible = responsible;
m_cms = cms;
}
/**
* Returns true, if there exists an editor for a specific resource.<p>
*
* @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) {
if ((resource.getTypeId() == CmsResourceTypeJsp.getStaticTypeId())
|| (resource.getTypeId() == CmsResourceTypePlain.getStaticTypeId())
|| (resource.getTypeId() == CmsResourceTypeXmlPage.getStaticTypeId())) {
return true;
}
return false;
}
/**
* Returns the responsible.<p>
*
* @return the responsible
*/
public CmsUser getResponsible() {
return m_responsible;
}
/**
*
* @see org.apache.commons.mail.Email#send()
*/
public void send() throws MessagingException {
try {
m_mailContent = CmsXmlContentFactory.unmarshal(m_cms, m_cms.readFile(
NOTIFICATION_CONTENT,
CmsResourceFilter.ALL));
List locales = m_mailContent.getLocales();
Locale userLocale = new CmsUserSettings(m_responsible).getLocale();
if (locales.contains(userLocale)) {
// mail is localized in the user locale, use that
m_locale = userLocale;
} else if (locales.contains(OpenCms.getWorkplaceManager().getDefaultLocale())) {
// mail is localized in the system default locale, use that
m_locale = OpenCms.getWorkplaceManager().getDefaultLocale();
} else {
// use any localization
m_locale = (Locale)locales.get(0);
}
// set the messages
m_messages = Messages.get().getBundle(m_locale);
addTo(m_responsible.getEmail(), m_responsible.getFirstname() + ' ' + m_responsible.getLastname());
setSubject(m_mailContent.getStringValue(m_cms, "Subject", m_locale));
setHtmlMsg(generateHtmlMsg());
super.send();
} catch (CmsException e) {
LOG.error(e);
}
}
/**
* Creates the mail to be sent to the responsible user.<p>
*
* @return the mail to be sent to the responsible user
* @throws CmsException if something goes wrong
*/
protected String generateHtmlMsg() throws CmsException {
StringBuffer htmlMsg = new StringBuffer("<html><head><style type=\"text/css\">");
htmlMsg.append("<!-- body { font-family: Verdana, Arial, Helvetica, sans-serif; background-color:#ffefdb; }");
htmlMsg.append("a {color:#b22222;} table { white-space: nowrap; font-size: x-small; } tr.trow1 { background-color: #cdc0b0; } ");
htmlMsg.append("tr.trow2 { background-color: #eedfcc; } tr.trow3 { background-color: #ffefdb; } a { text-decoration:none }--></style>");
htmlMsg.append("</head><body><span style='font-size:8.0pt;'> <table border=\"0\" cellpadding=\"0\" ");
htmlMsg.append("cellspacing=\"0\" width=\"100%\"><tr><td colspan=\"5\"><br/>");
m_mailContent = CmsXmlContentFactory.unmarshal(m_cms, m_cms.readFile(NOTIFICATION_CONTENT));
CmsMacroResolver macroResolver = new CmsMacroResolver();
macroResolver.addMacro("firstname", m_responsible.getFirstname());
macroResolver.addMacro("lastname", m_responsible.getLastname());
htmlMsg.append(CmsMacroResolver.resolveMacros(
m_mailContent.getStringValue(m_cms, "Header", m_locale),
macroResolver));
htmlMsg.append("<br/></td>");
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 = (CmsExtendedNotificationCause)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);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -