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.portlet.wiki.util;
022:
023: import com.liferay.portal.kernel.search.DocumentSummary;
024: import com.liferay.portal.kernel.search.SearchException;
025: import com.liferay.portal.kernel.util.GetterUtil;
026: import com.liferay.portal.kernel.util.StringUtil;
027: import com.liferay.portal.lucene.LuceneFields;
028: import com.liferay.portal.lucene.LuceneUtil;
029: import com.liferay.portal.util.PortletKeys;
030: import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
031: import com.liferay.util.Html;
032:
033: import java.io.IOException;
034:
035: import javax.portlet.PortletURL;
036:
037: import org.apache.lucene.document.Document;
038: import org.apache.lucene.document.Field;
039: import org.apache.lucene.index.IndexReader;
040: import org.apache.lucene.index.IndexWriter;
041: import org.apache.lucene.index.Term;
042: import org.apache.lucene.queryParser.ParseException;
043: import org.apache.lucene.search.BooleanQuery;
044: import org.apache.lucene.search.Hits;
045: import org.apache.lucene.search.Searcher;
046:
047: /**
048: * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
049: *
050: * @author Brian Wing Shun Chan
051: * @author Harry Mark
052: *
053: */
054: public class Indexer implements
055: com.liferay.portal.kernel.search.Indexer {
056:
057: public static final String PORTLET_ID = PortletKeys.WIKI;
058:
059: public static void addPage(long companyId, long groupId,
060: long nodeId, String title, String content,
061: String[] tagsEntries) throws IOException {
062:
063: try {
064: deletePage(companyId, nodeId, title);
065: } catch (IOException ioe) {
066: }
067:
068: Document doc = getAddPageDocument(companyId, groupId, nodeId,
069: title, content, tagsEntries);
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 deletePage(long companyId, long nodeId,
085: String title) throws IOException {
086:
087: LuceneUtil.deleteDocuments(companyId, new Term(
088: LuceneFields.UID, LuceneFields.getUID(PORTLET_ID,
089: nodeId, title)));
090: }
091:
092: public static void deletePages(long companyId, long nodeId)
093: throws IOException, ParseException {
094:
095: BooleanQuery booleanQuery = new BooleanQuery();
096:
097: LuceneUtil.addRequiredTerm(booleanQuery,
098: LuceneFields.PORTLET_ID, PORTLET_ID);
099:
100: LuceneUtil.addRequiredTerm(booleanQuery, "nodeId", nodeId);
101:
102: Searcher searcher = LuceneUtil.getSearcher(companyId);
103:
104: try {
105: Hits hits = searcher.search(booleanQuery);
106:
107: if (hits.length() > 0) {
108: IndexReader reader = null;
109:
110: try {
111: LuceneUtil.acquireLock(companyId);
112:
113: reader = LuceneUtil.getReader(companyId);
114:
115: for (int i = 0; i < hits.length(); i++) {
116: Document doc = hits.doc(i);
117:
118: Field field = doc.getField(LuceneFields.UID);
119:
120: reader.deleteDocuments(new Term(
121: LuceneFields.UID, field.stringValue()));
122: }
123: } finally {
124: if (reader != null) {
125: reader.close();
126: }
127:
128: LuceneUtil.releaseLock(companyId);
129: }
130: }
131: } finally {
132: LuceneUtil.closeSearcher(searcher);
133: }
134: }
135:
136: public static Document getAddPageDocument(long companyId,
137: long groupId, long nodeId, String title, String content,
138: String[] tagsEntries) {
139:
140: content = Html.stripHtml(content);
141:
142: Document doc = new Document();
143:
144: LuceneUtil.addKeyword(doc, LuceneFields.UID, LuceneFields
145: .getUID(PORTLET_ID, nodeId, title));
146:
147: LuceneUtil.addKeyword(doc, LuceneFields.COMPANY_ID, companyId);
148: LuceneUtil.addKeyword(doc, LuceneFields.PORTLET_ID, PORTLET_ID);
149: LuceneUtil.addKeyword(doc, LuceneFields.GROUP_ID, groupId);
150:
151: LuceneUtil.addText(doc, LuceneFields.TITLE, title);
152: LuceneUtil.addText(doc, LuceneFields.CONTENT, content);
153:
154: LuceneUtil.addModifiedDate(doc);
155:
156: LuceneUtil.addKeyword(doc, "nodeId", nodeId);
157:
158: LuceneUtil.addKeyword(doc, LuceneFields.TAG_ENTRY, tagsEntries);
159:
160: return doc;
161: }
162:
163: public static void updatePage(long companyId, long groupId,
164: long nodeId, String title, String content,
165: String[] tagsEntries) throws IOException {
166:
167: try {
168: deletePage(companyId, nodeId, title);
169: } catch (IOException ioe) {
170: }
171:
172: addPage(companyId, groupId, nodeId, title, content, tagsEntries);
173: }
174:
175: public DocumentSummary getDocumentSummary(
176: com.liferay.portal.kernel.search.Document doc,
177: PortletURL portletURL) {
178:
179: // Title
180:
181: String title = doc.get(LuceneFields.TITLE);
182:
183: // Content
184:
185: String content = doc.get(LuceneFields.CONTENT);
186:
187: content = StringUtil.shorten(content, 200);
188:
189: // URL
190:
191: long nodeId = GetterUtil.getLong(doc.get("nodeId"));
192:
193: portletURL.setParameter("struts_action", "/wiki/view");
194: portletURL.setParameter("nodeId", String.valueOf(nodeId));
195: portletURL.setParameter("title", title);
196:
197: return new DocumentSummary(title, content, portletURL);
198: }
199:
200: public void reIndex(String[] ids) throws SearchException {
201: try {
202: WikiNodeLocalServiceUtil.reIndex(ids);
203: } catch (Exception e) {
204: throw new SearchException(e);
205: }
206: }
207:
208: }
|