1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.wiki.util;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryUtil;
18  import com.liferay.portal.kernel.search.BooleanQuery;
19  import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
20  import com.liferay.portal.kernel.search.Document;
21  import com.liferay.portal.kernel.search.DocumentImpl;
22  import com.liferay.portal.kernel.search.DocumentSummary;
23  import com.liferay.portal.kernel.search.Field;
24  import com.liferay.portal.kernel.search.Hits;
25  import com.liferay.portal.kernel.search.SearchEngineUtil;
26  import com.liferay.portal.kernel.search.SearchException;
27  import com.liferay.portal.kernel.util.HtmlUtil;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.Group;
31  import com.liferay.portal.service.GroupLocalServiceUtil;
32  import com.liferay.portal.util.PortletKeys;
33  import com.liferay.portlet.expando.model.ExpandoBridge;
34  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
35  import com.liferay.portlet.wiki.model.WikiPage;
36  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
37  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
38  
39  import java.util.Date;
40  
41  import javax.portlet.PortletURL;
42  
43  /**
44   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   * @author Harry Mark
48   * @author Bruno Farache
49   * @author Raymond Augé
50   */
51  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
52  
53      public static final String PORTLET_ID = PortletKeys.WIKI;
54  
55      public static void addPage(
56              long companyId, long groupId, long userId, long resourcePrimKey,
57              long nodeId, String title, String content, Date modifiedDate,
58              String[] tagsCategories, String[] tagsEntries,
59              ExpandoBridge expandoBridge)
60          throws SearchException {
61  
62          try {
63              deletePage(companyId, nodeId, title);
64          }
65          catch (SearchException se) {
66          }
67  
68          Document doc = getPageDocument(
69              companyId, groupId, userId, resourcePrimKey, nodeId, title, content,
70              modifiedDate, tagsCategories, tagsEntries, expandoBridge);
71  
72          SearchEngineUtil.addDocument(companyId, doc);
73      }
74  
75      /**
76       * @deprecated
77       */
78      public static void addPage(
79              long companyId, long groupId, long resourcePrimKey, long nodeId,
80              String title, String content, Date modifiedDate,
81              String[] tagsCategories, String[] tagsEntries,
82              ExpandoBridge expandoBridge)
83          throws SearchException {
84  
85          addPage(
86              companyId, groupId, 0, resourcePrimKey, nodeId, title, content,
87              modifiedDate, tagsCategories, tagsEntries, expandoBridge);
88      }
89  
90      public static void deletePage(long companyId, long nodeId, String title)
91          throws SearchException {
92  
93          SearchEngineUtil.deleteDocument(companyId, getPageUID(nodeId, title));
94      }
95  
96      public static void deletePages(long companyId, long nodeId)
97          throws SearchException {
98  
99          BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create();
100 
101         booleanQuery.addRequiredTerm(Field.PORTLET_ID, PORTLET_ID);
102 
103         booleanQuery.addRequiredTerm("nodeId", nodeId);
104 
105         Hits hits = SearchEngineUtil.search(
106             companyId, booleanQuery, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
107 
108         for (int i = 0; i < hits.getLength(); i++) {
109             Document doc = hits.doc(i);
110 
111             SearchEngineUtil.deleteDocument(companyId, doc.get(Field.UID));
112         }
113     }
114 
115     public static Document getPageDocument(
116         long companyId, long groupId, long userId, long resourcePrimKey,
117         long nodeId, String title, String content, Date modifiedDate,
118         String[] tagsCategories, String[] tagsEntries,
119         ExpandoBridge expandoBridge) {
120 
121         long scopeGroupId = groupId;
122 
123         try {
124             Group group = GroupLocalServiceUtil.getGroup(groupId);
125 
126             if (group.isLayout()) {
127                 groupId = group.getParentGroupId();
128             }
129         }
130         catch (Exception e) {
131         }
132 
133         content = HtmlUtil.extractText(content);
134 
135         Document doc = new DocumentImpl();
136 
137         doc.addUID(PORTLET_ID, nodeId, title);
138 
139         doc.addModifiedDate(modifiedDate);
140 
141         doc.addKeyword(Field.COMPANY_ID, companyId);
142         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
143         doc.addKeyword(Field.GROUP_ID, groupId);
144         doc.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
145         doc.addKeyword(Field.USER_ID, userId);
146 
147         doc.addText(Field.TITLE, title);
148         doc.addText(Field.CONTENT, content);
149         doc.addKeyword(Field.TAGS_CATEGORIES, tagsCategories);
150         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
151 
152         doc.addKeyword(Field.ENTRY_CLASS_NAME, WikiPage.class.getName());
153         doc.addKeyword(Field.ENTRY_CLASS_PK, resourcePrimKey);
154         doc.addKeyword("nodeId", nodeId);
155 
156         ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
157 
158         return doc;
159     }
160 
161     /**
162      * @deprecated
163      */
164     public static Document getPageDocument(
165         long companyId, long groupId, long resourcePrimKey, long nodeId,
166         String title, String content, Date modifiedDate,
167         String[] tagsCategories, String[] tagsEntries,
168         ExpandoBridge expandoBridge) {
169 
170         return getPageDocument(
171             companyId, groupId, 0, resourcePrimKey, nodeId, title, content,
172             modifiedDate, tagsCategories, tagsEntries, expandoBridge);
173     }
174 
175     public static String getPageUID(long nodeId, String title) {
176         Document doc = new DocumentImpl();
177 
178         doc.addUID(PORTLET_ID, nodeId, title);
179 
180         return doc.get(Field.UID);
181     }
182 
183     public static void updatePage(
184             long companyId, long groupId, long userId, long resourcePrimKey,
185             long nodeId, String title, String content, Date modifiedDate,
186             String[] tagsCategories, String[] tagsEntries,
187             ExpandoBridge expandoBridge)
188         throws SearchException {
189 
190         Document doc = getPageDocument(
191             companyId, groupId, userId, resourcePrimKey, nodeId, title, content,
192             modifiedDate, tagsCategories, tagsEntries, expandoBridge);
193 
194         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
195     }
196 
197     /**
198      * @deprecated
199      */
200     public static void updatePage(
201             long companyId, long groupId, long resourcePrimKey, long nodeId,
202             String title, String content, Date modifiedDate,
203             String[] tagsCategories, String[] tagsEntries,
204             ExpandoBridge expandoBridge)
205         throws SearchException {
206 
207         updatePage(
208             companyId, groupId, 0, resourcePrimKey, nodeId, title, content,
209             modifiedDate, tagsCategories, tagsEntries, expandoBridge);
210     }
211 
212     public String[] getClassNames() {
213         return _CLASS_NAMES;
214     }
215 
216     public DocumentSummary getDocumentSummary(
217         Document doc, String snippet, PortletURL portletURL) {
218 
219         // Title
220 
221         String title = doc.get(Field.TITLE);
222 
223         // Content
224 
225         String content = snippet;
226 
227         if (Validator.isNull(snippet)) {
228             content = StringUtil.shorten(doc.get(Field.CONTENT), 200);
229         }
230 
231         // Portlet URL
232 
233         String nodeId = doc.get("nodeId");
234 
235         portletURL.setParameter("struts_action", "/wiki/view");
236         portletURL.setParameter("nodeId", nodeId);
237         portletURL.setParameter("title", title);
238 
239         return new DocumentSummary(title, content, portletURL);
240     }
241 
242     public void reIndex(String className, long classPK) throws SearchException {
243         try {
244             WikiPageLocalServiceUtil.reIndex(classPK);
245         }
246         catch (Exception e) {
247             throw new SearchException(e);
248         }
249     }
250 
251     public void reIndex(String[] ids) throws SearchException {
252         try {
253             WikiNodeLocalServiceUtil.reIndex(ids);
254         }
255         catch (Exception e) {
256             throw new SearchException(e);
257         }
258     }
259 
260     private static final String[] _CLASS_NAMES = new String[] {
261         WikiPage.class.getName()
262     };
263 
264 }