KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > content > search > SearchWorker


1 /*
2  * $Id: SearchWorker.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.content.search;
25
26 import java.io.FileNotFoundException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import org.ofbiz.base.util.Debug;
34 import org.ofbiz.base.util.UtilDateTime;
35 import org.ofbiz.base.util.UtilMisc;
36 import org.ofbiz.base.util.UtilProperties;
37 import org.ofbiz.base.util.UtilValidate;
38 import org.ofbiz.content.content.ContentWorker;
39 import org.ofbiz.entity.GenericDelegator;
40 import org.ofbiz.entity.GenericEntityException;
41 import org.ofbiz.entity.GenericValue;
42
43 import org.apache.lucene.analysis.standard.StandardAnalyzer;
44 import org.apache.lucene.document.Document;
45 import org.apache.lucene.index.IndexReader;
46 import org.apache.lucene.index.IndexWriter;
47 import org.apache.lucene.index.Term;
48
49
50
51 /**
52  * SearchWorker Class
53  *
54  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a> Hacked from Lucene demo file
55  * @version $Rev: 5462 $
56  * @since 3.1
57  *
58  *
59  */

60 public class SearchWorker {
61
62     public static final String JavaDoc module = SearchWorker.class.getName();
63
64         public static Map JavaDoc indexTree(GenericDelegator delegator, String JavaDoc siteId, Map JavaDoc context, String JavaDoc path) throws Exception JavaDoc {
65
66             Map JavaDoc results = new HashMap JavaDoc();
67             GenericValue content = delegator.makeValue("Content", UtilMisc.toMap("contentId", siteId));
68                 if (Debug.infoOn()) Debug.logInfo("in indexTree, siteId:" + siteId + " content:" + content, module);
69             List JavaDoc siteList = ContentWorker.getAssociatedContent(content, "From", UtilMisc.toList("SUBSITE", "PUBLISH_LINK"), null, UtilDateTime.nowTimestamp().toString(), null);
70         //if (Debug.infoOn()) Debug.logInfo("in indexTree, siteList:" + siteList, module);
71
if (siteList != null) {
72                 Iterator JavaDoc iter = siteList.iterator();
73                 while (iter.hasNext()) {
74                     GenericValue siteContent = (GenericValue)iter.next();
75                     String JavaDoc siteContentId = siteContent.getString("contentId");
76                     List JavaDoc subContentList = ContentWorker.getAssociatedContent(siteContent, "From", UtilMisc.toList("SUBSITE", "PUBLISH_LINK", "SUB_CONTENT"), null, UtilDateTime.nowTimestamp().toString(), null);
77             //if (Debug.infoOn()) Debug.logInfo("in indexTree, subContentList:" + subContentList, module);
78
if (subContentList != null) {
79                         List JavaDoc contentIdList = new ArrayList JavaDoc();
80                         Iterator JavaDoc iter2 = subContentList.iterator();
81                         while (iter2.hasNext()) {
82                             GenericValue subContent = (GenericValue)iter2.next();
83                             contentIdList.add(subContent.getString("contentId"));
84                         }
85                 //if (Debug.infoOn()) Debug.logInfo("in indexTree, contentIdList:" + contentIdList, module);
86
indexContentList(contentIdList, delegator, context);
87         
88                         String JavaDoc subSiteId = siteContent.getString("contentId");
89                         indexTree(delegator, subSiteId, context, path);
90                     } else {
91                         List JavaDoc badIndexList = (List JavaDoc)context.get("badIndexList");
92                         badIndexList.add(siteContentId + " had no sub-entities.");
93                     }
94                 }
95             } else {
96                 List JavaDoc badIndexList = (List JavaDoc)context.get("badIndexList");
97                 badIndexList.add(siteId + " had no sub-entities.");
98             }
99             results.put("badIndexList", context.get("badIndexList"));
100             results.put("goodIndexCount", context.get("goodIndexCount"));
101             //if (Debug.infoOn()) Debug.logInfo("in indexTree, results:" + results, module);
102
return results;
103         }
104     
105     public static void indexContentList(List JavaDoc idList, GenericDelegator delegator, Map JavaDoc context) throws Exception JavaDoc {
106         String JavaDoc path = null;
107         indexContentList(delegator, context, idList, path);
108     }
109     
110     public static void indexContentList(GenericDelegator delegator, Map JavaDoc context, List JavaDoc idList, String JavaDoc path) throws Exception JavaDoc {
111         String JavaDoc indexAllPath = getIndexPath(path);
112         if (Debug.infoOn())
113             Debug.logInfo("in indexContent, indexAllPath:" + indexAllPath, module);
114         GenericValue content = null;
115         // Delete existing documents
116
Iterator JavaDoc iter = null;
117         List JavaDoc contentList = null;
118         IndexReader reader = null;
119         try {
120             reader = IndexReader.open(indexAllPath);
121         } catch (Exception JavaDoc e) {
122             // ignore
123
}
124         //if (Debug.infoOn()) Debug.logInfo("in indexContent, reader:" +
125
// reader, module);
126
contentList = new ArrayList JavaDoc();
127         iter = idList.iterator();
128         while (iter.hasNext()) {
129             String JavaDoc id = (String JavaDoc) iter.next();
130             if (Debug.infoOn())
131                 Debug.logInfo("in indexContent, id:" + id, module);
132             try {
133                 content = delegator.findByPrimaryKeyCache("Content", UtilMisc .toMap("contentId", id));
134                 if (content != null) {
135                     if (reader != null) {
136                         deleteContentDocument(content, reader);
137                     }
138                     contentList.add(content);
139                 }
140             } catch (GenericEntityException e) {
141                 Debug.logError(e, module);
142                 return;
143             }
144         }
145         if (reader != null) {
146             reader.close();
147         }
148         // Now create
149
IndexWriter writer = null;
150         try {
151             writer = new IndexWriter(indexAllPath, new StandardAnalyzer(), false);
152         } catch (Exception JavaDoc e) {
153             writer = new IndexWriter(indexAllPath, new StandardAnalyzer(), true);
154         }
155         //if (Debug.infoOn()) Debug.logInfo("in indexContent, writer:" +
156
// writer, module);
157
iter = contentList.iterator();
158         while (iter.hasNext()) {
159             content = (GenericValue) iter.next();
160             indexContent(delegator, context, content, writer);
161         }
162         writer.optimize();
163         writer.close();
164     }
165     
166     
167     public static void deleteContentDocument(GenericValue content, String JavaDoc path) throws Exception JavaDoc {
168         String JavaDoc indexAllPath = null;
169         indexAllPath = getIndexPath(path);
170         IndexReader reader = IndexReader.open(indexAllPath);
171             deleteContentDocument(content, reader);
172             reader.close();
173     }
174     
175     public static void deleteContentDocument(GenericValue content, IndexReader reader) throws Exception JavaDoc {
176             String JavaDoc contentId = content.getString("contentId");
177         Term term = new Term("contentId", contentId);
178         if (Debug.infoOn()) Debug.logInfo("in indexContent, term:" + term, module);
179         int qtyDeleted = reader.delete(term);
180         if (Debug.infoOn()) Debug.logInfo("in indexContent, qtyDeleted:" + term, module);
181         String JavaDoc dataResourceId = content.getString("dataResourceId");
182         if (dataResourceId != null) {
183             deleteDataResourceDocument(dataResourceId, reader);
184         }
185
186             return;
187     }
188     
189
190     public static void deleteDataResourceDocument(String JavaDoc dataResourceId, IndexReader reader) throws Exception JavaDoc {
191         Term term = new Term("dataResourceId", dataResourceId);
192         if (Debug.infoOn()) Debug.logInfo("in indexContent, term:" + term, module);
193         int qtyDeleted = reader.delete(term);
194         if (Debug.infoOn()) Debug.logInfo("in indexContent, qtyDeleted:" + term, module);
195
196             return;
197     }
198
199     public static void indexContent(GenericDelegator delegator, Map JavaDoc context, GenericValue content, String JavaDoc path) throws Exception JavaDoc {
200         String JavaDoc indexAllPath = getIndexPath(path);
201         IndexWriter writer = null;
202         try {
203             writer = new IndexWriter(indexAllPath, new StandardAnalyzer(), false);
204                     if (Debug.infoOn()) Debug.logInfo("Used old directory:" + indexAllPath, module);
205         } catch(FileNotFoundException JavaDoc e) {
206             writer = new IndexWriter(indexAllPath, new StandardAnalyzer(), true);
207                     if (Debug.infoOn()) Debug.logInfo("Created new directory:" + indexAllPath, module);
208         }
209         
210         indexContent(delegator, context, content, writer);
211                 writer.optimize();
212                 writer.close();
213         return;
214     }
215     
216     public static void indexContent(GenericDelegator delegator, Map JavaDoc context, GenericValue content, IndexWriter writer) throws Exception JavaDoc {
217         Document doc = ContentDocument.Document(content, context);
218         //if (Debug.infoOn()) Debug.logInfo("in indexContent, content:" + content, module);
219
if (doc != null) {
220                 writer.addDocument(doc);
221                 Integer JavaDoc goodIndexCount = (Integer JavaDoc)context.get("goodIndexCount");
222                 int newCount = goodIndexCount.intValue() + 1;
223                 Integer JavaDoc newIndexCount = new Integer JavaDoc(newCount);
224                 context.put("goodIndexCount", newIndexCount);
225             }
226             /*
227             String dataResourceId = content.getString("dataResourceId");
228             if (UtilValidate.isNotEmpty(dataResourceId)) {
229                 indexDataResource(delegator, context, dataResourceId, writer);
230             }
231             */

232         
233             return;
234     }
235     
236     public static void indexDataResource(GenericDelegator delegator, Map JavaDoc context, String JavaDoc id) throws Exception JavaDoc {
237         String JavaDoc path = null;
238         indexDataResource(delegator, context, id, path );
239     }
240     
241     public static void indexDataResource(GenericDelegator delegator, Map JavaDoc context, String JavaDoc id, String JavaDoc path) throws Exception JavaDoc {
242         String JavaDoc indexAllPath = getIndexPath(path);
243         IndexWriter writer = null;
244         try {
245             writer = new IndexWriter(indexAllPath, new StandardAnalyzer(), false);
246         } catch(FileNotFoundException JavaDoc e) {
247             writer = new IndexWriter(indexAllPath, new StandardAnalyzer(), true);
248         }
249         indexDataResource(delegator, context, id, writer);
250         writer.optimize();
251             writer.close();
252
253     }
254
255     public static void indexDataResource(GenericDelegator delegator, Map JavaDoc context, String JavaDoc id, IndexWriter writer) throws Exception JavaDoc {
256         Document doc = DataResourceDocument.Document(id, delegator, context);
257         writer.addDocument(doc);
258     }
259     
260     public static String JavaDoc getIndexPath(String JavaDoc path) {
261         String JavaDoc indexAllPath = path;
262         if (UtilValidate.isEmpty(indexAllPath))
263             indexAllPath = UtilProperties.getPropertyValue("search", "defaultIndex");
264         if (UtilValidate.isEmpty(indexAllPath))
265             indexAllPath = "index";
266         return indexAllPath;
267
268     }
269 }
270
Popular Tags