KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > notification > CmsNotificationCandidates


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/notification/CmsNotificationCandidates.java,v $
3  * Date : $Date: 2006/09/21 09:34:47 $
4  * Version: $Revision: 1.3 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2002 - 2004 Alkacon Software (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.notification;
33
34 import org.opencms.db.CmsDbEntryNotFoundException;
35 import org.opencms.db.CmsUserSettings;
36 import org.opencms.file.CmsObject;
37 import org.opencms.file.CmsPropertyDefinition;
38 import org.opencms.file.CmsResource;
39 import org.opencms.file.CmsResourceFilter;
40 import org.opencms.file.CmsUser;
41 import org.opencms.i18n.CmsLocaleManager;
42 import org.opencms.main.CmsException;
43 import org.opencms.main.CmsLog;
44 import org.opencms.main.OpenCms;
45 import org.opencms.util.CmsStringUtil;
46
47 import java.util.ArrayList JavaDoc;
48 import java.util.Calendar JavaDoc;
49 import java.util.Collection JavaDoc;
50 import java.util.Date JavaDoc;
51 import java.util.GregorianCalendar JavaDoc;
52 import java.util.HashMap JavaDoc;
53 import java.util.Iterator JavaDoc;
54 import java.util.List JavaDoc;
55 import java.util.Map JavaDoc;
56 import java.util.TimeZone JavaDoc;
57
58 import javax.mail.MessagingException JavaDoc;
59
60 import org.apache.commons.logging.Log;
61
62 /**
63  * The basic class for the content notification feature in OpenCms. Collects all resources that require a notification,
64  * creates and sends notifications to their responsible users.<p/>
65  *
66  * @author Jan Baudisch
67  *
68  */

69 public class CmsNotificationCandidates {
70
71     /** The resources which come into question for notifications of responsible users. */
72     private List JavaDoc m_resources;
73
74     /** The log object for this class. */
75     private static final Log LOG = CmsLog.getLog(CmsNotificationCandidates.class);
76
77     /** the CmsObject. */
78     private CmsObject m_cms;
79
80     /**
81      * Collects all resources that will expire in short time, or will become valid, or are not modified since a long time.<p>
82      *
83      * @param cms the CmsObject
84      *
85      * @throws CmsException if something goes wrong
86      */

87     public CmsNotificationCandidates(CmsObject cms)
88     throws CmsException {
89
90         m_resources = new ArrayList JavaDoc();
91         m_cms = cms;
92         m_cms.getRequestContext().setCurrentProject(m_cms.readProject(OpenCms.getSystemInfo().getNotificationProject()));
93         String JavaDoc folder = "/";
94         GregorianCalendar JavaDoc now = new GregorianCalendar JavaDoc(TimeZone.getDefault(), CmsLocaleManager.getDefaultLocale());
95         now.setTimeInMillis(System.currentTimeMillis());
96         GregorianCalendar JavaDoc inOneWeek = (GregorianCalendar JavaDoc)now.clone();
97         inOneWeek.add(Calendar.WEEK_OF_YEAR, 1);
98         Iterator JavaDoc resources;
99         CmsResource resource;
100
101         // read all files with the 'notification-interval' property set
102
try {
103             resources = m_cms.readResourcesWithProperty(folder, CmsPropertyDefinition.PROPERTY_NOTIFICATION_INTERVAL).iterator();
104             while (resources.hasNext()) {
105                 resource = (CmsResource)resources.next();
106                 int notification_interval = Integer.parseInt(m_cms.readPropertyObject(
107                     resource,
108                     CmsPropertyDefinition.PROPERTY_NOTIFICATION_INTERVAL,
109                     true).getValue());
110                 GregorianCalendar JavaDoc intervalBefore = new GregorianCalendar JavaDoc(
111                     TimeZone.getDefault(),
112                     CmsLocaleManager.getDefaultLocale());
113                 intervalBefore.setTimeInMillis(resource.getDateLastModified());
114                 intervalBefore.add(Calendar.DAY_OF_YEAR, notification_interval);
115                 GregorianCalendar JavaDoc intervalAfter = (GregorianCalendar JavaDoc)intervalBefore.clone();
116                 intervalAfter.add(Calendar.WEEK_OF_YEAR, -1);
117
118                 for (int i = 0; i < 100 && intervalAfter.getTime().before(now.getTime()); i++) {
119                     if (intervalBefore.getTime().after(now.getTime())) {
120                         m_resources.add(new CmsExtendedNotificationCause(
121                             resource,
122                             CmsExtendedNotificationCause.RESOURCE_UPDATE_REQUIRED,
123                             intervalBefore.getTime()));
124                     }
125                     intervalBefore.add(Calendar.DAY_OF_YEAR, notification_interval);
126                     intervalAfter.add(Calendar.DAY_OF_YEAR, notification_interval);
127                 }
128             }
129         } catch (CmsDbEntryNotFoundException e) {
130             // no resources with property 'notification-interval', ignore
131
}
132
133         // read all files that were not modified longer than the max notification-time
134
GregorianCalendar JavaDoc oneYearAgo = (GregorianCalendar JavaDoc)now.clone();
135         oneYearAgo.add(Calendar.DAY_OF_YEAR, -OpenCms.getSystemInfo().getNotificationTime());
136         // create a resource filter to get the resources with
137
CmsResourceFilter filter = CmsResourceFilter.IGNORE_EXPIRATION.addRequireLastModifiedBefore(oneYearAgo.getTimeInMillis());
138         resources = m_cms.readResources(folder, filter).iterator();
139         while (resources.hasNext()) {
140             resource = (CmsResource)resources.next();
141             m_resources.add(new CmsExtendedNotificationCause(
142                 resource,
143                 CmsExtendedNotificationCause.RESOURCE_OUTDATED,
144                 new Date JavaDoc(resource.getDateLastModified())));
145         }
146
147         // get all resources that will expire within the next week
148
CmsResourceFilter resourceFilter = CmsResourceFilter.IGNORE_EXPIRATION.addRequireExpireBefore(inOneWeek.getTimeInMillis());
149         resourceFilter = resourceFilter.addRequireExpireAfter(now.getTimeInMillis());
150         resources = m_cms.readResources(folder, resourceFilter).iterator();
151         while (resources.hasNext()) {
152             resource = (CmsResource)resources.next();
153             m_resources.add(new CmsExtendedNotificationCause(
154                 resource,
155                 CmsExtendedNotificationCause.RESOURCE_EXPIRES,
156                 new Date JavaDoc(resource.getDateExpired())));
157         }
158
159         // get all resources that will release within the next week
160
resourceFilter = CmsResourceFilter.IGNORE_EXPIRATION.addRequireReleaseBefore(inOneWeek.getTimeInMillis());
161         resourceFilter = resourceFilter.addRequireReleaseAfter(now.getTimeInMillis());
162         resources = m_cms.readResources(folder, resourceFilter).iterator();
163         while (resources.hasNext()) {
164             resource = (CmsResource)resources.next();
165             m_resources.add(new CmsExtendedNotificationCause(
166                 resource,
167                 CmsExtendedNotificationCause.RESOURCE_RELEASE,
168                 new Date JavaDoc(resource.getDateReleased())));
169         }
170     }
171
172     /**
173      * Returns a collection of CmsContentNotifications, one for each responsible that receives a notification.<p>
174      *
175      * @return the list of CmsContentNotifications, one for each responsible that receives a notification
176      *
177      * @throws CmsException if something goes wrong
178      */

179     protected Collection JavaDoc getContentNotifications() throws CmsException {
180
181         // get all owners for the resource
182
Iterator JavaDoc notificationCandidates = m_resources.iterator();
183         Map JavaDoc result = new HashMap JavaDoc();
184         while (notificationCandidates.hasNext()) {
185             CmsExtendedNotificationCause resourceInfo = (CmsExtendedNotificationCause)notificationCandidates.next();
186             CmsResource resource = resourceInfo.getResource();
187             // skip, if content notification is not enabled for this resource
188
String JavaDoc enableNotification = m_cms.readPropertyObject(
189                 resource,
190                 CmsPropertyDefinition.PROPERTY_ENABLE_NOTIFICATION,
191                 true).getValue();
192             if (Boolean.valueOf(enableNotification).booleanValue()) {
193                 try {
194                     Iterator JavaDoc responsibles = m_cms.readResponsibleUsers(resource).iterator();
195                     while (responsibles.hasNext()) {
196                         CmsUser responsible = (CmsUser)responsibles.next();
197                         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(responsible.getEmail())) {
198                             // check, if resultset already contains a content notification for the user
199
CmsContentNotification contentNotification = (CmsContentNotification)result.get(responsible);
200
201                             // if not add a new content notification
202
if (contentNotification == null) {
203                                 contentNotification = new CmsContentNotification(responsible, m_cms);
204                                 result.put(responsible, contentNotification);
205                             }
206                             List JavaDoc resourcesForResponsible = contentNotification.getNotificationCauses();
207                             if (resourcesForResponsible == null) {
208                                 resourcesForResponsible = new ArrayList JavaDoc();
209                                 contentNotification.setNotificationCauses(resourcesForResponsible);
210                             }
211                             resourcesForResponsible.add(resourceInfo);
212                         }
213                     }
214                 } catch (CmsException e) {
215                     if (LOG.isInfoEnabled()) {
216                         LOG.error(e);
217                     }
218                 }
219             }
220         }
221         return result.values();
222     }
223
224     /**
225      * Sends all notifications to the responsible users.<p>
226      *
227      * @return a string listing all responsibles that a notification was sent to
228      *
229      * @throws CmsException if something goes wrong
230      */

231     public String JavaDoc notifyResponsibles() throws CmsException {
232
233         Iterator JavaDoc notifications = filterConfirmedResources(getContentNotifications()).iterator();
234         if (notifications.hasNext()) {
235             StringBuffer JavaDoc result = new StringBuffer JavaDoc(Messages.get().getBundle().key(Messages.LOG_NOTIFICATIONS_SENT_TO_0));
236             result.append(' ');
237             while (notifications.hasNext()) {
238                 CmsContentNotification contentNotification = (CmsContentNotification)notifications.next();
239                 result.append(contentNotification.getResponsible().getName());
240                 if (notifications.hasNext()) {
241                     result.append(", ");
242                 }
243                 try {
244                     contentNotification.send();
245                 } catch (MessagingException JavaDoc e) {
246                     LOG.error(e);
247                 }
248             }
249             return result.toString();
250         } else {
251             return Messages.get().getBundle().key(Messages.LOG_NO_NOTIFICATIONS_SENT_0);
252         }
253     }
254
255     /**
256      * Updates the resources that were confirmed by the user. That means deletes the resources that need not a
257      * notification any more.
258      * removes all resources which do not occur in the candidate list.<p>
259      *
260      * @param resources the list of resources to remove from the set of confirmed resources
261      * @return a new CmsConfirmedResources Object which all the resource removed
262      */

263     private Collection JavaDoc filterConfirmedResources(Collection JavaDoc contentNotifications) {
264
265         Iterator JavaDoc notifications = contentNotifications.iterator();
266         while (notifications.hasNext()) {
267             CmsContentNotification contentNotification = (CmsContentNotification)notifications.next();
268             CmsUser responsible = contentNotification.getResponsible();
269             // check, if user was already notified
270
List JavaDoc confirmedResourcesList = (List JavaDoc)responsible.getAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_CONFIRMED_RESOURCES);
271             if (confirmedResourcesList == null) {
272                 confirmedResourcesList = new ArrayList JavaDoc();
273                 responsible.setAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_CONFIRMED_RESOURCES, new ArrayList JavaDoc());
274             }
275
276             List JavaDoc notificationCandidates = contentNotification.getNotificationCauses();
277
278             List JavaDoc notificationResources = new ArrayList JavaDoc(notificationCandidates);
279             // remove already confirmed resources
280
Iterator JavaDoc i = confirmedResourcesList.iterator();
281             while (i.hasNext()) {
282                 Object JavaDoc o = i.next();
283                 if (notificationResources.contains(o)) {
284                     notificationResources.remove(o);
285                 }
286             }
287             // filter confirmed resources
288
i = new ArrayList JavaDoc(confirmedResourcesList).iterator();
289             while (i.hasNext()) {
290                 Object JavaDoc o = i.next();
291                 if (!notificationCandidates.contains(o)) {
292                     confirmedResourcesList.remove(o);
293                 }
294             }
295             contentNotification.setNotificationCauses(notificationResources);
296             // Remove notification, if resource list is empty
297
if (notificationCandidates.isEmpty()) {
298                 contentNotifications.remove(contentNotification);
299             }
300             try {
301                 m_cms.writeUser(responsible);
302             } catch (CmsException e) {
303                 LOG.error(e);
304             }
305         }
306         return contentNotifications;
307     }
308 }
Popular Tags