KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > product > product > KeywordIndex


1 /*
2  * $Id: KeywordIndex.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  */

25 package org.ofbiz.product.product;
26
27 import java.io.IOException JavaDoc;
28 import java.sql.Timestamp JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.LinkedList JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Set JavaDoc;
36 import java.util.TreeMap JavaDoc;
37
38 import org.ofbiz.base.util.Debug;
39 import org.ofbiz.base.util.GeneralException;
40 import org.ofbiz.base.util.UtilDateTime;
41 import org.ofbiz.base.util.UtilMisc;
42 import org.ofbiz.base.util.UtilProperties;
43 import org.ofbiz.content.data.DataResourceWorker;
44 import org.ofbiz.entity.GenericDelegator;
45 import org.ofbiz.entity.GenericEntityException;
46 import org.ofbiz.entity.GenericValue;
47 import org.ofbiz.entity.util.EntityUtil;
48
49 /**
50  * Does indexing in preparation for a keyword search.
51  *
52  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
53  * @version $Rev: 5462 $
54  * @since 2.0
55  */

56 public class KeywordIndex {
57     
58     public static final String JavaDoc module = KeywordIndex.class.getName();
59
60     public static void indexKeywords(GenericValue product, boolean doAll) throws GenericEntityException {
61         if (product == null) return;
62         Timestamp JavaDoc nowTimestamp = UtilDateTime.nowTimestamp();
63         
64         if (!doAll) {
65             if ("N".equals(product.getString("autoCreateKeywords"))) {
66                 return;
67             }
68             if ("Y".equals(product.getString("isVariant")) && "true".equals(UtilProperties.getPropertyValue("prodsearch", "index.ignore.variants"))) {
69                 return;
70             }
71             Timestamp JavaDoc salesDiscontinuationDate = product.getTimestamp("salesDiscontinuationDate");
72             if (salesDiscontinuationDate != null && salesDiscontinuationDate.before(nowTimestamp) &&
73                     "true".equals(UtilProperties.getPropertyValue("prodsearch", "index.ignore.discontinued.sales"))) {
74                 return;
75             }
76         }
77         
78         GenericDelegator delegator = product.getDelegator();
79         if (delegator == null) return;
80         String JavaDoc productId = product.getString("productId");
81
82         // get these in advance just once since they will be used many times for the multiple strings to index
83
String JavaDoc separators = KeywordSearch.getSeparators();
84         String JavaDoc stopWordBagOr = KeywordSearch.getStopWordBagOr();
85         String JavaDoc stopWordBagAnd = KeywordSearch.getStopWordBagAnd();
86         boolean removeStems = KeywordSearch.getRemoveStems();
87         Set JavaDoc stemSet = KeywordSearch.getStemSet();
88         
89         Map JavaDoc keywords = new TreeMap JavaDoc();
90         List JavaDoc strings = new ArrayList JavaDoc(50);
91
92         int pidWeight = 1;
93         try {
94             pidWeight = Integer.parseInt(UtilProperties.getPropertyValue("prodsearch", "index.weight.Product.productId", "1"));
95         } catch (Exception JavaDoc e) {
96             Debug.logWarning("Could not parse weight number: " + e.toString(), module);
97         }
98         keywords.put(product.getString("productId").toLowerCase(), new Long JavaDoc(pidWeight));
99
100         addWeightedKeywordSourceString(product, "productName", strings);
101         addWeightedKeywordSourceString(product, "internalName", strings);
102         addWeightedKeywordSourceString(product, "brandName", strings);
103         addWeightedKeywordSourceString(product, "description", strings);
104         addWeightedKeywordSourceString(product, "longDescription", strings);
105
106         if (!"0".equals(UtilProperties.getPropertyValue("prodsearch", "index.weight.ProductFeatureAndAppl.description", "1")) ||
107             !"0".equals(UtilProperties.getPropertyValue("prodsearch", "index.weight.ProductFeatureAndAppl.abbrev", "1")) ||
108             !"0".equals(UtilProperties.getPropertyValue("prodsearch", "index.weight.ProductFeatureAndAppl.idCode", "1"))) {
109             // get strings from attributes and features
110
Iterator JavaDoc productFeatureAndAppls = UtilMisc.toIterator(delegator.findByAnd("ProductFeatureAndAppl", UtilMisc.toMap("productId", productId)));
111             while (productFeatureAndAppls != null && productFeatureAndAppls.hasNext()) {
112                 GenericValue productFeatureAndAppl = (GenericValue) productFeatureAndAppls.next();
113                 addWeightedKeywordSourceString(productFeatureAndAppl, "description", strings);
114                 addWeightedKeywordSourceString(productFeatureAndAppl, "abbrev", strings);
115                 addWeightedKeywordSourceString(productFeatureAndAppl, "idCode", strings);
116             }
117         }
118
119         // ProductAttribute
120
if (!"0".equals(UtilProperties.getPropertyValue("prodsearch", "index.weight.ProductAttribute.attrName", "1")) ||
121                 !"0".equals(UtilProperties.getPropertyValue("prodsearch", "index.weight.ProductAttribute.attrValue", "1"))) {
122             Iterator JavaDoc productAttributes = UtilMisc.toIterator(delegator.findByAnd("ProductAttribute", UtilMisc.toMap("productId", productId)));
123             while (productAttributes != null && productAttributes.hasNext()) {
124                 GenericValue productAttribute = (GenericValue) productAttributes.next();
125                 addWeightedKeywordSourceString(productAttribute, "attrName", strings);
126                 addWeightedKeywordSourceString(productAttribute, "attrValue", strings);
127             }
128         }
129
130         // GoodIdentification
131
if (!"0".equals(UtilProperties.getPropertyValue("prodsearch", "index.weight.GoodIdentification.idValue", "1"))) {
132             Iterator JavaDoc goodIdentifications = UtilMisc.toIterator(delegator.findByAnd("GoodIdentification", UtilMisc.toMap("productId", productId)));
133             while (goodIdentifications != null && goodIdentifications.hasNext()) {
134                 GenericValue goodIdentification = (GenericValue) goodIdentifications.next();
135                 addWeightedKeywordSourceString(goodIdentification, "idValue", strings);
136             }
137         }
138         
139         // Variant Product IDs
140
if ("Y".equals(product.getString("isVirtual"))) {
141             if (!"0".equals(UtilProperties.getPropertyValue("prodsearch", "index.weight.Variant.Product.productId", "1"))) {
142                 Iterator JavaDoc variantProductAssocs = UtilMisc.toIterator(delegator.findByAnd("ProductAssoc", UtilMisc.toMap("productId", productId, "productAssocTypeId", "PRODUCT_VARIANT")));
143                 while (variantProductAssocs != null && variantProductAssocs.hasNext()) {
144                     GenericValue variantProductAssoc = (GenericValue) variantProductAssocs.next();
145                     int weight = 1;
146                     try {
147                         weight = Integer.parseInt(UtilProperties.getPropertyValue("prodsearch", "index.weight.Variant.Product.productId", "1"));
148                     } catch (Exception JavaDoc e) {
149                         Debug.logWarning("Could not parse weight number: " + e.toString(), module);
150                     }
151                     for (int i = 0; i < weight; i++) {
152                         strings.add(variantProductAssoc.getString("productIdTo"));
153                     }
154                 }
155             }
156         }
157         
158         String JavaDoc productContentTypes = UtilProperties.getPropertyValue("prodsearch", "index.include.ProductContentTypes");
159         List JavaDoc productContentTypeList = Arrays.asList(productContentTypes.split(","));
160         Iterator JavaDoc productContentTypeIter = productContentTypeList.iterator();
161         while (productContentTypeIter.hasNext()) {
162             String JavaDoc productContentTypeId = (String JavaDoc) productContentTypeIter.next();
163
164             int weight = 1;
165             try {
166                 weight = Integer.parseInt(UtilProperties.getPropertyValue("prodsearch", "index.weight.ProductContent." + productContentTypeId, "1"));
167             } catch (Exception JavaDoc e) {
168                 Debug.logWarning("Could not parse weight number: " + e.toString(), module);
169             }
170             
171             List JavaDoc productContentAndInfos = delegator.findByAnd("ProductContentAndInfo", UtilMisc.toMap("productId", productId, "productContentTypeId", productContentTypeId), null);
172             Iterator JavaDoc productContentAndInfoIter = productContentAndInfos.iterator();
173             while (productContentAndInfoIter.hasNext()) {
174                 GenericValue productContentAndInfo = (GenericValue) productContentAndInfoIter.next();
175                 addWeightedDataResourceString(productContentAndInfo, weight, strings, delegator, product);
176                 
177                 List JavaDoc alternateViews = productContentAndInfo.getRelated("ContentAssocDataResourceViewTo", UtilMisc.toMap("caContentAssocTypeId", "ALTERNATE_LOCALE"), UtilMisc.toList("-caFromDate"));
178                 alternateViews = EntityUtil.filterByDate(alternateViews, UtilDateTime.nowTimestamp(), "caFromDate", "caThruDate", true);
179                 Iterator JavaDoc alternateViewIter = alternateViews.iterator();
180                 while (alternateViewIter.hasNext()) {
181                     GenericValue thisView = (GenericValue) alternateViewIter.next();
182                     addWeightedDataResourceString(thisView, weight, strings, delegator, product);
183                 }
184             }
185         }
186         
187         Iterator JavaDoc strIter = strings.iterator();
188         while (strIter.hasNext()) {
189             String JavaDoc str = (String JavaDoc) strIter.next();
190             // call process keywords method here
191
KeywordSearch.processKeywordsForIndex(str, keywords, separators, stopWordBagAnd, stopWordBagOr, removeStems, stemSet);
192         }
193
194         List JavaDoc toBeStored = new LinkedList JavaDoc();
195         Iterator JavaDoc kiter = keywords.entrySet().iterator();
196         while (kiter.hasNext()) {
197             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) kiter.next();
198             GenericValue productKeyword = delegator.makeValue("ProductKeyword", UtilMisc.toMap("productId", product.getString("productId"), "keyword", entry.getKey(), "relevancyWeight", entry.getValue()));
199             toBeStored.add(productKeyword);
200         }
201         if (toBeStored.size() > 0) {
202             if (Debug.verboseOn()) Debug.logVerbose("[KeywordSearch.induceKeywords] Storing " + toBeStored.size() + " keywords for productId " + product.getString("productId"), module);
203             delegator.storeAll(toBeStored);
204         }
205     }
206     
207     public static void addWeightedDataResourceString(GenericValue drView, int weight, List JavaDoc strings, GenericDelegator delegator, GenericValue product) {
208         Map JavaDoc drContext = UtilMisc.toMap("product", product);
209         try {
210             String JavaDoc contentText = DataResourceWorker.renderDataResourceAsText(delegator, drView.getString("dataResourceId"), drContext, drView, null, null);
211             for (int i = 0; i < weight; i++) {
212                 strings.add(contentText);
213             }
214         } catch (IOException JavaDoc e1) {
215             Debug.logError(e1, "Error getting content text to index", module);
216         } catch (GeneralException e1) {
217             Debug.logError(e1, "Error getting content text to index", module);
218         }
219     }
220
221     public static void addWeightedKeywordSourceString(GenericValue value, String JavaDoc fieldName, List JavaDoc strings) {
222         if (value.getString(fieldName) != null) {
223             int weight = 1;
224
225             try {
226                 weight = Integer.parseInt(UtilProperties.getPropertyValue("prodsearch", "index.weight." + value.getEntityName() + "." + fieldName, "1"));
227             } catch (Exception JavaDoc e) {
228                 Debug.logWarning("Could not parse weight number: " + e.toString(), module);
229             }
230
231             for (int i = 0; i < weight; i++) {
232                 strings.add(value.getString(fieldName));
233             }
234         }
235     }
236 }
237
Popular Tags