1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.wiki.util;
21  
22  import com.liferay.portal.kernel.dao.orm.QueryUtil;
23  import com.liferay.portal.kernel.search.BooleanQuery;
24  import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
25  import com.liferay.portal.kernel.search.Document;
26  import com.liferay.portal.kernel.search.DocumentImpl;
27  import com.liferay.portal.kernel.search.DocumentSummary;
28  import com.liferay.portal.kernel.search.Field;
29  import com.liferay.portal.kernel.search.Hits;
30  import com.liferay.portal.kernel.search.SearchEngineUtil;
31  import com.liferay.portal.kernel.search.SearchException;
32  import com.liferay.portal.kernel.util.HtmlUtil;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.util.PortletKeys;
35  import com.liferay.portlet.expando.model.ExpandoBridge;
36  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
37  import com.liferay.portlet.wiki.model.WikiPage;
38  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
39  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
40  
41  import java.util.Date;
42  
43  import javax.portlet.PortletURL;
44  
45  /**
46   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   * @author Harry Mark
50   * @author Bruno Farache
51   * @author Raymond Augé
52   *
53   */
54  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
55  
56      public static final String PORTLET_ID = PortletKeys.WIKI;
57  
58      public static void addPage(
59              long companyId, long groupId, long resourcePrimKey, long nodeId,
60              String title, String content, Date modifiedDate,
61              String[] tagsCategories, String[] tagsEntries,
62              ExpandoBridge expandoBridge)
63          throws SearchException {
64  
65          try {
66              deletePage(companyId, nodeId, title);
67          }
68          catch (SearchException se) {
69          }
70  
71          Document doc = getPageDocument(
72              companyId, groupId, resourcePrimKey, nodeId, title, content,
73              modifiedDate, tagsCategories, tagsEntries, expandoBridge);
74  
75          SearchEngineUtil.addDocument(companyId, doc);
76      }
77  
78      public static void deletePage(long companyId, long nodeId, String title)
79          throws SearchException {
80  
81          SearchEngineUtil.deleteDocument(companyId, getPageUID(nodeId, title));
82      }
83  
84      public static void deletePages(long companyId, long nodeId)
85          throws SearchException {
86  
87          BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create();
88  
89          booleanQuery.addRequiredTerm(Field.PORTLET_ID, PORTLET_ID);
90  
91          booleanQuery.addRequiredTerm("nodeId", nodeId);
92  
93          Hits hits = SearchEngineUtil.search(
94              companyId, booleanQuery, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
95  
96          for (int i = 0; i < hits.getLength(); i++) {
97              Document doc = hits.doc(i);
98  
99              SearchEngineUtil.deleteDocument(companyId, doc.get(Field.UID));
100         }
101     }
102 
103     public static Document getPageDocument(
104         long companyId, long groupId, long resourcePrimKey, long nodeId,
105         String title, String content, Date modifiedDate,
106         String[] tagsCategories, String[] tagsEntries,
107         ExpandoBridge expandoBridge) {
108 
109         content = HtmlUtil.extractText(content);
110 
111         Document doc = new DocumentImpl();
112 
113         doc.addUID(PORTLET_ID, nodeId, title);
114 
115         doc.addModifiedDate(modifiedDate);
116 
117         doc.addKeyword(Field.COMPANY_ID, companyId);
118         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
119         doc.addKeyword(Field.GROUP_ID, groupId);
120 
121         doc.addText(Field.TITLE, title);
122         doc.addText(Field.CONTENT, content);
123         doc.addKeyword(Field.TAGS_CATEGORIES, tagsCategories);
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[] tagsCategories, String[] tagsEntries,
147             ExpandoBridge expandoBridge)
148         throws SearchException {
149 
150         Document doc = getPageDocument(
151             companyId, groupId, resourcePrimKey, nodeId, title, content,
152             modifiedDate, tagsCategories, tagsEntries, expandoBridge);
153 
154         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
155     }
156 
157     public String[] getClassNames() {
158         return _CLASS_NAMES;
159     }
160 
161     public DocumentSummary getDocumentSummary(
162         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
163 
164         // Title
165 
166         String title = doc.get(Field.TITLE);
167 
168         // Content
169 
170         String content = doc.get(Field.CONTENT);
171 
172         content = StringUtil.shorten(content, 200);
173 
174         // Portlet URL
175 
176         String nodeId = doc.get("nodeId");
177 
178         portletURL.setParameter("struts_action", "/wiki/view");
179         portletURL.setParameter("nodeId", nodeId);
180         portletURL.setParameter("title", title);
181 
182         return new DocumentSummary(title, content, portletURL);
183     }
184 
185     public void reIndex(String className, long classPK) throws SearchException {
186         try {
187             WikiPageLocalServiceUtil.reIndex(classPK);
188         }
189         catch (Exception e) {
190             throw new SearchException(e);
191         }
192     }
193 
194     public void reIndex(String[] ids) throws SearchException {
195         try {
196             WikiNodeLocalServiceUtil.reIndex(ids);
197         }
198         catch (Exception e) {
199             throw new SearchException(e);
200         }
201     }
202 
203     private static final String[] _CLASS_NAMES = new String[] {
204         WikiPage.class.getName()
205     };
206 
207 }