1 5 package org.exoplatform.services.communication.forum.hibernate; 6 7 import java.util.List ; 8 import java.util.Date ; 9 import org.apache.lucene.document.DateField; 10 import org.apache.lucene.document.Document; 11 import org.apache.lucene.document.Field; 12 import org.apache.lucene.index.Term; 13 import org.exoplatform.commons.utils.PageList; 14 import org.exoplatform.container.PortalContainer; 15 import org.exoplatform.services.communication.forum.*; 16 import org.exoplatform.services.database.HibernateService; 17 import org.exoplatform.services.indexing.BaseIndexerPlugin; 18 import org.exoplatform.services.indexing.IndexingService; 19 24 public class ForumIndexerPluginImpl extends BaseIndexerPlugin implements ForumIndexerPlugin { 25 private HibernateService hservice_ ; 26 27 public ForumIndexerPluginImpl(IndexingService iservice, 28 HibernateService hservice) { 29 super(iservice) ; 30 hservice_ = hservice ; 31 } 32 33 public Document createDocument(Forum forum, PostImpl post) throws Exception { 34 String identifier = post.getId() ; 35 String title = post.getSubject() ; 36 if(title == null ) title = "-----------No Subject----------" ; 37 String textToIndex = post.getMessage(); 38 if(textToIndex == null) textToIndex = "" ; 39 String owner = post.getOwner() ; 40 if(owner == null) owner = "" ; 41 String desc = getContentDescription(textToIndex, 200); 42 Document doc = createBaseDocument(identifier, owner, title, desc , 43 textToIndex, forum.getViewForumRole()) ; 44 doc.add(Field.Text(CATEGORY_ID_FIELD, forum.getCategoryId())) ; 45 doc.add(Field.Text(FORUM_ID_FIELD, post.getForumId())) ; 46 doc.add(Field.Text(TOPIC_ID_FIELD, post.getTopicId())) ; 47 doc.add(Field.Text(POST_ID_FIELD, post.getId())) ; 48 Date date = post.getModifiedDate() ; 49 if(date == null || date.getTime() == 0) { 50 } 52 doc.add(Field.Text(DATE_FIELD, DateField.dateToString(post.getModifiedDate()))) ; 53 return doc ; 54 } 55 56 public String getPluginIdentifier() { return IDENTIFIER ; } 57 58 public Object getObject(String user,String objectId) throws Exception { 59 return hservice_.findOne(PostImpl.class, objectId); 60 } 61 62 public String getObjectAsText(String userName, String objectId) throws Exception { 63 PostImpl postImpl = (PostImpl) getObject(userName, objectId) ; 64 if(postImpl == null) return UNSYNCHRONIZED_DATABASE ; 65 return postImpl.getMessage() ; 66 } 67 68 public String getObjectAsXHTML(String userName, String objectId) throws Exception { 69 PostImpl postImpl = (PostImpl) getObject(userName, objectId) ; 70 if(postImpl == null) return UNSYNCHRONIZED_DATABASE ; 71 return postImpl.getMessage() ; 72 } 73 74 public String getObjectAsXML(String userName, String objectId) throws Exception { 75 PostImpl postImpl = (PostImpl) getObject(userName, objectId) ; 76 if(postImpl == null) return UNSYNCHRONIZED_DATABASE ; 77 return postImpl.getMessage() ; 78 } 79 80 public void reindex() throws Exception { 81 removeIndex() ; 82 PortalContainer pcontainer = PortalContainer.getInstance() ; 83 ForumServiceContainer container = 84 (ForumServiceContainer)pcontainer.getComponentInstanceOfType(ForumServiceContainer.class) ; 85 List forumOwners = container.getForumOwners() ; 86 for(int i = 0 ; i < forumOwners.size() ; i++) { 87 String owner = (String ) forumOwners.get(i) ; 88 ForumService service = container.findForumService(owner); 89 List categories = service.getCategories() ; 90 for(int j = 0; j < categories.size(); j++ ) { 91 Category category = (Category) categories.get(j) ; 92 List forums = service.getForums(category.getId()) ; 93 for(int k = 0; k < forums.size(); k++) { 94 Forum forum = (Forum) forums.get(k) ; 95 indexForum(service, forum) ; 96 } 97 } 98 } 99 } 100 101 private void indexForum(ForumService service, Forum forum) throws Exception { 102 PageList pages = service.getTopics(forum.getId()) ; 103 pages.setPageSize(100) ; 104 for(int i = 1; i <= pages.getAvailablePage(); i++) { 105 List topics = pages.getPage(i) ; 106 for(int j = 0; j < topics.size(); j++) { 107 Topic topic = (Topic) topics.get(j) ; 108 List posts = service.getPosts(topic.getId()).getAll() ; 109 for(int k = 0; k < posts.size(); k++) { 110 PostImpl post = (PostImpl) posts.get(k) ; 111 createPost(forum, post) ; 112 } 113 } 114 } 115 } 116 117 void removeCategory(Category category) throws Exception { 118 Term term = new Term(CATEGORY_ID_FIELD, category.getId()) ; 119 iservice_.queueDeleteDocuments(term) ; 120 } 121 122 void removeForum(Forum forum) throws Exception { 123 Term term = new Term(FORUM_ID_FIELD, forum.getId()) ; 124 iservice_.queueDeleteDocuments(term) ; 125 } 126 127 void removeTopic(Topic topic) throws Exception { 128 Term term = new Term(TOPIC_ID_FIELD, topic.getId()) ; 129 iservice_.queueDeleteDocuments(term) ; 130 } 131 132 void removePost(Post post) throws Exception { 133 Term term = new Term(IDENTIFIER , post.getId()) ; 134 iservice_.queueDeleteDocuments(term) ; 135 } 136 137 void createPost(Forum forum, PostImpl post) throws Exception { 138 iservice_.queueIndexDocument(createDocument(forum, post)) ; 139 } 140 141 void updatePost(Forum forum, PostImpl post) throws Exception { 142 Term term = new Term(IDENTIFIER, post.getId()) ; 143 iservice_.queueDeleteDocuments(term) ; 144 iservice_.queueIndexDocument(createDocument(forum, post)) ; 145 } 146 } | Popular Tags |