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.BooleanQuery;
019 import com.liferay.portal.kernel.search.Document;
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.security.permission.ActionKeys;
031 import com.liferay.portal.security.permission.PermissionChecker;
032 import com.liferay.portal.util.PortletKeys;
033 import com.liferay.portlet.blogs.model.BlogsEntry;
034 import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
035 import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission;
036
037 import java.util.ArrayList;
038 import java.util.Collection;
039 import java.util.Date;
040 import java.util.List;
041 import java.util.Locale;
042
043 import javax.portlet.PortletURL;
044
045
051 public class BlogsIndexer extends BaseIndexer {
052
053 public static final String[] CLASS_NAMES = {BlogsEntry.class.getName()};
054
055 public static final String PORTLET_ID = PortletKeys.BLOGS;
056
057 public String[] getClassNames() {
058 return CLASS_NAMES;
059 }
060
061 public String getPortletId() {
062 return PORTLET_ID;
063 }
064
065 @Override
066 public boolean hasPermission(
067 PermissionChecker permissionChecker, long entryClassPK,
068 String actionId)
069 throws Exception {
070
071 return BlogsEntryPermission.contains(
072 permissionChecker, entryClassPK, ActionKeys.VIEW);
073 }
074
075 @Override
076 public boolean isFilterSearch() {
077 return _FILTER_SEARCH;
078 }
079
080 @Override
081 public boolean isPermissionAware() {
082 return _PERMISSION_AWARE;
083 }
084
085 @Override
086 public void postProcessContextQuery(
087 BooleanQuery contextQuery, SearchContext searchContext)
088 throws Exception {
089
090 int status = GetterUtil.getInteger(
091 searchContext.getAttribute(Field.STATUS),
092 WorkflowConstants.STATUS_ANY);
093
094 if (status != WorkflowConstants.STATUS_ANY) {
095 contextQuery.addRequiredTerm(Field.STATUS, status);
096 }
097 }
098
099 @Override
100 protected void doDelete(Object obj) throws Exception {
101 BlogsEntry entry = (BlogsEntry)obj;
102
103 deleteDocument(entry.getCompanyId(), entry.getEntryId());
104 }
105
106 @Override
107 protected Document doGetDocument(Object obj) throws Exception {
108 BlogsEntry entry = (BlogsEntry)obj;
109
110 Document document = getBaseModelDocument(PORTLET_ID, entry);
111
112 document.addText(
113 Field.CONTENT, HtmlUtil.extractText(entry.getContent()));
114 document.addDate(Field.MODIFIED_DATE, entry.getDisplayDate());
115 document.addText(Field.TITLE, entry.getTitle());
116
117 return document;
118 }
119
120 @Override
121 protected Summary doGetSummary(
122 Document document, Locale locale, String snippet,
123 PortletURL portletURL) {
124
125 String title = document.get(Field.TITLE);
126
127 String content = snippet;
128
129 if (Validator.isNull(snippet)) {
130 content = StringUtil.shorten(document.get(Field.CONTENT), 200);
131 }
132
133 String entryId = document.get(Field.ENTRY_CLASS_PK);
134
135 portletURL.setParameter("struts_action", "/blogs/view_entry");
136 portletURL.setParameter("entryId", entryId);
137
138 return new Summary(title, content, portletURL);
139 }
140
141 @Override
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 @Override
155 protected void doReindex(String className, long classPK) throws Exception {
156 BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
157
158 doReindex(entry);
159 }
160
161 @Override
162 protected void doReindex(String[] ids) throws Exception {
163 long companyId = GetterUtil.getLong(ids[0]);
164
165 reindexEntries(companyId);
166 }
167
168 @Override
169 protected String getPortletId(SearchContext searchContext) {
170 return PORTLET_ID;
171 }
172
173 protected void reindexEntries(long companyId) throws Exception {
174 int count = BlogsEntryLocalServiceUtil.getCompanyEntriesCount(
175 companyId, new Date(), WorkflowConstants.STATUS_APPROVED);
176
177 int pages = count / Indexer.DEFAULT_INTERVAL;
178
179 for (int i = 0; i <= pages; i++) {
180 int start = (i * Indexer.DEFAULT_INTERVAL);
181 int end = start + Indexer.DEFAULT_INTERVAL;
182
183 reindexEntries(companyId, start, end);
184 }
185 }
186
187 protected void reindexEntries(long companyId, int start, int end)
188 throws Exception {
189
190 List<BlogsEntry> entries = BlogsEntryLocalServiceUtil.getCompanyEntries(
191 companyId, new Date(), WorkflowConstants.STATUS_APPROVED, start,
192 end);
193
194 if (entries.isEmpty()) {
195 return;
196 }
197
198 Collection<Document> documents = new ArrayList<Document>();
199
200 for (BlogsEntry entry : entries) {
201 Document document = getDocument(entry);
202
203 documents.add(document);
204 }
205
206 SearchEngineUtil.updateDocuments(companyId, documents);
207 }
208
209 private static final boolean _FILTER_SEARCH = true;
210
211 private static final boolean _PERMISSION_AWARE = true;
212
213 }