simple and stable, about to be broken again soon
[mir.git] / source / mircoders / producer / IndexingProducerNode.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package mircoders.producer;
33
34 import java.util.*;
35 import java.io.*;
36
37 import org.apache.lucene.analysis.standard.StandardAnalyzer;
38 import org.apache.lucene.index.*;
39 import org.apache.lucene.document.Document;
40 import org.apache.lucene.document.Field;
41 import org.apache.lucene.store.FSDirectory;
42
43 import freemarker.template.*;
44
45
46 import mir.util.*;
47 import mir.log.*;
48 import mir.producer.*;
49 //import mir.generator.*;
50 import mircoders.global.*;
51 import mircoders.localizer.*;
52 import mir.entity.*;
53 import mir.entity.adapter.*;
54 import mircoders.entity.*;
55 import mircoders.storage.*;
56 import mircoders.search.*;
57
58
59 public class IndexingProducerNode implements ProducerNode {
60   private String contentKey;
61   private String indexPath;
62
63
64   public IndexingProducerNode(String aContentKey, String pathToIndex) {
65     contentKey = aContentKey;
66     indexPath=pathToIndex;
67   }
68
69   public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) throws ProducerFailure {
70     IndexReader indexReader = null;
71     IndexWriter indexWriter = null;
72     Object data;
73     Entity entity;
74
75     long startTime;
76     long endTime;
77
78     startTime = System.currentTimeMillis();
79
80     try {
81       data = ParameterExpander.findValueForKey( aValueMap, contentKey );
82
83       if (! (data instanceof EntityAdapter)) {
84         throw new ProducerFailure("IndexingProducerNode: value of '"+contentKey+"' is not an EntityAdapter, but an " + data.getClass().getName(), null);
85       }
86
87       entity = ((EntityAdapter) data).getEntity();
88       if (! (entity instanceof EntityContent)) {
89         throw new ProducerFailure("IndexingProducerNode: value of '"+contentKey+"' is not a content EntityAdapter, but a " + entity.getClass().getName() + " adapter", null);
90       }
91       aLogger.info("Indexing " + (String) entity.getValue("id") + " into " + indexPath);
92
93       indexReader = IndexReader.open(indexPath);
94       indexReader.delete(new Term("id",entity.getValue("id")));
95       indexReader.close();
96
97       indexWriter = new IndexWriter(indexPath, new StandardAnalyzer(), false);
98       Document theDoc =  new Document();
99
100       // Keyword is stored and indexed, but not tokenized
101       // Text is tokenized,stored, indexed
102       // Unindexed is not tokenized or indexed, only stored
103       // Unstored is tokenized and indexed, but not stored
104       
105       //this initialization should go somewhere global like an xml file....
106
107
108       (new KeywordSearchTerm("id","","id","","id")).index(theDoc,entity);
109       (new KeywordSearchTerm("date_formatted","search_date","webdb_create_formatted","webdb_create_formatted","webdb_create_formatted")).index(theDoc,entity);
110
111       (new UnIndexedSearchTerm("","","","where","where")).indexValue(theDoc,entity.getValue("publish_path")+entity.getValue("id")+".shtml");
112
113       (new TextSearchTerm("creator","search_creator","creator","creator","creator")).index(theDoc,entity);
114       (new TextSearchTerm("title","search_title","title","title","title")).index(theDoc,entity);
115       (new UnIndexedSearchTerm("description","search_content","description","description","description")).index(theDoc,entity);
116
117       (new ContentSearchTerm("content_data","search_content","content","","")).indexValue(theDoc,
118                                                                                      entity.getValue("content_data")+ " "
119                                                                                      + entity.getValue("description")+ " "
120                                                                                      + entity.getValue("title")
121                                                                                      );
122
123       (new TopicSearchTerm()).index(theDoc,entity);
124
125       (new ImagesSearchTerm()).index(theDoc,entity);
126       
127       (new AudioSearchTerm()).index(theDoc,entity);
128       
129       (new VideoSearchTerm()).index(theDoc,entity);
130
131
132       //comments-just aggregate all relevant fields
133       //removed until i get a chance to do this right
134
135       //String commentsAggregate = "";
136       //TemplateModel comments=entity.get("to_comments");
137       //if (comments != null){
138       // while (((TemplateListModel)comments).hasNext()){
139       //    TemplateModel aComment = ((TemplateListModel)comments).next();
140       //    commentsAggregate = commentsAggregate + " " + ((TemplateHashModel)aComment).get("title").toString()
141       //     + " " + ((TemplateHashModel)aComment).get("creator").toString()
142       //      + " " + ((TemplateHashModel)aComment).get("text").toString();
143       //  }
144       //}
145       //theDoc.add(Field.UnStored("comments",commentsAggregate));
146
147       indexWriter.addDocument(theDoc);
148
149
150     }
151     catch (Throwable t) {
152       aLogger.error("Error while indexing content: " + t.getMessage());
153       t.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(aLogger, LoggerWrapper.DEBUG_MESSAGE)));
154     }
155     finally {
156       if (indexReader != null){
157         try{
158           indexReader.close();
159         }
160         catch (Throwable t) {
161           aLogger.warn("Error while closing indexReader: " + t.getMessage());
162         }
163
164       }
165
166       if (indexWriter != null){
167         try{
168           indexWriter.close();
169         }
170         catch (Throwable t) {
171           aLogger.warn("Error while closing indexWriter: " + t.getMessage());
172         }
173
174       }
175
176
177       try{
178         FSDirectory theIndexDir=FSDirectory.getDirectory(indexPath,false);
179         if (indexReader.isLocked(theIndexDir)){
180           indexReader.unlock(theIndexDir);
181         }
182       }
183       catch (Throwable t) {
184         aLogger.warn("Error while unlocking index: " + t.getMessage());
185       }
186     }
187
188
189
190
191     endTime = System.currentTimeMillis();
192
193     aLogger.info("  IndexTime: " + (endTime-startTime) + " ms<br>");
194   }
195 }
196
197
198
199
200
201
202
203
204
205