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