2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
30 package mircoders.producer;
32 import java.text.SimpleDateFormat;
33 import java.util.Calendar;
34 import java.util.Date;
35 import java.util.GregorianCalendar;
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;
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;
65 public class IndexingProducerNode implements ProducerNode {
66 private String contentKey;
67 private String indexPath;
68 private File indexBasePath;
70 public IndexingProducerNode(File anIndexBasePath, String aContentKey, String pathToIndex) {
71 contentKey = aContentKey;
72 indexPath = pathToIndex;
73 indexBasePath = anIndexBasePath;
76 public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger)
77 throws ProducerFailure {
78 IndexWriter indexWriter = null;
83 File indexFile = null;
85 startTime = System.currentTimeMillis();
88 indexFile = FileFunctions.getAbsoluteOrRelativeFile(indexBasePath, ParameterExpander.expandExpression(aValueMap, indexPath));
89 data = ParameterExpander.findValueForKey(aValueMap, contentKey);
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);
97 entity = ((EntityAdapter) data).getEntity();
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);
105 aLogger.info("Indexing " + entity.getFieldValue("id") + " into " + indexFile.getAbsolutePath());
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());
111 IndexWriter indexCreator = new IndexWriter(indexFile, new StandardAnalyzer(), true);
112 indexCreator.close();
115 IndexUtil.unindexEntity((EntityContent) entity, indexFile);
117 indexWriter = new IndexWriter(indexFile, new StandardAnalyzer(), false);
119 Document theDoc = new Document();
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);
128 String textValue = entity.getFieldValue("webdb_create");
129 Calendar calendar = GregorianCalendar.getInstance();
136 String formattedDate="";
138 if (textValue!=null) {
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));
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);
151 catch (Throwable t) {
152 aLogger.error("Error while generating content date to index: " + t.getMessage());
153 t.printStackTrace(aLogger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
156 (new KeywordSearchTerm("webdb_create_formatted", "search_date",
157 "webdb_create_formatted", "webdb_create_formatted",
158 "webdb_create_formatted")).indexValue(theDoc,formattedDate);
161 (new UnIndexedSearchTerm("", "", "", "where", "where")).indexValue(theDoc,
162 StringUtil.webdbDate2path(entity.getFieldValue("date")) +
163 entity.getFieldValue("id") + ".shtml");
165 (new TextSearchTerm("creator", "search_creator", "creator", "creator",
166 "creator")).index(theDoc, entity);
167 (new TextSearchTerm("title", "search_title", "title", "title", "title")).index(theDoc,
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);
174 (new ContentSearchTerm("content_data", "search_content", "content", "", "")).indexValue(theDoc,
175 entity.getFieldValue("content_data") + " " + entity.getFieldValue("description") +
176 " " + entity.getFieldValue("title"));
178 (new TopicSearchTerm()).index(theDoc, entity);
180 (new ImagesSearchTerm()).index(theDoc, entity);
182 (new AudioSearchTerm()).index(theDoc, entity);
184 (new VideoSearchTerm()).index(theDoc, entity);
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();
198 //theDoc.add(Field.UnStored("comments",commentsAggregate));
199 indexWriter.addDocument(theDoc);
201 catch (Throwable t) {
202 aLogger.error("Error while indexing content: " + t.getMessage());
203 t.printStackTrace(aLogger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
206 if (indexWriter != null) {
210 catch (Throwable t) {
211 aLogger.warn("Error while closing indexWriter: " + t.getMessage());
215 if (indexFile!=null) {
217 FSDirectory theIndexDir = FSDirectory.getDirectory(indexFile, false);
219 if (IndexReader.isLocked(theIndexDir)) {
220 IndexReader.unlock(theIndexDir);
223 catch (Throwable t) {
224 aLogger.warn("Error while unlocking index: " + t.getMessage());
229 endTime = System.currentTimeMillis();
231 aLogger.info(" IndexTime: " + (endTime - startTime) + " ms<br>");