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.blogs.util;
24  
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.SearchEngineUtil;
30  import com.liferay.portal.kernel.search.SearchException;
31  import com.liferay.portal.kernel.util.HtmlUtil;
32  import com.liferay.portal.kernel.util.StringUtil;
33  import com.liferay.portal.util.PortletKeys;
34  import com.liferay.portlet.blogs.model.BlogsEntry;
35  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
36  
37  import javax.portlet.PortletURL;
38  
39  /**
40   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   * @author Harry Mark
44   * @author Bruno Farache
45   *
46   */
47  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
48  
49      public static final String PORTLET_ID = PortletKeys.BLOGS;
50  
51      public static void addEntry(
52              long companyId, long groupId, long userId, long entryId,
53              String title, String content, String[] tagsEntries)
54          throws SearchException {
55  
56          Document doc = getEntryDocument(
57              companyId, groupId, userId, entryId, title, content, tagsEntries);
58  
59          SearchEngineUtil.addDocument(companyId, doc);
60      }
61  
62      public static void deleteEntry(long companyId, long entryId)
63          throws SearchException {
64  
65          SearchEngineUtil.deleteDocument(companyId, getEntryUID(entryId));
66      }
67  
68      public static Document getEntryDocument(
69          long companyId, long groupId, long userId, long entryId, String title,
70          String content, String[] tagsEntries) {
71  
72          content = HtmlUtil.extractText(content);
73  
74          Document doc = new DocumentImpl();
75  
76          doc.addUID(PORTLET_ID, entryId);
77  
78          doc.addKeyword(Field.COMPANY_ID, companyId);
79          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
80          doc.addKeyword(Field.GROUP_ID, groupId);
81          doc.addKeyword(Field.USER_ID, userId);
82  
83          doc.addText(Field.TITLE, title);
84          doc.addText(Field.CONTENT, content);
85  
86          doc.addModifiedDate();
87  
88          doc.addKeyword(Field.ENTRY_CLASS_NAME, BlogsEntry.class.getName());
89          doc.addKeyword(Field.ENTRY_CLASS_PK, entryId);
90  
91          doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
92  
93          return doc;
94      }
95  
96      public static String getEntryUID(long entryId) {
97          Document doc = new DocumentImpl();
98  
99          doc.addUID(PORTLET_ID, entryId);
100 
101         return doc.get(Field.UID);
102     }
103 
104     public static void updateEntry(
105             long companyId, long groupId, long userId, long entryId,
106             String title, String content, String[] tagsEntries)
107         throws SearchException {
108 
109         Document doc = getEntryDocument(
110             companyId, groupId, userId, entryId, title, content, tagsEntries);
111 
112         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
113     }
114 
115     public DocumentSummary getDocumentSummary(
116         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
117 
118         // Title
119 
120         String title = doc.get(Field.TITLE);
121 
122         // Content
123 
124         String content = doc.get(Field.CONTENT);
125 
126         content = StringUtil.shorten(content, 200);
127 
128         // Portlet URL
129 
130         String entryId = doc.get(Field.ENTRY_CLASS_PK);
131 
132         portletURL.setParameter("struts_action", "/blogs/view_entry");
133         portletURL.setParameter("entryId", entryId);
134 
135         return new DocumentSummary(title, content, portletURL);
136     }
137 
138     public void reIndex(String[] ids) throws SearchException {
139         try {
140             BlogsEntryLocalServiceUtil.reIndex(ids);
141         }
142         catch (Exception e) {
143             throw new SearchException(e);
144         }
145     }
146 
147 }