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.Document;
18  import com.liferay.portal.kernel.search.DocumentImpl;
19  import com.liferay.portal.kernel.search.Field;
20  import com.liferay.portal.kernel.search.Indexer;
21  import com.liferay.portal.kernel.search.SearchContext;
22  import com.liferay.portal.kernel.search.SearchEngineUtil;
23  import com.liferay.portal.kernel.search.Summary;
24  import com.liferay.portal.kernel.util.GetterUtil;
25  import com.liferay.portal.kernel.util.HtmlUtil;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.kernel.workflow.StatusConstants;
29  import com.liferay.portal.search.BaseIndexer;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portal.util.PortletKeys;
32  import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
33  import com.liferay.portlet.blogs.model.BlogsEntry;
34  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
35  import com.liferay.portlet.expando.model.ExpandoBridge;
36  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
37  
38  import java.util.Date;
39  import java.util.List;
40  
41  import javax.portlet.PortletURL;
42  
43  /**
44   * <a href="BlogsIndexer.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   * @author Harry Mark
48   * @author Bruno Farache
49   * @author Raymond Augé
50   */
51  public class BlogsIndexer extends BaseIndexer {
52  
53      public static final String[] CLASS_NAMES = {BlogsEntry.class.getName()};
54  
55      public static final String PORTLET_ID = PortletKeys.BLOGS;
56  
57      public String[] getClassNames() {
58          return CLASS_NAMES;
59      }
60  
61      public Summary getSummary(
62          Document document, String snippet, PortletURL portletURL) {
63  
64          String title = document.get(Field.TITLE);
65  
66          String content = snippet;
67  
68          if (Validator.isNull(snippet)) {
69              content = StringUtil.shorten(document.get(Field.CONTENT), 200);
70          }
71  
72          String entryId = document.get(Field.ENTRY_CLASS_PK);
73  
74          portletURL.setParameter("struts_action", "/blogs/view_entry");
75          portletURL.setParameter("entryId", entryId);
76  
77          return new Summary(title, content, portletURL);
78      }
79  
80      protected void doDelete(Object obj) throws Exception {
81          BlogsEntry entry = (BlogsEntry)obj;
82  
83          Document document = new DocumentImpl();
84  
85          document.addUID(PORTLET_ID, entry.getEntryId());
86  
87          SearchEngineUtil.deleteDocument(
88              entry.getCompanyId(), document.get(Field.UID));
89      }
90  
91      protected Document doGetDocument(Object obj) throws Exception {
92          BlogsEntry entry = (BlogsEntry)obj;
93  
94          long companyId = entry.getCompanyId();
95          long groupId = getParentGroupId(entry.getGroupId());
96          long scopeGroupId = entry.getGroupId();
97          long userId = entry.getUserId();
98          String userName = PortalUtil.getUserName(userId, entry.getUserName());
99          long entryId = entry.getEntryId();
100         String title = entry.getTitle();
101         String content = HtmlUtil.extractText(entry.getContent());
102         Date displayDate = entry.getDisplayDate();
103 
104         String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
105             BlogsEntry.class.getName(), entryId);
106 
107         ExpandoBridge expandoBridge = entry.getExpandoBridge();
108 
109         Document document = new DocumentImpl();
110 
111         document.addUID(PORTLET_ID, entryId);
112 
113         document.addModifiedDate(displayDate);
114 
115         document.addKeyword(Field.COMPANY_ID, companyId);
116         document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
117         document.addKeyword(Field.GROUP_ID, groupId);
118         document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
119         document.addKeyword(Field.USER_ID, userId);
120         document.addText(Field.USER_NAME, userName);
121 
122         document.addText(Field.TITLE, title);
123         document.addText(Field.CONTENT, content);
124         document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
125 
126         document.addKeyword(Field.ENTRY_CLASS_NAME, BlogsEntry.class.getName());
127         document.addKeyword(Field.ENTRY_CLASS_PK, entryId);
128 
129         ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
130 
131         return document;
132     }
133 
134     protected void doReindex(Object obj) throws Exception {
135         BlogsEntry entry = (BlogsEntry)obj;
136 
137         if (!entry.isApproved()) {
138             return;
139         }
140 
141         Document document = getDocument(entry);
142 
143         SearchEngineUtil.updateDocument(
144             entry.getCompanyId(), document.get(Field.UID), document);
145     }
146 
147     protected void doReindex(String className, long classPK) throws Exception {
148         BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
149 
150         doReindex(entry);
151     }
152 
153     protected void doReindex(String[] ids) throws Exception {
154         long companyId = GetterUtil.getLong(ids[0]);
155 
156         reindexEntries(companyId);
157     }
158 
159     protected String getPortletId(SearchContext searchContext) {
160         return PORTLET_ID;
161     }
162 
163     protected void reindexEntries(long companyId) throws Exception {
164         int count = BlogsEntryLocalServiceUtil.getCompanyEntriesCount(
165             companyId, StatusConstants.APPROVED);
166 
167         int pages = count / Indexer.DEFAULT_INTERVAL;
168 
169         for (int i = 0; i <= pages; i++) {
170             int start = (i * Indexer.DEFAULT_INTERVAL);
171             int end = start + Indexer.DEFAULT_INTERVAL;
172 
173             reindexEntries(companyId, start, end);
174         }
175     }
176 
177     protected void reindexEntries(long companyId, int start, int end)
178         throws Exception {
179 
180         List<BlogsEntry> entries = BlogsEntryLocalServiceUtil.getCompanyEntries(
181             companyId, StatusConstants.APPROVED, start, end);
182 
183         for (BlogsEntry entry : entries) {
184             reindex(entry);
185         }
186     }
187 
188 }