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.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.search.BaseIndexer;
022 import com.liferay.portal.kernel.search.BooleanClauseOccur;
023 import com.liferay.portal.kernel.search.BooleanQuery;
024 import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
025 import com.liferay.portal.kernel.search.Document;
026 import com.liferay.portal.kernel.search.DocumentImpl;
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
041 import java.util.ArrayList;
042 import java.util.List;
043 import java.util.Locale;
044
045 import javax.portlet.PortletRequest;
046 import javax.portlet.PortletResponse;
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 setDefaultSelectedFieldNames(
061 Field.ASSET_CATEGORY_ID, Field.COMPANY_ID, Field.GROUP_ID,
062 Field.UID);
063 setFilterSearch(true);
064 setPermissionAware(true);
065 }
066
067 @Override
068 public String[] getClassNames() {
069 return CLASS_NAMES;
070 }
071
072 @Override
073 public String getPortletId() {
074 return PORTLET_ID;
075 }
076
077 @Override
078 public boolean hasPermission(
079 PermissionChecker permissionChecker, String entryClassName,
080 long entryClassPK, String actionId)
081 throws Exception {
082
083 AssetCategory category = AssetCategoryLocalServiceUtil.getCategory(
084 entryClassPK);
085
086 return AssetCategoryPermission.contains(
087 permissionChecker, category, ActionKeys.VIEW);
088 }
089
090 @Override
091 public void postProcessContextQuery(
092 BooleanQuery contextQuery, SearchContext searchContext)
093 throws Exception {
094
095 long[] parentCategoryIds = (long[])searchContext.getAttribute(
096 Field.ASSET_PARENT_CATEGORY_IDS);
097
098 if (!ArrayUtil.isEmpty(parentCategoryIds)) {
099 BooleanQuery parentCategoryQuery = BooleanQueryFactoryUtil.create(
100 searchContext);
101
102 for (long parentCategoryId : parentCategoryIds) {
103 parentCategoryQuery.addTerm(
104 Field.ASSET_PARENT_CATEGORY_ID,
105 String.valueOf(parentCategoryId));
106 }
107
108 contextQuery.add(parentCategoryQuery, BooleanClauseOccur.MUST);
109 }
110
111 long[] vocabularyIds = (long[])searchContext.getAttribute(
112 Field.ASSET_VOCABULARY_IDS);
113
114 if (!ArrayUtil.isEmpty(vocabularyIds)) {
115 BooleanQuery vocabularyQuery = BooleanQueryFactoryUtil.create(
116 searchContext);
117
118 for (long vocabularyId : vocabularyIds) {
119 vocabularyQuery.addTerm(
120 Field.ASSET_VOCABULARY_ID, String.valueOf(vocabularyId));
121 }
122
123 contextQuery.add(vocabularyQuery, BooleanClauseOccur.MUST);
124 }
125 }
126
127 @Override
128 public void postProcessSearchQuery(
129 BooleanQuery searchQuery, SearchContext searchContext)
130 throws Exception {
131
132 String title = (String)searchContext.getAttribute(Field.TITLE);
133
134 if (Validator.isNotNull(title)) {
135 BooleanQuery localizedQuery = BooleanQueryFactoryUtil.create(
136 searchContext);
137
138 searchContext.setAttribute(Field.ASSET_CATEGORY_TITLE, title);
139
140 addSearchLocalizedTerm(
141 localizedQuery, searchContext, Field.ASSET_CATEGORY_TITLE,
142 true);
143 addSearchLocalizedTerm(
144 localizedQuery, searchContext, Field.TITLE, true);
145
146 searchQuery.add(localizedQuery, BooleanClauseOccur.SHOULD);
147 }
148 }
149
150 @Override
151 protected void doDelete(Object obj) throws Exception {
152 AssetCategory assetCategory = (AssetCategory)obj;
153
154 Document document = new DocumentImpl();
155
156 document.addUID(PORTLET_ID, assetCategory.getCategoryId());
157
158 SearchEngineUtil.deleteDocument(
159 getSearchEngineId(), assetCategory.getCompanyId(),
160 document.get(Field.UID), isCommitImmediately());
161 }
162
163 @Override
164 protected Document doGetDocument(Object obj) throws Exception {
165 AssetCategory category = (AssetCategory)obj;
166
167 if (_log.isDebugEnabled()) {
168 _log.debug("Indexing category " + category);
169 }
170
171 Document document = getBaseModelDocument(PORTLET_ID, category);
172
173 document.addKeyword(Field.ASSET_CATEGORY_ID, category.getCategoryId());
174
175 List<AssetCategory> categories = new ArrayList<AssetCategory>(1);
176
177 categories.add(category);
178
179 addSearchAssetCategoryTitles(
180 document, Field.ASSET_CATEGORY_TITLE, categories);
181
182 document.addKeyword(
183 Field.ASSET_PARENT_CATEGORY_ID, category.getParentCategoryId());
184 document.addKeyword(
185 Field.ASSET_VOCABULARY_ID, category.getVocabularyId());
186 document.addLocalizedText(
187 Field.DESCRIPTION, category.getDescriptionMap());
188 document.addText(Field.NAME, category.getName());
189 document.addLocalizedText(Field.TITLE, category.getTitleMap());
190
191 if (_log.isDebugEnabled()) {
192 _log.debug("Document " + category + " indexed successfully");
193 }
194
195 return document;
196 }
197
198 @Override
199 protected Summary doGetSummary(
200 Document document, Locale locale, String snippet, PortletURL portletURL,
201 PortletRequest portletRequest, PortletResponse portletResponse) {
202
203 return null;
204 }
205
206 @Override
207 protected void doReindex(Object obj) throws Exception {
208 AssetCategory category = (AssetCategory)obj;
209
210 Document document = getDocument(category);
211
212 if (document != null) {
213 SearchEngineUtil.updateDocument(
214 getSearchEngineId(), category.getCompanyId(), document,
215 isCommitImmediately());
216 }
217 }
218
219 @Override
220 protected void doReindex(String className, long classPK) throws Exception {
221 AssetCategory category = AssetCategoryLocalServiceUtil.getCategory(
222 classPK);
223
224 doReindex(category);
225 }
226
227 @Override
228 protected void doReindex(String[] ids) throws Exception {
229 long companyId = GetterUtil.getLong(ids[0]);
230
231 reindexCategories(companyId);
232 }
233
234 @Override
235 protected String getPortletId(SearchContext searchContext) {
236 return PORTLET_ID;
237 }
238
239 protected void reindexCategories(final long companyId)
240 throws PortalException {
241
242 final ActionableDynamicQuery actionableDynamicQuery =
243 AssetCategoryLocalServiceUtil.getActionableDynamicQuery();
244
245 actionableDynamicQuery.setCompanyId(companyId);
246 actionableDynamicQuery.setPerformActionMethod(
247 new ActionableDynamicQuery.PerformActionMethod() {
248
249 @Override
250 public void performAction(Object object)
251 throws PortalException {
252
253 AssetCategory category = (AssetCategory)object;
254
255 Document document = getDocument(category);
256
257 if (document != null) {
258 actionableDynamicQuery.addDocument(document);
259 }
260 }
261
262 });
263 actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
264
265 actionableDynamicQuery.performActions();
266 }
267
268 private static final Log _log = LogFactoryUtil.getLog(
269 AssetCategoryIndexer.class);
270
271 }