001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/search/post/UpdateThreadIndexTask.java,v 1.1 2007/10/12 08:12:29 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.1 $
005: * $Date: 2007/10/12 08:12:29 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author: Minh Nguyen
039: */
040: package com.mvnforum.search.post;
041:
042: import java.io.IOException;
043: import java.util.*;
044:
045: import net.myvietnam.mvncore.exception.SearchException;
046:
047: import org.apache.commons.logging.Log;
048: import org.apache.commons.logging.LogFactory;
049: import org.apache.lucene.index.IndexWriter;
050: import org.apache.lucene.store.Directory;
051:
052: import com.mvnforum.db.DAOFactory;
053: import com.mvnforum.db.PostBean;
054: import com.mvnforum.service.MvnForumServiceFactory;
055: import com.mvnforum.service.SearchService;
056:
057: /**
058: * Update a thread task. This task do indexing of all post in the thread
059: */
060: public class UpdateThreadIndexTask extends TimerTask {
061:
062: private static Log log = LogFactory
063: .getLog(UpdateThreadIndexTask.class);
064:
065: private int threadID = 0;
066:
067: /*
068: * Contructor with default access, prevent new an instance from outside package
069: */
070: UpdateThreadIndexTask(int threadID) {
071: this .threadID = threadID;
072: }
073:
074: /**
075: * Delete all posts in the thread first, then add it again
076: */
077: public void run() {
078: long start = System.currentTimeMillis();
079:
080: Collection posts = null;
081: try {
082: //hard code 10000 post to get. is it enough???
083: posts = DAOFactory.getPostDAO()
084: .getEnablePosts_inThread_limit(threadID, 0, 10000);
085: } catch (Exception ex) {
086: log.error(
087: "UpdateThreadTask.run : cannot get all posts from a thread ("
088: + threadID + ") for indexing", ex);
089: }
090:
091: if (posts == null)
092: return;
093:
094: // First, delete all posts from the thread
095: try {
096: PostIndexer.deleteThreadFromIndex(threadID);
097: } catch (SearchException e) {
098: log.error("Cannot delete thread from lucene index.", e);
099: // now we should not continue, so we return
100: log.info("UpdateThreadTask failed. Took "
101: + (System.currentTimeMillis() - start) + " ms");
102: return;
103: }
104:
105: // then add all posts of the thread
106: Directory directory = null;
107: IndexWriter writer = null;
108: try {
109: SearchService service = MvnForumServiceFactory
110: .getMvnForumService().getSearchService();
111: directory = service.getSearchPostIndexDir();
112: writer = PostIndexer.getIndexWriter(directory, false);
113:
114: int i = 0;
115: for (Iterator iter = posts.iterator(); iter.hasNext();) {
116: PostBean post = (PostBean) iter.next();
117: PostIndexer.doIndexPost(post, writer);
118: i++;
119: }
120: writer.optimize();
121: log.info("Updating thread finished successfully! " + i
122: + " post(s) indexed.");
123: } catch (Exception e) {
124: log.error("Error while updating thread.", e);
125: } finally {
126: if (writer != null) {
127: try {
128: writer.close();
129: } catch (IOException e) {
130: log.debug("Error closing Lucene IndexWriter", e);
131: }
132: }
133: if (directory != null) {
134: try {
135: directory.close();
136: } catch (IOException e) {
137: log.debug("Cannot close directory.", e);
138: }
139: }
140: }
141: log.info("UpdateThreadTask took "
142: + (System.currentTimeMillis() - start) + " ms");
143: }
144:
145: }
|