1.1 restoration
[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  any library licensed under the Apache Software License,
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30 package mircoders.producer;
31
32 import java.text.SimpleDateFormat;
33 import java.util.Calendar;
34 import java.util.Date;
35 import java.util.GregorianCalendar;
36 import java.util.Map;
37 import java.io.File;
38
39 import mir.entity.Entity;
40 import mir.entity.adapter.EntityAdapter;
41 import mir.log.LoggerWrapper;
42 import mir.misc.StringUtil;
43 import mir.producer.ProducerFailure;
44 import mir.producer.ProducerNode;
45 import mir.util.ParameterExpander;
46 import mir.util.FileFunctions;
47 import mircoders.entity.EntityContent;
48 import mircoders.search.AudioSearchTerm;
49 import mircoders.search.ContentSearchTerm;
50 import mircoders.search.ImagesSearchTerm;
51 import mircoders.search.IndexUtil;
52 import mircoders.search.KeywordSearchTerm;
53 import mircoders.search.TextSearchTerm;
54 import mircoders.search.TopicSearchTerm;
55 import mircoders.search.UnIndexedSearchTerm;
56 import mircoders.search.VideoSearchTerm;
57
58 import org.apache.lucene.analysis.standard.StandardAnalyzer;
59 import org.apache.lucene.document.Document;
60 import org.apache.lucene.index.IndexReader;
61 import org.apache.lucene.index.IndexWriter;
62 import org.apache.lucene.store.FSDirectory;
63
64
65 public class IndexingProducerNode implements ProducerNode {
66   private String contentKey;
67   private String indexPath;
68   private File indexBasePath;
69
70   public IndexingProducerNode(File anIndexBasePath, String aContentKey, String pathToIndex) {
71     contentKey = aContentKey;
72     indexPath = pathToIndex;
73     indexBasePath = anIndexBasePath;
74   }
75
76   public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger)
77     throws ProducerFailure {
78     IndexWriter indexWriter = null;
79     Object data;
80     Entity entity;
81     long startTime;
82     long endTime;
83     File indexFile = null;
84
85     startTime = System.currentTimeMillis();
86
87     try {
88       indexFile = FileFunctions.getAbsoluteOrRelativeFile(indexBasePath, ParameterExpander.expandExpression(aValueMap, indexPath));
89       data = ParameterExpander.findValueForKey(aValueMap, contentKey);
90
91       if (!(data instanceof EntityAdapter)) {
92         throw new ProducerFailure("IndexingProducerNode: value of '" +
93           contentKey + "' is not an EntityAdapter, but an " +
94           data.getClass().getName(), null);
95       }
96
97       entity = ((EntityAdapter) data).getEntity();
98
99       if (!(entity instanceof EntityContent)) {
100         throw new ProducerFailure("IndexingProducerNode: value of '" +
101           contentKey + "' is not a content EntityAdapter, but a " +
102           entity.getClass().getName() + " adapter", null);
103       }
104
105       aLogger.info("Indexing " + entity.getFieldValue("id") + " into " +  indexFile.getAbsolutePath());
106
107       // create an index here if one did not already exist
108       if (!(IndexReader.indexExists(indexFile))) {
109         aLogger.error("Didn't find existing index, so I'm making one in " + indexFile.getAbsolutePath());
110
111         IndexWriter indexCreator = new IndexWriter(indexFile, new StandardAnalyzer(), true);
112         indexCreator.close();
113       }
114
115       IndexUtil.unindexEntity((EntityContent) entity, indexFile);
116
117       indexWriter = new IndexWriter(indexFile, new StandardAnalyzer(), false);
118
119       Document theDoc = new Document();
120
121       // Keyword is stored and indexed, but not tokenized
122       // Text is tokenized,stored, indexed
123       // Unindexed is not tokenized or indexed, only stored
124       // Unstored is tokenized and indexed, but not stored
125       //this initialization should go somewhere global like an xml file....
126       (new KeywordSearchTerm("id", "", "id", "", "id")).index(theDoc, entity);
127
128       String textValue = entity.getFieldValue("webdb_create");
129       Calendar calendar = GregorianCalendar.getInstance();
130       int year;
131       int month;
132       int day;
133       int hours;
134       int minutes;
135       Date date;
136       String formattedDate="";
137
138       if (textValue!=null) {
139         try {
140           year = Integer.parseInt(textValue.substring(0, 4));
141           month = Integer.parseInt(textValue.substring(5, 7));
142           day = Integer.parseInt(textValue.substring(8, 10));
143           hours = Integer.parseInt(textValue.substring(11, 13));
144           minutes = Integer.parseInt(textValue.substring(14, 16));
145
146           calendar.set(year, month - 1, day, hours, minutes);
147           date = calendar.getTime();
148           SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd hh:mm");
149           formattedDate = formatter.format(date);
150         }
151         catch (Throwable t) {
152           aLogger.error("Error while generating content date to index: " + t.getMessage());
153           t.printStackTrace(aLogger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
154         }
155       }
156       (new KeywordSearchTerm("webdb_create_formatted", "search_date",
157         "webdb_create_formatted", "webdb_create_formatted",
158         "webdb_create_formatted")).indexValue(theDoc,formattedDate);
159
160
161       (new UnIndexedSearchTerm("", "", "", "where", "where")).indexValue(theDoc,
162         StringUtil.webdbDate2path(entity.getFieldValue("date")) +
163         entity.getFieldValue("id") + ".shtml");
164
165       (new TextSearchTerm("creator", "search_creator", "creator", "creator",
166         "creator")).index(theDoc, entity);
167       (new TextSearchTerm("title", "search_title", "title", "title", "title")).index(theDoc,
168         entity);
169       (new UnIndexedSearchTerm("description", "search_content", "description",
170         "description", "description")).index(theDoc, entity);
171       (new UnIndexedSearchTerm("webdb_create", "search_irrelevant",
172         "creationDate", "creationDate", "creationDate")).index(theDoc, entity);
173
174       (new ContentSearchTerm("content_data", "search_content", "content", "", "")).indexValue(theDoc,
175         entity.getFieldValue("content_data") + " " + entity.getFieldValue("description") +
176         " " + entity.getFieldValue("title"));
177
178       (new TopicSearchTerm()).index(theDoc, entity);
179
180       (new ImagesSearchTerm()).index(theDoc, entity);
181
182       (new AudioSearchTerm()).index(theDoc, entity);
183
184       (new VideoSearchTerm()).index(theDoc, entity);
185
186       //comments-just aggregate all relevant fields
187       //removed until i get a chance to do this right
188       //String commentsAggregate = "";
189       //TemplateModel comments=entity.get("to_comments");
190       //if (comments != null){
191       // while (((TemplateListModel)comments).hasNext()){
192       //    TemplateModel aComment = ((TemplateListModel)comments).next();
193       //    commentsAggregate = commentsAggregate + " " + ((TemplateHashModel)aComment).get("title").toString()
194       //     + " " + ((TemplateHashModel)aComment).get("creator").toString()
195       //      + " " + ((TemplateHashModel)aComment).get("text").toString();
196       //  }
197       //}
198       //theDoc.add(Field.UnStored("comments",commentsAggregate));
199       indexWriter.addDocument(theDoc);
200     }
201     catch (Throwable t) {
202       aLogger.error("Error while indexing content: " + t.getMessage());
203       t.printStackTrace(aLogger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
204     }
205     finally {
206       if (indexWriter != null) {
207         try {
208           indexWriter.close();
209         }
210         catch (Throwable t) {
211           aLogger.warn("Error while closing indexWriter: " + t.getMessage());
212         }
213       }
214
215       if (indexFile!=null) {
216         try {
217           FSDirectory theIndexDir = FSDirectory.getDirectory(indexFile, false);
218
219           if (IndexReader.isLocked(theIndexDir)) {
220             IndexReader.unlock(theIndexDir);
221           }
222         }
223         catch (Throwable t) {
224           aLogger.warn("Error while unlocking index: " + t.getMessage());
225         }
226       }
227     }
228
229     endTime = System.currentTimeMillis();
230
231     aLogger.info("  IndexTime: " + (endTime - startTime) + " ms<br>");
232   }
233 }