1 12 13 package com.openedit.modules.changelog; 14 15 import java.util.ArrayList ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.dom4j.DocumentException; 25 import org.dom4j.io.SAXReader; 26 27 import com.openedit.OpenEditException; 28 import com.openedit.OpenEditRuntimeException; 29 import com.openedit.WebPageRequest; 30 import com.openedit.config.XMLConfiguration; 31 import com.openedit.hittracker.HitTracker; 32 import com.openedit.modules.BaseModule; 33 import com.openedit.modules.email.Recipient; 34 import com.openedit.modules.email.TemplateWebEmail; 35 import com.openedit.modules.workflow.WorkFlowModule; 36 import com.openedit.page.Page; 37 import com.openedit.users.Group; 38 import com.openedit.users.User; 39 import com.openedit.modules.email.PostMail; 40 42 public class ChangeLogModule extends BaseModule 43 { 44 private static Log log = LogFactory.getLog(ChangeLogModule.class); 45 protected ChangeLog fieldChangeLog; 46 protected Map fieldActions; 47 48 protected String fieldFromAddress = null; 49 protected String fieldGroupEmailProperty = "oe.edit.notify"; 50 protected String fieldSMTPServer; 51 protected String fieldDefaultMailTemplate = "/openedit/notification/notification_layout.html"; 52 protected PostMail postMail; 53 protected boolean hasInit = false; 54 60 public void setFromAddress(String fromAddress) 61 { 62 fieldFromAddress = fromAddress; 63 } 64 65 70 public String getFromAddress() 71 { 72 return fieldFromAddress; 73 } 74 75 81 public void setGroupEmailProperty(String groupEmailProperty) 82 { 83 fieldGroupEmailProperty = groupEmailProperty; 84 } 85 86 91 public String getGroupEmailProperty() 92 { 93 return fieldGroupEmailProperty; 94 } 95 96 102 public void setSMTPServer(String smtpServer) 103 { 104 fieldSMTPServer = smtpServer; 105 } 106 107 112 public String getSMTPServer() 113 { 114 return fieldSMTPServer; 115 } 116 117 public void notifyUsers(WebPageRequest inContext) throws OpenEditException 118 { 119 if (getChangeLog().changeNotificationCount() == 0) 120 { 121 inContext.putPageValue("changeMessage", "No changes found"); 122 log.info("No changes found"); 123 } 124 else 125 { 126 Set users = findNotificationEmailAddresses(); 127 128 TemplateWebEmail webmail = postMail.getTemplateWebEmail(); 130 Page layoutPage = getLayoutPage(); 131 webmail.setMailTemplatePage(layoutPage); 132 webmail.setFrom(getFromAddress()); 133 134 List list; 135 try 136 { 137 list = buildRecipients(users); 138 139 if (list.size() > 0) 140 { 141 WorkFlowModule mod = (WorkFlowModule)getModule("WorkFlow"); 142 mod.listDrafts(inContext); 143 inContext.putPageValue("changeLog", getChangeLog()); 144 String host = layoutPage.get("hostName"); 145 if ( host == null) 147 { 148 host = getRoot().getPath(); 149 } 150 else 151 { 152 inContext.putPageValue("baselink","http://" + host); 153 } 154 webmail.setSubject("[" + host + "] edit notifications"); 155 webmail.setRecipients(list); 156 webmail.setWebPageContext(inContext); 157 log.info("Mailing " + list); 158 webmail.send(); 159 } 160 else 161 { 162 log.info("No users to send to"); 163 } 164 } 165 catch (Exception e) 166 { 167 throw new OpenEditException(e); 168 } 169 getChangeLog().clearNotifications(); 170 inContext.putPageValue("userList", list); 171 } 172 } 173 174 protected Page getLayoutPage() throws OpenEditException 175 { 176 Page layoutPage = getPageManager().getPage(getDefaultMailTemplate()); 177 return layoutPage; 178 } 181 182 190 protected List buildRecipients(Set users) throws DocumentException 191 { 192 List list = new ArrayList (); 193 SAXReader parser = new SAXReader(false); 194 195 for (Iterator iter = users.iterator(); iter.hasNext();) 196 { 197 User user = (User) iter.next(); 198 Recipient rec = new Recipient(); 199 rec.setEmailAddress(user.getEmail()); 200 rec.setLastName(user.getLastName()); 201 rec.setFirstName(user.getFirstName()); 202 list.add(rec); 203 } 204 205 return list; 206 } 207 208 217 public Set findNotificationEmailAddresses() throws OpenEditException 218 { 219 Set emailAddresses = new HashSet (); 220 221 for (Iterator groupIter = getUserManager().getGroups().iterator(); groupIter.hasNext();) 222 { 223 Group group = (Group) groupIter.next(); 224 225 if (group.hasPermission(getGroupEmailProperty())) 226 { 227 HitTracker list = getUserManager().getUsersInGroup(group); 229 for (Iterator userIter = list.iterator(); userIter.hasNext();) 230 { 231 User user = (User) userIter.next(); 232 String email = user.getEmail(); 233 234 if ((email != null) && (email.length() > 0)) 235 { 236 emailAddresses.add(user); 237 } 238 } 239 } 240 } 241 242 return emailAddresses; 243 } 244 245 public String getDefaultMailTemplate() 246 { 247 return fieldDefaultMailTemplate; 248 } 249 250 public void setDefaultMailTemplate(String inDefaultMailTemplate) 251 { 252 fieldDefaultMailTemplate = inDefaultMailTemplate; 253 } 254 255 260 public void setChangeLog(ChangeLog inLog) 261 { 262 fieldChangeLog = inLog; 263 } 264 265 267 public void init() 268 { 269 if (hasInit) 270 { 271 return; 272 } 273 hasInit = true; 274 try 275 { 276 Page config = getPageManager().getPage("/openedit/notification/notifysettings.xml"); 277 if (!config.exists()) 278 { 279 return; 280 } 281 XMLConfiguration notificationConfig = new XMLConfiguration(); 282 notificationConfig.readXML(config.getReader()); 283 284 getChangeLog().init(); 285 286 String fromEmail = notificationConfig.getChildValue("from-email"); 287 288 if (fromEmail != null) 289 { 290 setFromAddress(fromEmail); 291 } 292 293 String groupEmailProperty = notificationConfig.getChildValue("group-email-property"); 294 295 if (groupEmailProperty != null) 296 { 297 setGroupEmailProperty(groupEmailProperty); 298 } 299 300 String smtpServer = notificationConfig.getChildValue("smtp-server"); 301 302 if (smtpServer != null) 303 { 304 setSMTPServer(smtpServer); 305 } 306 } 307 catch (Exception e) 308 { 309 throw new OpenEditRuntimeException(e); 310 } 311 312 } 313 314 315 public void getChangeLog(WebPageRequest inContext) throws OpenEditException 316 { 317 inContext.putPageValue("changeLog", getChangeLog()); 318 inContext.putPageValue("userEmailList", findNotificationEmailAddresses()); 319 } 320 321 324 public ChangeLog getChangeLog() 325 { 326 init(); 327 return fieldChangeLog; 328 } 329 330 public PostMail getPostMail() { 331 return postMail; 332 } 333 334 public void setPostMail(PostMail postMail) { 335 this.postMail = postMail; 336 } 337 338 } 339 | Popular Tags |