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