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.StringUtil;
29 import com.liferay.portal.lucene.LuceneFields;
30 import com.liferay.portal.lucene.LuceneUtil;
31 import com.liferay.portal.util.PortletKeys;
32 import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
33 import com.liferay.util.Html;
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)
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);
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) {
147
148 content = Html.stripHtml(content);
149
150 Document doc = new Document();
151
152 doc.add(
153 LuceneFields.getKeyword(
154 LuceneFields.UID,
155 LuceneFields.getUID(PORTLET_ID, nodeId, title)));
156
157 doc.add(LuceneFields.getKeyword(LuceneFields.COMPANY_ID, companyId));
158 doc.add(LuceneFields.getKeyword(LuceneFields.PORTLET_ID, PORTLET_ID));
159 doc.add(LuceneFields.getKeyword(LuceneFields.GROUP_ID, groupId));
160
161 doc.add(LuceneFields.getText(LuceneFields.TITLE, title));
162 doc.add(LuceneFields.getText(LuceneFields.CONTENT, content));
163
164 doc.add(LuceneFields.getDate(LuceneFields.MODIFIED));
165
166 doc.add(LuceneFields.getKeyword("nodeId", nodeId));
167
168 return doc;
169 }
170
171 public static void updatePage(
172 long companyId, long groupId, long nodeId, String title,
173 String content)
174 throws IOException {
175
176 try {
177 deletePage(companyId, nodeId, title);
178 }
179 catch (IOException ioe) {
180 }
181
182 addPage(companyId, groupId, nodeId, title, content);
183 }
184
185 public DocumentSummary getDocumentSummary(
186 com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
187
188
190 String title = doc.get(LuceneFields.TITLE);
191
192
194 String content = doc.get(LuceneFields.CONTENT);
195
196 content = StringUtil.shorten(content, 200);
197
198
200 long nodeId = GetterUtil.getLong(doc.get("nodeId"));
201
202 portletURL.setParameter("struts_action", "/wiki/view_page");
203 portletURL.setParameter("nodeId", String.valueOf(nodeId));
204 portletURL.setParameter("title", title);
205
206 return new DocumentSummary(title, content, portletURL);
207 }
208
209 public void reIndex(String[] ids) throws SearchException {
210 try {
211 WikiNodeLocalServiceUtil.reIndex(ids);
212 }
213 catch (Exception e) {
214 throw new SearchException(e);
215 }
216 }
217
218 }