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.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    /**
051     * @author Istvan Andras Dezsi
052     */
053    @OSGiBeanProperties
054    public class AssetCategoryIndexer extends BaseIndexer {
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(Object obj) throws Exception {
144                    AssetCategory assetCategory = (AssetCategory)obj;
145    
146                    Document document = new DocumentImpl();
147    
148                    document.addUID(CLASS_NAME, assetCategory.getCategoryId());
149    
150                    SearchEngineUtil.deleteDocument(
151                            getSearchEngineId(), assetCategory.getCompanyId(),
152                            document.get(Field.UID), isCommitImmediately());
153            }
154    
155            @Override
156            protected Document doGetDocument(Object obj) throws Exception {
157                    AssetCategory category = (AssetCategory)obj;
158    
159                    if (_log.isDebugEnabled()) {
160                            _log.debug("Indexing category " + category);
161                    }
162    
163                    Document document = getBaseModelDocument(CLASS_NAME, category);
164    
165                    document.addKeyword(Field.ASSET_CATEGORY_ID, category.getCategoryId());
166    
167                    List<AssetCategory> categories = new ArrayList<>(1);
168    
169                    categories.add(category);
170    
171                    addSearchAssetCategoryTitles(
172                            document, Field.ASSET_CATEGORY_TITLE, categories);
173    
174                    document.addKeyword(
175                            Field.ASSET_PARENT_CATEGORY_ID, category.getParentCategoryId());
176                    document.addKeyword(
177                            Field.ASSET_VOCABULARY_ID, category.getVocabularyId());
178                    document.addLocalizedText(
179                            Field.DESCRIPTION, category.getDescriptionMap());
180                    document.addText(Field.NAME, category.getName());
181                    document.addLocalizedText(Field.TITLE, category.getTitleMap());
182    
183                    if (_log.isDebugEnabled()) {
184                            _log.debug("Document " + category + " 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(Object obj) throws Exception {
200                    AssetCategory category = (AssetCategory)obj;
201    
202                    Document document = getDocument(category);
203    
204                    if (document != null) {
205                            SearchEngineUtil.updateDocument(
206                                    getSearchEngineId(), category.getCompanyId(), document,
207                                    isCommitImmediately());
208                    }
209            }
210    
211            @Override
212            protected void doReindex(String className, long classPK) throws Exception {
213                    AssetCategory category = AssetCategoryLocalServiceUtil.getCategory(
214                            classPK);
215    
216                    doReindex(category);
217            }
218    
219            @Override
220            protected void doReindex(String[] ids) throws Exception {
221                    long companyId = GetterUtil.getLong(ids[0]);
222    
223                    reindexCategories(companyId);
224            }
225    
226            protected void reindexCategories(final long companyId)
227                    throws PortalException {
228    
229                    final ActionableDynamicQuery actionableDynamicQuery =
230                            AssetCategoryLocalServiceUtil.getActionableDynamicQuery();
231    
232                    actionableDynamicQuery.setCompanyId(companyId);
233                    actionableDynamicQuery.setPerformActionMethod(
234                            new ActionableDynamicQuery.PerformActionMethod() {
235    
236                                    @Override
237                                    public void performAction(Object object) {
238                                            AssetCategory category = (AssetCategory)object;
239    
240                                            try {
241                                                    Document document = getDocument(category);
242    
243                                                    if (document != null) {
244                                                            actionableDynamicQuery.addDocument(document);
245                                                    }
246                                            }
247                                            catch (PortalException pe) {
248                                                    if (_log.isWarnEnabled()) {
249                                                            _log.warn(
250                                                                    "Unable to index asset category " +
251                                                                            category.getCategoryId(),
252                                                                    pe);
253                                                    }
254                                            }
255                                    }
256    
257                            });
258                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
259    
260                    actionableDynamicQuery.performActions();
261            }
262    
263            private static final Log _log = LogFactoryUtil.getLog(
264                    AssetCategoryIndexer.class);
265    
266    }