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.blogs.util;
16  
17  import com.liferay.portal.kernel.search.Document;
18  import com.liferay.portal.kernel.search.DocumentImpl;
19  import com.liferay.portal.kernel.search.DocumentSummary;
20  import com.liferay.portal.kernel.search.Field;
21  import com.liferay.portal.kernel.search.SearchEngineUtil;
22  import com.liferay.portal.kernel.search.SearchException;
23  import com.liferay.portal.kernel.util.HtmlUtil;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.util.PortalUtil;
26  import com.liferay.portal.util.PortletKeys;
27  import com.liferay.portlet.blogs.model.BlogsEntry;
28  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
29  
30  import java.util.Date;
31  
32  import javax.portlet.PortletURL;
33  
34  /**
35   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   * @author Harry Mark
39   * @author Bruno Farache
40   */
41  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
42  
43      public static final String PORTLET_ID = PortletKeys.BLOGS;
44  
45      public static void addEntry(
46              long companyId, long groupId, long userId, String userName,
47              long entryId, String title, String content,  Date displayDate,
48              String[] tagsEntries)
49          throws SearchException {
50  
51          Document doc = getEntryDocument(
52              companyId, groupId, userId, userName, entryId, title, content,
53              displayDate, tagsEntries);
54  
55          SearchEngineUtil.addDocument(companyId, doc);
56      }
57  
58      public static void deleteEntry(long companyId, long entryId)
59          throws SearchException {
60  
61          SearchEngineUtil.deleteDocument(companyId, getEntryUID(entryId));
62      }
63  
64      public static Document getEntryDocument(
65          long companyId, long groupId, long userId, String userName,
66          long entryId, String title, String content, Date displayDate,
67          String[] tagsEntries) {
68  
69          userName = PortalUtil.getUserName(userId, userName);
70          content = HtmlUtil.extractText(content);
71  
72          Document doc = new DocumentImpl();
73  
74          doc.addUID(PORTLET_ID, entryId);
75  
76          doc.addModifiedDate(displayDate);
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          doc.addText(Field.USER_NAME, userName);
83  
84          doc.addText(Field.TITLE, title);
85          doc.addText(Field.CONTENT, content);
86          doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
87  
88          doc.addKeyword(Field.ENTRY_CLASS_NAME, BlogsEntry.class.getName());
89          doc.addKeyword(Field.ENTRY_CLASS_PK, entryId);
90  
91          return doc;
92      }
93  
94      public static String getEntryUID(long entryId) {
95          Document doc = new DocumentImpl();
96  
97          doc.addUID(PORTLET_ID, entryId);
98  
99          return doc.get(Field.UID);
100     }
101 
102     public static void updateEntry(
103             long companyId, long groupId, long userId, String userName,
104             long entryId, String title, String content, Date displayDate,
105             String[] tagsEntries)
106         throws SearchException {
107 
108         Document doc = getEntryDocument(
109             companyId, groupId, userId, userName, entryId, title, content,
110             displayDate, 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 }