1 24 package org.ofbiz.content.search; 25 26 import java.io.FileNotFoundException ; 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 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 60 public class SearchWorker { 61 62 public static final String module = SearchWorker.class.getName(); 63 64 public static Map indexTree(GenericDelegator delegator, String siteId, Map context, String path) throws Exception { 65 66 Map results = new HashMap (); 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 siteList = ContentWorker.getAssociatedContent(content, "From", UtilMisc.toList("SUBSITE", "PUBLISH_LINK"), null, UtilDateTime.nowTimestamp().toString(), null); 70 if (siteList != null) { 72 Iterator iter = siteList.iterator(); 73 while (iter.hasNext()) { 74 GenericValue siteContent = (GenericValue)iter.next(); 75 String siteContentId = siteContent.getString("contentId"); 76 List subContentList = ContentWorker.getAssociatedContent(siteContent, "From", UtilMisc.toList("SUBSITE", "PUBLISH_LINK", "SUB_CONTENT"), null, UtilDateTime.nowTimestamp().toString(), null); 77 if (subContentList != null) { 79 List contentIdList = new ArrayList (); 80 Iterator iter2 = subContentList.iterator(); 81 while (iter2.hasNext()) { 82 GenericValue subContent = (GenericValue)iter2.next(); 83 contentIdList.add(subContent.getString("contentId")); 84 } 85 indexContentList(contentIdList, delegator, context); 87 88 String subSiteId = siteContent.getString("contentId"); 89 indexTree(delegator, subSiteId, context, path); 90 } else { 91 List badIndexList = (List )context.get("badIndexList"); 92 badIndexList.add(siteContentId + " had no sub-entities."); 93 } 94 } 95 } else { 96 List badIndexList = (List )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 return results; 103 } 104 105 public static void indexContentList(List idList, GenericDelegator delegator, Map context) throws Exception { 106 String path = null; 107 indexContentList(delegator, context, idList, path); 108 } 109 110 public static void indexContentList(GenericDelegator delegator, Map context, List idList, String path) throws Exception { 111 String indexAllPath = getIndexPath(path); 112 if (Debug.infoOn()) 113 Debug.logInfo("in indexContent, indexAllPath:" + indexAllPath, module); 114 GenericValue content = null; 115 Iterator iter = null; 117 List contentList = null; 118 IndexReader reader = null; 119 try { 120 reader = IndexReader.open(indexAllPath); 121 } catch (Exception e) { 122 } 124 contentList = new ArrayList (); 127 iter = idList.iterator(); 128 while (iter.hasNext()) { 129 String id = (String ) 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 IndexWriter writer = null; 150 try { 151 writer = new IndexWriter(indexAllPath, new StandardAnalyzer(), false); 152 } catch (Exception e) { 153 writer = new IndexWriter(indexAllPath, new StandardAnalyzer(), true); 154 } 155 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 path) throws Exception { 168 String 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 { 176 String 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 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 dataResourceId, IndexReader reader) throws Exception { 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 context, GenericValue content, String path) throws Exception { 200 String 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 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 context, GenericValue content, IndexWriter writer) throws Exception { 217 Document doc = ContentDocument.Document(content, context); 218 if (doc != null) { 220 writer.addDocument(doc); 221 Integer goodIndexCount = (Integer )context.get("goodIndexCount"); 222 int newCount = goodIndexCount.intValue() + 1; 223 Integer newIndexCount = new Integer (newCount); 224 context.put("goodIndexCount", newIndexCount); 225 } 226 232 233 return; 234 } 235 236 public static void indexDataResource(GenericDelegator delegator, Map context, String id) throws Exception { 237 String path = null; 238 indexDataResource(delegator, context, id, path ); 239 } 240 241 public static void indexDataResource(GenericDelegator delegator, Map context, String id, String path) throws Exception { 242 String indexAllPath = getIndexPath(path); 243 IndexWriter writer = null; 244 try { 245 writer = new IndexWriter(indexAllPath, new StandardAnalyzer(), false); 246 } catch(FileNotFoundException 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 context, String id, IndexWriter writer) throws Exception { 256 Document doc = DataResourceDocument.Document(id, delegator, context); 257 writer.addDocument(doc); 258 } 259 260 public static String getIndexPath(String path) { 261 String 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 |