KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > search > company > RebuildCompanyIndexTask


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/search/company/RebuildCompanyIndexTask.java,v 1.6 2006/04/14 17:05:27 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.6 $
5  * $Date: 2006/04/14 17:05:27 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding mvnForum MUST remain
12  * intact in the scripts and in the outputted HTML.
13  * The "powered by" text/logo with a link back to
14  * http://www.mvnForum.com and http://www.MyVietnam.net in
15  * the footer of the pages MUST remain visible when the pages
16  * are viewed on the internet or intranet.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  * Support can be obtained from support forums at:
33  * http://www.mvnForum.com/mvnforum/index
34  *
35  * Correspondence and Marketing Questions can be sent to:
36  * info at MyVietnam net
37  *
38  * @author: Minh Nguyen
39  * @author: Dejan Krsmanovic dejan_krsmanovic@yahoo.com
40  */

41 package com.mvnforum.search.company;
42
43 import java.io.IOException JavaDoc;
44 import java.util.*;
45
46 import com.mvnforum.db.CompanyBean;
47 import com.mvnforum.db.DAOFactory;
48 import org.apache.commons.logging.Log;
49 import org.apache.commons.logging.LogFactory;
50 import org.apache.lucene.index.IndexWriter;
51
52 /**
53  * Rebuilding indices task. This task do indexing of all documents
54  */

55 public class RebuildCompanyIndexTask extends TimerTask
56 {
57     private static Log log = LogFactory.getLog(RebuildCompanyIndexTask.class);
58
59     public static final int MERGE_FACTOR = 20;
60
61     /*
62      * Contructor with default access, prevent new an instance from outside package
63      */

64     RebuildCompanyIndexTask() {
65     }
66
67     /**
68      * Create new index. If anything exist already - delete it
69      */

70     public void run() {
71         long start = System.currentTimeMillis();
72
73         Collection companies = null;
74         try {
75             companies = DAOFactory.getCompanyDAO().getCompanies();
76         } catch (Exception JavaDoc ex) {
77             log.error("RebuildCompanyIndexTask.run : cannot get all companies from database for indexing", ex);
78         }
79
80         if (companies == null) return;
81
82         // we index it even when there are not any companies, this will
83
// create an empty search index
84
//if (posts.size() == 0) return;
85

86         IndexWriter writer = null;
87         try {
88             writer = CompanyIndexer.getIndexWriter(true);
89             writer.mergeFactor = MERGE_FACTOR;
90
91             int i = 0;
92             for (Iterator iter = companies.iterator(); iter.hasNext(); ) {
93                 CompanyBean companyBean = (CompanyBean) iter.next();
94                 CompanyIndexer.doIndexCompany(companyBean, writer);
95                 i++;
96             }
97             writer.optimize();
98             log.info("Rebuilding index finished successfully! " + i + " company(s) indexed");
99         } catch (Exception JavaDoc e) {
100             log.error("Error while rebuilding index", e);
101         } finally {
102             try {
103                 if (writer != null) {
104                     writer.close();
105                 }
106             } catch (IOException JavaDoc ignore) {
107             }
108         }
109         log.info("RebuildCompanyIndexTask took " + (System.currentTimeMillis() - start) + " ms");
110     }
111 }
112
Popular Tags