1 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 ; 48 import java.util.Calendar ; 49 import java.util.Collection ; 50 import java.util.Date ; 51 import java.util.GregorianCalendar ; 52 import java.util.HashMap ; 53 import java.util.Iterator ; 54 import java.util.List ; 55 import java.util.Map ; 56 import java.util.TimeZone ; 57 58 import javax.mail.MessagingException ; 59 60 import org.apache.commons.logging.Log; 61 62 69 public class CmsNotificationCandidates { 70 71 72 private List m_resources; 73 74 75 private static final Log LOG = CmsLog.getLog(CmsNotificationCandidates.class); 76 77 78 private CmsObject m_cms; 79 80 87 public CmsNotificationCandidates(CmsObject cms) 88 throws CmsException { 89 90 m_resources = new ArrayList (); 91 m_cms = cms; 92 m_cms.getRequestContext().setCurrentProject(m_cms.readProject(OpenCms.getSystemInfo().getNotificationProject())); 93 String folder = "/"; 94 GregorianCalendar now = new GregorianCalendar (TimeZone.getDefault(), CmsLocaleManager.getDefaultLocale()); 95 now.setTimeInMillis(System.currentTimeMillis()); 96 GregorianCalendar inOneWeek = (GregorianCalendar )now.clone(); 97 inOneWeek.add(Calendar.WEEK_OF_YEAR, 1); 98 Iterator resources; 99 CmsResource resource; 100 101 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 intervalBefore = new GregorianCalendar ( 111 TimeZone.getDefault(), 112 CmsLocaleManager.getDefaultLocale()); 113 intervalBefore.setTimeInMillis(resource.getDateLastModified()); 114 intervalBefore.add(Calendar.DAY_OF_YEAR, notification_interval); 115 GregorianCalendar intervalAfter = (GregorianCalendar )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 } 132 133 GregorianCalendar oneYearAgo = (GregorianCalendar )now.clone(); 135 oneYearAgo.add(Calendar.DAY_OF_YEAR, -OpenCms.getSystemInfo().getNotificationTime()); 136 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 (resource.getDateLastModified()))); 145 } 146 147 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 (resource.getDateExpired()))); 157 } 158 159 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 (resource.getDateReleased()))); 169 } 170 } 171 172 179 protected Collection getContentNotifications() throws CmsException { 180 181 Iterator notificationCandidates = m_resources.iterator(); 183 Map result = new HashMap (); 184 while (notificationCandidates.hasNext()) { 185 CmsExtendedNotificationCause resourceInfo = (CmsExtendedNotificationCause)notificationCandidates.next(); 186 CmsResource resource = resourceInfo.getResource(); 187 String enableNotification = m_cms.readPropertyObject( 189 resource, 190 CmsPropertyDefinition.PROPERTY_ENABLE_NOTIFICATION, 191 true).getValue(); 192 if (Boolean.valueOf(enableNotification).booleanValue()) { 193 try { 194 Iterator responsibles = m_cms.readResponsibleUsers(resource).iterator(); 195 while (responsibles.hasNext()) { 196 CmsUser responsible = (CmsUser)responsibles.next(); 197 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(responsible.getEmail())) { 198 CmsContentNotification contentNotification = (CmsContentNotification)result.get(responsible); 200 201 if (contentNotification == null) { 203 contentNotification = new CmsContentNotification(responsible, m_cms); 204 result.put(responsible, contentNotification); 205 } 206 List resourcesForResponsible = contentNotification.getNotificationCauses(); 207 if (resourcesForResponsible == null) { 208 resourcesForResponsible = new ArrayList (); 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 231 public String notifyResponsibles() throws CmsException { 232 233 Iterator notifications = filterConfirmedResources(getContentNotifications()).iterator(); 234 if (notifications.hasNext()) { 235 StringBuffer result = new StringBuffer (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 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 263 private Collection filterConfirmedResources(Collection contentNotifications) { 264 265 Iterator notifications = contentNotifications.iterator(); 266 while (notifications.hasNext()) { 267 CmsContentNotification contentNotification = (CmsContentNotification)notifications.next(); 268 CmsUser responsible = contentNotification.getResponsible(); 269 List confirmedResourcesList = (List )responsible.getAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_CONFIRMED_RESOURCES); 271 if (confirmedResourcesList == null) { 272 confirmedResourcesList = new ArrayList (); 273 responsible.setAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_CONFIRMED_RESOURCES, new ArrayList ()); 274 } 275 276 List notificationCandidates = contentNotification.getNotificationCauses(); 277 278 List notificationResources = new ArrayList (notificationCandidates); 279 Iterator i = confirmedResourcesList.iterator(); 281 while (i.hasNext()) { 282 Object o = i.next(); 283 if (notificationResources.contains(o)) { 284 notificationResources.remove(o); 285 } 286 } 287 i = new ArrayList (confirmedResourcesList).iterator(); 289 while (i.hasNext()) { 290 Object o = i.next(); 291 if (!notificationCandidates.contains(o)) { 292 confirmedResourcesList.remove(o); 293 } 294 } 295 contentNotification.setNotificationCauses(notificationResources); 296 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 |