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[] tagsEntries, ExpandoBridge expandoBridge)
65          throws SearchException {
66  
67          try {
68              deletePage(companyId, nodeId, title);
69          }
70          catch (SearchException se) {
71          }
72  
73          Document doc = getPageDocument(
74              companyId, groupId, resourcePrimKey, nodeId, title, content,
75              modifiedDate, tagsEntries, expandoBridge);
76  
77          SearchEngineUtil.addDocument(companyId, doc);
78      }
79  
80      public static void deletePage(long companyId, long nodeId, String title)
81          throws SearchException {
82  
83          SearchEngineUtil.deleteDocument(companyId, getPageUID(nodeId, title));
84      }
85  
86      public static void deletePages(long companyId, long nodeId)
87          throws SearchException {
88  
89          BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create();
90  
91          booleanQuery.addRequiredTerm(Field.PORTLET_ID, PORTLET_ID);
92  
93          booleanQuery.addRequiredTerm("nodeId", nodeId);
94  
95          Hits hits = SearchEngineUtil.search(
96              companyId, booleanQuery, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
97  
98          for (int i = 0; i < hits.getLength(); i++) {
99              Document doc = hits.doc(i);
100 
101             SearchEngineUtil.deleteDocument(companyId, doc.get(Field.UID));
102         }
103     }
104 
105     public static Document getPageDocument(
106         long companyId, long groupId, long resourcePrimKey, long nodeId,
107         String title, String content, Date modifiedDate, String[] tagsEntries,
108         ExpandoBridge expandoBridge) {
109 
110         content = HtmlUtil.extractText(content);
111 
112         Document doc = new DocumentImpl();
113 
114         doc.addUID(PORTLET_ID, nodeId, title);
115 
116         doc.addModifiedDate(modifiedDate);
117 
118         doc.addKeyword(Field.COMPANY_ID, companyId);
119         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
120         doc.addKeyword(Field.GROUP_ID, groupId);
121 
122         doc.addText(Field.TITLE, title);
123         doc.addText(Field.CONTENT, content);
124         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
125 
126         doc.addKeyword(Field.ENTRY_CLASS_NAME, WikiPage.class.getName());
127         doc.addKeyword(Field.ENTRY_CLASS_PK, resourcePrimKey);
128         doc.addKeyword("nodeId", nodeId);
129 
130         ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
131 
132         return doc;
133     }
134 
135     public static String getPageUID(long nodeId, String title) {
136         Document doc = new DocumentImpl();
137 
138         doc.addUID(PORTLET_ID, nodeId, title);
139 
140         return doc.get(Field.UID);
141     }
142 
143     public static void updatePage(
144             long companyId, long groupId, long resourcePrimKey, long nodeId,
145             String title, String content, Date modifiedDate,
146             String[] tagsEntries, ExpandoBridge expandoBridge)
147         throws SearchException {
148 
149         Document doc = getPageDocument(
150             companyId, groupId, resourcePrimKey, nodeId, title, content,
151             modifiedDate, tagsEntries, expandoBridge);
152 
153         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
154     }
155 
156     public String[] getClassNames() {
157         return _CLASS_NAMES;
158     }
159 
160     public DocumentSummary getDocumentSummary(
161         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
162 
163         // Title
164 
165         String title = doc.get(Field.TITLE);
166 
167         // Content
168 
169         String content = doc.get(Field.CONTENT);
170 
171         content = StringUtil.shorten(content, 200);
172 
173         // Portlet URL
174 
175         String nodeId = doc.get("nodeId");
176 
177         portletURL.setParameter("struts_action", "/wiki/view");
178         portletURL.setParameter("nodeId", nodeId);
179         portletURL.setParameter("title", title);
180 
181         return new DocumentSummary(title, content, portletURL);
182     }
183 
184     public void reIndex(String className, long classPK) throws SearchException {
185         try {
186             WikiPageLocalServiceUtil.reIndex(classPK);
187         }
188         catch (Exception e) {
189             throw new SearchException(e);
190         }
191     }
192 
193     public void reIndex(String[] ids) throws SearchException {
194         try {
195             WikiNodeLocalServiceUtil.reIndex(ids);
196         }
197         catch (Exception e) {
198             throw new SearchException(e);
199         }
200     }
201 
202     private static final String[] _CLASS_NAMES = new String[] {
203         WikiPage.class.getName()
204     };
205 
206 }