1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.wiki.util;
24  
25  import com.liferay.portal.kernel.dao.orm.QueryUtil;
26  import com.liferay.portal.kernel.search.BooleanQuery;
27  import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
28  import com.liferay.portal.kernel.search.Document;
29  import com.liferay.portal.kernel.search.DocumentImpl;
30  import com.liferay.portal.kernel.search.DocumentSummary;
31  import com.liferay.portal.kernel.search.Field;
32  import com.liferay.portal.kernel.search.Hits;
33  import com.liferay.portal.kernel.search.SearchEngineUtil;
34  import com.liferay.portal.kernel.search.SearchException;
35  import com.liferay.portal.kernel.util.HtmlUtil;
36  import com.liferay.portal.kernel.util.StringUtil;
37  import com.liferay.portal.util.PortletKeys;
38  import com.liferay.portlet.expando.model.ExpandoBridge;
39  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
40  import com.liferay.portlet.wiki.model.WikiPage;
41  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
42  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
43  
44  import java.util.Date;
45  
46  import javax.portlet.PortletURL;
47  
48  /**
49   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   * @author Harry Mark
53   * @author Bruno Farache
54   * @author Raymond Augé
55   *
56   */
57  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
58  
59      public static final String PORTLET_ID = PortletKeys.WIKI;
60  
61      public static void addPage(
62              long companyId, long groupId, long resourcePrimKey, long nodeId,
63              String title, String content, Date modifiedDate,
64              String[] tagsCategories, String[] tagsEntries,
65              ExpandoBridge expandoBridge)
66          throws SearchException {
67  
68          try {
69              deletePage(companyId, nodeId, title);
70          }
71          catch (SearchException se) {
72          }
73  
74          Document doc = getPageDocument(
75              companyId, groupId, resourcePrimKey, nodeId, title, content,
76              modifiedDate, tagsCategories, tagsEntries, expandoBridge);
77  
78          SearchEngineUtil.addDocument(companyId, doc);
79      }
80  
81      public static void deletePage(long companyId, long nodeId, String title)
82          throws SearchException {
83  
84          SearchEngineUtil.deleteDocument(companyId, getPageUID(nodeId, title));
85      }
86  
87      public static void deletePages(long companyId, long nodeId)
88          throws SearchException {
89  
90          BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create();
91  
92          booleanQuery.addRequiredTerm(Field.PORTLET_ID, PORTLET_ID);
93  
94          booleanQuery.addRequiredTerm("nodeId", nodeId);
95  
96          Hits hits = SearchEngineUtil.search(
97              companyId, booleanQuery, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
98  
99          for (int i = 0; i < hits.getLength(); i++) {
100             Document doc = hits.doc(i);
101 
102             SearchEngineUtil.deleteDocument(companyId, doc.get(Field.UID));
103         }
104     }
105 
106     public static Document getPageDocument(
107         long companyId, long groupId, long resourcePrimKey, long nodeId,
108         String title, String content, Date modifiedDate,
109         String[] tagsCategories, String[] tagsEntries,
110         ExpandoBridge expandoBridge) {
111 
112         content = HtmlUtil.extractText(content);
113 
114         Document doc = new DocumentImpl();
115 
116         doc.addUID(PORTLET_ID, nodeId, title);
117 
118         doc.addModifiedDate(modifiedDate);
119 
120         doc.addKeyword(Field.COMPANY_ID, companyId);
121         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
122         doc.addKeyword(Field.GROUP_ID, groupId);
123 
124         doc.addText(Field.TITLE, title);
125         doc.addText(Field.CONTENT, content);
126         doc.addKeyword(Field.TAGS_CATEGORIES, tagsCategories);
127         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
128 
129         doc.addKeyword(Field.ENTRY_CLASS_NAME, WikiPage.class.getName());
130         doc.addKeyword(Field.ENTRY_CLASS_PK, resourcePrimKey);
131         doc.addKeyword("nodeId", nodeId);
132 
133         ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
134 
135         return doc;
136     }
137 
138     public static String getPageUID(long nodeId, String title) {
139         Document doc = new DocumentImpl();
140 
141         doc.addUID(PORTLET_ID, nodeId, title);
142 
143         return doc.get(Field.UID);
144     }
145 
146     public static void updatePage(
147             long companyId, long groupId, long resourcePrimKey, long nodeId,
148             String title, String content, Date modifiedDate,
149             String[] tagsCategories, String[] tagsEntries,
150             ExpandoBridge expandoBridge)
151         throws SearchException {
152 
153         Document doc = getPageDocument(
154             companyId, groupId, resourcePrimKey, nodeId, title, content,
155             modifiedDate, tagsCategories, tagsEntries, expandoBridge);
156 
157         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
158     }
159 
160     public String[] getClassNames() {
161         return _CLASS_NAMES;
162     }
163 
164     public DocumentSummary getDocumentSummary(
165         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
166 
167         // Title
168 
169         String title = doc.get(Field.TITLE);
170 
171         // Content
172 
173         String content = doc.get(Field.CONTENT);
174 
175         content = StringUtil.shorten(content, 200);
176 
177         // Portlet URL
178 
179         String nodeId = doc.get("nodeId");
180 
181         portletURL.setParameter("struts_action", "/wiki/view");
182         portletURL.setParameter("nodeId", nodeId);
183         portletURL.setParameter("title", title);
184 
185         return new DocumentSummary(title, content, portletURL);
186     }
187 
188     public void reIndex(String className, long classPK) throws SearchException {
189         try {
190             WikiPageLocalServiceUtil.reIndex(classPK);
191         }
192         catch (Exception e) {
193             throw new SearchException(e);
194         }
195     }
196 
197     public void reIndex(String[] ids) throws SearchException {
198         try {
199             WikiNodeLocalServiceUtil.reIndex(ids);
200         }
201         catch (Exception e) {
202             throw new SearchException(e);
203         }
204     }
205 
206     private static final String[] _CLASS_NAMES = new String[] {
207         WikiPage.class.getName()
208     };
209 
210 }