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="";
129 if (textValue!=null) {
131 year = Integer.parseInt(textValue.substring(0, 4));
132 month = Integer.parseInt(textValue.substring(5, 7));
133 day = Integer.parseInt(textValue.substring(8, 10));
134 hours = Integer.parseInt(textValue.substring(11, 13));
135 minutes = Integer.parseInt(textValue.substring(14, 16));
137 calendar.set(year, month - 1, day, hours, minutes);
138 date = calendar.getTime();
139 SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd hh:mm");
140 formattedDate = formatter.format(date);
142 catch (Throwable t) {
143 aLogger.warn("Error while generating content date to index", t);
146 (new KeywordSearchTerm("webdb_create_formatted", "search_date",
147 "webdb_create_formatted", "webdb_create_formatted",
148 "webdb_create_formatted")).indexValue(theDoc,formattedDate);
151 (new UnIndexedSearchTerm("", "", "", "where", "where")).indexValue(theDoc,
152 StringUtil.webdbDate2path(entity.getFieldValue("date")) +
153 entity.getFieldValue("id") + ".shtml");
155 (new TextSearchTerm("creator", "search_creator", "creator", "creator",
156 "creator")).index(theDoc, entity);
157 (new TextSearchTerm("title", "search_title", "title", "title", "title")).index(theDoc,
159 (new UnIndexedSearchTerm("description", "search_content", "description",
160 "description", "description")).index(theDoc, entity);
161 (new UnIndexedSearchTerm("webdb_create", "search_irrelevant",
162 "creationDate", "creationDate", "creationDate")).index(theDoc, entity);
164 (new ContentSearchTerm("content_data", "search_content", "content", "", "")).indexValue(theDoc,
165 entity.getFieldValue("content_data") + " " + entity.getFieldValue("description") +
166 " " + entity.getFieldValue("title"));
168 (new TopicSearchTerm()).index(theDoc, entity);
170 (new ImagesSearchTerm()).index(theDoc, entity);
172 (new AudioSearchTerm()).index(theDoc, entity);
174 (new VideoSearchTerm()).index(theDoc, entity);
176 //comments-just aggregate all relevant fields
177 //removed until i get a chance to do this right
178 //String commentsAggregate = "";
179 //TemplateModel comments=entity.get("to_comments");
180 //if (comments != null){
181 // while (((TemplateListModel)comments).hasNext()){
182 // TemplateModel aComment = ((TemplateListModel)comments).next();
183 // commentsAggregate = commentsAggregate + " " + ((TemplateHashModel)aComment).get("title").toString()
184 // + " " + ((TemplateHashModel)aComment).get("creator").toString()
185 // + " " + ((TemplateHashModel)aComment).get("text").toString();
188 //theDoc.add(Field.UnStored("comments",commentsAggregate));
189 indexWriter.addDocument(theDoc);
191 catch (Throwable t) {
192 aLogger.error("Error while indexing content: " + t.getMessage(), t);
195 if (indexWriter != null) {
199 catch (Throwable t) {
200 aLogger.warn("Error while closing indexWriter", t);
204 if (indexFile!=null) {
206 FSDirectory theIndexDir = FSDirectory.getDirectory(indexFile, false);
208 if (IndexReader.isLocked(theIndexDir)) {
209 IndexReader.unlock(theIndexDir);
212 catch (Throwable t) {
213 aLogger.warn("Error while unlocking index", t);
218 endTime = System.currentTimeMillis();
220 aLogger.debug(" IndexTime: " + (endTime - startTime) + " ms<br>");