1 24 25 package org.ofbiz.content.search; 26 27 import java.io.IOException ; 28 import java.io.StringWriter ; 29 import java.util.Locale ; 30 import java.util.Map ; 31 32 import org.ofbiz.base.util.Debug; 33 import org.ofbiz.base.util.GeneralException; 34 import org.ofbiz.base.util.UtilMisc; 35 import org.ofbiz.base.util.UtilValidate; 36 import org.ofbiz.content.data.DataResourceWorker; 37 import org.ofbiz.entity.GenericDelegator; 38 import org.ofbiz.entity.GenericEntityException; 39 import org.ofbiz.entity.GenericValue; 40 41 import org.apache.lucene.document.Document; 42 import org.apache.lucene.document.Field; 43 44 53 54 public class DataResourceDocument { 55 static char dirSep = System.getProperty("file.separator").charAt(0); 56 public static final String module = ContentDocument.class.getName(); 57 58 public static Document Document(String id, GenericDelegator delegator, Map context) throws InterruptedException { 59 60 Document doc = null; 61 GenericValue dataResource = null; 62 try { 63 dataResource = delegator.findByPrimaryKeyCache("DataResource", UtilMisc.toMap("dataResourceId",id)); 64 } catch(GenericEntityException e) { 65 Debug.logError(e, module); 66 return doc; 67 } 68 doc = new Document(); 70 71 doc.add(Field.Keyword("dataResourceId", id)); 72 73 String mimeTypeId = dataResource.getString("mimeTypeId"); 74 if (UtilValidate.isEmpty(mimeTypeId)) { 75 mimeTypeId = "text/html"; 76 } 77 78 Locale locale = Locale.getDefault(); 79 String currentLocaleString = dataResource.getString("localeString"); 80 if (UtilValidate.isNotEmpty(currentLocaleString)) { 81 locale = UtilMisc.parseLocale(currentLocaleString); 82 } 83 84 StringWriter outWriter = new StringWriter (); 85 try { 86 DataResourceWorker.writeDataResourceTextCache(dataResource, mimeTypeId, locale, context, delegator, outWriter); 87 } catch(GeneralException e) { 88 Debug.logError(e, module); 89 } catch(IOException e) { 90 Debug.logError(e, module); 91 } 92 String text = outWriter.toString(); 93 Debug.logInfo("in DataResourceDocument, text:" + text, module); 94 if (UtilValidate.isNotEmpty(text)) 95 doc.add(Field.UnStored("content", text)); 96 97 return doc; 98 } 99 100 } 101 | Popular Tags |