1
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.ArrayList;
39 import java.util.Collection;
40 import java.util.Date;
41 import java.util.List;
42
43 import javax.portlet.PortletURL;
44
45
53 public class BlogsIndexer extends BaseIndexer {
54
55 public static final String[] CLASS_NAMES = {BlogsEntry.class.getName()};
56
57 public static final String PORTLET_ID = PortletKeys.BLOGS;
58
59 public String[] getClassNames() {
60 return CLASS_NAMES;
61 }
62
63 public Summary getSummary(
64 Document document, String snippet, PortletURL portletURL) {
65
66 String title = document.get(Field.TITLE);
67
68 String content = snippet;
69
70 if (Validator.isNull(snippet)) {
71 content = StringUtil.shorten(document.get(Field.CONTENT), 200);
72 }
73
74 String entryId = document.get(Field.ENTRY_CLASS_PK);
75
76 portletURL.setParameter("struts_action", "/blogs/view_entry");
77 portletURL.setParameter("entryId", entryId);
78
79 return new Summary(title, content, portletURL);
80 }
81
82 protected void doDelete(Object obj) throws Exception {
83 BlogsEntry entry = (BlogsEntry)obj;
84
85 Document document = new DocumentImpl();
86
87 document.addUID(PORTLET_ID, entry.getEntryId());
88
89 SearchEngineUtil.deleteDocument(
90 entry.getCompanyId(), document.get(Field.UID));
91 }
92
93 protected Document doGetDocument(Object obj) throws Exception {
94 BlogsEntry entry = (BlogsEntry)obj;
95
96 long companyId = entry.getCompanyId();
97 long groupId = getParentGroupId(entry.getGroupId());
98 long scopeGroupId = entry.getGroupId();
99 long userId = entry.getUserId();
100 String userName = PortalUtil.getUserName(userId, entry.getUserName());
101 long entryId = entry.getEntryId();
102 String title = entry.getTitle();
103 String content = HtmlUtil.extractText(entry.getContent());
104 Date displayDate = entry.getDisplayDate();
105
106 String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
107 BlogsEntry.class.getName(), entryId);
108
109 ExpandoBridge expandoBridge = entry.getExpandoBridge();
110
111 Document document = new DocumentImpl();
112
113 document.addUID(PORTLET_ID, entryId);
114
115 document.addModifiedDate(displayDate);
116
117 document.addKeyword(Field.COMPANY_ID, companyId);
118 document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
119 document.addKeyword(Field.GROUP_ID, groupId);
120 document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
121 document.addKeyword(Field.USER_ID, userId);
122 document.addText(Field.USER_NAME, userName);
123
124 document.addText(Field.TITLE, title);
125 document.addText(Field.CONTENT, content);
126 document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
127
128 document.addKeyword(Field.ENTRY_CLASS_NAME, BlogsEntry.class.getName());
129 document.addKeyword(Field.ENTRY_CLASS_PK, entryId);
130
131 ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
132
133 return document;
134 }
135
136 protected void doReindex(Object obj) throws Exception {
137 BlogsEntry entry = (BlogsEntry)obj;
138
139 if (!entry.isApproved()) {
140 return;
141 }
142
143 Document document = getDocument(entry);
144
145 SearchEngineUtil.updateDocument(entry.getCompanyId(), document);
146 }
147
148 protected void doReindex(String className, long classPK) throws Exception {
149 BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
150
151 doReindex(entry);
152 }
153
154 protected void doReindex(String[] ids) throws Exception {
155 long companyId = GetterUtil.getLong(ids[0]);
156
157 reindexEntries(companyId);
158 }
159
160 protected String getPortletId(SearchContext searchContext) {
161 return PORTLET_ID;
162 }
163
164 protected void reindexEntries(long companyId) throws Exception {
165 int count = BlogsEntryLocalServiceUtil.getCompanyEntriesCount(
166 companyId, StatusConstants.APPROVED);
167
168 int pages = count / Indexer.DEFAULT_INTERVAL;
169
170 for (int i = 0; i <= pages; i++) {
171 int start = (i * Indexer.DEFAULT_INTERVAL);
172 int end = start + Indexer.DEFAULT_INTERVAL;
173
174 reindexEntries(companyId, start, end);
175 }
176 }
177
178 protected void reindexEntries(long companyId, int start, int end)
179 throws Exception {
180
181 List<BlogsEntry> entries = BlogsEntryLocalServiceUtil.getCompanyEntries(
182 companyId, StatusConstants.APPROVED, start, end);
183
184 if (entries.isEmpty()) {
185 return;
186 }
187
188 Collection<Document> documents = new ArrayList<Document>();
189
190 for (BlogsEntry entry : entries) {
191 Document document = getDocument(entry);
192
193 documents.add(document);
194 }
195
196 SearchEngineUtil.updateDocuments(companyId, documents);
197 }
198
199 }