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