1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.blogs.util;
16  
17  import com.liferay.portal.kernel.search.BaseIndexer;
18  import com.liferay.portal.kernel.search.Document;
19  import com.liferay.portal.kernel.search.DocumentImpl;
20  import com.liferay.portal.kernel.search.Field;
21  import com.liferay.portal.kernel.search.Indexer;
22  import com.liferay.portal.kernel.search.SearchContext;
23  import com.liferay.portal.kernel.search.SearchEngineUtil;
24  import com.liferay.portal.kernel.search.Summary;
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.HtmlUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.kernel.workflow.WorkflowConstants;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portal.util.PortletKeys;
32  import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
33  import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
34  import com.liferay.portlet.blogs.model.BlogsEntry;
35  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
36  import com.liferay.portlet.expando.model.ExpandoBridge;
37  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
38  
39  import java.util.ArrayList;
40  import java.util.Collection;
41  import java.util.Date;
42  import java.util.List;
43  
44  import javax.portlet.PortletURL;
45  
46  /**
47   * <a href="BlogsIndexer.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   * @author Harry Mark
51   * @author Bruno Farache
52   * @author Raymond Augé
53   */
54  public class BlogsIndexer extends BaseIndexer {
55  
56      public static final String[] CLASS_NAMES = {BlogsEntry.class.getName()};
57  
58      public static final String PORTLET_ID = PortletKeys.BLOGS;
59  
60      public String[] getClassNames() {
61          return CLASS_NAMES;
62      }
63  
64      public Summary getSummary(
65          Document document, String snippet, PortletURL portletURL) {
66  
67          String title = document.get(Field.TITLE);
68  
69          String content = snippet;
70  
71          if (Validator.isNull(snippet)) {
72              content = StringUtil.shorten(document.get(Field.CONTENT), 200);
73          }
74  
75          String entryId = document.get(Field.ENTRY_CLASS_PK);
76  
77          portletURL.setParameter("struts_action", "/blogs/view_entry");
78          portletURL.setParameter("entryId", entryId);
79  
80          return new Summary(title, content, portletURL);
81      }
82  
83      protected void doDelete(Object obj) throws Exception {
84          BlogsEntry entry = (BlogsEntry)obj;
85  
86          Document document = new DocumentImpl();
87  
88          document.addUID(PORTLET_ID, entry.getEntryId());
89  
90          SearchEngineUtil.deleteDocument(
91              entry.getCompanyId(), document.get(Field.UID));
92      }
93  
94      protected Document doGetDocument(Object obj) throws Exception {
95          BlogsEntry entry = (BlogsEntry)obj;
96  
97          long companyId = entry.getCompanyId();
98          long groupId = getParentGroupId(entry.getGroupId());
99          long scopeGroupId = entry.getGroupId();
100         long userId = entry.getUserId();
101         String userName = PortalUtil.getUserName(userId, entry.getUserName());
102         long entryId = entry.getEntryId();
103         String title = entry.getTitle();
104         String content = HtmlUtil.extractText(entry.getContent());
105         Date displayDate = entry.getDisplayDate();
106 
107         long[] assetCategoryIds = AssetCategoryLocalServiceUtil.getCategoryIds(
108             BlogsEntry.class.getName(), entryId);
109         String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
110             BlogsEntry.class.getName(), entryId);
111 
112         ExpandoBridge expandoBridge = entry.getExpandoBridge();
113 
114         Document document = new DocumentImpl();
115 
116         document.addUID(PORTLET_ID, entryId);
117 
118         document.addModifiedDate(displayDate);
119 
120         document.addKeyword(Field.COMPANY_ID, companyId);
121         document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
122         document.addKeyword(Field.GROUP_ID, groupId);
123         document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
124         document.addKeyword(Field.USER_ID, userId);
125         document.addText(Field.USER_NAME, userName);
126 
127         document.addText(Field.TITLE, title);
128         document.addText(Field.CONTENT, content);
129         document.addKeyword(Field.ASSET_CATEGORY_IDS, assetCategoryIds);
130         document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
131 
132         document.addKeyword(Field.ENTRY_CLASS_NAME, BlogsEntry.class.getName());
133         document.addKeyword(Field.ENTRY_CLASS_PK, entryId);
134 
135         ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
136 
137         return document;
138     }
139 
140     protected void doReindex(Object obj) throws Exception {
141         BlogsEntry entry = (BlogsEntry)obj;
142 
143         if (!entry.isApproved()) {
144             return;
145         }
146 
147         Document document = getDocument(entry);
148 
149         SearchEngineUtil.updateDocument(entry.getCompanyId(), document);
150     }
151 
152     protected void doReindex(String className, long classPK) throws Exception {
153         BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
154 
155         doReindex(entry);
156     }
157 
158     protected void doReindex(String[] ids) throws Exception {
159         long companyId = GetterUtil.getLong(ids[0]);
160 
161         reindexEntries(companyId);
162     }
163 
164     protected String getPortletId(SearchContext searchContext) {
165         return PORTLET_ID;
166     }
167 
168     protected void reindexEntries(long companyId) throws Exception {
169         int count = BlogsEntryLocalServiceUtil.getCompanyEntriesCount(
170             companyId, WorkflowConstants.STATUS_APPROVED);
171 
172         int pages = count / Indexer.DEFAULT_INTERVAL;
173 
174         for (int i = 0; i <= pages; i++) {
175             int start = (i * Indexer.DEFAULT_INTERVAL);
176             int end = start + Indexer.DEFAULT_INTERVAL;
177 
178             reindexEntries(companyId, start, end);
179         }
180     }
181 
182     protected void reindexEntries(long companyId, int start, int end)
183         throws Exception {
184 
185         List<BlogsEntry> entries = BlogsEntryLocalServiceUtil.getCompanyEntries(
186             companyId, WorkflowConstants.STATUS_APPROVED, start, end);
187 
188         if (entries.isEmpty()) {
189             return;
190         }
191 
192         Collection<Document> documents = new ArrayList<Document>();
193 
194         for (BlogsEntry entry : entries) {
195             Document document = getDocument(entry);
196 
197             documents.add(document);
198         }
199 
200         SearchEngineUtil.updateDocuments(companyId, documents);
201     }
202 
203 }