1 4 package za.org.coefficient.modules.mailforum.util; 5 6 import java.util.Collection ; 7 import java.util.HashSet ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 import java.util.Set ; 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 34 public class DBUpdateUtil { 35 47 48 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 footer = settings.getFooterText(); 61 if (currentProject != null) { 65 footer += "\nYou are currently subscribed to " 66 + currentProject.getName() + "\n"; 67 } else { 68 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 96 private static void addChampionsAndSettings() { 97 Collection projects = null; 98 Set projectForums = new HashSet (); 99 100 103 try { 104 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 projectsIterator = projects.iterator(); 120 while (projectsIterator.hasNext()) { 122 Project currentProject = (Project) projectsIterator.next(); 123 Collection forums = null; 124 125 try { 126 forums = getForumList(currentProject); 128 } catch (Exception e) { 129 e.printStackTrace(); 130 } 132 if (forums == null) { 133 return; 134 } 135 136 Iterator forumsIterator = forums.iterator(); 137 while (forumsIterator.hasNext()) { Forum forum = (Forum) forumsIterator.next(); 139 projectForums.add(forum); 140 141 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 if (forum.getNotificationSet() == null 158 || forum.getNotificationSet().size() == 0) { 159 System.out 160 .println("DBUpdateUtil.addChampionsAndSettings() -> updating mail forum notification set"); 161 162 Set emailsToNotify = addNotificationLists(currentProject 163 .getChampionsAsUsers(), forum); 164 165 forum.setNotificationSet(emailsToNotify); 167 try { 168 forum.save(); 169 } catch (Exception e) { 176 e.printStackTrace(); 177 } 178 } 180 } 182 } 183 184 186 190 Set allForumsSet = new HashSet (); 191 System.out 192 .println("DBUpdateUtil.addChampionsAndSettings() -> updating mail forum settings for forum without project........."); 193 try { 194 195 Collection allForums = getAllForums(); 196 Iterator allIterator = allForums.iterator(); 197 198 while (allIterator.hasNext()) { Forum forum = (Forum) allIterator.next(); 200 allForumsSet.add(forum); 201 } 202 203 } catch (PersistenceException p) { 204 p.printStackTrace(); 205 } 206 Iterator allForumsIterator = allForumsSet.iterator(); 207 208 while (allForumsIterator.hasNext()) { 209 Forum forum = (Forum) allForumsIterator.next(); 210 211 if (!projectForums.contains(forum)) { 212 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 235 private static Collection getForumList(Project project) throws Exception { 236 237 Resource resource = ResourceFactory.ensureResourceExistsFor(project); 238 239 Long resourceId = resource.getResourceId(); 240 241 Collection forums = Forum.getForumsForResourceId(resourceId); 243 return forums; 244 } 245 246 257 private static Set addNotificationLists(List projectChampionUserList, 258 Forum forum) { 259 Iterator projectChampionList = projectChampionUserList.iterator(); 260 Set emailsToNotify = new HashSet (); 261 262 while (projectChampionList.hasNext()) { 263 CoefficientUser championUser = (CoefficientUser) projectChampionList 264 .next(); 265 String 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 e) { 273 e.printStackTrace(); 274 } 275 276 emailsToNotify.add(forumChamp); 277 } 278 return emailsToNotify; 279 } 280 281 286 private static Collection getAllProjects() throws PersistenceException { 287 String sqlQuery = "FROM Project"; 288 Collection projects = PersistenceService.find(sqlQuery); 289 return projects; 290 } 291 292 298 private static Collection getAllUsers() throws PersistenceException { 299 String sqlQuery = "FROM CoefficientUser"; 300 Collection projects = PersistenceService.find(sqlQuery); 301 return projects; 302 } 303 304 309 private static Collection getAllMessages() throws PersistenceException { 310 String sqlQuery = "FROM Message "; 311 Collection projects = PersistenceService.find(sqlQuery); 312 return projects; 313 } 314 315 320 private static Collection getAllForums() throws PersistenceException { 321 String sqlQuery = "FROM Forum "; 322 Collection projects = PersistenceService.find(sqlQuery); 323 return projects; 324 } 325 326 private static void convertMessageDates() { 327 328 330 } 331 332 341 private static void addAliasEmailAddresses() { 342 Collection 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 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 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 aliasUsers = null; 379 try { 380 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 } 408 changeUserResourceURI(user); 411 412 } 414 } 416 } 417 418 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 oldURI = userResource.getURI(); 433 int endOfURIPrefix = oldURI.indexOf(":"); 434 String 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 455 public static void updateToNewVersion() { 456 addAliasEmailAddresses(); 457 addChampionsAndSettings(); 458 459 } 460 461 } 462 | Popular Tags |