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 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.
32 package mircoders.producer;
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;
42 import freemarker.template.*;
46 import mir.producer.*;
47 //import mir.generator.*;
48 import mircoders.global.*;
49 import mircoders.localizer.*;
51 import mir.entity.adapter.*;
52 import mircoders.entity.*;
53 import mircoders.storage.*;
56 public class IndexingProducerNode implements ProducerNode {
57 private String contentKey;
58 private String indexPath;
61 public IndexingProducerNode(String aContentKey, String pathToIndex) {
62 contentKey = aContentKey;
63 indexPath=pathToIndex;
66 public void produce(Map aValueMap, String aVerb, PrintWriter aLogger) throws ProducerFailure {
67 IndexWriter indexWriter;
74 startTime = System.currentTimeMillis();
77 data = ParameterExpander.findValueForKey( aValueMap, contentKey );
79 if (! (data instanceof EntityAdapter)) {
80 throw new ProducerFailure("IndexingProducerNode: value of '"+contentKey+"' is not an EntityAdapter, but an " + data.getClass().getName(), null);
83 entity = ((EntityAdapter) data).getEntity();
84 if (! (entity instanceof EntityContent)) {
85 throw new ProducerFailure("IndexingProducerNode: value of '"+contentKey+"' is not a content EntityAdapter, but a " + entity.getClass().getName() + " adapter", null);
87 aLogger.println("Indexing " + (String) entity.getValue("id") + " into " + indexPath);
90 IndexReader indexReader = IndexReader.open(indexPath);
91 indexReader.delete(new Term("id",entity.getValue("id")));
94 indexWriter = new IndexWriter(indexPath, new StandardAnalyzer(), false);
95 Document theDoc = new Document();
97 // Keyword is stored and indexed, but not tokenized
98 // Text is tokenized,stored, indexed
99 // Unindexed is not tokenized or indexed, only stored
100 // Unstored is tokenized and indexed, but not stored
102 theDoc.add(Field.Keyword("id",entity.getValue("id")));
103 theDoc.add(Field.Keyword("where",entity.getValue("publish_path")+entity.getValue("id")+".shtml"));
104 theDoc.add(Field.Text("creator",entity.getValue("creator")));
105 theDoc.add(Field.Text("title",entity.getValue("title")));
106 theDoc.add(Field.Keyword("webdb_create",entity.getValue("webdb_create_formatted")));
107 theDoc.add(Field.UnStored("content_and_description",entity.getValue("description")+entity.getValue("content_data")));
110 TemplateModel topics=entity.get("to_topics");
111 aLogger.println("THE CLASS NAME WAS: "+entity.get("to_topics").getClass().getName());
112 while (((TemplateListModel)topics).hasNext()){
113 theDoc.add(Field.UnStored("topic",((TemplateHashModel)((TemplateListModel)topics).next()).get("title").toString()));
120 TemplateModel images=entity.get("to_media_images");
122 theDoc.add(Field.UnStored("media","images"));
125 TemplateModel audio=entity.get("to_media_audio");
127 theDoc.add(Field.UnStored("media","audio"));
130 TemplateModel video=entity.get("to_media_video");
132 theDoc.add(Field.UnStored("media","video"));
135 //comments-just aggregate all relevant fields
136 String commentsAggregate = "";
137 TemplateModel comments=entity.get("to_comments");
138 if (comments != null){
139 while (((TemplateListModel)comments).hasNext()){
140 TemplateModel aComment = ((TemplateListModel)comments).next();
141 commentsAggregate = commentsAggregate + " " + ((TemplateHashModel)aComment).get("title").toString()
142 + " " + ((TemplateHashModel)aComment).get("creator").toString()
143 + " " + ((TemplateHashModel)aComment).get("text").toString();
146 theDoc.add(Field.UnStored("comments",commentsAggregate));
148 indexWriter.addDocument(theDoc);
152 catch (Throwable t) {
153 aLogger.println("Error while indexing content: " + t.getMessage());
154 t.printStackTrace(aLogger);
155 //should remove index lock here.....jd
156 throw new ProducerFailure(t.getMessage(), t);
162 endTime = System.currentTimeMillis();
164 aLogger.println(" IndexTime: " + (endTime-startTime) + " ms<br>");