made the indexPath parameter-expanded, so we can reference
[mir.git] / source / mircoders / producer / UnIndexingProducerNode.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
38 import org.apache.lucene.index.*;
39 import org.apache.lucene.document.Document;
40 import org.apache.lucene.document.Field;
41 import org.apache.lucene.store.FSDirectory;
42
43 import freemarker.template.*;
44
45
46 import mir.util.*;
47 import mir.log.*;
48 import mir.producer.*;
49 //import mir.generator.*;
50 import mircoders.global.*;
51 import mircoders.localizer.*;
52 import mir.entity.*;
53 import mir.entity.adapter.*;
54 import mircoders.entity.*;
55 import mircoders.storage.*;
56 import mircoders.search.*;
57
58
59 public class UnIndexingProducerNode implements ProducerNode {
60   private String contentKey;
61   private String indexPath;
62
63
64   public UnIndexingProducerNode(String aContentKey, String pathToIndex) {
65     contentKey = aContentKey;
66     indexPath=pathToIndex;
67   }
68
69   public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) throws ProducerFailure {
70     IndexReader indexReader = null;
71     Object data;
72     Entity entity;
73     String index=null;
74
75     long startTime;
76     long endTime;
77
78     startTime = System.currentTimeMillis();
79
80     try {
81       index = ParameterExpander.expandExpression(aValueMap, indexPath);
82       data = ParameterExpander.findValueForKey( aValueMap, contentKey );
83
84       if (! (data instanceof EntityAdapter)) {
85         throw new ProducerFailure("UnIndexingProducerNode: value of '"+contentKey+"' is not an EntityAdapter, but an " + data.getClass().getName(), null);
86       }
87
88       entity = ((EntityAdapter) data).getEntity();
89       if (! (entity instanceof EntityContent)) {
90         throw new ProducerFailure("UnIndexingProducerNode: value of '"+contentKey+"' is not a content EntityAdapter, but a " + entity.getClass().getName() + " adapter", null);
91       }
92       aLogger.info("UnIndexing " + (String) entity.getValue("id") + " out of " + index);
93
94       indexReader = IndexReader.open(index);
95       indexReader.delete(new Term("id",entity.getValue("id")));
96       indexReader.close();
97       
98     }
99     catch (Throwable t) {
100       aLogger.error("Error while unindexing content: " + t.getMessage());
101       t.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(aLogger, LoggerWrapper.DEBUG_MESSAGE)));
102     }
103     finally {
104       if (indexReader != null){
105         try{
106           indexReader.close();
107         }
108         catch (Throwable t) {
109           aLogger.warn("Error while closing indexReader: " + t.getMessage());
110         }
111
112       }
113
114       try{
115         if (index != null){
116           FSDirectory theIndexDir=FSDirectory.getDirectory(index,false);
117           if (indexReader.isLocked(theIndexDir)){
118             indexReader.unlock(theIndexDir);
119           }
120         }
121       }
122       catch (Throwable t) {
123         aLogger.warn("Error while unlocking index: " + t.getMessage());
124       }
125     }
126
127
128
129
130     endTime = System.currentTimeMillis();
131
132     aLogger.info("  UnIndexTime: " + (endTime-startTime) + " ms<br>");
133   }
134 }
135
136
137
138
139
140
141
142
143
144