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.journal.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.StringMaker;
026: import com.liferay.portal.kernel.util.StringPool;
027: import com.liferay.portal.kernel.util.StringUtil;
028: import com.liferay.portal.lucene.LuceneFields;
029: import com.liferay.portal.lucene.LuceneUtil;
030: import com.liferay.portal.util.PortletKeys;
031: import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
032: import com.liferay.util.Html;
033:
034: import java.io.IOException;
035: import java.io.StringReader;
036:
037: import java.util.Date;
038: import java.util.Iterator;
039:
040: import javax.portlet.PortletURL;
041:
042: import org.apache.lucene.document.Document;
043: import org.apache.lucene.index.IndexWriter;
044: import org.apache.lucene.index.Term;
045:
046: import org.dom4j.Element;
047: import org.dom4j.io.SAXReader;
048:
049: /**
050: * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
051: *
052: * @author Brian Wing Shun Chan
053: * @author Harry Mark
054: *
055: */
056: public class Indexer implements
057: com.liferay.portal.kernel.search.Indexer {
058:
059: public static final String PORTLET_ID = PortletKeys.JOURNAL;
060:
061: public static void addArticle(long companyId, long groupId,
062: String articleId, double version, String title,
063: String description, String content, String type,
064: Date displayDate, String[] tagsEntries) throws IOException {
065:
066: Document doc = getAddArticleDocument(companyId, groupId,
067: articleId, version, title, description, content, type,
068: displayDate, tagsEntries);
069:
070: IndexWriter writer = null;
071:
072: try {
073: writer = LuceneUtil.getWriter(companyId);
074:
075: writer.addDocument(doc);
076: } finally {
077: if (writer != null) {
078: LuceneUtil.write(companyId);
079: }
080: }
081: }
082:
083: public static void deleteArticle(long companyId, String articleId)
084: throws IOException {
085:
086: LuceneUtil.deleteDocuments(companyId, new Term(
087: LuceneFields.UID, LuceneFields.getUID(PORTLET_ID,
088: articleId)));
089: }
090:
091: public static Document getAddArticleDocument(long companyId,
092: long groupId, String articleId, double version,
093: String title, String description, String content,
094: String type, Date displayDate, String[] tagsEntries) {
095:
096: if ((content != null)
097: && ((content.indexOf("<dynamic-content>") != -1) || (content
098: .indexOf("<static-content") != -1))) {
099:
100: content = _getIndexableContent(content);
101:
102: content = StringUtil.replace(content, "<![CDATA[",
103: StringPool.BLANK);
104: content = StringUtil.replace(content, "]]>",
105: StringPool.BLANK);
106: }
107:
108: content = StringUtil.replace(content, "&", "&");
109: content = StringUtil.replace(content, "<", "<");
110: content = StringUtil.replace(content, ">", ">");
111:
112: content = Html.stripHtml(content);
113:
114: Document doc = new Document();
115:
116: LuceneUtil.addKeyword(doc, LuceneFields.UID, LuceneFields
117: .getUID(PORTLET_ID, articleId));
118:
119: LuceneUtil.addKeyword(doc, LuceneFields.COMPANY_ID, companyId);
120: LuceneUtil.addKeyword(doc, LuceneFields.PORTLET_ID, PORTLET_ID);
121: LuceneUtil.addKeyword(doc, LuceneFields.GROUP_ID, groupId);
122:
123: LuceneUtil.addText(doc, LuceneFields.TITLE, title);
124: LuceneUtil.addText(doc, LuceneFields.CONTENT, content);
125: LuceneUtil.addText(doc, LuceneFields.DESCRIPTION, description);
126:
127: LuceneUtil.addModifiedDate(doc);
128:
129: LuceneUtil.addKeyword(doc, "articleId", articleId);
130: LuceneUtil.addKeyword(doc, "version", version);
131: LuceneUtil.addKeyword(doc, "type", type);
132: LuceneUtil.addDate(doc, "displayDate", displayDate);
133:
134: LuceneUtil.addKeyword(doc, LuceneFields.TAG_ENTRY, tagsEntries);
135:
136: return doc;
137: }
138:
139: public static void updateArticle(long companyId, long groupId,
140: String articleId, double version, String title,
141: String description, String content, String type,
142: Date displayDate, String[] tagsEntries) throws IOException {
143:
144: try {
145: deleteArticle(companyId, articleId);
146: } catch (IOException ioe) {
147: }
148:
149: addArticle(companyId, groupId, articleId, version, title,
150: description, content, type, displayDate, tagsEntries);
151: }
152:
153: public DocumentSummary getDocumentSummary(
154: com.liferay.portal.kernel.search.Document doc,
155: PortletURL portletURL) {
156:
157: // Title
158:
159: String title = doc.get(LuceneFields.TITLE);
160:
161: // Content
162:
163: String content = doc.get(LuceneFields.CONTENT);
164:
165: content = StringUtil.shorten(content, 200);
166:
167: // URL
168:
169: String groupId = doc.get("groupId");
170: String articleId = doc.get("articleId");
171: String version = doc.get("version");
172:
173: portletURL.setParameter("struts_action",
174: "/journal/edit_article");
175: portletURL.setParameter("groupId", groupId);
176: portletURL.setParameter("articleId", articleId);
177: portletURL.setParameter("version", version);
178:
179: return new DocumentSummary(title, content, portletURL);
180: }
181:
182: public void reIndex(String[] ids) throws SearchException {
183: try {
184: JournalArticleLocalServiceUtil.reIndex(ids);
185: } catch (Exception e) {
186: throw new SearchException(e);
187: }
188: }
189:
190: private static String _getIndexableContent(String content) {
191: try {
192: StringMaker sm = new StringMaker();
193:
194: SAXReader reader = new SAXReader();
195:
196: org.dom4j.Document doc = reader.read(new StringReader(
197: content));
198:
199: Element root = doc.getRootElement();
200:
201: _getIndexableContent(sm, root);
202:
203: return sm.toString();
204: } catch (Exception e) {
205: e.printStackTrace();
206:
207: return content;
208: }
209: }
210:
211: private static void _getIndexableContent(StringMaker sm,
212: Element root) throws Exception {
213:
214: Iterator itr = root.elements().iterator();
215:
216: while (itr.hasNext()) {
217: Element el = (Element) itr.next();
218:
219: String elType = el.attributeValue("type", StringPool.BLANK);
220:
221: if (elType.equals("text") || elType.equals("text_box")
222: || elType.equals("text_area")) {
223:
224: Element dynamicContent = el.element("dynamic-content");
225:
226: String text = dynamicContent.getText();
227:
228: sm.append(text);
229: sm.append(StringPool.BLANK);
230: } else if (el.getName().equals("static-content")) {
231: String text = el.getText();
232:
233: sm.append(text);
234: sm.append(StringPool.BLANK);
235: }
236:
237: _getIndexableContent(sm, el);
238: }
239: }
240:
241: }
|