KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > communication > forum > hibernate > ForumIndexerPluginImpl


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.communication.forum.hibernate;
6
7 import java.util.List JavaDoc;
8 import java.util.Date JavaDoc;
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 /**
20  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
21  * @since Sep 13, 2004
22  * @version $Id: ForumIndexerPluginImpl.java,v 1.3 2004/11/01 15:07:50 tuan08 Exp $
23  */

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 JavaDoc {
34     String JavaDoc identifier = post.getId() ;
35     String JavaDoc title = post.getSubject() ;
36     if(title == null ) title = "-----------No Subject----------" ;
37     String JavaDoc textToIndex = post.getMessage();
38     if(textToIndex == null) textToIndex = "" ;
39     String JavaDoc owner = post.getOwner() ;
40     if(owner == null) owner = "" ;
41     String JavaDoc 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 JavaDoc date = post.getModifiedDate() ;
49     if(date == null || date.getTime() == 0) {
50       //System.out.println("date is null for: " + post.getSubject()) ;
51
}
52     doc.add(Field.Text(DATE_FIELD, DateField.dateToString(post.getModifiedDate()))) ;
53     return doc ;
54   }
55   
56   public String JavaDoc getPluginIdentifier() { return IDENTIFIER ; }
57   
58   public Object JavaDoc getObject(String JavaDoc user,String JavaDoc objectId) throws Exception JavaDoc {
59     return hservice_.findOne(PostImpl.class, objectId);
60   }
61   
62   public String JavaDoc getObjectAsText(String JavaDoc userName, String JavaDoc objectId) throws Exception JavaDoc {
63     PostImpl postImpl = (PostImpl) getObject(userName, objectId) ;
64     if(postImpl == null) return UNSYNCHRONIZED_DATABASE ;
65     return postImpl.getMessage() ;
66   }
67   
68   public String JavaDoc getObjectAsXHTML(String JavaDoc userName, String JavaDoc objectId) throws Exception JavaDoc {
69     PostImpl postImpl = (PostImpl) getObject(userName, objectId) ;
70     if(postImpl == null) return UNSYNCHRONIZED_DATABASE ;
71     return postImpl.getMessage() ;
72   }
73   
74   public String JavaDoc getObjectAsXML(String JavaDoc userName, String JavaDoc objectId) throws Exception JavaDoc {
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 JavaDoc {
81     removeIndex() ;
82     PortalContainer pcontainer = PortalContainer.getInstance() ;
83     ForumServiceContainer container =
84       (ForumServiceContainer)pcontainer.getComponentInstanceOfType(ForumServiceContainer.class) ;
85     List JavaDoc forumOwners = container.getForumOwners() ;
86     for(int i = 0 ; i < forumOwners.size() ; i++) {
87       String JavaDoc owner = (String JavaDoc) forumOwners.get(i) ;
88       ForumService service = container.findForumService(owner);
89       List JavaDoc categories = service.getCategories() ;
90       for(int j = 0; j < categories.size(); j++ ) {
91         Category category = (Category) categories.get(j) ;
92         List JavaDoc 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 JavaDoc {
102     PageList pages = service.getTopics(forum.getId()) ;
103     pages.setPageSize(100) ;
104     for(int i = 1; i <= pages.getAvailablePage(); i++) {
105       List JavaDoc topics = pages.getPage(i) ;
106       for(int j = 0; j < topics.size(); j++) {
107         Topic topic = (Topic) topics.get(j) ;
108         List JavaDoc 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 JavaDoc {
118     Term term = new Term(CATEGORY_ID_FIELD, category.getId()) ;
119     iservice_.queueDeleteDocuments(term) ;
120   }
121   
122   void removeForum(Forum forum) throws Exception JavaDoc {
123     Term term = new Term(FORUM_ID_FIELD, forum.getId()) ;
124     iservice_.queueDeleteDocuments(term) ;
125   }
126   
127   void removeTopic(Topic topic) throws Exception JavaDoc {
128     Term term = new Term(TOPIC_ID_FIELD, topic.getId()) ;
129     iservice_.queueDeleteDocuments(term) ;
130   }
131   
132   void removePost(Post post) throws Exception JavaDoc {
133     Term term = new Term(IDENTIFIER , post.getId()) ;
134     iservice_.queueDeleteDocuments(term) ;
135   }
136   
137   void createPost(Forum forum, PostImpl post) throws Exception JavaDoc {
138     iservice_.queueIndexDocument(createDocument(forum, post)) ;
139   }
140   
141   void updatePost(Forum forum, PostImpl post) throws Exception JavaDoc {
142     Term term = new Term(IDENTIFIER, post.getId()) ;
143     iservice_.queueDeleteDocuments(term) ;
144     iservice_.queueIndexDocument(createDocument(forum, post)) ;
145   }
146 }
Popular Tags