001    /**
002     * Copyright (c) 2000-present 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.asset.util;
016    
017    import com.liferay.asset.kernel.model.AssetCategory;
018    import com.liferay.asset.kernel.service.AssetCategoryLocalServiceUtil;
019    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
020    import com.liferay.portal.kernel.dao.orm.IndexableActionableDynamicQuery;
021    import com.liferay.portal.kernel.exception.PortalException;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.search.BaseIndexer;
025    import com.liferay.portal.kernel.search.BooleanClauseOccur;
026    import com.liferay.portal.kernel.search.BooleanQuery;
027    import com.liferay.portal.kernel.search.Document;
028    import com.liferay.portal.kernel.search.Field;
029    import com.liferay.portal.kernel.search.IndexWriterHelperUtil;
030    import com.liferay.portal.kernel.search.SearchContext;
031    import com.liferay.portal.kernel.search.Summary;
032    import com.liferay.portal.kernel.search.filter.BooleanFilter;
033    import com.liferay.portal.kernel.search.filter.TermsFilter;
034    import com.liferay.portal.kernel.search.generic.BooleanQueryImpl;
035    import com.liferay.portal.kernel.security.permission.ActionKeys;
036    import com.liferay.portal.kernel.security.permission.PermissionChecker;
037    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
038    import com.liferay.portal.kernel.util.ArrayUtil;
039    import com.liferay.portal.kernel.util.GetterUtil;
040    import com.liferay.portal.kernel.util.Validator;
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    /**
051     * @author Istvan Andras Dezsi
052     */
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                    setDefaultSelectedFieldNames(
060                            Field.ASSET_CATEGORY_ID, Field.COMPANY_ID, Field.GROUP_ID,
061                            Field.UID);
062                    setFilterSearch(true);
063                    setPermissionAware(true);
064            }
065    
066            @Override
067            public String getClassName() {
068                    return CLASS_NAME;
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 void postProcessContextBooleanFilter(
086                            BooleanFilter contextBooleanFilter, SearchContext searchContext)
087                    throws Exception {
088    
089                    long[] parentCategoryIds = (long[])searchContext.getAttribute(
090                            Field.ASSET_PARENT_CATEGORY_IDS);
091    
092                    if (!ArrayUtil.isEmpty(parentCategoryIds)) {
093                            TermsFilter parentCategoryTermsFilter = new TermsFilter(
094                                    Field.ASSET_PARENT_CATEGORY_ID);
095    
096                            parentCategoryTermsFilter.addValues(
097                                    ArrayUtil.toStringArray(parentCategoryIds));
098    
099                            contextBooleanFilter.add(
100                                    parentCategoryTermsFilter, BooleanClauseOccur.MUST);
101                    }
102    
103                    long[] vocabularyIds = (long[])searchContext.getAttribute(
104                            Field.ASSET_VOCABULARY_IDS);
105    
106                    if (!ArrayUtil.isEmpty(vocabularyIds)) {
107                            TermsFilter vocabularyTermsFilter = new TermsFilter(
108                                    Field.ASSET_VOCABULARY_ID);
109    
110                            vocabularyTermsFilter.addValues(
111                                    ArrayUtil.toStringArray(vocabularyIds));
112    
113                            contextBooleanFilter.add(
114                                    vocabularyTermsFilter, BooleanClauseOccur.MUST);
115                    }
116            }
117    
118            @Override
119            public void postProcessSearchQuery(
120                            BooleanQuery searchQuery, BooleanFilter fullQueryBooleanFilter,
121                            SearchContext searchContext)
122                    throws Exception {
123    
124                    String title = (String)searchContext.getAttribute(Field.TITLE);
125    
126                    if (Validator.isNotNull(title)) {
127                            BooleanQuery localizedQuery = new BooleanQueryImpl();
128    
129                            searchContext.setAttribute(Field.ASSET_CATEGORY_TITLE, title);
130    
131                            addSearchLocalizedTerm(
132                                    localizedQuery, searchContext, Field.ASSET_CATEGORY_TITLE,
133                                    true);
134                            addSearchLocalizedTerm(
135                                    localizedQuery, searchContext, Field.TITLE, true);
136    
137                            searchQuery.add(localizedQuery, BooleanClauseOccur.SHOULD);
138                    }
139            }
140    
141            @Override
142            protected void doDelete(AssetCategory assetCategory) throws Exception {
143                    deleteDocument(
144                            assetCategory.getCompanyId(), assetCategory.getCategoryId());
145            }
146    
147            @Override
148            protected Document doGetDocument(AssetCategory assetCategory)
149                    throws Exception {
150    
151                    if (_log.isDebugEnabled()) {
152                            _log.debug("Indexing asset category " + assetCategory);
153                    }
154    
155                    Document document = getBaseModelDocument(CLASS_NAME, assetCategory);
156    
157                    document.addKeyword(
158                            Field.ASSET_CATEGORY_ID, assetCategory.getCategoryId());
159    
160                    List<AssetCategory> categories = new ArrayList<>(1);
161    
162                    categories.add(assetCategory);
163    
164                    addSearchAssetCategoryTitles(
165                            document, Field.ASSET_CATEGORY_TITLE, categories);
166    
167                    document.addKeyword(
168                            Field.ASSET_PARENT_CATEGORY_ID,
169                            assetCategory.getParentCategoryId());
170                    document.addKeyword(
171                            Field.ASSET_VOCABULARY_ID, assetCategory.getVocabularyId());
172                    document.addLocalizedText(
173                            Field.DESCRIPTION, assetCategory.getDescriptionMap());
174                    document.addText(Field.NAME, assetCategory.getName());
175                    document.addLocalizedText(Field.TITLE, assetCategory.getTitleMap());
176    
177                    if (_log.isDebugEnabled()) {
178                            _log.debug("Document " + assetCategory + " indexed successfully");
179                    }
180    
181                    return document;
182            }
183    
184            @Override
185            protected Summary doGetSummary(
186                    Document document, Locale locale, String snippet,
187                    PortletRequest portletRequest, PortletResponse portletResponse) {
188    
189                    return null;
190            }
191    
192            @Override
193            protected void doReindex(AssetCategory assetCategory) throws Exception {
194                    Document document = getDocument(assetCategory);
195    
196                    IndexWriterHelperUtil.updateDocument(
197                            getSearchEngineId(), assetCategory.getCompanyId(), document,
198                            isCommitImmediately());
199            }
200    
201            @Override
202            protected void doReindex(String className, long classPK) throws Exception {
203                    AssetCategory category = AssetCategoryLocalServiceUtil.getCategory(
204                            classPK);
205    
206                    doReindex(category);
207            }
208    
209            @Override
210            protected void doReindex(String[] ids) throws Exception {
211                    long companyId = GetterUtil.getLong(ids[0]);
212    
213                    reindexCategories(companyId);
214            }
215    
216            protected void reindexCategories(final long companyId)
217                    throws PortalException {
218    
219                    final IndexableActionableDynamicQuery indexableActionableDynamicQuery =
220                            AssetCategoryLocalServiceUtil.getIndexableActionableDynamicQuery();
221    
222                    indexableActionableDynamicQuery.setCompanyId(companyId);
223                    indexableActionableDynamicQuery.setPerformActionMethod(
224                            new ActionableDynamicQuery.PerformActionMethod<AssetCategory>() {
225    
226                                    @Override
227                                    public void performAction(AssetCategory category) {
228                                            try {
229                                                    Document document = getDocument(category);
230    
231                                                    if (document != null) {
232                                                            indexableActionableDynamicQuery.addDocuments(
233                                                                    document);
234                                                    }
235                                            }
236                                            catch (PortalException pe) {
237                                                    if (_log.isWarnEnabled()) {
238                                                            _log.warn(
239                                                                    "Unable to index asset category " +
240                                                                            category.getCategoryId(),
241                                                                    pe);
242                                                    }
243                                            }
244                                    }
245    
246                            });
247                    indexableActionableDynamicQuery.setSearchEngineId(getSearchEngineId());
248    
249                    indexableActionableDynamicQuery.performActions();
250            }
251    
252            private static final Log _log = LogFactoryUtil.getLog(
253                    AssetCategoryIndexer.class);
254    
255    }