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