001
014
015 package com.liferay.portlet.asset.util;
016
017 import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.exception.SystemException;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.kernel.search.BaseIndexer;
023 import com.liferay.portal.kernel.search.BooleanClauseOccur;
024 import com.liferay.portal.kernel.search.BooleanQuery;
025 import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
026 import com.liferay.portal.kernel.search.Document;
027 import com.liferay.portal.kernel.search.Field;
028 import com.liferay.portal.kernel.search.SearchContext;
029 import com.liferay.portal.kernel.search.SearchEngineUtil;
030 import com.liferay.portal.kernel.search.Summary;
031 import com.liferay.portal.kernel.util.ArrayUtil;
032 import com.liferay.portal.kernel.util.GetterUtil;
033 import com.liferay.portal.kernel.util.Validator;
034 import com.liferay.portal.security.permission.ActionKeys;
035 import com.liferay.portal.security.permission.PermissionChecker;
036 import com.liferay.portal.util.PortletKeys;
037 import com.liferay.portlet.asset.model.AssetCategory;
038 import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
039 import com.liferay.portlet.asset.service.permission.AssetCategoryPermission;
040 import com.liferay.portlet.asset.service.persistence.AssetCategoryActionableDynamicQuery;
041
042 import java.util.ArrayList;
043 import java.util.Collection;
044 import java.util.List;
045 import java.util.Locale;
046
047 import javax.portlet.PortletURL;
048
049
052 public class AssetCategoryIndexer extends BaseIndexer {
053
054 public static final String[] CLASS_NAMES = {AssetCategory.class.getName()};
055
056 public static final String PORTLET_ID = PortletKeys.ASSET_CATEGORIES_ADMIN;
057
058 public AssetCategoryIndexer() {
059 setCommitImmediately(true);
060 }
061
062 public String[] getClassNames() {
063 return CLASS_NAMES;
064 }
065
066 public String getPortletId() {
067 return PORTLET_ID;
068 }
069
070 @Override
071 public boolean hasPermission(
072 PermissionChecker permissionChecker, String entryClassName,
073 long entryClassPK, String actionId)
074 throws Exception {
075
076 AssetCategory category = AssetCategoryLocalServiceUtil.getCategory(
077 entryClassPK);
078
079 return AssetCategoryPermission.contains(
080 permissionChecker, category, ActionKeys.VIEW);
081 }
082
083 @Override
084 public boolean isFilterSearch() {
085 return _FILTER_SEARCH;
086 }
087
088 @Override
089 public boolean isPermissionAware() {
090 return _PERMISSION_AWARE;
091 }
092
093 @Override
094 public void postProcessContextQuery(
095 BooleanQuery contextQuery, SearchContext searchContext)
096 throws Exception {
097
098 long[] vocabularyIds = (long[])searchContext.getAttribute(
099 Field.ASSET_VOCABULARY_IDS);
100
101 if (!ArrayUtil.isEmpty(vocabularyIds)) {
102 BooleanQuery vocabularyQuery = BooleanQueryFactoryUtil.create(
103 searchContext);
104
105 for (long vocabularyId : vocabularyIds) {
106 vocabularyQuery.addTerm(
107 Field.ASSET_VOCABULARY_ID, String.valueOf(vocabularyId));
108 }
109
110 contextQuery.add(vocabularyQuery, BooleanClauseOccur.MUST);
111 }
112 }
113
114 @Override
115 public void postProcessSearchQuery(
116 BooleanQuery searchQuery, SearchContext searchContext)
117 throws Exception {
118
119 String title = (String)searchContext.getAttribute(Field.TITLE);
120
121 if (Validator.isNotNull(title)) {
122 BooleanQuery localizedQuery = BooleanQueryFactoryUtil.create(
123 searchContext);
124
125 searchContext.setAttribute(Field.ASSET_CATEGORY_TITLE, title);
126
127 addSearchLocalizedTerm(
128 localizedQuery, searchContext, Field.ASSET_CATEGORY_TITLE,
129 true);
130 addSearchLocalizedTerm(
131 localizedQuery, searchContext, Field.TITLE, true);
132
133 searchQuery.add(localizedQuery, BooleanClauseOccur.SHOULD);
134 }
135 }
136
137 @Override
138 protected void doDelete(Object obj) throws Exception {
139 AssetCategory category = (AssetCategory)obj;
140
141 deleteDocument(category.getCompanyId(), category.getCategoryId());
142 }
143
144 @Override
145 protected Document doGetDocument(Object obj) throws Exception {
146 AssetCategory category = (AssetCategory)obj;
147
148 if (_log.isDebugEnabled()) {
149 _log.debug("Indexing category " + category);
150 }
151
152 Document document = getBaseModelDocument(PORTLET_ID, category);
153
154 document.addKeyword(Field.ASSET_CATEGORY_ID, category.getCategoryId());
155
156 List<AssetCategory> categories = new ArrayList<AssetCategory>(1);
157
158 categories.add(category);
159
160 addSearchAssetCategoryTitles(
161 document, Field.ASSET_CATEGORY_TITLE, categories);
162
163 document.addKeyword(
164 Field.ASSET_VOCABULARY_ID, category.getVocabularyId());
165 document.addLocalizedText(
166 Field.DESCRIPTION, category.getDescriptionMap());
167 document.addText(Field.NAME, category.getName());
168 document.addLocalizedText(Field.TITLE, category.getTitleMap());
169
170 if (_log.isDebugEnabled()) {
171 _log.debug("Document " + category + " indexed successfully");
172 }
173
174 return document;
175 }
176
177 @Override
178 protected Summary doGetSummary(
179 Document document, Locale locale, String snippet,
180 PortletURL portletURL) {
181
182 return null;
183 }
184
185 @Override
186 protected void doReindex(Object obj) throws Exception {
187 AssetCategory category = (AssetCategory)obj;
188
189 Document document = getDocument(category);
190
191 if (document != null) {
192 SearchEngineUtil.updateDocument(
193 getSearchEngineId(), category.getCompanyId(), document,
194 isCommitImmediately());
195 }
196 }
197
198 @Override
199 protected void doReindex(String className, long classPK) throws Exception {
200 AssetCategory category = AssetCategoryLocalServiceUtil.getCategory(
201 classPK);
202
203 doReindex(category);
204 }
205
206 @Override
207 protected void doReindex(String[] ids) throws Exception {
208 long companyId = GetterUtil.getLong(ids[0]);
209
210 reindexCategories(companyId);
211 }
212
213 @Override
214 protected String getPortletId(SearchContext searchContext) {
215 return PORTLET_ID;
216 }
217
218 protected void reindexCategories(final long companyId)
219 throws PortalException, SystemException {
220
221 final Collection<Document> documents = new ArrayList<Document>();
222
223 ActionableDynamicQuery actionableDynamicQuery =
224 new AssetCategoryActionableDynamicQuery() {
225
226 @Override
227 protected void performAction(Object object) throws PortalException {
228 AssetCategory category = (AssetCategory)object;
229
230 Document document = getDocument(category);
231
232 if (document != null) {
233 documents.add(document);
234 }
235 }
236
237 };
238
239 actionableDynamicQuery.setCompanyId(companyId);
240
241 actionableDynamicQuery.performActions();
242
243 SearchEngineUtil.updateDocuments(
244 getSearchEngineId(), companyId, documents);
245 }
246
247 private static final boolean _FILTER_SEARCH = true;
248
249 private static final boolean _PERMISSION_AWARE = true;
250
251 private static Log _log = LogFactoryUtil.getLog(AssetCategoryIndexer.class);
252
253 }