1 88 package com.ivata.groupware.business.library; 89 90 import java.text.MessageFormat ; 91 import java.util.Collections ; 92 import java.util.Comparator ; 93 import java.util.Iterator ; 94 import java.util.List ; 95 import java.util.Vector ; 96 97 import javax.mail.MethodNotSupportedException ; 98 99 import org.apache.log4j.Logger; 100 101 import com.ivata.groupware.admin.security.server.SecuritySession; 102 import com.ivata.groupware.admin.security.user.UserDO; 103 import com.ivata.groupware.admin.setting.Settings; 104 import com.ivata.groupware.business.BusinessLogic; 105 import com.ivata.groupware.business.addressbook.AddressBook; 106 import com.ivata.groupware.business.addressbook.person.PersonDO; 107 import com.ivata.groupware.business.library.comment.CommentDO; 108 import com.ivata.groupware.business.library.faq.FAQDO; 109 import com.ivata.groupware.business.library.faq.category.FAQCategoryDO; 110 import com.ivata.groupware.business.library.item.LibraryItemConstants; 111 import com.ivata.groupware.business.library.item.LibraryItemDO; 112 import com.ivata.groupware.business.library.page.PageDO; 113 import com.ivata.groupware.business.library.topic.TopicDO; 114 import com.ivata.groupware.business.mail.Mail; 115 import com.ivata.groupware.business.mail.session.MailSession; 116 import com.ivata.groupware.business.search.SearchEngine; 117 import com.ivata.groupware.container.persistence.QueryPersistenceManager; 118 import com.ivata.groupware.container.persistence.TimestampDOHandling; 119 import com.ivata.groupware.web.format.SanitizerFormat; 120 import com.ivata.mask.Mask; 121 import com.ivata.mask.MaskFactory; 122 import com.ivata.mask.persistence.PersistenceException; 123 import com.ivata.mask.persistence.PersistenceSession; 124 import com.ivata.mask.util.StringHandling; 125 import com.ivata.mask.util.SystemException; 126 import com.ivata.mask.validation.ValidationError; 127 import com.ivata.mask.validation.ValidationErrors; 128 import com.ivata.mask.validation.ValidationException; 129 import com.ivata.mask.web.format.CharacterEntityFormat; 130 import com.ivata.mask.web.format.FormatConstants; 131 import com.ivata.mask.web.format.HTMLFormatter; 132 import com.ivata.mask.web.format.LineBreakFormat; 133 134 140 public class LibraryImpl extends BusinessLogic implements Library { 141 142 143 146 private class CommentComparator implements Comparator { 147 148 151 public int compare(final Object o1, 152 final Object o2) { 153 CommentDO comment1 = (CommentDO) o1; 154 CommentDO comment2 = (CommentDO) o2; 155 long test1 = comment1.getModified().getTime(); 156 long test2 = comment2.getModified().getTime(); 157 158 return (test1 > test2) ? -1 : 1; 161 } 162 } 163 164 165 168 private class LibraryItemComparator implements Comparator { 169 170 184 public int compare(final Object o1, 185 final Object o2) { 186 LibraryItemDO itemRight1 = (LibraryItemDO) o1; 187 LibraryItemDO itemRight2 = (LibraryItemDO) o2; 188 long test1 = itemRight1.getModified().getTime(); 189 long test2 = itemRight2.getModified().getTime(); 190 191 return (test1 > test2) ? -1 : 1; 194 } 195 } 196 201 private static Logger log = Logger.getLogger(Library.class); 202 private AddressBook addressBook; 203 private HTMLFormatter formatter; 204 private Mail mail; 205 208 private QueryPersistenceManager persistenceManager; 209 private SearchEngine searchEngine; 210 private Settings settings; 211 private MaskFactory maskFactory; 212 217 public LibraryImpl(QueryPersistenceManager persistenceManager, 218 AddressBook addressBook, 219 Mail mail, 220 Settings settings, 221 SearchEngine searchEngine, 222 HTMLFormatter formatter, 223 MaskFactory maskFactory) { 224 this.persistenceManager = persistenceManager; 225 this.addressBook = addressBook; 226 this.mail = mail; 227 this.settings = settings; 228 this.searchEngine = searchEngine; 229 this.formatter = formatter; 230 this.maskFactory = maskFactory; 231 } 232 233 251 public CommentDO addComment(final SecuritySession securitySession, 252 final CommentDO commentParam) 253 throws SystemException { 254 PersistenceSession persistenceSession = persistenceManager.openSession(securitySession); 255 try { 256 ValidationErrors errors = validate(securitySession, commentParam); 258 if (!errors.isEmpty()) { 259 throw new ValidationException(errors); 260 } 261 TimestampDOHandling.add(securitySession, commentParam); 262 CommentDO comment = (CommentDO) persistenceManager.add( 263 persistenceSession, 264 commentParam); 265 266 LibraryItemDO item = comment.getItem(); 267 searchEngine.updateIndex(securitySession, 268 item.getId(), 269 LibraryItemDO.class.getName(), 270 item.getTopic().getId().toString(), 271 comment.getId(), 272 CommentDO.class.getName(), 273 comment.getText(), 274 comment.getFormat()); 275 return comment; 276 } catch (Exception e) { 277 persistenceSession.cancel(); 278 throw new SystemException(e); 279 } finally { 280 persistenceSession.close(); 281 } 282 } 283 310 public LibraryItemDO addItem(final SecuritySession securitySession, 311 final LibraryItemDO itemParam, 312 final String comment) 313 throws SystemException { 314 LibraryItemDO item = itemParam; 315 PersistenceSession persistenceSession = persistenceManager.openSession(securitySession); 316 try { 317 ValidationErrors errors = validate(securitySession, item); 319 if (!errors.isEmpty()) { 320 throw new ValidationException(errors); 321 } 322 323 if (item.getPages() != null) { 325 Iterator pagesIterator = item.getPages().iterator(); 326 int pageNumber = 0; 327 while (pagesIterator.hasNext()) { 328 PageDO page = (PageDO) pagesIterator.next(); 329 page.setId(null); 331 page.setNumber(new Integer (pageNumber++)); 332 } 333 } 334 335 TimestampDOHandling.add(securitySession, item); 336 item = (LibraryItemDO) persistenceManager.add(persistenceSession, item); 337 updateSearchIndexForItem(securitySession, item); 338 339 381 } catch (Exception e) { 382 persistenceSession.cancel(); 383 throw new SystemException(e); 384 } finally { 385 persistenceSession.close(); 386 } 387 notifyForSubmission(securitySession, item, true); 388 return item; 389 } 390 406 public TopicDO addTopic(final SecuritySession securitySession, 407 final TopicDO topic) 408 throws SystemException { 409 PersistenceSession persistenceSession = persistenceManager.openSession(securitySession); 410 try { 411 ValidationErrors errors = validate(securitySession, topic); 413 if (!errors.isEmpty()) { 414 throw new ValidationException(errors); 415 } 416 return (TopicDO) persistenceManager.add(persistenceSession, topic); 417 } catch (Exception e) { 418 persistenceSession.cancel(); 419 throw new SystemException(e); 420 } finally { 421 persistenceSession.close(); 422 } 423 } 424 425 443 public CommentDO amendComment(final SecuritySession securitySession, 444 final CommentDO comment) 445 throws SystemException { 446 PersistenceSession persistenceSession = persistenceManager.openSession(securitySession); 447 try { 448 ValidationErrors errors = validate(securitySession, comment); 450 if (!errors.isEmpty()) { 451 throw new ValidationException(errors); 452 } 453 TimestampDOHandling.amend(securitySession, comment); 454 persistenceManager.amend(persistenceSession, comment); 455 456 LibraryItemDO item = comment.getItem(); 457 searchEngine.updateIndex(securitySession, item.getId(), 458 LibraryItemDO.class.getName(), 459 item.getTopic().getId().toString(), 460 comment.getId(), 461 CommentDO.class.getName(), 462 comment.getText(), 463 comment.getFormat()); 464 return comment; 465 } catch (Exception e) { 466 persistenceSession.cancel(); 467 throw new SystemException(e); 468 } finally { 469 persistenceSession.close(); 470 } 471 } 472 499 public LibraryItemDO amendItem(final SecuritySession securitySession, 500 final LibraryItemDO item, 501 final String comment) 502 throws SystemException { 503 PersistenceSession persistenceSession = persistenceManager.openSession(securitySession); 504 try { 505 ValidationErrors errors = validate(securitySession, item); 507 if (!errors.isEmpty()) { 508 throw new ValidationException(errors); 509 } 510 if (item.getPages() != null) { 512 Iterator pagesIterator = item.getPages().iterator(); 513 int pageNumber = 0; 514 while (pagesIterator.hasNext()) { 515 PageDO page = (PageDO) pagesIterator.next(); 516 page.setNumber(new Integer (pageNumber++)); 517 } 518 } 519 TimestampDOHandling.amend(securitySession, item); 520 persistenceManager.amend(persistenceSession, item); 521 updateSearchIndexForItem(securitySession, item); 522 523 557 558 notifyForSubmission(securitySession, item, false); 559 return item; 560 } catch (Exception e) { 561 persistenceSession.cancel(); 562 throw new SystemException(e); 563 } finally { 564 persistenceSession.close(); 565 } 566 } 567 582 public TopicDO amendTopic(final SecuritySession securitySession, 583 final TopicDO topic) 584 throws SystemException { 585 PersistenceSession persistenceSession = persistenceManager.openSession(securitySession); 586 try { 587 ValidationErrors errors = validate(securitySession, topic); 589 if (!errors.isEmpty()) { 590 throw new ValidationException(errors); 591 } 592 persistenceManager.amend(persistenceSession, topic); 593 return topic; 594 } catch (Exception e) { 595 persistenceSession.cancel(); 596 throw new SystemException(e); 597 } finally { 598 persistenceSession.close(); 599 } 600 } 601 602 610 public int countCommentsForItem(final SecuritySession securitySession, 611 final Integer itemId) 612 throws SystemException { 613 PersistenceSession persistenceSession = persistenceManager.openSession(securitySession); 614 try { 615 Integer count = persistenceManager.findInteger(persistenceSession, 616 "libraryCountCommentByItemId", 617 new Object [] {itemId}); 618 return (count == null) ? 0 : count.intValue(); 619 } catch (Exception e) { 620 persistenceSession.cancel(); 621 throw new SystemException(e); 622 } finally { 623 persistenceSession.close(); 624 } 625 } 626 635 public List findCommentByItem(final 636 SecuritySession securitySession, 637 final Integer itemId) 638 throws SystemException { 639 PersistenceSession persistenceSession = persistenceManager.openSession(securitySession); 640 try { 641 return persistenceManager.find(persistenceSession, 642 "libraryCommentByItemId", 643 new Object [] {itemId}); 644 } catch (Exception e) { 645 persistenceSession.cancel(); 646 throw new SystemException(e); 647 } finally { 648 persistenceSession.close(); 649 } 650 } 651 658 public List findCommentByParent(final 659 SecuritySession securitySession, 660 final Integer parentId) 661 throws SystemException { 662 PersistenceSession persistenceSession = persistenceManager.openSession(securitySession); 663 try { 664 return persistenceManager.find(persistenceSession, 665 "libraryCommentByParentId", 666 new Object [] {parentId}); 667 } catch (Exception e) { 668 persistenceSession.cancel(); 669 throw new SystemException(e); 670 } finally { 671 persistenceSession.close(); 672 } 673 } 674 675 685 public CommentDO findCommentByPrimaryKey(final SecuritySession securitySession, 686 final Integer id) 687 throws SystemException { 688 PersistenceSession persistenceSession = persistenceManager.openSession(securitySession); 689 try { 690 return (CommentDO) persistenceManager.findByPrimaryKey(persistenceSession, 691 CommentDO.class, id); 692 } catch (Exception e) { 693 persistenceSession.cancel(); 694 throw new SystemException(e); 695 } finally { 696 persistenceSession.close(); 697 } 698 } 699 700 707 public LibraryItemDO findItemByPrimaryKey(final SecuritySession securitySession, 708 final Integer id) 709 throws SystemException { 710 PersistenceSession persistenceSession = persistenceManager.openSession(securitySession); 711 try { 712 return (LibraryItemDO) persistenceManager.findByPrimaryKey(persistenceSession, 713 LibraryItemDO.class, id); 714 } catch (Exception e) { 715 persistenceSession.cancel(); 716 throw new SystemException(e); 717 } finally { 718 persistenceSession.close(); 719 } 720 } 721 722 729 public LibraryItemDO findItemByRevision(final SecuritySession securitySession, 730 final String itemId, 731 final String rev) 732 throws SystemException { 733 PersistenceSession persistenceSession = persistenceManager.openSession(securitySession); 734 try { 735 LibraryItemDO item = (LibraryItemDO) 737 persistenceManager.findByPrimaryKey(persistenceSession, 738 LibraryItemDO.class, itemId); 739 740 754 return item; 755 } catch (Exception e) { 756 persistenceSession.cancel(); 757 throw new SystemException(e); 758 } finally { 759 persistenceSession.close(); 760 } 761 } 762 763 785 public List findRecentItems(final SecuritySession securitySession, 786 final Integer count, 787 final Integer access, 788 final Integer topicId) 789 throws SystemException { 790 PersistenceSession persistenceSession = persistenceManager.openSession(securitySession); 791 try { 792 if (topicId == null) { 793 return Collections.synchronizedList(persistenceManager.find(persistenceSession, 794 "libraryItemRecent", new Object []{}, count, new Integer (0))); 795 } else { 796 return Collections.synchronizedList(persistenceManager.find(persistenceSession, 797 "libraryItemRecentByTopic", new Object []{topicId}, count, 798 new Integer (0))); 799 } 800 } catch (Exception e) { 801 persistenceSession.cancel(); 802 throw new SystemException(e); 803 } finally { 804 persistenceSession.close(); 805 } 806 } 807 812 837 838 844 public TopicDO findTopicByPrimaryKey(final SecuritySession securitySession, 845 final Integer id) 846 throws SystemException { 847 PersistenceSession persistenceSession = persistenceManager.openSession(securitySession); 848 try { 849 return (TopicDO) persistenceManager.findByPrimaryKey(persistenceSession, 850 TopicDO.class, id); 851 } catch (Exception e) { 852 persistenceSession.cancel(); 853 throw new SystemException(e); 854 } finally { 855 persistenceSession.close(); 856 } 857 } 858 859 878 public List findTopics(final SecuritySession securitySession) 879 throws SystemException { 880 PersistenceSession persistenceSession = persistenceManager.openSession(securitySession); 881 try { 882 return Collections.synchronizedList(persistenceManager.findAll(persistenceSession, 883 TopicDO.class)); 884 } catch (Exception e) { 885 persistenceSession.cancel(); 886 throw new SystemException(e); 887 } finally { 888 persistenceSession.close(); 889 } 890 } 891 892 898 public List findUnacknowledgedComments(final SecuritySession securitySession, 899 final Integer count) 900 throws SystemException { 901 PersistenceSession persistenceSession = persistenceManager.openSession(securitySession); 902 try { 903 return Collections.synchronizedList( 904 persistenceManager.find(persistenceSession, 905 "libraryCommentByUserNameUnacknowledged", 906 new Object [] {securitySession.getUser().getName(), 907 Boolean.FALSE}, count, new Integer (0))); 908 } catch (Exception e) { 909 persistenceSession.cancel(); 910 throw new SystemException(e); 911 } finally { 912 persistenceSession.close(); 913 } 914 } 915 916 928 private void notifyForSubmission(final SecuritySession securitySession, 929 final Object submission, 930 final boolean isNew) 931 throws NotificationException, PersistenceException { 932 if (securitySession == null) { 934 return; 937 } 938 PersistenceSession persistenceSession = 939 persistenceManager.openSession(securitySession); 940 String senderAddress = null; 941 List recipientAddresses = null; 942 List to = null; 943 List cc = null; 944 try { 945 UserDO user = securitySession.getUser(); 946 947 String URL = settings.getStringSetting(securitySession, 948 "pathContext", user); 949 950 Integer topicId; UserDO originalAuthor = null; UserDO author; senderAddress = "\"ivata groupware library\" <" + settings.getStringSetting(securitySession, 955 "emailLibrary", 956 user) + ">"; 957 String content; String subject; 960 if (LibraryItemDO.class.isInstance(submission)) { 963 LibraryItemDO item = (LibraryItemDO) submission; 964 String settingName = "libraryNotificationContentItem"; 965 966 topicId = item.getTopic().getId(); 967 author = item.getCreatedBy(); 968 subject = item.getTitle(); 969 URL += "/display.jsp?id=" + item.getId(); 970 if (!isNew) { 973 settingName += "Amend"; 974 author = item.getModifiedBy(); 975 } 976 String [] arguments = { 977 author.getName(), 978 item.getTitle(), 979 item.getSummary(), 980 URL}; 981 String setting = settings.getStringSetting(securitySession, 982 settingName, user); 983 984 if (setting == null) { 985 throw new SystemException("ERROR in Library.notifyForSubmission: " 986 + "the administrator must set the system setting '" 987 + settingName 988 + "'"); 989 } 990 content = MessageFormat.format(setting, arguments); 991 } else { 992 if (!CommentDO.class.isInstance(submission)) { 994 throw new SystemException("ERROR in Library: cannot notify on " 995 + "submission of class '" 996 + submission.getClass() 997 + "'"); 998 } 999 CommentDO comment = (CommentDO) submission; 1000 1001 topicId = comment.getItem().getTopic().getId(); 1002 author = comment.getCreatedBy(); 1003 subject = comment.getSubject(); 1004 1005 String settingName, replyTitle, replySummary, commentText; 1008 1009 int formatOfOrigComment = -1; 1010 int formatOfComment = comment.getFormat(); 1011 1012 if (comment.getParent() != null) { 1013 originalAuthor = comment.getParent().getCreatedBy(); 1014 settingName = "libraryNotificationContentComment"; 1015 replyTitle = comment.getParent().getSubject(); 1016 replySummary = comment.getParent().getText(); 1017 formatOfOrigComment = comment.getParent().getFormat(); 1018 if (!isNew) { 1021 settingName += "Amend"; 1022 if (!author.getId().equals(user.getId())) { 1023 settingName += "Strange"; 1024 author = user; 1025 originalAuthor = comment.getCreatedBy(); 1026 replyTitle = comment.getSubject(); 1027 } 1028 } 1029 } else { 1030 originalAuthor = comment.getItem().getCreatedBy(); 1033 settingName = "libraryNotificationContentItemReply"; 1034 replyTitle = comment.getItem().getTitle(); 1035 replySummary = comment.getItem().getSummary(); 1036 formatOfOrigComment = FormatConstants.FORMAT_HTML; 1037 if (!isNew) { 1039 settingName += "Amend"; 1040 if (!author.getId().equals(user.getId())) { 1041 author = user; 1042 settingName = "libraryNotificationContentCommentAmendStrange"; 1043 originalAuthor = comment.getCreatedBy(); 1044 replyTitle = comment.getSubject(); 1045 } 1046 } 1047 } 1048 commentText = comment.getText(); 1049 LineBreakFormat lineBreakFormat = new LineBreakFormat(); 1050 1051 lineBreakFormat.setConvertLineBreaks(true); 1052 formatter.add(new CharacterEntityFormat()); 1053 formatter.add(lineBreakFormat); 1054 if (formatOfComment == FormatConstants.FORMAT_TEXT) { 1055 commentText = formatter.format(commentText); 1057 } 1058 if (formatOfOrigComment == FormatConstants.FORMAT_TEXT) { 1059 replySummary = formatter.format(replySummary); 1061 } 1062 URL += "/display.jsp?id=" + comment.getItem().getId() + "#comments"; 1063 String [] arguments = { 1064 author.getName(), 1065 originalAuthor.getName(), 1066 replyTitle, 1067 replySummary, 1068 comment.getSubject(), 1069 commentText, URL}; 1070 String setting = settings.getStringSetting(securitySession, 1071 settingName, 1072 user); 1073 1074 if (setting == null) { 1075 throw new SystemException("ERROR in Library.notifyForSubmission: " 1076 + "the administrator must set the system setting '" 1077 + settingName 1078 + "'"); 1079 } 1080 content = MessageFormat.format(setting, arguments); 1081 } 1082 List recipients = persistenceManager.findAll(persistenceSession, 1086 UserDO.class); 1087 1088 1089 recipients.remove(author); 1091 PersonDO originalAuthorPerson = null; 1093 if (originalAuthor != null) { 1094 recipients.remove(originalAuthor); 1095 originalAuthorPerson = addressBook.findPersonByUserName(securitySession, 1096 originalAuthor.getName()); 1097 } 1098 recipientAddresses = new Vector (recipients.size()); 1101 1102 for (Iterator i = recipients.iterator(); i.hasNext();) { 1103 UserDO thisUser = (UserDO) i.next(); 1104 PersonDO thisPerson = addressBook.findPersonByUserName(securitySession, 1106 thisUser.getName()); 1107 String address = thisPerson.getEmailAddress(); 1108 1109 if (user.isEnabled() 1110 && !user.isDeleted() 1111 && (address != null)) { 1112 recipientAddresses.add(address); 1113 } 1114 } 1115 String address; 1116 1117 if ((originalAuthor != null) && 1120 (!originalAuthor.isDeleted()) && 1121 ((address = originalAuthorPerson.getEmailAddress()) != null)) { 1122 to = new Vector (); 1123 to.add(address); 1124 cc = recipientAddresses; 1125 } else { 1126 to = recipientAddresses; 1127 cc = null; 1128 } 1129 String contentType = settings.getStringSetting(securitySession, 1130 "libraryNotificationContentType", user); 1131 if (!to.isEmpty() 1132 && (securitySession instanceof MailSession)) { 1133 mail.send((MailSession) securitySession, senderAddress, to, cc, 1134 null, subject, content, contentType, false); 1135 } 1136 } catch (Exception e) { 1137 persistenceSession.cancel(); 1138 List allRecipients = new Vector (); 1139 if (to != null) { 1140 allRecipients.addAll(to); 1141 } 1142 if (cc != null) { 1143 allRecipients.addAll(cc); 1144 } 1145 throw new NotificationException(e, senderAddress, allRecipients); 1146 } finally { 1147 persistenceSession.close(); 1148 } 1149 } 1150 1164 public void removeComment(final SecuritySession securitySession, 1165 final CommentDO comment) 1166 throws SystemException { 1167 PersistenceSession persistenceSession = persistenceManager.openSession(securitySession); 1168 try { 1169 persistenceManager.remove(persistenceSession, 1170 comment.getClass(), comment.getId()); 1171 } catch (Exception e) { 1172 persistenceSession.cancel(); 1173 throw new SystemException(e); 1174 } finally { 1175 persistenceSession.close(); 1176 } 1177 } 1178 1190 public void removeItem(final SecuritySession securitySession, 1191 final LibraryItemDO item) 1192 throws SystemException { 1193 PersistenceSession persistenceSession = persistenceManager.openSession(securitySession); 1194 try { 1195 1200 persistenceManager.remove(persistenceSession, 1201 item.getClass(), item.getId()); 1202 1203 1214 } catch (Exception e) { 1215 persistenceSession.cancel(); 1216 throw new SystemException(e); 1217 } finally { 1218 persistenceSession.close(); 1219 } 1220 } 1221 1222 1235 public void removeTopic(final SecuritySession securitySession, 1236 final TopicDO topic) 1237 throws SystemException { 1238 PersistenceSession persistenceSession = persistenceManager.openSession(securitySession); 1239 try { 1240 persistenceManager.remove(persistenceSession, 1241 topic.getClass(), topic.getId()); 1242 } catch (Exception e) { 1243 persistenceSession.cancel(); 1244 throw new SystemException(e); 1245 } finally { 1246 persistenceSession.close(); 1247 } 1248 } 1249 1259 public LibraryItemDO revertItemToRevision(final SecuritySession securitySession, 1260 final String itemId, 1261 final String revision, 1262 final String comment) 1263 throws SystemException { 1264 throw new SystemException(new MethodNotSupportedException ("ERROR: revertItemToRevision not implemented.")); 1265 1353 } 1354 1355 1363 public void sanitize(SecuritySession securitySession) 1364 throws SystemException { 1365 PersistenceSession persistenceSession = persistenceManager 1366 .openSession(securitySession); 1367 SanitizerFormat sanitizer = new SanitizerFormat(); 1368 sanitizer.setOnlyBodyContents(true); 1369 1370 try { 1371 List allItems = Collections.synchronizedList(persistenceManager.findAll(persistenceSession, 1372 LibraryItemDO.class)); 1373 Iterator itemIterator = allItems.iterator(); 1374 while (itemIterator.hasNext()) { 1375 sanitizeItem((LibraryItemDO) itemIterator.next(), persistenceSession, 1376 sanitizer); 1377 } 1378 List allComments = Collections.synchronizedList(persistenceManager.findAll(persistenceSession, 1379 CommentDO.class)); 1380 Iterator commentIterator = allComments.iterator(); 1381 List commentsToRemove = new Vector (); 1382 while (commentIterator.hasNext()) { 1383 CommentDO comment = (CommentDO) commentIterator.next(); 1384 if (comment.getFormat() == FormatConstants.FORMAT_HTML) { 1385 if (log.isDebugEnabled()) { 1386 log.debug("Sanitize text for comment id " 1387 + comment.getId() 1388 + " - subject '" 1389 + comment.getSubject() 1390 + "'"); 1391 } 1392 String newText = sanitizer.format(comment.getText()); 1393 if (StringHandling.isNullOrEmpty(newText)) { 1394 commentsToRemove.add(comment); 1395 } else { 1396 comment.setText(newText); 1397 persistenceManager.amend(persistenceSession, comment); 1398 } 1399 } else { 1400 if (log.isDebugEnabled()) { 1401 log.debug("Ignoring non-HTML text for comment id " 1402 + comment.getId() 1403 + " - subject '" 1404 + comment.getSubject() 1405 + "'"); 1406 } 1407 1408 } 1409 } 1410 commentIterator = commentsToRemove.iterator(); 1411 while (commentIterator.hasNext()) { 1412 CommentDO comment = (CommentDO) commentIterator.next(); 1413 persistenceManager.remove(persistenceSession, comment); 1414 } 1415 } catch (Exception e) { 1416 persistenceSession.cancel(); 1417 throw new SystemException(e); 1418 } finally { 1419 persistenceSession.close(); 1420 } 1421 1422 } 1423 1424 1433 private void sanitizeItem(final LibraryItemDO item, 1434 final PersistenceSession persistenceSession, 1435 final SanitizerFormat sanitizer) 1436 throws PersistenceException { 1437 if (log.isDebugEnabled()) { 1438 log.debug("Sanitize text for item id " 1439 + item.getId() 1440 + " - title '" 1441 + item.getTitle() 1442 + "'"); 1443 } 1444 item.setSummary(sanitizer.format(item.getSummary())); 1445 1446 if (item.getPages() != null) { 1448 Iterator pagesIterator = item.getPages().iterator(); 1449 int pageNumber = 0; 1451 List pagesToRemove = new Vector (); 1452 while (pagesIterator.hasNext()) { 1453 if (log.isDebugEnabled()) { 1454 log.debug("Sanitize text for item id " 1455 + item.getId() 1456 + " - page " 1457 + ++pageNumber); 1458 } 1459 PageDO page = (PageDO) pagesIterator.next(); 1460 String newText = sanitizer.format(page.getText()); 1461 if (StringHandling.isNullOrEmpty(newText)) { 1463 pagesToRemove.add(page); 1464 } else { 1465 page.setText(newText); 1466 persistenceManager.amend(persistenceSession, page); 1467 } 1468 } 1469 pagesIterator = pagesToRemove.iterator(); 1470 while(pagesIterator.hasNext()) { 1471 PageDO page = (PageDO) pagesIterator.next(); 1472 persistenceManager.remove(persistenceSession, page); 1473 item.getPages().remove(page); 1474 } 1475 1476 } 1477 if (item.getFAQCategories() != null) { 1479 Iterator fAQCategoriesIterator = item.getFAQCategories().iterator(); 1480 while (fAQCategoriesIterator.hasNext()) { 1481 FAQCategoryDO faqCategory = (FAQCategoryDO) fAQCategoriesIterator.next(); 1482 1483 if (log.isDebugEnabled()) { 1484 log.debug("Sanitize text for item id " 1485 + item.getId() 1486 + " - faq category " 1487 + faqCategory.getName()); 1488 } 1489 faqCategory.setDescription(sanitizer.format(faqCategory.getDescription())); 1490 1491 1492 if (faqCategory.getFAQs() != null) { 1493 Iterator faqIterator = faqCategory.getFAQs().iterator(); 1494 while (faqIterator.hasNext()) { 1495 FAQDO faq = (FAQDO) faqIterator.next(); 1496 faq.setAnswer(sanitizer.format(faq.getAnswer())); 1497 persistenceManager.amend(persistenceSession, faq); 1498 } 1499 } 1500 persistenceManager.amend(persistenceSession, faqCategory); 1501 } 1502 } 1503 persistenceManager.amend(persistenceSession, item); 1504 1505 } 1506 1507 1513 public void updateSearchIndex (SecuritySession securitySession) 1514 throws SystemException { 1515 PersistenceSession persistenceSession = persistenceManager 1516 .openSession(securitySession); 1517 try { 1518 List allItems = Collections.synchronizedList(persistenceManager.findAll(persistenceSession, 1519 LibraryItemDO.class)); 1520 Iterator itemIterator = allItems.iterator(); 1521 while (itemIterator.hasNext()) { 1522 updateSearchIndexForItem(securitySession, 1523 (LibraryItemDO) itemIterator.next()); 1524 } 1525 List allComments = Collections.synchronizedList(persistenceManager.findAll(persistenceSession, 1526 CommentDO.class)); 1527 Iterator commentIterator = allComments.iterator(); 1528 while (commentIterator.hasNext()) { 1529 CommentDO comment = (CommentDO) commentIterator.next(); 1530 LibraryItemDO item = comment.getItem(); 1531 searchEngine.updateIndex(securitySession, item.getId(), 1532 LibraryItemDO.class.getName(), 1533 item.getTopic().getId().toString(), 1534 comment.getId(), 1535 CommentDO.class.getName(), 1536 comment.getText(), 1537 comment.getFormat()); 1538 } 1539 } catch (Exception e) { 1540 persistenceSession.cancel(); 1541 throw new SystemException(e); 1542 } finally { 1543 persistenceSession.close(); 1544 } 1545 } 1546 1547 1558 private void updateSearchIndexForItem(final SecuritySession securitySession, 1559 final LibraryItemDO item) 1560 throws SystemException { 1561 if (log.isDebugEnabled()) { 1562 log.debug("Update search index for item id " 1563 + item.getId() 1564 + " - title '" 1565 + item.getTitle() 1566 + "'"); 1567 } 1568 1569 searchEngine.updateIndex(securitySession, item.getId(), 1571 LibraryItemDO.class.getName(), 1572 item.getTopic().getId().toString(), 1573 item.getId(), 1574 LibraryItemDO.class.getName(), 1575 item.getTitle() 1576 + " " 1577 + item.getSummary(), 1578 FormatConstants.FORMAT_HTML); 1579 1580 if (item.getPages() != null) { 1582 Iterator pagesIterator = item.getPages().iterator(); 1583 int pageNumber = 0; 1585 while (pagesIterator.hasNext()) { 1586 if (log.isDebugEnabled()) { 1587 log.debug("Update search index for item id " 1588 + item.getId() 1589 + " - page " 1590 + ++pageNumber); 1591 } 1592 PageDO page = (PageDO) pagesIterator.next(); 1593 searchEngine.updateIndex(securitySession, item.getId(), 1594 LibraryItemDO.class.getName(), 1595 item.getTopic().getId().toString(), 1596 page.getId(), 1597 PageDO.class.getName(), 1598 page.getText(), 1599 FormatConstants.FORMAT_HTML); 1600 } 1601 } 1602 if (item.getFAQCategories() != null) { 1604 Iterator fAQCategoriesIterator = item.getFAQCategories().iterator(); 1605 while (fAQCategoriesIterator.hasNext()) { 1606 FAQCategoryDO faqCategory = (FAQCategoryDO) fAQCategoriesIterator.next(); 1607 1608 if (log.isDebugEnabled()) { 1609 log.debug("Update search index for item id " 1610 + item.getId() 1611 + " - faq category " 1612 + faqCategory.getName()); 1613 } 1614 1615 searchEngine.updateIndex(securitySession, item.getId(), 1616 LibraryItemDO.class.getName(), 1617 item.getTopic().getId().toString(), 1618 faqCategory.getId(), 1619 FAQCategoryDO.class.getName(), 1620 faqCategory.getName() 1621 + " " 1622 + faqCategory.getDescription(), 1623 FormatConstants.FORMAT_HTML); 1624 1625 if (faqCategory.getFAQs() != null) { 1626 Iterator faqIterator = faqCategory.getFAQs().iterator(); 1627 while (faqIterator.hasNext()) { 1628 FAQDO faq = (FAQDO) faqIterator.next(); 1629 1630 searchEngine.updateIndex(securitySession, item.getId(), 1631 LibraryItemDO.class.getName(), 1632 item.getTopic().getId().toString(), 1633 faq.getId(), 1634 FAQDO.class.getName(), 1635 faq.getQuestion() 1636 + " " 1637 + faq.getAnswer(), 1638 FormatConstants.FORMAT_HTML); 1639 } 1640 } 1641 } 1642 } 1643 } 1644 1653 public ValidationErrors validate(final SecuritySession securitySession, 1654 final CommentDO comment) { 1655 if (log.isDebugEnabled()) { 1656 log.debug("START validating " 1657 + comment); 1658 } 1659 1660 ValidationErrors errors = new ValidationErrors(); 1661 Mask mask = maskFactory.getMask(CommentDO.class); 1662 1663 if (comment.getItem() == null) { 1664 errors.add(new ValidationError( 1665 "submitComment", 1666 Library.BUNDLE_PATH, 1667 mask.getField("item"), 1668 "errors.required")); 1669 } 1670 if (StringHandling.isNullOrEmpty(comment.getSubject())) { 1671 errors.add(new ValidationError( 1672 "submitComment", 1673 Library.BUNDLE_PATH, 1674 mask.getField("subject"), 1675 "errors.required")); 1676 } 1677 if (StringHandling.isNullOrEmpty(comment.getText())) { 1678 errors.add(new ValidationError( 1679 "submitComment", 1680 Library.BUNDLE_PATH, 1681 mask.getField("text"), 1682 "errors.required")); 1683 } 1684 if (comment.getCreatedBy() == null) { 1685 errors.add(new ValidationError( 1686 "submitComment", 1687 Library.BUNDLE_PATH, 1688 mask.getField("createdBy"), 1689 "errors.required")); 1690 } 1691 1692 if (log.isDebugEnabled()) { 1693 log.debug("END validating " 1694 + comment 1695 + "- errors: " 1696 + errors); 1697 } 1698 return errors; 1699 } 1700 1709 public ValidationErrors validate(final SecuritySession securitySession, 1710 final LibraryItemDO item) { 1711 if (log.isDebugEnabled()) { 1712 log.debug("START validating " 1713 + item); 1714 } 1715 1716 ValidationErrors errors = new ValidationErrors(); 1717 Mask mask = maskFactory.getMask(LibraryItemDO.class); 1718 1719 if (StringHandling.isNullOrEmpty(item.getSummary())) { 1720 errors.add(new ValidationError( 1721 "submit", 1722 Library.BUNDLE_PATH, 1723 mask.getField("summary"), 1724 "errors.required")); 1725 } 1726 if (StringHandling.isNullOrEmpty(item.getTitle())) { 1727 errors.add(new ValidationError( 1728 "submit", 1729 Library.BUNDLE_PATH, 1730 mask.getField("title"), 1731 "errors.required")); 1732 } 1733 if (item.getTopic() == null) { 1734 errors.add(new ValidationError( 1735 "submit", 1736 Library.BUNDLE_PATH, 1737 mask.getField("topic"), 1738 "errors.required")); 1739 } 1740 if (item.getType() == null) { 1741 errors.add(new ValidationError( 1742 "submit", 1743 Library.BUNDLE_PATH, 1744 mask.getField("type"), 1745 "errors.required")); 1746 } 1747 if (LibraryItemConstants.ITEM_FAQ.equals(item.getType())) { 1749 boolean noCategoryName = false; 1750 1751 for (Iterator i = item.getFAQCategories().iterator(); i.hasNext();) { 1752 FAQCategoryDO faqCategory = (FAQCategoryDO) i.next(); 1753 String categoryName = faqCategory.getName(); 1754 1755 if (!noCategoryName && StringHandling.isNullOrEmpty(categoryName)) { 1756 errors.add(new ValidationError( 1757 "submitFaq", 1758 Library.BUNDLE_PATH, 1759 mask.getField("category"), 1760 "errors.required")); 1761 noCategoryName = true; 1763 } 1764 for (Iterator j = faqCategory.getFAQs().iterator(); j.hasNext();) { 1765 FAQDO faq = (FAQDO) j.next(); 1766 1767 if (StringHandling.isNullOrEmpty(faq.getQuestion())) { 1768 errors.add(new ValidationError( 1769 "submitFaq", 1770 Library.BUNDLE_PATH, 1771 mask.getField("question"), 1772 "errors.required")); 1773 break; 1774 } 1775 } 1776 } 1777 } 1778 1779 if (log.isDebugEnabled()) { 1780 log.debug("END validating " 1781 + item 1782 + "- errors: " 1783 + errors); 1784 } 1785 return errors; 1786 } 1787 1788 1797 public ValidationErrors validate(final SecuritySession securitySession, 1798 final TopicDO topic) { 1799 ValidationErrors errors = new ValidationErrors(); 1800 Mask mask = maskFactory.getMask(TopicDO.class); 1801 1802 if (StringHandling.isNullOrEmpty(topic.getCaption())) { 1803 errors.add(new ValidationError( 1804 "topicModify", 1805 Library.BUNDLE_PATH, 1806 mask.getField("caption"), 1807 "errors.required")); 1808 } 1809 return errors; 1810 } 1811 1812} 1813 | Popular Tags |