001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.documentlibrary.util;
022:
023: import com.liferay.documentlibrary.service.impl.DLServiceImpl;
024: import com.liferay.portal.PortalException;
025: import com.liferay.portal.SystemException;
026: import com.liferay.portal.kernel.search.DocumentSummary;
027: import com.liferay.portal.kernel.search.SearchException;
028: import com.liferay.portal.kernel.util.GetterUtil;
029: import com.liferay.portal.kernel.util.StringMaker;
030: import com.liferay.portal.kernel.util.StringPool;
031: import com.liferay.portal.kernel.util.Validator;
032: import com.liferay.portal.lucene.LuceneFields;
033: import com.liferay.portal.lucene.LuceneUtil;
034: import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
035: import com.liferay.portlet.documentlibrary.model.DLFileEntry;
036: import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
037: import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
038:
039: import java.io.IOException;
040: import java.io.InputStream;
041:
042: import java.util.Iterator;
043: import java.util.Map;
044: import java.util.Properties;
045:
046: import javax.portlet.PortletURL;
047:
048: import org.apache.commons.logging.Log;
049: import org.apache.commons.logging.LogFactory;
050: import org.apache.lucene.document.Document;
051: import org.apache.lucene.index.IndexWriter;
052: import org.apache.lucene.index.Term;
053:
054: /**
055: * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
056: *
057: * @author Brian Wing Shun Chan
058: * @author Harry Mark
059: *
060: */
061: public class Indexer implements
062: com.liferay.portal.kernel.search.Indexer {
063:
064: public static void addFile(long companyId, String portletId,
065: long groupId, long repositoryId, String fileName)
066: throws IOException {
067:
068: Document doc = getAddFileDocument(companyId, portletId,
069: groupId, repositoryId, fileName);
070:
071: IndexWriter writer = null;
072:
073: try {
074: writer = LuceneUtil.getWriter(companyId);
075:
076: writer.addDocument(doc);
077: } finally {
078: if (writer != null) {
079: LuceneUtil.write(companyId);
080: }
081: }
082: }
083:
084: public static void addFile(long companyId, String portletId,
085: long groupId, long repositoryId, String fileName,
086: String properties, String[] tagsEntries) throws IOException {
087:
088: Document doc = getAddFileDocument(companyId, portletId,
089: groupId, repositoryId, fileName, properties,
090: tagsEntries);
091:
092: IndexWriter writer = null;
093:
094: try {
095: writer = LuceneUtil.getWriter(companyId);
096:
097: writer.addDocument(doc);
098: } finally {
099: if (writer != null) {
100: LuceneUtil.write(companyId);
101: }
102: }
103: }
104:
105: public static void deleteFile(long companyId, String portletId,
106: long repositoryId, String fileName) throws IOException {
107:
108: LuceneUtil.deleteDocuments(companyId, new Term(
109: LuceneFields.UID, LuceneFields.getUID(portletId,
110: repositoryId, fileName)));
111: }
112:
113: public static Document getAddFileDocument(long companyId,
114: String portletId, long groupId, long repositoryId,
115: String fileName) throws IOException {
116:
117: try {
118: DLFileEntry fileEntry = null;
119:
120: try {
121: fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
122: repositoryId, fileName);
123: } catch (NoSuchFileEntryException nsfe) {
124: if (_log.isWarnEnabled()) {
125: _log.warn("File " + fileName + " in repository "
126: + repositoryId
127: + " exists in the JCR but does "
128: + "not exist in the database");
129: }
130:
131: return null;
132: }
133:
134: StringMaker sm = new StringMaker();
135:
136: sm.append(fileEntry.getTitle());
137: sm.append(StringPool.SPACE);
138: sm.append(fileEntry.getDescription());
139: sm.append(StringPool.SPACE);
140:
141: Properties extraSettingsProps = fileEntry
142: .getExtraSettingsProperties();
143:
144: Iterator itr = (Iterator) extraSettingsProps.entrySet()
145: .iterator();
146:
147: while (itr.hasNext()) {
148: Map.Entry entry = (Map.Entry) itr.next();
149:
150: String value = GetterUtil.getString((String) entry
151: .getValue());
152:
153: sm.append(value);
154: }
155:
156: String properties = sm.toString();
157:
158: String[] tagsEntries = TagsEntryLocalServiceUtil
159: .getEntryNames(DLFileEntry.class.getName(),
160: fileEntry.getFileEntryId());
161:
162: return getAddFileDocument(companyId, portletId, groupId,
163: repositoryId, fileName, properties, tagsEntries);
164: } catch (PortalException pe) {
165: throw new IOException(pe.getMessage());
166: } catch (SystemException se) {
167: throw new IOException(se.getMessage());
168: }
169: }
170:
171: public static Document getAddFileDocument(long companyId,
172: String portletId, long groupId, long repositoryId,
173: String fileName, String properties, String[] tagsEntries)
174: throws IOException {
175:
176: if (_log.isDebugEnabled()) {
177: _log.debug("Indexing document " + companyId + " "
178: + portletId + " " + groupId + " " + repositoryId
179: + " " + fileName);
180: }
181:
182: String fileExt = StringPool.BLANK;
183:
184: int fileExtVersionPos = fileName.indexOf(DLServiceImpl.VERSION);
185:
186: if (fileExtVersionPos != -1) {
187: int fileExtPos = fileName.lastIndexOf(StringPool.PERIOD,
188: fileExtVersionPos);
189:
190: if (fileExtPos != -1) {
191: fileExt = fileName.substring(fileExtPos,
192: fileExtVersionPos);
193: }
194: } else {
195: int fileExtPos = fileName.lastIndexOf(StringPool.PERIOD);
196:
197: if (fileExtPos != -1) {
198: fileExt = fileName.substring(fileExtPos, fileName
199: .length());
200: }
201: }
202:
203: InputStream is = null;
204:
205: try {
206: Hook hook = HookFactory.getInstance();
207:
208: is = hook
209: .getFileAsStream(companyId, repositoryId, fileName);
210: } catch (Exception e) {
211: }
212:
213: if (is == null) {
214: if (_log.isDebugEnabled()) {
215: _log.debug("Document " + companyId + " " + portletId
216: + " " + groupId + " " + repositoryId + " "
217: + fileName + " does not have any content");
218: }
219:
220: return null;
221: }
222:
223: Document doc = new Document();
224:
225: LuceneUtil.addKeyword(doc, LuceneFields.UID, LuceneFields
226: .getUID(portletId, repositoryId, fileName));
227:
228: LuceneUtil.addKeyword(doc, LuceneFields.COMPANY_ID, companyId);
229: LuceneUtil.addKeyword(doc, LuceneFields.PORTLET_ID, portletId);
230: LuceneUtil.addKeyword(doc, LuceneFields.GROUP_ID, groupId);
231:
232: doc
233: .add(LuceneFields.getFile(LuceneFields.CONTENT, is,
234: fileExt));
235:
236: if (Validator.isNotNull(properties)) {
237: LuceneUtil
238: .addText(doc, LuceneFields.PROPERTIES, properties);
239: }
240:
241: LuceneUtil.addModifiedDate(doc);
242:
243: LuceneUtil.addKeyword(doc, "repositoryId", repositoryId);
244: LuceneUtil.addKeyword(doc, "path", fileName);
245:
246: LuceneUtil.addKeyword(doc, LuceneFields.TAG_ENTRY, tagsEntries);
247:
248: if (_log.isDebugEnabled()) {
249: _log.debug("Document " + companyId + " " + portletId + " "
250: + groupId + " " + repositoryId + " " + fileName
251: + " indexed successfully");
252: }
253:
254: return doc;
255: }
256:
257: public static void updateFile(long companyId, String portletId,
258: long groupId, long repositoryId, String fileName,
259: String properties, String[] tagsEntries) throws IOException {
260:
261: try {
262: deleteFile(companyId, portletId, repositoryId, fileName);
263: } catch (IOException ioe) {
264: }
265:
266: addFile(companyId, portletId, groupId, repositoryId, fileName,
267: properties, tagsEntries);
268: }
269:
270: public DocumentSummary getDocumentSummary(
271: com.liferay.portal.kernel.search.Document doc,
272: PortletURL portletURL) {
273:
274: return null;
275: }
276:
277: public void reIndex(String[] ids) throws SearchException {
278: if (LuceneUtil.INDEX_READ_ONLY) {
279: return;
280: }
281:
282: Hook hook = HookFactory.getInstance();
283:
284: hook.reIndex(ids);
285: }
286:
287: private static Log _log = LogFactory.getLog(Indexer.class);
288:
289: }
|