1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  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 nodeId, String title,
57              String content, Date modifiedDate, String[] tagsEntries)
58          throws SearchException {
59  
60          try {
61              deletePage(companyId, nodeId, title);
62          }
63          catch (SearchException se) {
64          }
65  
66          Document doc = getPageDocument(
67              companyId, groupId, nodeId, title, content, modifiedDate,
68              tagsEntries);
69  
70          SearchEngineUtil.addDocument(companyId, doc);
71      }
72  
73      public static void deletePage(long companyId, long nodeId, String title)
74          throws SearchException {
75  
76          SearchEngineUtil.deleteDocument(companyId, getPageUID(nodeId, title));
77      }
78  
79      public static void deletePages(long companyId, long nodeId)
80          throws SearchException {
81  
82          BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create();
83  
84          booleanQuery.addRequiredTerm(Field.PORTLET_ID, PORTLET_ID);
85  
86          booleanQuery.addRequiredTerm("nodeId", nodeId);
87  
88          Hits hits = SearchEngineUtil.search(
89              companyId, booleanQuery, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
90  
91          for (int i = 0; i < hits.getLength(); i++) {
92              Document doc = hits.doc(i);
93  
94              SearchEngineUtil.deleteDocument(companyId, doc.get(Field.UID));
95          }
96      }
97  
98      public static Document getPageDocument(
99          long companyId, long groupId, long nodeId, String title,
100         String content, Date modifiedDate, String[] tagsEntries) {
101 
102         content = HtmlUtil.extractText(content);
103 
104         Document doc = new DocumentImpl();
105 
106         doc.addUID(PORTLET_ID, nodeId, title);
107 
108         doc.addModifiedDate(modifiedDate);
109 
110         doc.addKeyword(Field.COMPANY_ID, companyId);
111         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
112         doc.addKeyword(Field.GROUP_ID, groupId);
113 
114         doc.addText(Field.TITLE, title);
115         doc.addText(Field.CONTENT, content);
116         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
117 
118         doc.addKeyword(Field.ENTRY_CLASS_PK, nodeId);
119 
120         return doc;
121     }
122 
123     public static String getPageUID(long nodeId, String title) {
124         Document doc = new DocumentImpl();
125 
126         doc.addUID(PORTLET_ID, nodeId, title);
127 
128         return doc.get(Field.UID);
129     }
130 
131     public static void updatePage(
132             long companyId, long groupId, long nodeId, String title,
133             String content, Date modifiedDate, String[] tagsEntries)
134         throws SearchException {
135 
136         Document doc = getPageDocument(
137             companyId, groupId, nodeId, title, content, modifiedDate,
138             tagsEntries);
139 
140         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
141     }
142 
143     public DocumentSummary getDocumentSummary(
144         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
145 
146         // Title
147 
148         String title = doc.get(Field.TITLE);
149 
150         // Content
151 
152         String content = doc.get(Field.CONTENT);
153 
154         content = StringUtil.shorten(content, 200);
155 
156         // Portlet URL
157 
158         String nodeId = doc.get(Field.ENTRY_CLASS_PK);
159 
160         portletURL.setParameter("struts_action", "/wiki/view");
161         portletURL.setParameter("nodeId", nodeId);
162         portletURL.setParameter("title", title);
163 
164         return new DocumentSummary(title, content, portletURL);
165     }
166 
167     public void reIndex(String[] ids) throws SearchException {
168         try {
169             WikiNodeLocalServiceUtil.reIndex(ids);
170         }
171         catch (Exception e) {
172             throw new SearchException(e);
173         }
174     }
175 
176 }