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 mir.entity.Entity;
33 import mir.entity.adapter.EntityAdapter;
34 import mir.log.LoggerWrapper;
35 import mir.misc.StringUtil;
36 import mir.producer.AbstractProducerNode;
37 import mir.producer.ProducerFailure;
38 import mir.util.FileRoutines;
39 import mir.util.ParameterExpander;
40 import mircoders.entity.EntityContent;
41 import mircoders.search.*;
42 import org.apache.lucene.analysis.standard.StandardAnalyzer;
43 import org.apache.lucene.document.Document;
44 import org.apache.lucene.index.IndexReader;
45 import org.apache.lucene.index.IndexWriter;
46 import org.apache.lucene.store.FSDirectory;
49 import java.text.SimpleDateFormat;
50 import java.util.Calendar;
51 import java.util.Date;
52 import java.util.GregorianCalendar;
56 public class IndexingProducerNode extends AbstractProducerNode {
57 private String contentKey;
58 private String indexPath;
59 private File indexBasePath;
61 public IndexingProducerNode(File anIndexBasePath, String aContentKey, String pathToIndex) {
62 contentKey = aContentKey;
63 indexPath = pathToIndex;
64 indexBasePath = anIndexBasePath;
67 public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger)
68 throws ProducerFailure {
69 IndexWriter indexWriter = null;
74 File indexFile = null;
76 startTime = System.currentTimeMillis();
79 indexFile = FileRoutines.getAbsoluteOrRelativeFile(indexBasePath, ParameterExpander.expandExpression(aValueMap, indexPath));
80 data = ParameterExpander.findValueForKey(aValueMap, contentKey);
82 if (!(data instanceof EntityAdapter)) {
83 throw new ProducerFailure("IndexingProducerNode: value of '" +
84 contentKey + "' is not an EntityAdapter, but an " +
85 data.getClass().getName(), null);
88 entity = ((EntityAdapter) data).getEntity();
90 if (!(entity instanceof EntityContent)) {
91 throw new ProducerFailure("IndexingProducerNode: value of '" +
92 contentKey + "' is not a content EntityAdapter, but a " +
93 entity.getClass().getName() + " adapter", null);
96 aLogger.debug("Indexing " + entity.getFieldValue("id") + " into " + indexFile.getAbsolutePath());
98 // create an index here if one did not already exist
99 if (!(IndexReader.indexExists(indexFile))) {
100 aLogger.warn("Didn't find existing index, so I'm making one in " + indexFile.getAbsolutePath());
102 IndexWriter indexCreator = new IndexWriter(indexFile, new StandardAnalyzer(), true);
103 indexCreator.close();
106 IndexUtil.unindexEntity((EntityContent) entity, indexFile);
108 indexWriter = new IndexWriter(indexFile, new StandardAnalyzer(), false);
110 Document theDoc = new Document();
112 // Keyword is stored and indexed, but not tokenized
113 // Text is tokenized,stored, indexed
114 // Unindexed is not tokenized or indexed, only stored
115 // Unstored is tokenized and indexed, but not stored
116 //this initialization should go somewhere global like an xml file....
117 (new KeywordSearchTerm("id", "", "id", "", "id")).index(theDoc, entity);
119 String textValue = entity.getFieldValue("webdb_create");
120 Calendar calendar = GregorianCalendar.getInstance();
127 String formattedDate="";
130 if (textValue!=null) {
132 year = Integer.parseInt(textValue.substring(0, 4));
133 month = Integer.parseInt(textValue.substring(5, 7));
134 day = Integer.parseInt(textValue.substring(8, 10));
135 hours = Integer.parseInt(textValue.substring(11, 13));
136 minutes = Integer.parseInt(textValue.substring(14, 16));
138 calendar.set(year, month - 1, day, hours, minutes);
139 date = calendar.getTime();
140 SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd hh:mm");
141 formattedDate = formatter.format(date);
143 SimpleDateFormat pathFormatter = new SimpleDateFormat("/yyyy/MM/");
144 pathDate = pathFormatter.format(date);
146 catch (Throwable t) {
147 aLogger.warn("Error while generating content date to index", t);
150 (new KeywordSearchTerm("webdb_create_formatted", "search_date",
151 "webdb_create_formatted", "webdb_create_formatted",
152 "webdb_create_formatted")).indexValue(theDoc,formattedDate);
155 (new UnIndexedSearchTerm("", "", "", "where", "where")).indexValue(theDoc,
156 pathDate + entity.getFieldValue("id") + ".shtml");
158 (new TextSearchTerm("creator", "search_creator", "creator", "creator",
159 "creator")).index(theDoc, entity);
160 (new TextSearchTerm("title", "search_title", "title", "title", "title")).index(theDoc,
162 (new UnIndexedSearchTerm("description", "search_content", "description",
163 "description", "description")).index(theDoc, entity);
164 (new UnIndexedSearchTerm("webdb_create", "search_irrelevant",
165 "creationDate", "creationDate", "creationDate")).index(theDoc, entity);
167 (new ContentSearchTerm("content_data", "search_content", "content", "", "")).indexValue(theDoc,
168 entity.getFieldValue("content_data") + " " + entity.getFieldValue("description") +
169 " " + entity.getFieldValue("title"));
171 (new TopicSearchTerm()).index(theDoc, entity);
173 (new ImagesSearchTerm()).index(theDoc, entity);
175 (new AudioSearchTerm()).index(theDoc, entity);
177 (new VideoSearchTerm()).index(theDoc, entity);
179 //comments-just aggregate all relevant fields
180 //removed until i get a chance to do this right
181 //String commentsAggregate = "";
182 //TemplateModel comments=entity.get("to_comments");
183 //if (comments != null){
184 // while (((TemplateListModel)comments).hasNext()){
185 // TemplateModel aComment = ((TemplateListModel)comments).next();
186 // commentsAggregate = commentsAggregate + " " + ((TemplateHashModel)aComment).get("title").toString()
187 // + " " + ((TemplateHashModel)aComment).get("creator").toString()
188 // + " " + ((TemplateHashModel)aComment).get("text").toString();
191 //theDoc.add(Field.UnStored("comments",commentsAggregate));
192 indexWriter.addDocument(theDoc);
194 catch (Throwable t) {
195 aLogger.error("Error while indexing content: " + t.getMessage(), t);
198 if (indexWriter != null) {
202 catch (Throwable t) {
203 aLogger.warn("Error while closing indexWriter", t);
207 if (indexFile!=null) {
209 FSDirectory theIndexDir = FSDirectory.getDirectory(indexFile, false);
211 if (IndexReader.isLocked(theIndexDir)) {
212 IndexReader.unlock(theIndexDir);
215 catch (Throwable t) {
216 aLogger.warn("Error while unlocking index", t);
221 endTime = System.currentTimeMillis();
223 aLogger.debug(" IndexTime: " + (endTime - startTime) + " ms<br>");