KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ContentDocument.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.IOException JavaDoc;
27 import java.sql.Timestamp JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Locale JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import org.ofbiz.base.util.Debug;
36 import org.ofbiz.base.util.GeneralException;
37 import org.ofbiz.base.util.StringUtil;
38 import org.ofbiz.base.util.UtilMisc;
39 import org.ofbiz.base.util.UtilValidate;
40 import org.ofbiz.content.content.ContentWorker;
41 import org.ofbiz.entity.GenericDelegator;
42 import org.ofbiz.entity.GenericEntityException;
43 import org.ofbiz.entity.GenericValue;
44
45 import org.apache.lucene.document.Document;
46 import org.apache.lucene.document.Field;
47
48 /**
49  * ContentDocument Class
50  *
51  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
52  * @version $Rev: 5462 $
53  * @since 3.1
54  *
55  *
56  */

57
58 public class ContentDocument {
59
60     static char dirSep = System.getProperty("file.separator").charAt(0);
61     public static final String JavaDoc module = ContentDocument.class.getName();
62     
63     public static Document Document(String JavaDoc id, GenericDelegator delegator) throws InterruptedException JavaDoc {
64         
65         Document doc = null;
66         GenericValue content = null;
67         try {
68             content = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId",id));
69         } catch(GenericEntityException e) {
70             Debug.logError(e, module);
71             return doc;
72         }
73         
74                 Map JavaDoc map = new HashMap JavaDoc();
75         doc = Document(content, map);
76                 return doc;
77     }
78     
79     public static Document Document(GenericValue content, Map JavaDoc context)
80             throws InterruptedException JavaDoc {
81         Document doc = null;
82         // make a new, empty document
83
doc = new Document();
84         String JavaDoc contentId = content.getString("contentId");
85         doc.add(Field.Keyword("contentId", contentId));
86         // Add the last modified date of the file a field named "modified". Use
87
// a
88
// Keyword field, so that it's searchable, but so that no attempt is
89
// made
90
// to tokenize the field into words.
91
Timestamp JavaDoc modDate = (Timestamp JavaDoc) content.get("lastModifiedDate");
92         if (modDate == null) {
93             modDate = (Timestamp JavaDoc) content.get("createdDate");
94         }
95         if (modDate != null) {
96             doc.add(Field.Keyword("modified", modDate.toString()));
97         }
98         String JavaDoc contentName = content.getString("contentName");
99         if (UtilValidate.isNotEmpty(contentName))
100             doc.add(Field.Text("title", contentName));
101         String JavaDoc description = content.getString("description");
102         if (UtilValidate.isNotEmpty(description))
103             doc.add(Field.Text("description", description));
104         List JavaDoc ancestorList = new ArrayList JavaDoc();
105         GenericDelegator delegator = content.getDelegator();
106         ContentWorker.getContentAncestryAll(delegator, contentId, "WEB_SITE_PUB_PT", "TO", ancestorList);
107         String JavaDoc ancestorString = StringUtil.join(ancestorList, " ");
108         //Debug.logInfo("in ContentDocument, ancestorString:" + ancestorString,
109
// module);
110
if (UtilValidate.isNotEmpty(ancestorString)) {
111             Field field = Field.UnStored("site", ancestorString);
112             //Debug.logInfo("in ContentDocument, field:" + field.stringValue(),
113
// module);
114
doc.add(field);
115         }
116         boolean retVal = indexDataResource(content, doc, context);
117         //Debug.logInfo("in DataResourceDocument, context.badIndexList:" +
118
// context.get("badIndexList"), module);
119
if (!retVal)
120             doc = null;
121         return doc;
122     }
123
124     public static boolean indexDataResource(GenericValue content, Document doc,
125             Map JavaDoc context) {
126         GenericDelegator delegator = content.getDelegator();
127         String JavaDoc contentId = content.getString("contentId");
128         //Debug.logInfo("in ContentDocument, contentId:" + contentId,
129
// module);
130
String JavaDoc dataResourceId = content.getString("dataResourceId");
131         //Debug.logInfo("in ContentDocument, dataResourceId:" + dataResourceId, module);
132
GenericValue dataResource = null;
133         try {
134             dataResource = delegator.findByPrimaryKeyCache("DataResource", UtilMisc.toMap("dataResourceId", dataResourceId));
135         } catch (GenericEntityException e) {
136             Debug.logError(e, module);
137             List JavaDoc badIndexList = (List JavaDoc) context.get("badIndexList");
138             badIndexList.add(contentId + " - " + e.getMessage());
139             //Debug.logInfo("in DataResourceDocument, badIndexList:" + badIndexList, module);
140
return false;
141         }
142         if (dataResource == null) {
143             List JavaDoc badIndexList = (List JavaDoc) context.get("badIndexList");
144             badIndexList.add(contentId + " - dataResource is null.");
145             //Debug.logInfo("in DataResourceDocument, badIndexList:" + badIndexList, module);
146
return false;
147         }
148         String JavaDoc mimeTypeId = dataResource.getString("mimeTypeId");
149         if (UtilValidate.isEmpty(mimeTypeId)) {
150             mimeTypeId = "text/html";
151         }
152         Locale JavaDoc locale = Locale.getDefault();
153         String JavaDoc currentLocaleString = dataResource.getString("localeString");
154         if (UtilValidate.isNotEmpty(currentLocaleString)) {
155             locale = UtilMisc.parseLocale(currentLocaleString);
156         }
157         String JavaDoc text = null;
158         try {
159             text = ContentWorker.renderContentAsTextCache(delegator, contentId, context, content, locale, mimeTypeId);
160         } catch (GeneralException e) {
161             Debug.logError(e, module);
162             List JavaDoc badIndexList = (List JavaDoc) context.get("badIndexList");
163             badIndexList.add(contentId + " - " + e.getMessage());
164             //Debug.logInfo("in DataResourceDocument, badIndexList:" + badIndexList, module);
165
return false;
166         } catch (IOException JavaDoc e2) {
167             Debug.logError(e2, module);
168             List JavaDoc badIndexList = (List JavaDoc) context.get("badIndexList");
169             badIndexList.add(contentId + " - " + e2.getMessage());
170             //Debug.logInfo("in DataResourceDocument, badIndexList:" + badIndexList, module);
171
return false;
172         }
173         //Debug.logInfo("in DataResourceDocument, text:" + text, module);
174
if (UtilValidate.isNotEmpty(text)) {
175             Field field = Field.UnStored("content", text);
176             //Debug.logInfo("in ContentDocument, field:" + field.stringValue(), module);
177
doc.add(field);
178         }
179         List JavaDoc featureDataResourceList = null;
180         try {
181             featureDataResourceList = content.getRelatedCache("ProductFeatureDataResource");
182         } catch (GenericEntityException e) {
183             Debug.logError(e, module);
184             List JavaDoc badIndexList = (List JavaDoc) context.get("badIndexList");
185             badIndexList.add(contentId + " - " + e.getMessage());
186             return false;
187         }
188         List JavaDoc featureList = new ArrayList JavaDoc();
189         Iterator JavaDoc iter = featureDataResourceList.iterator();
190         while (iter.hasNext()) {
191             GenericValue productFeatureDataResource = (GenericValue) iter .next();
192             String JavaDoc feature = productFeatureDataResource.getString("productFeatureId");
193             featureList.add(feature);
194         }
195         String JavaDoc featureString = StringUtil.join(featureList, " ");
196         //Debug.logInfo("in ContentDocument, featureString:" + featureString, module);
197
if (UtilValidate.isNotEmpty(featureString)) {
198             Field field = Field.UnStored("feature", featureString);
199             doc.add(field);
200         }
201         return true;
202     }
203 }
204
Popular Tags