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.blogs.util;
21  
22  import com.liferay.portal.kernel.search.Document;
23  import com.liferay.portal.kernel.search.DocumentImpl;
24  import com.liferay.portal.kernel.search.DocumentSummary;
25  import com.liferay.portal.kernel.search.Field;
26  import com.liferay.portal.kernel.search.SearchEngineUtil;
27  import com.liferay.portal.kernel.search.SearchException;
28  import com.liferay.portal.kernel.util.HtmlUtil;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portal.util.PortletKeys;
32  import com.liferay.portlet.blogs.model.BlogsEntry;
33  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
34  
35  import java.util.Date;
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, String userName,
53              long entryId, String title, String content,  Date displayDate,
54              String[] tagsEntries)
55          throws SearchException {
56  
57          Document doc = getEntryDocument(
58              companyId, groupId, userId, userName, entryId, title, content,
59              displayDate, tagsEntries);
60  
61          SearchEngineUtil.addDocument(companyId, doc);
62      }
63  
64      public static void deleteEntry(long companyId, long entryId)
65          throws SearchException {
66  
67          SearchEngineUtil.deleteDocument(companyId, getEntryUID(entryId));
68      }
69  
70      public static Document getEntryDocument(
71          long companyId, long groupId, long userId, String userName,
72          long entryId, String title, String content, Date displayDate,
73          String[] tagsEntries) {
74  
75          userName = PortalUtil.getUserName(userId, userName);
76          content = HtmlUtil.extractText(content);
77  
78          Document doc = new DocumentImpl();
79  
80          doc.addUID(PORTLET_ID, entryId);
81  
82          doc.addModifiedDate(displayDate);
83  
84          doc.addKeyword(Field.COMPANY_ID, companyId);
85          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
86          doc.addKeyword(Field.GROUP_ID, groupId);
87          doc.addKeyword(Field.USER_ID, userId);
88          doc.addText(Field.USER_NAME, userName);
89  
90          doc.addText(Field.TITLE, title);
91          doc.addText(Field.CONTENT, content);
92          doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
93  
94          doc.addKeyword(Field.ENTRY_CLASS_NAME, BlogsEntry.class.getName());
95          doc.addKeyword(Field.ENTRY_CLASS_PK, entryId);
96  
97          return doc;
98      }
99  
100     public static String getEntryUID(long entryId) {
101         Document doc = new DocumentImpl();
102 
103         doc.addUID(PORTLET_ID, entryId);
104 
105         return doc.get(Field.UID);
106     }
107 
108     public static void updateEntry(
109             long companyId, long groupId, long userId, String userName,
110             long entryId, String title, String content, Date displayDate,
111             String[] tagsEntries)
112         throws SearchException {
113 
114         Document doc = getEntryDocument(
115             companyId, groupId, userId, userName, entryId, title, content,
116             displayDate, tagsEntries);
117 
118         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
119     }
120 
121     public DocumentSummary getDocumentSummary(
122         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
123 
124         // Title
125 
126         String title = doc.get(Field.TITLE);
127 
128         // Content
129 
130         String content = doc.get(Field.CONTENT);
131 
132         content = StringUtil.shorten(content, 200);
133 
134         // Portlet URL
135 
136         String entryId = doc.get(Field.ENTRY_CLASS_PK);
137 
138         portletURL.setParameter("struts_action", "/blogs/view_entry");
139         portletURL.setParameter("entryId", entryId);
140 
141         return new DocumentSummary(title, content, portletURL);
142     }
143 
144     public void reIndex(String[] ids) throws SearchException {
145         try {
146             BlogsEntryLocalServiceUtil.reIndex(ids);
147         }
148         catch (Exception e) {
149             throw new SearchException(e);
150         }
151     }
152 
153 }