- producer links are moved to an "advanced" page, not intended for normal
[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 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.
30  */
31
32 package mircoders.producer;
33
34 import java.util.*;
35 import java.io.*;
36
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;
41
42 import freemarker.template.*;
43
44
45 import mir.util.*;
46 import mir.producer.*;
47 //import mir.generator.*;
48 import mircoders.global.*;
49 import mircoders.localizer.*;
50 import mir.entity.*;
51 import mir.entity.adapter.*;
52 import mircoders.entity.*;
53 import mircoders.storage.*;
54
55
56 public class IndexingProducerNode implements ProducerNode {
57   private String contentKey;
58   private String indexPath;
59
60
61   public IndexingProducerNode(String aContentKey, String pathToIndex) {
62     contentKey = aContentKey;
63     indexPath=pathToIndex;
64   }
65
66   public void produce(Map aValueMap, String aVerb, PrintWriter aLogger) throws ProducerFailure {
67     IndexWriter indexWriter;
68     Object data;
69     Entity entity;
70
71     long startTime;
72     long endTime;
73
74     startTime = System.currentTimeMillis();
75
76     try {
77       data = ParameterExpander.findValueForKey( aValueMap, contentKey );
78
79       if (! (data instanceof EntityAdapter)) {
80         throw new ProducerFailure("IndexingProducerNode: value of '"+contentKey+"' is not an EntityAdapter, but an " + data.getClass().getName(), null);
81       }
82
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);
86       }
87       aLogger.println("Indexing " + (String) entity.getValue("id") + " into " + indexPath);
88       aLogger.flush();
89
90       IndexReader indexReader = IndexReader.open(indexPath);
91       indexReader.delete(new Term("id",entity.getValue("id")));
92       indexReader.close();
93
94       indexWriter = new IndexWriter(indexPath, new StandardAnalyzer(), false);
95       Document theDoc =  new Document();
96
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
101
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")));
108
109       //topics
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()));
114       }
115
116
117       //media
118
119       //images
120       TemplateModel images=entity.get("to_media_images");
121       if (images != null){
122           theDoc.add(Field.UnStored("media","images"));
123       }
124       //audio
125       TemplateModel audio=entity.get("to_media_audio");
126       if (audio != null){
127           theDoc.add(Field.UnStored("media","audio"));
128       }
129       //video
130       TemplateModel video=entity.get("to_media_video");
131       if (video != null){
132           theDoc.add(Field.UnStored("media","video"));
133       }
134
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();
144         }
145       }
146       theDoc.add(Field.UnStored("comments",commentsAggregate));
147
148       indexWriter.addDocument(theDoc);
149       indexWriter.close();
150
151     }
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);
157     }
158
159
160
161
162     endTime = System.currentTimeMillis();
163
164     aLogger.println("  IndexTime: " + (endTime-startTime) + " ms<br>");
165     aLogger.flush();
166   }
167 }
168
169
170