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.kernel.util.Validator;
38  import com.liferay.portal.model.Group;
39  import com.liferay.portal.service.GroupLocalServiceUtil;
40  import com.liferay.portal.util.PortletKeys;
41  import com.liferay.portlet.expando.model.ExpandoBridge;
42  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
43  import com.liferay.portlet.wiki.model.WikiPage;
44  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
45  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
46  
47  import java.util.Date;
48  
49  import javax.portlet.PortletURL;
50  
51  /**
52   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   * @author Harry Mark
56   * @author Bruno Farache
57   * @author Raymond Augé
58   */
59  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
60  
61      public static final String PORTLET_ID = PortletKeys.WIKI;
62  
63      public static void addPage(
64              long companyId, long groupId, long resourcePrimKey, long nodeId,
65              String title, String content, Date modifiedDate,
66              String[] tagsCategories, String[] tagsEntries,
67              ExpandoBridge expandoBridge)
68          throws SearchException {
69  
70          try {
71              deletePage(companyId, nodeId, title);
72          }
73          catch (SearchException se) {
74          }
75  
76          Document doc = getPageDocument(
77              companyId, groupId, resourcePrimKey, nodeId, title, content,
78              modifiedDate, tagsCategories, tagsEntries, expandoBridge);
79  
80          SearchEngineUtil.addDocument(companyId, doc);
81      }
82  
83      public static void deletePage(long companyId, long nodeId, String title)
84          throws SearchException {
85  
86          SearchEngineUtil.deleteDocument(companyId, getPageUID(nodeId, title));
87      }
88  
89      public static void deletePages(long companyId, long nodeId)
90          throws SearchException {
91  
92          BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create();
93  
94          booleanQuery.addRequiredTerm(Field.PORTLET_ID, PORTLET_ID);
95  
96          booleanQuery.addRequiredTerm("nodeId", nodeId);
97  
98          Hits hits = SearchEngineUtil.search(
99              companyId, booleanQuery, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
100 
101         for (int i = 0; i < hits.getLength(); i++) {
102             Document doc = hits.doc(i);
103 
104             SearchEngineUtil.deleteDocument(companyId, doc.get(Field.UID));
105         }
106     }
107 
108     public static Document getPageDocument(
109         long companyId, long groupId, long resourcePrimKey, long nodeId,
110         String title, String content, Date modifiedDate,
111         String[] tagsCategories, String[] tagsEntries,
112         ExpandoBridge expandoBridge) {
113 
114         long scopeGroupId = groupId;
115 
116         try {
117             Group group = GroupLocalServiceUtil.getGroup(groupId);
118 
119             if (group.isLayout()) {
120                 groupId = group.getParentGroupId();
121             }
122         }
123         catch (Exception e) {
124         }
125 
126         content = HtmlUtil.extractText(content);
127 
128         Document doc = new DocumentImpl();
129 
130         doc.addUID(PORTLET_ID, nodeId, title);
131 
132         doc.addModifiedDate(modifiedDate);
133 
134         doc.addKeyword(Field.COMPANY_ID, companyId);
135         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
136         doc.addKeyword(Field.GROUP_ID, groupId);
137         doc.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
138 
139         doc.addText(Field.TITLE, title);
140         doc.addText(Field.CONTENT, content);
141         doc.addKeyword(Field.TAGS_CATEGORIES, tagsCategories);
142         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
143 
144         doc.addKeyword(Field.ENTRY_CLASS_NAME, WikiPage.class.getName());
145         doc.addKeyword(Field.ENTRY_CLASS_PK, resourcePrimKey);
146         doc.addKeyword("nodeId", nodeId);
147 
148         ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
149 
150         return doc;
151     }
152 
153     public static String getPageUID(long nodeId, String title) {
154         Document doc = new DocumentImpl();
155 
156         doc.addUID(PORTLET_ID, nodeId, title);
157 
158         return doc.get(Field.UID);
159     }
160 
161     public static void updatePage(
162             long companyId, long groupId, long resourcePrimKey, long nodeId,
163             String title, String content, Date modifiedDate,
164             String[] tagsCategories, String[] tagsEntries,
165             ExpandoBridge expandoBridge)
166         throws SearchException {
167 
168         Document doc = getPageDocument(
169             companyId, groupId, resourcePrimKey, nodeId, title, content,
170             modifiedDate, tagsCategories, tagsEntries, expandoBridge);
171 
172         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
173     }
174 
175     public String[] getClassNames() {
176         return _CLASS_NAMES;
177     }
178 
179     public DocumentSummary getDocumentSummary(
180         Document doc, String snippet, PortletURL portletURL) {
181 
182         // Title
183 
184         String title = doc.get(Field.TITLE);
185 
186         // Content
187 
188         String content = snippet;
189 
190         if (Validator.isNull(snippet)) {
191             content = StringUtil.shorten(doc.get(Field.CONTENT), 200);
192         }
193 
194         // Portlet URL
195 
196         String nodeId = doc.get("nodeId");
197 
198         portletURL.setParameter("struts_action", "/wiki/view");
199         portletURL.setParameter("nodeId", nodeId);
200         portletURL.setParameter("title", title);
201 
202         return new DocumentSummary(title, content, portletURL);
203     }
204 
205     public void reIndex(String className, long classPK) throws SearchException {
206         try {
207             WikiPageLocalServiceUtil.reIndex(classPK);
208         }
209         catch (Exception e) {
210             throw new SearchException(e);
211         }
212     }
213 
214     public void reIndex(String[] ids) throws SearchException {
215         try {
216             WikiNodeLocalServiceUtil.reIndex(ids);
217         }
218         catch (Exception e) {
219             throw new SearchException(e);
220         }
221     }
222 
223     private static final String[] _CLASS_NAMES = new String[] {
224         WikiPage.class.getName()
225     };
226 
227 }