001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.security.permission.PermissionChecker;
030    import com.liferay.portal.security.permission.PermissionThreadLocal;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portal.util.PropsValues;
033    import com.liferay.portlet.asset.model.AssetCategory;
034    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
035    import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
036    import com.liferay.portlet.asset.util.AssetUtil;
037    
038    import java.util.ArrayList;
039    import java.util.List;
040    import java.util.Locale;
041    
042    import javax.portlet.PortletURL;
043    
044    /**
045     * @author Eudaldo Alonso
046     */
047    public class AssetSearcher extends BaseIndexer {
048    
049            public static Indexer getInstance() {
050                    return new AssetSearcher();
051            }
052    
053            public AssetSearcher() {
054                    setFilterSearch(true);
055                    setPermissionAware(true);
056            }
057    
058            @Override
059            public String[] getClassNames() {
060                    long[] classNameIds = _assetEntryQuery.getClassNameIds();
061    
062                    String[] classNames = new String[classNameIds.length];
063    
064                    for (int i = 0; i < classNames.length; i++) {
065                            long classNameId = classNameIds[i];
066    
067                            classNames[i] = PortalUtil.getClassName(classNameId);
068                    }
069    
070                    return classNames;
071            }
072    
073            @Override
074            public IndexerPostProcessor[] getIndexerPostProcessors() {
075                    throw new UnsupportedOperationException();
076            }
077    
078            @Override
079            public String getPortletId() {
080                    return null;
081            }
082    
083            @Override
084            public void registerIndexerPostProcessor(
085                    IndexerPostProcessor indexerPostProcessor) {
086    
087                    throw new UnsupportedOperationException();
088            }
089    
090            public void setAssetEntryQuery(AssetEntryQuery assetEntryQuery) {
091                    _assetEntryQuery = assetEntryQuery;
092            }
093    
094            protected void addImpossibleTerm(BooleanQuery contextQuery, String field)
095                    throws Exception {
096    
097                    contextQuery.addTerm(field, "-1", false, BooleanClauseOccur.MUST);
098            }
099    
100            protected void addSearchAllCategories(
101                            BooleanQuery contextQuery, SearchContext searchContext)
102                    throws Exception {
103    
104                    PermissionChecker permissionChecker =
105                            PermissionThreadLocal.getPermissionChecker();
106    
107                    long[] allCategoryIds = _assetEntryQuery.getAllCategoryIds();
108    
109                    if (allCategoryIds.length == 0) {
110                            return;
111                    }
112    
113                    long[] filteredAllCategoryIds = AssetUtil.filterCategoryIds(
114                            permissionChecker, allCategoryIds);
115    
116                    if (allCategoryIds.length != filteredAllCategoryIds.length) {
117                            addImpossibleTerm(contextQuery, Field.ASSET_CATEGORY_IDS);
118    
119                            return;
120                    }
121    
122                    BooleanQuery categoryIdsQuery = BooleanQueryFactoryUtil.create(
123                            searchContext);
124    
125                    for (long allCategoryId : filteredAllCategoryIds) {
126                            AssetCategory assetCategory =
127                                    AssetCategoryLocalServiceUtil.fetchAssetCategory(allCategoryId);
128    
129                            if (assetCategory == null) {
130                                    continue;
131                            }
132    
133                            List<Long> categoryIds = new ArrayList<Long>();
134    
135                            if (PropsValues.ASSET_CATEGORIES_SEARCH_HIERARCHICAL) {
136                                    categoryIds.addAll(
137                                            AssetCategoryLocalServiceUtil.getSubcategoryIds(
138                                                    allCategoryId));
139                            }
140    
141                            if (categoryIds.isEmpty()) {
142                                    categoryIds.add(allCategoryId);
143                            }
144    
145                            BooleanQuery categoryIdQuery = BooleanQueryFactoryUtil.create(
146                                    searchContext);
147    
148                            for (long categoryId : categoryIds) {
149                                    categoryIdQuery.addTerm(Field.ASSET_CATEGORY_IDS, categoryId);
150                            }
151    
152                            categoryIdsQuery.add(categoryIdQuery, BooleanClauseOccur.MUST);
153                    }
154    
155                    contextQuery.add(categoryIdsQuery, BooleanClauseOccur.MUST);
156            }
157    
158            protected void addSearchAllTags(
159                            BooleanQuery contextQuery, SearchContext searchContext)
160                    throws Exception {
161    
162                    PermissionChecker permissionChecker =
163                            PermissionThreadLocal.getPermissionChecker();
164    
165                    long[][] allTagIdsArray = _assetEntryQuery.getAllTagIdsArray();
166    
167                    if (allTagIdsArray.length == 0) {
168                            return;
169                    }
170    
171                    BooleanQuery tagIdsArrayQuery = BooleanQueryFactoryUtil.create(
172                            searchContext);
173    
174                    for (long[] allTagIds : allTagIdsArray) {
175                            if (allTagIds.length == 0) {
176                                    continue;
177                            }
178    
179                            long[] filteredAllTagIds = AssetUtil.filterTagIds(
180                                    permissionChecker, allTagIds);
181    
182                            if (allTagIds.length != filteredAllTagIds.length) {
183                                    addImpossibleTerm(contextQuery, Field.ASSET_TAG_IDS);
184    
185                                    return;
186                            }
187    
188                            BooleanQuery tagIdsQuery = BooleanQueryFactoryUtil.create(
189                                    searchContext);
190    
191                            for (long tagId : allTagIds) {
192                                    tagIdsQuery.addTerm(Field.ASSET_TAG_IDS, tagId);
193                            }
194    
195                            tagIdsArrayQuery.add(tagIdsQuery, BooleanClauseOccur.MUST);
196                    }
197    
198                    contextQuery.add(tagIdsArrayQuery, BooleanClauseOccur.MUST);
199            }
200    
201            protected void addSearchAnyCategories(
202                            BooleanQuery contextQuery, SearchContext searchContext)
203                    throws Exception {
204    
205                    PermissionChecker permissionChecker =
206                            PermissionThreadLocal.getPermissionChecker();
207    
208                    long[] anyCategoryIds = _assetEntryQuery.getAnyCategoryIds();
209    
210                    if (anyCategoryIds.length == 0) {
211                            return;
212                    }
213    
214                    long[] filteredAnyCategoryIds = AssetUtil.filterCategoryIds(
215                            permissionChecker, anyCategoryIds);
216    
217                    if (filteredAnyCategoryIds.length == 0) {
218                            addImpossibleTerm(contextQuery, Field.ASSET_CATEGORY_IDS);
219    
220                            return;
221                    }
222    
223                    BooleanQuery categoryIdsQuery = BooleanQueryFactoryUtil.create(
224                            searchContext);
225    
226                    for (long anyCategoryId : filteredAnyCategoryIds) {
227                            AssetCategory assetCategory =
228                                    AssetCategoryLocalServiceUtil.fetchAssetCategory(anyCategoryId);
229    
230                            if (assetCategory == null) {
231                                    continue;
232                            }
233    
234                            List<Long> categoryIds = new ArrayList<Long>();
235    
236                            if (PropsValues.ASSET_CATEGORIES_SEARCH_HIERARCHICAL) {
237                                    categoryIds.addAll(
238                                            AssetCategoryLocalServiceUtil.getSubcategoryIds(
239                                                    anyCategoryId));
240                            }
241    
242                            if (categoryIds.isEmpty()) {
243                                    categoryIds.add(anyCategoryId);
244                            }
245    
246                            for (long categoryId : categoryIds) {
247                                    categoryIdsQuery.addTerm(Field.ASSET_CATEGORY_IDS, categoryId);
248                            }
249                    }
250    
251                    contextQuery.add(categoryIdsQuery, BooleanClauseOccur.MUST);
252            }
253    
254            protected void addSearchAnyTags(
255                            BooleanQuery contextQuery, SearchContext searchContext)
256                    throws Exception {
257    
258                    PermissionChecker permissionChecker =
259                            PermissionThreadLocal.getPermissionChecker();
260    
261                    long[] anyTagIds = _assetEntryQuery.getAnyTagIds();
262    
263                    if (anyTagIds.length == 0) {
264                            return;
265                    }
266    
267                    long[] filteredAnyTagIds = AssetUtil.filterTagIds(
268                            permissionChecker, anyTagIds);
269    
270                    if (filteredAnyTagIds.length == 0) {
271                            addImpossibleTerm(contextQuery, Field.ASSET_TAG_IDS);
272    
273                            return;
274                    }
275    
276                    BooleanQuery tagIdsQuery = BooleanQueryFactoryUtil.create(
277                            searchContext);
278    
279                    for (long tagId : anyTagIds) {
280                            tagIdsQuery.addTerm(Field.ASSET_TAG_IDS, tagId);
281                    }
282    
283                    contextQuery.add(tagIdsQuery, BooleanClauseOccur.MUST);
284            }
285    
286            @Override
287            protected void addSearchAssetCategoryIds(
288                            BooleanQuery contextQuery, SearchContext searchContext)
289                    throws Exception {
290    
291                    addSearchAllCategories(contextQuery, searchContext);
292                    addSearchAnyCategories(contextQuery, searchContext);
293                    addSearchNotAnyCategories(contextQuery, searchContext);
294                    addSearchNotAllCategories(contextQuery, searchContext);
295            }
296    
297            @Override
298            protected void addSearchAssetTagNames(
299                            BooleanQuery contextQuery, SearchContext searchContext)
300                    throws Exception {
301    
302                    addSearchAllTags(contextQuery, searchContext);
303                    addSearchAnyTags(contextQuery, searchContext);
304                    addSearchNotAllTags(contextQuery, searchContext);
305                    addSearchNotAnyTags(contextQuery, searchContext);
306            }
307    
308            @Override
309            protected void addSearchKeywords(
310                            BooleanQuery searchQuery, SearchContext searchContext)
311                    throws Exception {
312    
313                    String keywords = searchContext.getKeywords();
314    
315                    if (Validator.isNull(keywords)) {
316                            return;
317                    }
318    
319                    super.addSearchKeywords(searchQuery, searchContext);
320    
321                    String field = DocumentImpl.getLocalizedName(
322                            searchContext.getLocale(), "localized_title");
323    
324                    searchQuery.addTerm(field, keywords, true);
325            }
326    
327            @Override
328            protected void addSearchLayout(
329                            BooleanQuery contextQuery, SearchContext searchContext)
330                    throws Exception {
331    
332                    String layoutUuid = (String)searchContext.getAttribute(
333                            Field.LAYOUT_UUID);
334    
335                    if (Validator.isNotNull(layoutUuid)) {
336                            contextQuery.addRequiredTerm(Field.LAYOUT_UUID, layoutUuid);
337                    }
338            }
339    
340            protected void addSearchNotAllCategories(
341                            BooleanQuery contextQuery, SearchContext searchContext)
342                    throws Exception {
343    
344                    long[] notAllCategoryIds = _assetEntryQuery.getNotAllCategoryIds();
345    
346                    if (notAllCategoryIds.length == 0) {
347                            return;
348                    }
349    
350                    BooleanQuery categoryIdsQuery = BooleanQueryFactoryUtil.create(
351                            searchContext);
352    
353                    for (long notAllCategoryId : notAllCategoryIds) {
354                            AssetCategory assetCategory =
355                                    AssetCategoryLocalServiceUtil.fetchAssetCategory(
356                                            notAllCategoryId);
357    
358                            if (assetCategory == null) {
359                                    continue;
360                            }
361    
362                            List<Long> categoryIds = new ArrayList<Long>();
363    
364                            if (PropsValues.ASSET_CATEGORIES_SEARCH_HIERARCHICAL) {
365                                    categoryIds.addAll(
366                                            AssetCategoryLocalServiceUtil.getSubcategoryIds(
367                                                    notAllCategoryId));
368                            }
369    
370                            if (categoryIds.isEmpty()) {
371                                    categoryIds.add(notAllCategoryId);
372                            }
373    
374                            BooleanQuery categoryIdQuery = BooleanQueryFactoryUtil.create(
375                                    searchContext);
376    
377                            for (long categoryId : categoryIds) {
378                                    categoryIdQuery.addTerm(Field.ASSET_CATEGORY_IDS, categoryId);
379                            }
380    
381                            categoryIdsQuery.add(categoryIdQuery, BooleanClauseOccur.MUST);
382                    }
383    
384                    contextQuery.add(categoryIdsQuery, BooleanClauseOccur.MUST_NOT);
385            }
386    
387            protected void addSearchNotAllTags(
388                            BooleanQuery contextQuery, SearchContext searchContext)
389                    throws Exception {
390    
391                    long[][] notAllTagIdsArray = _assetEntryQuery.getNotAllTagIdsArray();
392    
393                    if (notAllTagIdsArray.length == 0) {
394                            return;
395                    }
396    
397                    BooleanQuery tagIdsArrayQuery = BooleanQueryFactoryUtil.create(
398                            searchContext);
399    
400                    for (long[] notAllTagIds : notAllTagIdsArray) {
401                            if (notAllTagIds.length == 0) {
402                                    continue;
403                            }
404    
405                            BooleanQuery tagIdsQuery = BooleanQueryFactoryUtil.create(
406                                    searchContext);
407    
408                            for (long tagId : notAllTagIds) {
409                                    tagIdsQuery.addTerm(Field.ASSET_TAG_IDS, tagId);
410                            }
411    
412                            tagIdsArrayQuery.add(tagIdsQuery, BooleanClauseOccur.MUST);
413                    }
414    
415                    contextQuery.add(tagIdsArrayQuery, BooleanClauseOccur.MUST_NOT);
416            }
417    
418            protected void addSearchNotAnyCategories(
419                            BooleanQuery contextQuery, SearchContext searchContext)
420                    throws Exception {
421    
422                    long[] notAnyCategoryIds = _assetEntryQuery.getNotAnyCategoryIds();
423    
424                    if (notAnyCategoryIds.length == 0) {
425                            return;
426                    }
427    
428                    BooleanQuery categoryIdsQuery = BooleanQueryFactoryUtil.create(
429                            searchContext);
430    
431                    for (long notAnyCategoryId : notAnyCategoryIds) {
432                            AssetCategory assetCategory =
433                                    AssetCategoryLocalServiceUtil.fetchAssetCategory(
434                                            notAnyCategoryId);
435    
436                            if (assetCategory == null) {
437                                    continue;
438                            }
439    
440                            List<Long> categoryIds = new ArrayList<Long>();
441    
442                            if (PropsValues.ASSET_CATEGORIES_SEARCH_HIERARCHICAL) {
443                                    categoryIds.addAll(
444                                            AssetCategoryLocalServiceUtil.getSubcategoryIds(
445                                                    notAnyCategoryId));
446                            }
447    
448                            if (categoryIds.isEmpty()) {
449                                    categoryIds.add(notAnyCategoryId);
450                            }
451    
452                            for (long categoryId : categoryIds) {
453                                    categoryIdsQuery.addTerm(Field.ASSET_CATEGORY_IDS, categoryId);
454                            }
455                    }
456    
457                    contextQuery.add(categoryIdsQuery, BooleanClauseOccur.MUST_NOT);
458            }
459    
460            protected void addSearchNotAnyTags(
461                            BooleanQuery contextQuery, SearchContext searchContext)
462                    throws Exception {
463    
464                    long[] notAnyTagIds = _assetEntryQuery.getNotAnyTagIds();
465    
466                    if (notAnyTagIds.length == 0) {
467                            return;
468                    }
469    
470                    BooleanQuery tagIdsQuery = BooleanQueryFactoryUtil.create(
471                            searchContext);
472    
473                    for (long tagId : _assetEntryQuery.getNotAnyTagIds()) {
474                            tagIdsQuery.addTerm(Field.ASSET_TAG_IDS, tagId);
475                    }
476    
477                    contextQuery.add(tagIdsQuery, BooleanClauseOccur.MUST_NOT);
478            }
479    
480            @Override
481            protected void postProcessFullQuery(
482                            BooleanQuery fullQuery, SearchContext searchContext)
483                    throws Exception {
484    
485                    fullQuery.addRequiredTerm("visible", true);
486            }
487    
488            @Override
489            protected void doDelete(Object obj) throws Exception {
490                    throw new UnsupportedOperationException();
491            }
492    
493            @Override
494            protected Document doGetDocument(Object obj) throws Exception {
495                    throw new UnsupportedOperationException();
496            }
497    
498            @Override
499            protected Summary doGetSummary(
500                            Document document, Locale locale, String snippet,
501                            PortletURL portletURL)
502                    throws Exception {
503    
504                    throw new UnsupportedOperationException();
505            }
506    
507            @Override
508            protected void doReindex(Object obj) throws Exception {
509                    throw new UnsupportedOperationException();
510            }
511    
512            @Override
513            protected void doReindex(String className, long classPK) throws Exception {
514                    throw new UnsupportedOperationException();
515            }
516    
517            @Override
518            protected void doReindex(String[] ids) throws Exception {
519                    throw new UnsupportedOperationException();
520            }
521    
522            @Override
523            protected String getPortletId(SearchContext searchContext) {
524                    return null;
525            }
526    
527            private AssetEntryQuery _assetEntryQuery;
528    
529    }