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.wiki.service.WikiNodeLocalServiceUtil;
39  
40  import java.util.Date;
41  
42  import javax.portlet.PortletURL;
43  
44  /**
45   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   * @author Harry Mark
49   * @author Bruno Farache
50   *
51   */
52  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
53  
54      public static final String PORTLET_ID = PortletKeys.WIKI;
55  
56      public static void addPage(
57              long companyId, long groupId, long nodeId, String title,
58              String content, Date modifiedDate, String[] tagsEntries)
59          throws SearchException {
60  
61          try {
62              deletePage(companyId, nodeId, title);
63          }
64          catch (SearchException se) {
65          }
66  
67          Document doc = getPageDocument(
68              companyId, groupId, nodeId, title, content, modifiedDate,
69              tagsEntries);
70  
71          SearchEngineUtil.addDocument(companyId, doc);
72      }
73  
74      public static void deletePage(long companyId, long nodeId, String title)
75          throws SearchException {
76  
77          SearchEngineUtil.deleteDocument(companyId, getPageUID(nodeId, title));
78      }
79  
80      public static void deletePages(long companyId, long nodeId)
81          throws SearchException {
82  
83          BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create();
84  
85          booleanQuery.addRequiredTerm(Field.PORTLET_ID, PORTLET_ID);
86  
87          booleanQuery.addRequiredTerm("nodeId", nodeId);
88  
89          Hits hits = SearchEngineUtil.search(
90              companyId, booleanQuery, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
91  
92          for (int i = 0; i < hits.getLength(); i++) {
93              Document doc = hits.doc(i);
94  
95              SearchEngineUtil.deleteDocument(companyId, doc.get(Field.UID));
96          }
97      }
98  
99      public static Document getPageDocument(
100         long companyId, long groupId, long nodeId, String title,
101         String content, Date modifiedDate, String[] tagsEntries) {
102 
103         content = HtmlUtil.extractText(content);
104 
105         Document doc = new DocumentImpl();
106 
107         doc.addUID(PORTLET_ID, nodeId, title);
108 
109         doc.addModifiedDate(modifiedDate);
110 
111         doc.addKeyword(Field.COMPANY_ID, companyId);
112         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
113         doc.addKeyword(Field.GROUP_ID, groupId);
114 
115         doc.addText(Field.TITLE, title);
116         doc.addText(Field.CONTENT, content);
117         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
118 
119         doc.addKeyword(Field.ENTRY_CLASS_PK, nodeId);
120 
121         return doc;
122     }
123 
124     public static String getPageUID(long nodeId, String title) {
125         Document doc = new DocumentImpl();
126 
127         doc.addUID(PORTLET_ID, nodeId, title);
128 
129         return doc.get(Field.UID);
130     }
131 
132     public static void updatePage(
133             long companyId, long groupId, long nodeId, String title,
134             String content, Date modifiedDate, String[] tagsEntries)
135         throws SearchException {
136 
137         Document doc = getPageDocument(
138             companyId, groupId, nodeId, title, content, modifiedDate,
139             tagsEntries);
140 
141         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
142     }
143 
144     public DocumentSummary getDocumentSummary(
145         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
146 
147         // Title
148 
149         String title = doc.get(Field.TITLE);
150 
151         // Content
152 
153         String content = doc.get(Field.CONTENT);
154 
155         content = StringUtil.shorten(content, 200);
156 
157         // Portlet URL
158 
159         String nodeId = doc.get(Field.ENTRY_CLASS_PK);
160 
161         portletURL.setParameter("struts_action", "/wiki/view");
162         portletURL.setParameter("nodeId", nodeId);
163         portletURL.setParameter("title", title);
164 
165         return new DocumentSummary(title, content, portletURL);
166     }
167 
168     public void reIndex(String[] ids) throws SearchException {
169         try {
170             WikiNodeLocalServiceUtil.reIndex(ids);
171         }
172         catch (Exception e) {
173             throw new SearchException(e);
174         }
175     }
176 
177 }