KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > changelog > ChangeLogModule


1 /*
2  Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4  This library is free software; you can redistribute it and/or modify it under the terms
5  of the GNU Lesser General Public License as published by the Free Software Foundation;
6  either version 2.1 of the License, or (at your option) any later version.
7
8  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  See the GNU Lesser General Public License for more details.
11  */

12
13 package com.openedit.modules.changelog;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
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 /**
41  */

42 public class ChangeLogModule extends BaseModule
43 {
44     private static Log log = LogFactory.getLog(ChangeLogModule.class);
45     protected ChangeLog fieldChangeLog;
46     protected Map JavaDoc fieldActions;
47
48     protected String JavaDoc fieldFromAddress = null;
49     protected String JavaDoc fieldGroupEmailProperty = "oe.edit.notify";
50     protected String JavaDoc fieldSMTPServer;
51     protected String JavaDoc fieldDefaultMailTemplate = "/openedit/notification/notification_layout.html";
52     protected PostMail postMail;
53     protected boolean hasInit = false;
54     /**
55      * Sets the fromAddress.
56      *
57      * @param fromAddress
58      * The fromAddress to set
59      */

60     public void setFromAddress(String JavaDoc fromAddress)
61     {
62         fieldFromAddress = fromAddress;
63     }
64
65     /**
66      * Returns the fromAddress.
67      *
68      * @return String
69      */

70     public String JavaDoc getFromAddress()
71     {
72         return fieldFromAddress;
73     }
74
75     /**
76      * Sets the groupEmailProperty.
77      *
78      * @param groupEmailProperty
79      * The groupEmailProperty to set
80      */

81     public void setGroupEmailProperty(String JavaDoc groupEmailProperty)
82     {
83         fieldGroupEmailProperty = groupEmailProperty;
84     }
85
86     /**
87      * Returns the groupEmailProperty.
88      *
89      * @return String
90      */

91     public String JavaDoc getGroupEmailProperty()
92     {
93         return fieldGroupEmailProperty;
94     }
95
96     /**
97      * Sets the SMTP server via which to send notification emails.
98      *
99      * @param smtpServer
100      * The SMTP server
101      */

102     public void setSMTPServer(String JavaDoc smtpServer)
103     {
104         fieldSMTPServer = smtpServer;
105     }
106
107     /**
108      * Returns the SMTP server via which to send notification emails.
109      *
110      * @return String
111      */

112     public String JavaDoc 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 JavaDoc users = findNotificationEmailAddresses();
127
128             //make an EmailSettings object
129
TemplateWebEmail webmail = postMail.getTemplateWebEmail();
130             Page layoutPage = getLayoutPage();
131             webmail.setMailTemplatePage(layoutPage);
132             webmail.setFrom(getFromAddress());
133
134             List JavaDoc 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 JavaDoc host = layoutPage.get("hostName");
145                     //use send to send them
146
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 JavaDoc 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         //layoutPage = getPageManager().getPage( FAILOVER_DEFAULT_LAYOUT );
179
//return layoutPage;
180
}
181
182     /**
183      * DOCUMENT ME!
184      *
185      * @param users
186      *
187      * @return @throws
188      * DocumentException DOCUMENT ME!
189      */

190     protected List JavaDoc buildRecipients(Set JavaDoc users) throws DocumentException
191     {
192         List JavaDoc list = new ArrayList JavaDoc();
193         SAXReader parser = new SAXReader(false);
194
195         for (Iterator JavaDoc 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     /**
209      * Determine all the email addresses to notify for all the groups the given
210      * user is in. TODO: Move this to a business object
211      *
212      * @param inGroups
213      * The user for which to determine the email addresses
214      *
215      * @return All the email addresses
216      */

217     public Set JavaDoc findNotificationEmailAddresses() throws OpenEditException
218     {
219         Set JavaDoc emailAddresses = new HashSet JavaDoc();
220
221         for (Iterator JavaDoc groupIter = getUserManager().getGroups().iterator(); groupIter.hasNext();)
222         {
223             Group group = (Group) groupIter.next();
224
225             if (group.hasPermission(getGroupEmailProperty()))
226             {
227                 // Collect the email addresses of all the users in this group.
228
HitTracker list = getUserManager().getUsersInGroup(group);
229                 for (Iterator JavaDoc userIter = list.iterator(); userIter.hasNext();)
230                 {
231                     User user = (User) userIter.next();
232                     String JavaDoc 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 JavaDoc getDefaultMailTemplate()
246     {
247         return fieldDefaultMailTemplate;
248     }
249
250     public void setDefaultMailTemplate(String JavaDoc inDefaultMailTemplate)
251     {
252         fieldDefaultMailTemplate = inDefaultMailTemplate;
253     }
254
255     /**
256      * DOCUMENT ME!
257      *
258      * @param inLog
259      */

260     public void setChangeLog(ChangeLog inLog)
261     {
262         fieldChangeLog = inLog;
263     }
264
265     /**
266      */

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 JavaDoc fromEmail = notificationConfig.getChildValue("from-email");
287     
288             if (fromEmail != null)
289             {
290                 setFromAddress(fromEmail);
291             }
292     
293             String JavaDoc groupEmailProperty = notificationConfig.getChildValue("group-email-property");
294     
295             if (groupEmailProperty != null)
296             {
297                 setGroupEmailProperty(groupEmailProperty);
298             }
299     
300             String JavaDoc smtpServer = notificationConfig.getChildValue("smtp-server");
301     
302             if (smtpServer != null)
303             {
304                 setSMTPServer(smtpServer);
305             }
306         }
307         catch (Exception JavaDoc 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     /**
322      *
323      */

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