1 24 package org.ofbiz.content.search; 25 26 import java.io.IOException ; 27 import java.sql.Timestamp ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Locale ; 33 import java.util.Map ; 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 57 58 public class ContentDocument { 59 60 static char dirSep = System.getProperty("file.separator").charAt(0); 61 public static final String module = ContentDocument.class.getName(); 62 63 public static Document Document(String id, GenericDelegator delegator) throws InterruptedException { 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 map = new HashMap (); 75 doc = Document(content, map); 76 return doc; 77 } 78 79 public static Document Document(GenericValue content, Map context) 80 throws InterruptedException { 81 Document doc = null; 82 doc = new Document(); 84 String contentId = content.getString("contentId"); 85 doc.add(Field.Keyword("contentId", contentId)); 86 Timestamp modDate = (Timestamp ) content.get("lastModifiedDate"); 92 if (modDate == null) { 93 modDate = (Timestamp ) content.get("createdDate"); 94 } 95 if (modDate != null) { 96 doc.add(Field.Keyword("modified", modDate.toString())); 97 } 98 String contentName = content.getString("contentName"); 99 if (UtilValidate.isNotEmpty(contentName)) 100 doc.add(Field.Text("title", contentName)); 101 String description = content.getString("description"); 102 if (UtilValidate.isNotEmpty(description)) 103 doc.add(Field.Text("description", description)); 104 List ancestorList = new ArrayList (); 105 GenericDelegator delegator = content.getDelegator(); 106 ContentWorker.getContentAncestryAll(delegator, contentId, "WEB_SITE_PUB_PT", "TO", ancestorList); 107 String ancestorString = StringUtil.join(ancestorList, " "); 108 if (UtilValidate.isNotEmpty(ancestorString)) { 111 Field field = Field.UnStored("site", ancestorString); 112 doc.add(field); 115 } 116 boolean retVal = indexDataResource(content, doc, context); 117 if (!retVal) 120 doc = null; 121 return doc; 122 } 123 124 public static boolean indexDataResource(GenericValue content, Document doc, 125 Map context) { 126 GenericDelegator delegator = content.getDelegator(); 127 String contentId = content.getString("contentId"); 128 String dataResourceId = content.getString("dataResourceId"); 131 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 badIndexList = (List ) context.get("badIndexList"); 138 badIndexList.add(contentId + " - " + e.getMessage()); 139 return false; 141 } 142 if (dataResource == null) { 143 List badIndexList = (List ) context.get("badIndexList"); 144 badIndexList.add(contentId + " - dataResource is null."); 145 return false; 147 } 148 String mimeTypeId = dataResource.getString("mimeTypeId"); 149 if (UtilValidate.isEmpty(mimeTypeId)) { 150 mimeTypeId = "text/html"; 151 } 152 Locale locale = Locale.getDefault(); 153 String currentLocaleString = dataResource.getString("localeString"); 154 if (UtilValidate.isNotEmpty(currentLocaleString)) { 155 locale = UtilMisc.parseLocale(currentLocaleString); 156 } 157 String 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 badIndexList = (List ) context.get("badIndexList"); 163 badIndexList.add(contentId + " - " + e.getMessage()); 164 return false; 166 } catch (IOException e2) { 167 Debug.logError(e2, module); 168 List badIndexList = (List ) context.get("badIndexList"); 169 badIndexList.add(contentId + " - " + e2.getMessage()); 170 return false; 172 } 173 if (UtilValidate.isNotEmpty(text)) { 175 Field field = Field.UnStored("content", text); 176 doc.add(field); 178 } 179 List featureDataResourceList = null; 180 try { 181 featureDataResourceList = content.getRelatedCache("ProductFeatureDataResource"); 182 } catch (GenericEntityException e) { 183 Debug.logError(e, module); 184 List badIndexList = (List ) context.get("badIndexList"); 185 badIndexList.add(contentId + " - " + e.getMessage()); 186 return false; 187 } 188 List featureList = new ArrayList (); 189 Iterator iter = featureDataResourceList.iterator(); 190 while (iter.hasNext()) { 191 GenericValue productFeatureDataResource = (GenericValue) iter .next(); 192 String feature = productFeatureDataResource.getString("productFeatureId"); 193 featureList.add(feature); 194 } 195 String featureString = StringUtil.join(featureList, " "); 196 if (UtilValidate.isNotEmpty(featureString)) { 198 Field field = Field.UnStored("feature", featureString); 199 doc.add(field); 200 } 201 return true; 202 } 203 } 204 | Popular Tags |