001
014
015 package com.liferay.portlet.blogs.util;
016
017 import com.liferay.portal.kernel.search.BaseIndexer;
018 import com.liferay.portal.kernel.search.Document;
019 import com.liferay.portal.kernel.search.DocumentImpl;
020 import com.liferay.portal.kernel.search.Field;
021 import com.liferay.portal.kernel.search.Indexer;
022 import com.liferay.portal.kernel.search.SearchContext;
023 import com.liferay.portal.kernel.search.SearchEngineUtil;
024 import com.liferay.portal.kernel.search.Summary;
025 import com.liferay.portal.kernel.util.GetterUtil;
026 import com.liferay.portal.kernel.util.HtmlUtil;
027 import com.liferay.portal.kernel.util.StringUtil;
028 import com.liferay.portal.kernel.util.Validator;
029 import com.liferay.portal.kernel.workflow.WorkflowConstants;
030 import com.liferay.portal.util.PortalUtil;
031 import com.liferay.portal.util.PortletKeys;
032 import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
033 import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
034 import com.liferay.portlet.blogs.model.BlogsEntry;
035 import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
036 import com.liferay.portlet.expando.model.ExpandoBridge;
037 import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
038
039 import java.util.ArrayList;
040 import java.util.Collection;
041 import java.util.Date;
042 import java.util.List;
043
044 import javax.portlet.PortletURL;
045
046
052 public class BlogsIndexer extends BaseIndexer {
053
054 public static final String[] CLASS_NAMES = {BlogsEntry.class.getName()};
055
056 public static final String PORTLET_ID = PortletKeys.BLOGS;
057
058 public String[] getClassNames() {
059 return CLASS_NAMES;
060 }
061
062 public Summary getSummary(
063 Document document, String snippet, PortletURL portletURL) {
064
065 String title = document.get(Field.TITLE);
066
067 String content = snippet;
068
069 if (Validator.isNull(snippet)) {
070 content = StringUtil.shorten(document.get(Field.CONTENT), 200);
071 }
072
073 String entryId = document.get(Field.ENTRY_CLASS_PK);
074
075 portletURL.setParameter("struts_action", "/blogs/view_entry");
076 portletURL.setParameter("entryId", entryId);
077
078 return new Summary(title, content, portletURL);
079 }
080
081 protected void doDelete(Object obj) throws Exception {
082 BlogsEntry entry = (BlogsEntry)obj;
083
084 Document document = new DocumentImpl();
085
086 document.addUID(PORTLET_ID, entry.getEntryId());
087
088 SearchEngineUtil.deleteDocument(
089 entry.getCompanyId(), document.get(Field.UID));
090 }
091
092 protected Document doGetDocument(Object obj) throws Exception {
093 BlogsEntry entry = (BlogsEntry)obj;
094
095 long companyId = entry.getCompanyId();
096 long groupId = getParentGroupId(entry.getGroupId());
097 long scopeGroupId = entry.getGroupId();
098 long userId = entry.getUserId();
099 String userName = PortalUtil.getUserName(userId, entry.getUserName());
100 long entryId = entry.getEntryId();
101 String title = entry.getTitle();
102 String content = HtmlUtil.extractText(entry.getContent());
103 Date displayDate = entry.getDisplayDate();
104
105 long[] assetCategoryIds = AssetCategoryLocalServiceUtil.getCategoryIds(
106 BlogsEntry.class.getName(), entryId);
107 String[] assetCategoryNames =
108 AssetCategoryLocalServiceUtil.getCategoryNames(
109 BlogsEntry.class.getName(), entryId);
110 String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
111 BlogsEntry.class.getName(), entryId);
112
113 ExpandoBridge expandoBridge = entry.getExpandoBridge();
114
115 Document document = new DocumentImpl();
116
117 document.addUID(PORTLET_ID, entryId);
118
119 document.addModifiedDate(displayDate);
120
121 document.addKeyword(Field.COMPANY_ID, companyId);
122 document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
123 document.addKeyword(Field.GROUP_ID, groupId);
124 document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
125 document.addKeyword(Field.USER_ID, userId);
126 document.addText(Field.USER_NAME, userName);
127
128 document.addText(Field.TITLE, title);
129 document.addText(Field.CONTENT, content);
130 document.addKeyword(Field.ASSET_CATEGORY_IDS, assetCategoryIds);
131 document.addKeyword(Field.ASSET_CATEGORY_NAMES, assetCategoryNames);
132 document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
133
134 document.addKeyword(Field.ENTRY_CLASS_NAME, BlogsEntry.class.getName());
135 document.addKeyword(Field.ENTRY_CLASS_PK, entryId);
136
137 ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
138
139 return document;
140 }
141
142 protected void doReindex(Object obj) throws Exception {
143 BlogsEntry entry = (BlogsEntry)obj;
144
145 if (!entry.isApproved()) {
146 return;
147 }
148
149 Document document = getDocument(entry);
150
151 SearchEngineUtil.updateDocument(entry.getCompanyId(), document);
152 }
153
154 protected void doReindex(String className, long classPK) throws Exception {
155 BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
156
157 doReindex(entry);
158 }
159
160 protected void doReindex(String[] ids) throws Exception {
161 long companyId = GetterUtil.getLong(ids[0]);
162
163 reindexEntries(companyId);
164 }
165
166 protected String getPortletId(SearchContext searchContext) {
167 return PORTLET_ID;
168 }
169
170 protected void reindexEntries(long companyId) throws Exception {
171 int count = BlogsEntryLocalServiceUtil.getCompanyEntriesCount(
172 companyId, WorkflowConstants.STATUS_APPROVED);
173
174 int pages = count / Indexer.DEFAULT_INTERVAL;
175
176 for (int i = 0; i <= pages; i++) {
177 int start = (i * Indexer.DEFAULT_INTERVAL);
178 int end = start + Indexer.DEFAULT_INTERVAL;
179
180 reindexEntries(companyId, start, end);
181 }
182 }
183
184 protected void reindexEntries(long companyId, int start, int end)
185 throws Exception {
186
187 List<BlogsEntry> entries = BlogsEntryLocalServiceUtil.getCompanyEntries(
188 companyId, WorkflowConstants.STATUS_APPROVED, start, end);
189
190 if (entries.isEmpty()) {
191 return;
192 }
193
194 Collection<Document> documents = new ArrayList<Document>();
195
196 for (BlogsEntry entry : entries) {
197 Document document = getDocument(entry);
198
199 documents.add(document);
200 }
201
202 SearchEngineUtil.updateDocuments(companyId, documents);
203 }
204
205 }