1
22
23 package com.liferay.portlet.journal.util;
24
25 import com.liferay.portal.kernel.search.DocumentSummary;
26 import com.liferay.portal.kernel.search.SearchException;
27 import com.liferay.portal.kernel.util.StringMaker;
28 import com.liferay.portal.kernel.util.StringPool;
29 import com.liferay.portal.kernel.util.StringUtil;
30 import com.liferay.portal.lucene.LuceneFields;
31 import com.liferay.portal.lucene.LuceneUtil;
32 import com.liferay.portal.util.PortletKeys;
33 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
34 import com.liferay.util.Html;
35
36 import java.io.IOException;
37 import java.io.StringReader;
38
39 import java.util.Date;
40 import java.util.Iterator;
41
42 import javax.portlet.PortletURL;
43
44 import org.apache.lucene.document.Document;
45 import org.apache.lucene.index.IndexWriter;
46 import org.apache.lucene.index.Term;
47
48 import org.dom4j.Element;
49 import org.dom4j.io.SAXReader;
50
51
58 public class Indexer implements com.liferay.portal.kernel.search.Indexer {
59
60 public static final String PORTLET_ID = PortletKeys.JOURNAL;
61
62 public static void addArticle(
63 long companyId, long groupId, String articleId, double version,
64 String title, String description, String content, String type,
65 Date displayDate)
66 throws IOException {
67
68 Document doc = getAddArticleDocument(
69 companyId, groupId, articleId, version, title, description, content,
70 type, displayDate);
71
72 IndexWriter writer = null;
73
74 try {
75 writer = LuceneUtil.getWriter(companyId);
76
77 writer.addDocument(doc);
78 }
79 finally {
80 if (writer != null) {
81 LuceneUtil.write(companyId);
82 }
83 }
84 }
85
86 public static void deleteArticle(long companyId, String articleId)
87 throws IOException {
88
89 LuceneUtil.deleteDocuments(
90 companyId,
91 new Term(
92 LuceneFields.UID, LuceneFields.getUID(PORTLET_ID, articleId)));
93 }
94
95 public static Document getAddArticleDocument(
96 long companyId, long groupId, String articleId, double version,
97 String title, String description, String content, String type,
98 Date displayDate) {
99
100 if ((content != null) &&
101 ((content.indexOf("<dynamic-content>") != -1) ||
102 (content.indexOf("<static-content") != -1))) {
103
104 content = _getIndexableContent(content);
105
106 content = StringUtil.replace(
107 content, "<![CDATA[", StringPool.BLANK);
108 content = StringUtil.replace(content, "]]>", StringPool.BLANK);
109 }
110
111 content = StringUtil.replace(content, "&", "&");
112 content = StringUtil.replace(content, "<", "<");
113 content = StringUtil.replace(content, ">", ">");
114
115 content = Html.stripHtml(content);
116
117 Document doc = new Document();
118
119 doc.add(
120 LuceneFields.getKeyword(
121 LuceneFields.UID, LuceneFields.getUID(PORTLET_ID, articleId)));
122
123 doc.add(LuceneFields.getKeyword(LuceneFields.COMPANY_ID, companyId));
124 doc.add(LuceneFields.getKeyword(LuceneFields.PORTLET_ID, PORTLET_ID));
125 doc.add(LuceneFields.getKeyword(LuceneFields.GROUP_ID, groupId));
126
127 doc.add(LuceneFields.getText(LuceneFields.TITLE, title));
128 doc.add(LuceneFields.getText(LuceneFields.CONTENT, content));
129 doc.add(LuceneFields.getText(LuceneFields.DESCRIPTION, description));
130
131 doc.add(LuceneFields.getDate(LuceneFields.MODIFIED));
132
133 doc.add(LuceneFields.getKeyword("articleId", articleId));
134 doc.add(
135 LuceneFields.getKeyword("version", Double.toString(version)));
136 doc.add(LuceneFields.getKeyword("type", type));
137 doc.add(LuceneFields.getDate("displayDate", displayDate));
138
139 return doc;
140 }
141
142 public static void updateArticle(
143 long companyId, long groupId, String articleId, double version,
144 String title, String description, String content, String type,
145 Date displayDate)
146 throws IOException {
147
148 try {
149 deleteArticle(companyId, articleId);
150 }
151 catch (IOException ioe) {
152 }
153
154 addArticle(
155 companyId, groupId, articleId, version, title, description, content,
156 type, displayDate);
157 }
158
159 public DocumentSummary getDocumentSummary(
160 com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
161
162
164 String title = doc.get(LuceneFields.TITLE);
165
166
168 String content = doc.get(LuceneFields.CONTENT);
169
170 content = StringUtil.shorten(content, 200);
171
172
174 String groupId = doc.get("groupId");
175 String articleId = doc.get("articleId");
176 String version = doc.get("version");
177
178 portletURL.setParameter("struts_action", "/journal/edit_article");
179 portletURL.setParameter("groupId", groupId);
180 portletURL.setParameter("articleId", articleId);
181 portletURL.setParameter("version", version);
182
183 return new DocumentSummary(title, content, portletURL);
184 }
185
186 public void reIndex(String[] ids) throws SearchException {
187 try {
188 JournalArticleLocalServiceUtil.reIndex(ids);
189 }
190 catch (Exception e) {
191 throw new SearchException(e);
192 }
193 }
194
195 private static String _getIndexableContent(String content) {
196 try {
197 StringMaker sm = new StringMaker();
198
199 SAXReader reader = new SAXReader();
200
201 org.dom4j.Document doc = reader.read(new StringReader(content));
202
203 Element root = doc.getRootElement();
204
205 _getIndexableContent(sm, root);
206
207 return sm.toString();
208 }
209 catch (Exception e) {
210 e.printStackTrace();
211
212 return content;
213 }
214 }
215
216 private static void _getIndexableContent(StringMaker sm, Element root)
217 throws Exception {
218
219 Iterator itr = root.elements().iterator();
220
221 while (itr.hasNext()) {
222 Element el = (Element)itr.next();
223
224 String elType = el.attributeValue("type", StringPool.BLANK);
225
226 if (elType.equals("text") || elType.equals("text_box") ||
227 elType.equals("text_area")) {
228
229 Element dynamicContent = el.element("dynamic-content");
230
231 String text = dynamicContent.getText();
232
233 sm.append(text);
234 sm.append(StringPool.BLANK);
235 }
236 else if (el.getName().equals("static-content")) {
237 String text = el.getText();
238
239 sm.append(text);
240 sm.append(StringPool.BLANK);
241 }
242
243 _getIndexableContent(sm, el);
244 }
245 }
246
247 }