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