1
22
23 package com.liferay.portlet.wiki.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.GetterUtil;
28 import com.liferay.portal.kernel.util.HtmlUtil;
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.wiki.service.WikiNodeLocalServiceUtil;
34
35 import java.io.IOException;
36
37 import javax.portlet.PortletURL;
38
39 import org.apache.lucene.document.Document;
40 import org.apache.lucene.document.Field;
41 import org.apache.lucene.index.IndexReader;
42 import org.apache.lucene.index.IndexWriter;
43 import org.apache.lucene.index.Term;
44 import org.apache.lucene.queryParser.ParseException;
45 import org.apache.lucene.search.BooleanQuery;
46 import org.apache.lucene.search.Hits;
47 import org.apache.lucene.search.Searcher;
48
49
56 public class Indexer implements com.liferay.portal.kernel.search.Indexer {
57
58 public static final String PORTLET_ID = PortletKeys.WIKI;
59
60 public static void addPage(
61 long companyId, long groupId, long nodeId, String title,
62 String content, String[] tagsEntries)
63 throws IOException {
64
65 try {
66 deletePage(companyId, nodeId, title);
67 }
68 catch (IOException ioe) {
69 }
70
71 Document doc = getAddPageDocument(
72 companyId, groupId, nodeId, title, content, tagsEntries);
73
74 IndexWriter writer = null;
75
76 try {
77 writer = LuceneUtil.getWriter(companyId);
78
79 writer.addDocument(doc);
80 }
81 finally {
82 if (writer != null) {
83 LuceneUtil.write(companyId);
84 }
85 }
86 }
87
88 public static void deletePage(long companyId, long nodeId, String title)
89 throws IOException {
90
91 LuceneUtil.deleteDocuments(
92 companyId,
93 new Term(
94 LuceneFields.UID,
95 LuceneFields.getUID(PORTLET_ID, nodeId, title)));
96 }
97
98 public static void deletePages(long companyId, long nodeId)
99 throws IOException, ParseException {
100
101 BooleanQuery booleanQuery = new BooleanQuery();
102
103 LuceneUtil.addRequiredTerm(
104 booleanQuery, LuceneFields.PORTLET_ID, PORTLET_ID);
105
106 LuceneUtil.addRequiredTerm(booleanQuery, "nodeId", nodeId);
107
108 Searcher searcher = LuceneUtil.getSearcher(companyId);
109
110 try {
111 Hits hits = searcher.search(booleanQuery);
112
113 if (hits.length() > 0) {
114 IndexReader reader = null;
115
116 try {
117 LuceneUtil.acquireLock(companyId);
118
119 reader = LuceneUtil.getReader(companyId);
120
121 for (int i = 0; i < hits.length(); i++) {
122 Document doc = hits.doc(i);
123
124 Field field = doc.getField(LuceneFields.UID);
125
126 reader.deleteDocuments(
127 new Term(LuceneFields.UID, field.stringValue()));
128 }
129 }
130 finally {
131 if (reader != null) {
132 reader.close();
133 }
134
135 LuceneUtil.releaseLock(companyId);
136 }
137 }
138 }
139 finally {
140 LuceneUtil.closeSearcher(searcher);
141 }
142 }
143
144 public static Document getAddPageDocument(
145 long companyId, long groupId, long nodeId, String title,
146 String content, String[] tagsEntries) {
147
148 content = HtmlUtil.extractText(content);
149
150 Document doc = new Document();
151
152 LuceneUtil.addKeyword(
153 doc, LuceneFields.UID,
154 LuceneFields.getUID(PORTLET_ID, nodeId, title));
155
156 LuceneUtil.addKeyword(doc, LuceneFields.COMPANY_ID, companyId);
157 LuceneUtil.addKeyword(doc, LuceneFields.PORTLET_ID, PORTLET_ID);
158 LuceneUtil.addKeyword(doc, LuceneFields.GROUP_ID, groupId);
159
160 LuceneUtil.addText(doc, LuceneFields.TITLE, title);
161 LuceneUtil.addText(doc, LuceneFields.CONTENT, content);
162
163 LuceneUtil.addModifiedDate(doc);
164
165 LuceneUtil.addKeyword(doc, "nodeId", nodeId);
166
167 LuceneUtil.addKeyword(doc, LuceneFields.TAG_ENTRY, tagsEntries);
168
169 return doc;
170 }
171
172 public static void updatePage(
173 long companyId, long groupId, long nodeId, String title,
174 String content, String[] tagsEntries)
175 throws IOException {
176
177 try {
178 deletePage(companyId, nodeId, title);
179 }
180 catch (IOException ioe) {
181 }
182
183 addPage(companyId, groupId, nodeId, title, content, tagsEntries);
184 }
185
186 public DocumentSummary getDocumentSummary(
187 com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
188
189
191 String title = doc.get(LuceneFields.TITLE);
192
193
195 String content = doc.get(LuceneFields.CONTENT);
196
197 content = StringUtil.shorten(content, 200);
198
199
201 long nodeId = GetterUtil.getLong(doc.get("nodeId"));
202
203 portletURL.setParameter("struts_action", "/wiki/view");
204 portletURL.setParameter("nodeId", String.valueOf(nodeId));
205 portletURL.setParameter("title", title);
206
207 return new DocumentSummary(title, content, portletURL);
208 }
209
210 public void reIndex(String[] ids) throws SearchException {
211 try {
212 WikiNodeLocalServiceUtil.reIndex(ids);
213 }
214 catch (Exception e) {
215 throw new SearchException(e);
216 }
217 }
218
219 }