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.wiki.service.WikiNodeLocalServiceUtil;
36  
37  import java.util.Date;
38  
39  import javax.portlet.PortletURL;
40  
41  /**
42   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   * @author Harry Mark
46   * @author Bruno Farache
47   *
48   */
49  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
50  
51      public static final String PORTLET_ID = PortletKeys.WIKI;
52  
53      public static void addPage(
54              long companyId, long groupId, long nodeId, String title,
55              String content, Date modifiedDate, String[] tagsEntries)
56          throws SearchException {
57  
58          try {
59              deletePage(companyId, nodeId, title);
60          }
61          catch (SearchException se) {
62          }
63  
64          Document doc = getPageDocument(
65              companyId, groupId, nodeId, title, content, modifiedDate,
66              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, Date modifiedDate, 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(modifiedDate);
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, Date modifiedDate, String[] tagsEntries)
132         throws SearchException {
133 
134         Document doc = getPageDocument(
135             companyId, groupId, nodeId, title, content, modifiedDate,
136             tagsEntries);
137 
138         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
139     }
140 
141     public DocumentSummary getDocumentSummary(
142         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
143 
144         // Title
145 
146         String title = doc.get(Field.TITLE);
147 
148         // Content
149 
150         String content = doc.get(Field.CONTENT);
151 
152         content = StringUtil.shorten(content, 200);
153 
154         // Portlet URL
155 
156         String nodeId = doc.get(Field.ENTRY_CLASS_PK);
157 
158         portletURL.setParameter("struts_action", "/wiki/view");
159         portletURL.setParameter("nodeId", nodeId);
160         portletURL.setParameter("title", title);
161 
162         return new DocumentSummary(title, content, portletURL);
163     }
164 
165     public void reIndex(String[] ids) throws SearchException {
166         try {
167             WikiNodeLocalServiceUtil.reIndex(ids);
168         }
169         catch (Exception e) {
170             throw new SearchException(e);
171         }
172     }
173 
174 }