KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > modules > mailforum > util > DBUpdateUtil


1 /**
2  *
3  */

4 package za.org.coefficient.modules.mailforum.util;
5
6 import java.util.Collection JavaDoc;
7 import java.util.HashSet JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Set JavaDoc;
11 import net.sf.hibernate.HibernateException;
12 import net.sf.hibernate.util.HibernateUtil;
13 import org.dithaka.messaging.AliasUser;
14 import org.dithaka.messaging.Forum;
15 import org.dithaka.messaging.ForumChampion;
16 import org.dithaka.messaging.MailForumSettings;
17 import org.dithaka.metadata.MetadataException;
18 import org.dithaka.metadata.Resource;
19 import org.dithaka.metadata.ResourceFactory;
20 import org.dithaka.persistence.PersistenceException;
21 import org.dithaka.persistence.PersistenceService;
22 import za.org.coefficient.authentication.CoefficientUser;
23 import za.org.coefficient.core.Constants;
24 import za.org.coefficient.core.Project;
25
26 import za.org.coefficient.modules.mailforum.CoefficientUserExpert;
27
28 /**
29  * @author pieter20 Jun 13, 2005 This class is used to convert the old db
30  * structure of Dithaka/MailForum to the new structure. This new
31  * structure contains 3 new classes: MailForumSettings, ForumChampion
32  * and AliasUser.
33  */

34 public class DBUpdateUtil {
35     /*
36      * go through each forum and add the new classes/tables:
37      *
38      * 1.If there is no mail forum settings for this forum create new ones
39      *
40      * 2.If there is no project champion sets for this forum add them
41      *
42      * 3.Add the AliasUsers
43      *
44      * 4.Change the Resource uri to use the alias email address.
45      *
46      */

47
48     /**
49      * @param forum
50      * @param currentProject
51      * Jun 14, 2005 pieter20
52      */

53     private static void addForumSettings(Forum forum, Project currentProject) {
54
55         MailForumSettings settings = new MailForumSettings();
56
57         settings.setListName(forum.getFullName());
58         settings.setSubjectPrefix(forum.getMailAddressSnippet());
59
60         String JavaDoc footer = settings.getFooterText();
61         // TODO: pvz This has to become dynamic and not hardcoded: if droups:
62
// footer +="\n $user, you are currently subscribed to
63
// "+project.getName()+" as: $emailAddress \n";
64
if (currentProject != null) {
65             footer += "\nYou are currently subscribed to "
66                     + currentProject.getName() + "\n";
67         } else {
68             // global_discussion list
69
footer += "\nYou are currently subscribed to global_discussion list\n";
70         }
71         try {
72
73             settings.setFooterText(footer);
74
75             settings.save();
76
77             forum.setMailForumSettings(settings);
78
79             forum.save();
80
81         } catch (PersistenceException p) {
82             p.printStackTrace();
83         } catch (MetadataException m) {
84             m.printStackTrace();
85         }
86
87     }
88
89     /**
90      *
91      * Jun 14, 2005 pieter20 This method updates the forums in Dithaka. It
92      * associates the forum with a notification list of ForumChampions. It also
93      * associates the forum with settings that is stored in the
94      * MailForumSettings class.
95      */

96     private static void addChampionsAndSettings() {
97         Collection JavaDoc projects = null;
98         Set JavaDoc projectForums = new HashSet JavaDoc();
99
100         /*
101          * 1. update forums that are associated with a project
102          */

103         try {
104             // get a list of projects
105
projects = getAllProjects();
106
107         } catch (PersistenceException p) {
108             p.printStackTrace();
109
110         }
111
112         if (projects == null) {
113             return;
114         }
115
116         System.out
117                 .println("DBUpdateUtil.addChampionsAndSettings() -> checking if an update is needed.......");
118
119         Iterator JavaDoc projectsIterator = projects.iterator();
120         while (projectsIterator.hasNext()) {// for each project:
121

122             Project currentProject = (Project) projectsIterator.next();
123             Collection JavaDoc forums = null;
124
125             try {
126                 forums = getForumList(currentProject);// if
127

128             } catch (Exception JavaDoc e) {
129                 e.printStackTrace();
130             }// end of try getting forums
131

132             if (forums == null) {
133                 return;
134             }
135
136             Iterator JavaDoc forumsIterator = forums.iterator();
137             while (forumsIterator.hasNext()) {// for each forum:
138
Forum forum = (Forum) forumsIterator.next();
139                 projectForums.add(forum);
140
141                 // 1.a add MailForumSettings
142
if (forum.getMailForumSettings() == null) {
143                     System.out
144                             .println("DBUpdateUtil.addChampionsAndSettings() -> updating mail forum settings for new db. Project: "
145                                     + currentProject.getName());
146                     System.out
147                             .println("DBUpdateUtil.addChampionsAndSettings() -> updating mail forum settings for new db. Forum: "
148                                     + forum.getFullName());
149                     addForumSettings(forum, currentProject);
150                 }
151
152                 System.out
153                         .println("DBUpdateUtil.addChampionsAndSettings() -> forum.getNotificationSet() size: "
154                                 + forum.getNotificationSet());
155
156                 // 1.b add MailForumChampions:
157
if (forum.getNotificationSet() == null
158                         || forum.getNotificationSet().size() == 0) {
159                     System.out
160                             .println("DBUpdateUtil.addChampionsAndSettings() -> updating mail forum notification set");
161
162                     Set JavaDoc emailsToNotify = addNotificationLists(currentProject
163                             .getChampionsAsUsers(), forum);
164
165                     // add the new values
166
forum.setNotificationSet(emailsToNotify);
167                     try {
168                         forum.save();
169                         // if an error occurs here then we will have saved
170
// ForumChampions that is not associated to any
171
// forum in addNotificationLists
172
// TODO: pvz: Please fix this problem. Test by throwing
173
// an
174
// exception here.
175
} catch (Exception JavaDoc e) {
176                         e.printStackTrace();
177                     }
178                 }// end of adding notification set
179

180             }// end of iterating through forums
181

182         }
183
184         // end of iterating through projects
185

186         /*
187          * 2. update forums that are not associated with a project = the global
188          * discussion lists
189          */

190         Set JavaDoc allForumsSet = new HashSet JavaDoc();
191         System.out
192                 .println("DBUpdateUtil.addChampionsAndSettings() -> updating mail forum settings for forum without project.........");
193         try {
194
195             Collection JavaDoc allForums = getAllForums();
196             Iterator JavaDoc allIterator = allForums.iterator();
197
198             while (allIterator.hasNext()) {// for each forum:
199
Forum forum = (Forum) allIterator.next();
200                 allForumsSet.add(forum);
201             }
202
203         } catch (PersistenceException p) {
204             p.printStackTrace();
205         }
206         Iterator JavaDoc allForumsIterator = allForumsSet.iterator();
207
208         while (allForumsIterator.hasNext()) {
209             Forum forum = (Forum) allForumsIterator.next();
210
211             if (!projectForums.contains(forum)) {
212                 // this forum is not associated with a project
213
if (forum.getMailForumSettings() == null) {
214                     System.out
215                             .println("DBUpdateUtil.addChampionsAndSettings() -> updating mail forum settings for forum without project. Forum: "
216                                     + forum.getFullName());
217
218                     addForumSettings(forum, null);
219                 }
220             }
221         }
222         System.out
223                 .println("DBUpdateUtil.addChampionsAndSettings() -> updating mail forum settings for forum without project: done");
224
225     }
226
227     /**
228      * @param project
229      * @return
230      * @throws Exception
231      * Apr 25, 2005 pieter20
232      *
233      * Returns a collection of forums that are linked to this project
234      */

235     private static Collection JavaDoc getForumList(Project project) throws Exception JavaDoc {
236
237         Resource resource = ResourceFactory.ensureResourceExistsFor(project);
238
239         Long JavaDoc resourceId = resource.getResourceId();
240
241         // get the forums about the resource
242
Collection JavaDoc forums = Forum.getForumsForResourceId(resourceId);
243         return forums;
244     }
245
246     /**
247      * @param projectChampionUserList
248      * @param forum
249      * @return Apr 25, 2005 pieter20
250      *
251      * we need to set the ForumChampion's parent ForumId and for this we need
252      * the forum. If this is not set we will have forumChamp's that do not have
253      * any reference back to the forum. But are saved in the db. Please consult
254      * the Hibernate Reference Manual Ch 16: Parent/Child and one-to-many
255      * relationships
256      */

257     private static Set JavaDoc addNotificationLists(List JavaDoc projectChampionUserList,
258             Forum forum) {
259         Iterator JavaDoc projectChampionList = projectChampionUserList.iterator();
260         Set JavaDoc emailsToNotify = new HashSet JavaDoc();
261
262         while (projectChampionList.hasNext()) {
263             CoefficientUser championUser = (CoefficientUser) projectChampionList
264                     .next();
265             String JavaDoc emailAddress = championUser.getAliasEmail();
266             ForumChampion forumChamp = new ForumChampion();
267             forumChamp.setEMailAdrress(emailAddress);
268             forumChamp.setForum(forum);
269
270             try {
271                 forumChamp.save();
272             } catch (Exception JavaDoc e) {
273                 e.printStackTrace();
274             }
275
276             emailsToNotify.add(forumChamp);
277         }
278         return emailsToNotify;
279     }
280
281     /**
282      * @return
283      * @throws PersistenceException
284      * Jun 14, 2005 pieter20 This method returns all the Project's
285      */

286     private static Collection JavaDoc getAllProjects() throws PersistenceException {
287         String JavaDoc sqlQuery = "FROM Project";
288         Collection JavaDoc projects = PersistenceService.find(sqlQuery);
289         return projects;
290     }
291
292     /**
293      * @return
294      * @throws PersistenceException
295      * Jun 14, 2005 pieter20 This method returns all the
296      * CoefficientUser's
297      */

298     private static Collection JavaDoc getAllUsers() throws PersistenceException {
299         String JavaDoc sqlQuery = "FROM CoefficientUser";
300         Collection JavaDoc projects = PersistenceService.find(sqlQuery);
301         return projects;
302     }
303
304     /**
305      * @return
306      * @throws PersistenceException
307      * Jun 14, 2005 pieter20 This method returns all the Message's
308      */

309     private static Collection JavaDoc getAllMessages() throws PersistenceException {
310         String JavaDoc sqlQuery = "FROM Message ";
311         Collection JavaDoc projects = PersistenceService.find(sqlQuery);
312         return projects;
313     }
314
315     /**
316      * @return
317      * @throws PersistenceException
318      * Jun 15, 2005 pieter20 This method returns all the Forum's
319      */

320     private static Collection JavaDoc getAllForums() throws PersistenceException {
321         String JavaDoc sqlQuery = "FROM Forum ";
322         Collection JavaDoc projects = PersistenceService.find(sqlQuery);
323         return projects;
324     }
325
326     private static void convertMessageDates() {
327
328         // Collection messages = getAllMessages();
329

330     }
331
332     /**
333      *
334      * Jun 14, 2005 pieter20
335      * Add the new alias email address to the user, and creates a
336      * new AliasUser object in the system.
337      *
338      * The user resource entry is also updated to use the alias email
339      * address.
340      */

341     private static void addAliasEmailAddresses() {
342         Collection JavaDoc users = null;
343
344         try {
345             users = getAllUsers();
346         } catch (PersistenceException p) {
347             p.printStackTrace();
348         }
349
350         if (users == null) {
351             return;
352         }
353         System.out
354                 .println("DBUpdateUtil.addAliasEmailAddresses() -> checking if an update is needed.2......");
355
356         Iterator JavaDoc userIterator = users.iterator();
357
358         while (userIterator.hasNext()) {
359             CoefficientUser user = (CoefficientUser) userIterator.next();
360
361             if (user.getAliasEmail() == null || user.getAliasEmail() == "") {
362                 System.out
363                         .println("DBUpdateUtil.addAliasEmailAddresses()-> alias email address does not exist");
364                 // the alias has not been set! This could happen when moving
365
// from an old to a new coefficient (0.9.5-1 to new version)
366
user.setAliasEmail(user.getUserName() + "@"
367                         + Constants.ALIAS_MAIL_HOST_ADDRESS);
368                 try {
369                     HibernateUtil.saveOrUpdate(user);
370                 } catch (HibernateException he) {
371                     he.printStackTrace();
372                 }
373
374                 System.out
375                         .println("DBUpdateUtil.addAliasEmailAddresses()-> new alias email address: "
376                                 + user.getAliasEmail());
377
378                 Collection JavaDoc aliasUsers = null;
379                 try {
380                     // add the user to as an alias
381
aliasUsers = AliasUser.getAliasEmailForUserName(user
382                             .getUserName());
383
384                 } catch (PersistenceException p) {
385                     p.printStackTrace();
386                 }
387
388                 if (aliasUsers == null || aliasUsers.size() == 0) {
389                     System.out
390                             .println("DBUpdateUtil.addAliasEmailAddresses() -> adding user to alias user table");
391
392                     System.out
393                             .println("DBUpdateUtil.addAliasEmailAddresses() -> user: "
394                                     + user);
395                     AliasUser aliasUser = new AliasUser();
396                     aliasUser.setUserName(user.getUserName());
397                     aliasUser.setAliasEmailAddress(user.getAliasEmail());
398                     aliasUser.setRealEmailAddress(user.getEmail());
399                     try {
400                         aliasUser.save();
401                     } catch (PersistenceException p) {
402                         p.printStackTrace();
403                     } catch (MetadataException m) {
404                         m.printStackTrace();
405                     }
406                 }// end if alias does not exist
407

408                 // change the user resource uri using the new alias email
409
// address.
410
changeUserResourceURI(user);
411
412             }// end if users alias email is not set
413

414         }// end while users
415

416     }
417
418     /**
419      * @param user
420      * Jun 15, 2005 pieter20 This method converts the old user
421      * resource uri to a new one that uses the alias email address
422      * instead of the real email address.
423      */

424     private static void changeUserResourceURI(CoefficientUser user) {
425
426         try {
427             Resource userResource = ResourceFactory
428                     .ensureResourceExistsFor(user);
429
430             CoefficientUserExpert userExpert = new CoefficientUserExpert();
431
432             String JavaDoc oldURI = userResource.getURI();
433             int endOfURIPrefix = oldURI.indexOf(":");
434             String JavaDoc uriPrefix = oldURI.substring(0, endOfURIPrefix);
435             System.out
436                     .println("DBUpdateUtil.changeUserResourceURI() ->Changing the user resource uri. uriPrefix: "
437                             + uriPrefix);
438             userResource.setURI(uriPrefix + ":" + user.getAliasEmail());
439             userResource.save();
440
441         } catch (MetadataException m) {
442             m.printStackTrace();
443         }
444
445     }
446
447     /**
448      *
449      * Jun 15, 2005 pieter20 This method controls what updates needs to be made
450      * to move over from an old version of the MailForum and Dithaka to a new
451      * version.
452      *
453      * If there are other changes in the future they can be added here.
454      */

455     public static void updateToNewVersion() {
456         addAliasEmailAddresses();
457         addChampionsAndSettings();
458
459     }
460
461 }
462
Popular Tags