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.Document;
025 import com.liferay.portal.kernel.search.DocumentImpl;
026 import com.liferay.portal.kernel.search.Field;
027 import com.liferay.portal.kernel.search.SearchContext;
028 import com.liferay.portal.kernel.search.SearchEngineUtil;
029 import com.liferay.portal.kernel.search.Summary;
030 import com.liferay.portal.kernel.search.filter.BooleanFilter;
031 import com.liferay.portal.kernel.search.filter.TermsFilter;
032 import com.liferay.portal.kernel.search.generic.BooleanQueryImpl;
033 import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
034 import com.liferay.portal.kernel.util.ArrayUtil;
035 import com.liferay.portal.kernel.util.GetterUtil;
036 import com.liferay.portal.kernel.util.Validator;
037 import com.liferay.portal.security.permission.ActionKeys;
038 import com.liferay.portal.security.permission.PermissionChecker;
039 import com.liferay.portlet.asset.model.AssetCategory;
040 import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
041 import com.liferay.portlet.asset.service.permission.AssetCategoryPermission;
042
043 import java.util.ArrayList;
044 import java.util.List;
045 import java.util.Locale;
046
047 import javax.portlet.PortletRequest;
048 import javax.portlet.PortletResponse;
049
050
053 @OSGiBeanProperties
054 public class AssetCategoryIndexer extends BaseIndexer<AssetCategory> {
055
056 public static final String CLASS_NAME = AssetCategory.class.getName();
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 getClassName() {
069 return CLASS_NAME;
070 }
071
072 @Override
073 public boolean hasPermission(
074 PermissionChecker permissionChecker, String entryClassName,
075 long entryClassPK, String actionId)
076 throws Exception {
077
078 AssetCategory category = AssetCategoryLocalServiceUtil.getCategory(
079 entryClassPK);
080
081 return AssetCategoryPermission.contains(
082 permissionChecker, category, ActionKeys.VIEW);
083 }
084
085 @Override
086 public void postProcessContextBooleanFilter(
087 BooleanFilter contextBooleanFilter, SearchContext searchContext)
088 throws Exception {
089
090 long[] parentCategoryIds = (long[])searchContext.getAttribute(
091 Field.ASSET_PARENT_CATEGORY_IDS);
092
093 if (!ArrayUtil.isEmpty(parentCategoryIds)) {
094 TermsFilter parentCategoryTermsFilter = new TermsFilter(
095 Field.ASSET_PARENT_CATEGORY_ID);
096
097 parentCategoryTermsFilter.addValues(
098 ArrayUtil.toStringArray(parentCategoryIds));
099
100 contextBooleanFilter.add(
101 parentCategoryTermsFilter, BooleanClauseOccur.MUST);
102 }
103
104 long[] vocabularyIds = (long[])searchContext.getAttribute(
105 Field.ASSET_VOCABULARY_IDS);
106
107 if (!ArrayUtil.isEmpty(vocabularyIds)) {
108 TermsFilter vocabularyTermsFilter = new TermsFilter(
109 Field.ASSET_VOCABULARY_ID);
110
111 vocabularyTermsFilter.addValues(
112 ArrayUtil.toStringArray(vocabularyIds));
113
114 contextBooleanFilter.add(
115 vocabularyTermsFilter, BooleanClauseOccur.MUST);
116 }
117 }
118
119 @Override
120 public void postProcessSearchQuery(
121 BooleanQuery searchQuery, BooleanFilter fullQueryBooleanFilter,
122 SearchContext searchContext)
123 throws Exception {
124
125 String title = (String)searchContext.getAttribute(Field.TITLE);
126
127 if (Validator.isNotNull(title)) {
128 BooleanQuery localizedQuery = new BooleanQueryImpl();
129
130 searchContext.setAttribute(Field.ASSET_CATEGORY_TITLE, title);
131
132 addSearchLocalizedTerm(
133 localizedQuery, searchContext, Field.ASSET_CATEGORY_TITLE,
134 true);
135 addSearchLocalizedTerm(
136 localizedQuery, searchContext, Field.TITLE, true);
137
138 searchQuery.add(localizedQuery, BooleanClauseOccur.SHOULD);
139 }
140 }
141
142 @Override
143 protected void doDelete(AssetCategory assetCategory) throws Exception {
144 Document document = new DocumentImpl();
145
146 document.addUID(CLASS_NAME, assetCategory.getCategoryId());
147
148 SearchEngineUtil.deleteDocument(
149 getSearchEngineId(), assetCategory.getCompanyId(),
150 document.get(Field.UID), isCommitImmediately());
151 }
152
153 @Override
154 protected Document doGetDocument(AssetCategory assetCategory)
155 throws Exception {
156
157 if (_log.isDebugEnabled()) {
158 _log.debug("Indexing asset category " + assetCategory);
159 }
160
161 Document document = getBaseModelDocument(CLASS_NAME, assetCategory);
162
163 document.addKeyword(
164 Field.ASSET_CATEGORY_ID, assetCategory.getCategoryId());
165
166 List<AssetCategory> categories = new ArrayList<>(1);
167
168 categories.add(assetCategory);
169
170 addSearchAssetCategoryTitles(
171 document, Field.ASSET_CATEGORY_TITLE, categories);
172
173 document.addKeyword(
174 Field.ASSET_PARENT_CATEGORY_ID,
175 assetCategory.getParentCategoryId());
176 document.addKeyword(
177 Field.ASSET_VOCABULARY_ID, assetCategory.getVocabularyId());
178 document.addLocalizedText(
179 Field.DESCRIPTION, assetCategory.getDescriptionMap());
180 document.addText(Field.NAME, assetCategory.getName());
181 document.addLocalizedText(Field.TITLE, assetCategory.getTitleMap());
182
183 if (_log.isDebugEnabled()) {
184 _log.debug("Document " + assetCategory + " indexed successfully");
185 }
186
187 return document;
188 }
189
190 @Override
191 protected Summary doGetSummary(
192 Document document, Locale locale, String snippet,
193 PortletRequest portletRequest, PortletResponse portletResponse) {
194
195 return null;
196 }
197
198 @Override
199 protected void doReindex(AssetCategory assetCategory) throws Exception {
200 Document document = getDocument(assetCategory);
201
202 if (document != null) {
203 SearchEngineUtil.updateDocument(
204 getSearchEngineId(), assetCategory.getCompanyId(), document,
205 isCommitImmediately());
206 }
207 }
208
209 @Override
210 protected void doReindex(String className, long classPK) throws Exception {
211 AssetCategory category = AssetCategoryLocalServiceUtil.getCategory(
212 classPK);
213
214 doReindex(category);
215 }
216
217 @Override
218 protected void doReindex(String[] ids) throws Exception {
219 long companyId = GetterUtil.getLong(ids[0]);
220
221 reindexCategories(companyId);
222 }
223
224 protected void reindexCategories(final long companyId)
225 throws PortalException {
226
227 final ActionableDynamicQuery actionableDynamicQuery =
228 AssetCategoryLocalServiceUtil.getActionableDynamicQuery();
229
230 actionableDynamicQuery.setCompanyId(companyId);
231 actionableDynamicQuery.setPerformActionMethod(
232 new ActionableDynamicQuery.PerformActionMethod<AssetCategory>() {
233
234 @Override
235 public void performAction(AssetCategory category) {
236
237 try {
238 Document document = getDocument(category);
239
240 if (document != null) {
241 actionableDynamicQuery.addDocument(document);
242 }
243 }
244 catch (PortalException pe) {
245 if (_log.isWarnEnabled()) {
246 _log.warn(
247 "Unable to index asset category " +
248 category.getCategoryId(),
249 pe);
250 }
251 }
252 }
253
254 });
255 actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
256
257 actionableDynamicQuery.performActions();
258 }
259
260 private static final Log _log = LogFactoryUtil.getLog(
261 AssetCategoryIndexer.class);
262
263 }