01: /*******************************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: *******************************************************************************/package org.ofbiz.content.search;
19:
20: import java.io.IOException;
21: import java.io.StringWriter;
22: import java.util.Locale;
23: import java.util.Map;
24:
25: import org.ofbiz.base.util.Debug;
26: import org.ofbiz.base.util.GeneralException;
27: import org.ofbiz.base.util.UtilMisc;
28: import org.ofbiz.base.util.UtilValidate;
29: import org.ofbiz.content.data.DataResourceWorker;
30: import org.ofbiz.entity.GenericDelegator;
31: import org.ofbiz.entity.GenericEntityException;
32: import org.ofbiz.entity.GenericValue;
33:
34: import org.apache.lucene.document.Document;
35: import org.apache.lucene.document.Field;
36:
37: /**
38: * DataResourceDocument Class
39: */
40:
41: public class DataResourceDocument {
42: static char dirSep = System.getProperty("file.separator").charAt(0);
43: public static final String module = ContentDocument.class.getName();
44:
45: public static Document Document(String id,
46: GenericDelegator delegator, Map context)
47: throws InterruptedException {
48:
49: Document doc = null;
50: GenericValue dataResource = null;
51: try {
52: dataResource = delegator.findByPrimaryKeyCache(
53: "DataResource", UtilMisc
54: .toMap("dataResourceId", id));
55: } catch (GenericEntityException e) {
56: Debug.logError(e, module);
57: return doc;
58: }
59: // make a new, empty document
60: doc = new Document();
61:
62: doc.add(Field.Keyword("dataResourceId", id));
63:
64: String mimeTypeId = dataResource.getString("mimeTypeId");
65: if (UtilValidate.isEmpty(mimeTypeId)) {
66: mimeTypeId = "text/html";
67: }
68:
69: Locale locale = Locale.getDefault();
70: String currentLocaleString = dataResource
71: .getString("localeString");
72: if (UtilValidate.isNotEmpty(currentLocaleString)) {
73: locale = UtilMisc.parseLocale(currentLocaleString);
74: }
75:
76: StringWriter outWriter = new StringWriter();
77: try {
78: DataResourceWorker.writeDataResourceText(dataResource,
79: mimeTypeId, locale, context, delegator, outWriter,
80: true);
81: } catch (GeneralException e) {
82: Debug.logError(e, module);
83: } catch (IOException e) {
84: Debug.logError(e, module);
85: }
86: String text = outWriter.toString();
87: Debug.logInfo("in DataResourceDocument, text:" + text, module);
88: if (UtilValidate.isNotEmpty(text))
89: doc.add(Field.UnStored("content", text));
90:
91: return doc;
92: }
93:
94: }
|