001
014
015 package com.liferay.portlet.assetpublisher.util;
016
017 import com.liferay.portal.kernel.search.BaseIndexer;
018 import com.liferay.portal.kernel.search.BooleanClauseOccur;
019 import com.liferay.portal.kernel.search.BooleanQuery;
020 import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
021 import com.liferay.portal.kernel.search.Document;
022 import com.liferay.portal.kernel.search.DocumentImpl;
023 import com.liferay.portal.kernel.search.Field;
024 import com.liferay.portal.kernel.search.Indexer;
025 import com.liferay.portal.kernel.search.IndexerPostProcessor;
026 import com.liferay.portal.kernel.search.SearchContext;
027 import com.liferay.portal.kernel.search.Summary;
028 import com.liferay.portal.kernel.util.Validator;
029 import com.liferay.portal.util.PortalUtil;
030 import com.liferay.portal.util.PropsValues;
031 import com.liferay.portlet.asset.model.AssetCategory;
032 import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
033 import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
034
035 import java.util.ArrayList;
036 import java.util.List;
037 import java.util.Locale;
038
039 import javax.portlet.PortletURL;
040
041
044 public class AssetSearcher extends BaseIndexer {
045
046 public static Indexer getInstance() {
047 return new AssetSearcher();
048 }
049
050 public AssetSearcher() {
051 setFilterSearch(true);
052 setPermissionAware(true);
053 }
054
055 @Override
056 public String[] getClassNames() {
057 long[] classNameIds = _assetEntryQuery.getClassNameIds();
058
059 String[] classNames = new String[classNameIds.length];
060
061 for (int i = 0; i < classNames.length; i++) {
062 long classNameId = classNameIds[i];
063
064 classNames[i] = PortalUtil.getClassName(classNameId);
065 }
066
067 return classNames;
068 }
069
070 @Override
071 public IndexerPostProcessor[] getIndexerPostProcessors() {
072 throw new UnsupportedOperationException();
073 }
074
075 @Override
076 public String getPortletId() {
077 return null;
078 }
079
080 @Override
081 public void registerIndexerPostProcessor(
082 IndexerPostProcessor indexerPostProcessor) {
083
084 throw new UnsupportedOperationException();
085 }
086
087 public void setAssetEntryQuery(AssetEntryQuery assetEntryQuery) {
088 _assetEntryQuery = assetEntryQuery;
089 }
090
091 protected void addSearchAllCategories(
092 BooleanQuery contextQuery, SearchContext searchContext)
093 throws Exception {
094
095 long[] allCategoryIds = _assetEntryQuery.getAllCategoryIds();
096
097 if (allCategoryIds.length == 0) {
098 return;
099 }
100
101 BooleanQuery categoryIdsQuery = BooleanQueryFactoryUtil.create(
102 searchContext);
103
104 for (long allCategoryId : allCategoryIds) {
105 AssetCategory assetCategory =
106 AssetCategoryLocalServiceUtil.fetchAssetCategory(allCategoryId);
107
108 if (assetCategory == null) {
109 continue;
110 }
111
112 List<Long> categoryIds = new ArrayList<Long>();
113
114 if (PropsValues.ASSET_CATEGORIES_SEARCH_HIERARCHICAL) {
115 categoryIds = AssetCategoryLocalServiceUtil.getSubcategoryIds(
116 allCategoryId);
117 }
118
119 if (categoryIds.isEmpty()) {
120 categoryIds.add(allCategoryId);
121 }
122
123 BooleanQuery categoryIdQuery = BooleanQueryFactoryUtil.create(
124 searchContext);
125
126 for (long categoryId : categoryIds) {
127 categoryIdQuery.addTerm(Field.ASSET_CATEGORY_IDS, categoryId);
128 }
129
130 categoryIdsQuery.add(categoryIdQuery, BooleanClauseOccur.MUST);
131 }
132
133 contextQuery.add(categoryIdsQuery, BooleanClauseOccur.MUST);
134 }
135
136 protected void addSearchAllTags(
137 BooleanQuery contextQuery, SearchContext searchContext)
138 throws Exception {
139
140 long[] allTagIds = _assetEntryQuery.getAllTagIds();
141
142 if (allTagIds.length == 0) {
143 return;
144 }
145
146 BooleanQuery tagIdsQuery = BooleanQueryFactoryUtil.create(
147 searchContext);
148
149 for (long tagId : allTagIds) {
150 tagIdsQuery.addRequiredTerm(Field.ASSET_TAG_IDS, tagId);
151 }
152
153 contextQuery.add(tagIdsQuery, BooleanClauseOccur.MUST);
154 }
155
156 protected void addSearchAnyCategories(
157 BooleanQuery contextQuery, SearchContext searchContext)
158 throws Exception {
159
160 long[] anyCategoryIds = _assetEntryQuery.getAnyCategoryIds();
161
162 if (anyCategoryIds.length == 0) {
163 return;
164 }
165
166 BooleanQuery categoryIdsQuery = BooleanQueryFactoryUtil.create(
167 searchContext);
168
169 for (long anyCategoryId : anyCategoryIds) {
170 AssetCategory assetCategory =
171 AssetCategoryLocalServiceUtil.fetchAssetCategory(anyCategoryId);
172
173 if (assetCategory == null) {
174 continue;
175 }
176
177 List<Long> categoryIds = new ArrayList<Long>();
178
179 if (PropsValues.ASSET_CATEGORIES_SEARCH_HIERARCHICAL) {
180 categoryIds = AssetCategoryLocalServiceUtil.getSubcategoryIds(
181 anyCategoryId);
182 }
183
184 if (categoryIds.isEmpty()) {
185 categoryIds.add(anyCategoryId);
186 }
187
188 for (long categoryId : categoryIds) {
189 categoryIdsQuery.addTerm(Field.ASSET_CATEGORY_IDS, categoryId);
190 }
191 }
192
193 contextQuery.add(categoryIdsQuery, BooleanClauseOccur.MUST);
194 }
195
196 protected void addSearchAnyTags(
197 BooleanQuery contextQuery, SearchContext searchContext)
198 throws Exception {
199
200 long[] anyTagIds = _assetEntryQuery.getAnyTagIds();
201
202 if (anyTagIds.length == 0) {
203 return;
204 }
205
206 BooleanQuery tagIdsQuery = BooleanQueryFactoryUtil.create(
207 searchContext);
208
209 for (long tagId : anyTagIds) {
210 tagIdsQuery.addTerm(Field.ASSET_TAG_IDS, tagId);
211 }
212
213 contextQuery.add(tagIdsQuery, BooleanClauseOccur.MUST);
214 }
215
216 @Override
217 protected void addSearchAssetCategoryIds(
218 BooleanQuery contextQuery, SearchContext searchContext)
219 throws Exception {
220
221 addSearchAllCategories(contextQuery, searchContext);
222 addSearchAnyCategories(contextQuery, searchContext);
223 addSearchNotAnyCategories(contextQuery, searchContext);
224 addSearchNotAllCategories(contextQuery, searchContext);
225 }
226
227 @Override
228 protected void addSearchAssetTagNames(
229 BooleanQuery contextQuery, SearchContext searchContext)
230 throws Exception {
231
232 addSearchAllTags(contextQuery, searchContext);
233 addSearchAnyTags(contextQuery, searchContext);
234 addSearchNotAllTags(contextQuery, searchContext);
235 addSearchNotAnyTags(contextQuery, searchContext);
236 }
237
238 @Override
239 protected void addSearchKeywords(
240 BooleanQuery searchQuery, SearchContext searchContext)
241 throws Exception {
242
243 String keywords = searchContext.getKeywords();
244
245 if (Validator.isNull(keywords)) {
246 return;
247 }
248
249 super.addSearchKeywords(searchQuery, searchContext);
250
251 String field = DocumentImpl.getLocalizedName(
252 searchContext.getLocale(), "localized_title");
253
254 searchQuery.addTerm(field, keywords, true);
255 }
256
257 @Override
258 protected void addSearchLayout(
259 BooleanQuery contextQuery, SearchContext searchContext)
260 throws Exception {
261
262 String layoutUuid = (String)searchContext.getAttribute(
263 Field.LAYOUT_UUID);
264
265 if (Validator.isNotNull(layoutUuid)) {
266 contextQuery.addRequiredTerm(Field.LAYOUT_UUID, layoutUuid);
267 }
268 }
269
270 protected void addSearchNotAllCategories(
271 BooleanQuery contextQuery, SearchContext searchContext)
272 throws Exception {
273
274 long[] notAllCategoryIds = _assetEntryQuery.getNotAllCategoryIds();
275
276 if (notAllCategoryIds.length == 0) {
277 return;
278 }
279
280 BooleanQuery categoryIdsQuery = BooleanQueryFactoryUtil.create(
281 searchContext);
282
283 for (long notAllCategoryId : notAllCategoryIds) {
284 AssetCategory assetCategory =
285 AssetCategoryLocalServiceUtil.fetchAssetCategory(
286 notAllCategoryId);
287
288 if (assetCategory == null) {
289 continue;
290 }
291
292 List<Long> categoryIds = new ArrayList<Long>();
293
294 if (PropsValues.ASSET_CATEGORIES_SEARCH_HIERARCHICAL) {
295 categoryIds = AssetCategoryLocalServiceUtil.getSubcategoryIds(
296 notAllCategoryId);
297 }
298
299 if (categoryIds.isEmpty()) {
300 categoryIds.add(notAllCategoryId);
301 }
302
303 BooleanQuery categoryIdQuery = BooleanQueryFactoryUtil.create(
304 searchContext);
305
306 for (long categoryId : categoryIds) {
307 categoryIdQuery.addTerm(Field.ASSET_CATEGORY_IDS, categoryId);
308 }
309
310 categoryIdsQuery.add(categoryIdQuery, BooleanClauseOccur.MUST);
311 }
312
313 contextQuery.add(categoryIdsQuery, BooleanClauseOccur.MUST_NOT);
314 }
315
316 protected void addSearchNotAllTags(
317 BooleanQuery contextQuery, SearchContext searchContext)
318 throws Exception {
319
320 long[] notAllTagIds = _assetEntryQuery.getNotAllTagIds();
321
322 if (notAllTagIds.length == 0) {
323 return;
324 }
325
326 BooleanQuery tagIdsQuery = BooleanQueryFactoryUtil.create(
327 searchContext);
328
329 for (long tagId : notAllTagIds) {
330 tagIdsQuery.addRequiredTerm(Field.ASSET_TAG_IDS, tagId);
331 }
332
333 contextQuery.add(tagIdsQuery, BooleanClauseOccur.MUST_NOT);
334 }
335
336 protected void addSearchNotAnyCategories(
337 BooleanQuery contextQuery, SearchContext searchContext)
338 throws Exception {
339
340 long[] notAnyCategoryIds = _assetEntryQuery.getNotAnyCategoryIds();
341
342 if (notAnyCategoryIds.length == 0) {
343 return;
344 }
345
346 BooleanQuery categoryIdsQuery = BooleanQueryFactoryUtil.create(
347 searchContext);
348
349 for (long notAnyCategoryId : notAnyCategoryIds) {
350 AssetCategory assetCategory =
351 AssetCategoryLocalServiceUtil.fetchAssetCategory(
352 notAnyCategoryId);
353
354 if (assetCategory == null) {
355 continue;
356 }
357
358 List<Long> categoryIds = new ArrayList<Long>();
359
360 if (PropsValues.ASSET_CATEGORIES_SEARCH_HIERARCHICAL) {
361 categoryIds = AssetCategoryLocalServiceUtil.getSubcategoryIds(
362 notAnyCategoryId);
363 }
364
365 if (categoryIds.isEmpty()) {
366 categoryIds.add(notAnyCategoryId);
367 }
368
369 for (long categoryId : categoryIds) {
370 categoryIdsQuery.addTerm(Field.ASSET_CATEGORY_IDS, categoryId);
371 }
372 }
373
374 contextQuery.add(categoryIdsQuery, BooleanClauseOccur.MUST_NOT);
375 }
376
377 protected void addSearchNotAnyTags(
378 BooleanQuery contextQuery, SearchContext searchContext)
379 throws Exception {
380
381 long[] notAnyTagIds = _assetEntryQuery.getNotAnyTagIds();
382
383 if (notAnyTagIds.length == 0) {
384 return;
385 }
386
387 BooleanQuery tagIdsQuery = BooleanQueryFactoryUtil.create(
388 searchContext);
389
390 for (long tagId : _assetEntryQuery.getNotAnyTagIds()) {
391 tagIdsQuery.addTerm(Field.ASSET_TAG_IDS, tagId);
392 }
393
394 contextQuery.add(tagIdsQuery, BooleanClauseOccur.MUST_NOT);
395 }
396
397 @Override
398 protected void doDelete(Object obj) throws Exception {
399 throw new UnsupportedOperationException();
400 }
401
402 @Override
403 protected Document doGetDocument(Object obj) throws Exception {
404 throw new UnsupportedOperationException();
405 }
406
407 @Override
408 protected Summary doGetSummary(
409 Document document, Locale locale, String snippet,
410 PortletURL portletURL)
411 throws Exception {
412
413 throw new UnsupportedOperationException();
414 }
415
416 @Override
417 protected void doReindex(Object obj) throws Exception {
418 throw new UnsupportedOperationException();
419 }
420
421 @Override
422 protected void doReindex(String className, long classPK) throws Exception {
423 throw new UnsupportedOperationException();
424 }
425
426 @Override
427 protected void doReindex(String[] ids) throws Exception {
428 throw new UnsupportedOperationException();
429 }
430
431 @Override
432 protected String getPortletId(SearchContext searchContext) {
433 return null;
434 }
435
436 private AssetEntryQuery _assetEntryQuery;
437
438 }