KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: DataResourceDocument.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.content.search;
26
27 import java.io.IOException JavaDoc;
28 import java.io.StringWriter JavaDoc;
29 import java.util.Locale JavaDoc;
30 import java.util.Map JavaDoc;
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 /**
45  * DataResourceDocument Class
46  *
47  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
48  * @version $Rev: 5462 $
49  * @since 3.1
50  *
51  *
52  */

53
54 public class DataResourceDocument {
55     static char dirSep = System.getProperty("file.separator").charAt(0);
56     public static final String JavaDoc module = ContentDocument.class.getName();
57     
58     public static Document Document(String JavaDoc id, GenericDelegator delegator, Map JavaDoc context) throws InterruptedException JavaDoc {
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         // make a new, empty document
69
doc = new Document();
70         
71         doc.add(Field.Keyword("dataResourceId", id));
72         
73         String JavaDoc mimeTypeId = dataResource.getString("mimeTypeId");
74         if (UtilValidate.isEmpty(mimeTypeId)) {
75             mimeTypeId = "text/html";
76         }
77
78         Locale JavaDoc locale = Locale.getDefault();
79         String JavaDoc currentLocaleString = dataResource.getString("localeString");
80         if (UtilValidate.isNotEmpty(currentLocaleString)) {
81             locale = UtilMisc.parseLocale(currentLocaleString);
82         }
83         
84         StringWriter JavaDoc outWriter = new StringWriter JavaDoc();
85         try {
86             DataResourceWorker.writeDataResourceTextCache(dataResource, mimeTypeId, locale, context, delegator, outWriter);
87         } catch(GeneralException e) {
88             Debug.logError(e, module);
89         } catch(IOException JavaDoc e) {
90             Debug.logError(e, module);
91         }
92         String JavaDoc 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