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.generic.BooleanQueryImpl;
032    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
033    import com.liferay.portal.kernel.util.GetterUtil;
034    import com.liferay.portal.kernel.util.Validator;
035    import com.liferay.portal.security.permission.ActionKeys;
036    import com.liferay.portal.security.permission.PermissionChecker;
037    import com.liferay.portlet.asset.model.AssetVocabulary;
038    import com.liferay.portlet.asset.service.AssetVocabularyLocalServiceUtil;
039    import com.liferay.portlet.asset.service.permission.AssetVocabularyPermission;
040    
041    import java.util.Locale;
042    
043    import javax.portlet.PortletRequest;
044    import javax.portlet.PortletResponse;
045    
046    /**
047     * @author Istvan Andras Dezsi
048     */
049    @OSGiBeanProperties
050    public class AssetVocabularyIndexer extends BaseIndexer<AssetVocabulary> {
051    
052            public static final String CLASS_NAME = AssetVocabulary.class.getName();
053    
054            public AssetVocabularyIndexer() {
055                    setCommitImmediately(true);
056                    setDefaultSelectedFieldNames(
057                            Field.ASSET_VOCABULARY_ID, Field.COMPANY_ID, Field.GROUP_ID,
058                            Field.UID);
059                    setFilterSearch(true);
060                    setPermissionAware(true);
061            }
062    
063            @Override
064            public String getClassName() {
065                    return CLASS_NAME;
066            }
067    
068            @Override
069            public boolean hasPermission(
070                            PermissionChecker permissionChecker, String entryClassName,
071                            long entryClassPK, String actionId)
072                    throws Exception {
073    
074                    AssetVocabulary vocabulary =
075                            AssetVocabularyLocalServiceUtil.getVocabulary(entryClassPK);
076    
077                    return AssetVocabularyPermission.contains(
078                            permissionChecker, vocabulary, ActionKeys.VIEW);
079            }
080    
081            @Override
082            public void postProcessSearchQuery(
083                            BooleanQuery searchQuery, BooleanFilter fullQueryBooleanFilter,
084                            SearchContext searchContext)
085                    throws Exception {
086    
087                    String title = (String)searchContext.getAttribute(Field.TITLE);
088    
089                    if (Validator.isNotNull(title)) {
090                            BooleanQuery localizedQuery = new BooleanQueryImpl();
091    
092                            addSearchLocalizedTerm(
093                                    localizedQuery, searchContext, Field.TITLE, true);
094    
095                            searchQuery.add(localizedQuery, BooleanClauseOccur.SHOULD);
096                    }
097            }
098    
099            @Override
100            protected void doDelete(AssetVocabulary assetVocabulary) throws Exception {
101                    Document document = new DocumentImpl();
102    
103                    document.addUID(CLASS_NAME, assetVocabulary.getVocabularyId());
104    
105                    SearchEngineUtil.deleteDocument(
106                            getSearchEngineId(), assetVocabulary.getCompanyId(),
107                            document.get(Field.UID), isCommitImmediately());
108            }
109    
110            @Override
111            protected Document doGetDocument(AssetVocabulary assetVocabulary)
112                    throws Exception {
113    
114                    if (_log.isDebugEnabled()) {
115                            _log.debug("Indexing asset vocabulary " + assetVocabulary);
116                    }
117    
118                    Document document = getBaseModelDocument(CLASS_NAME, assetVocabulary);
119    
120                    document.addKeyword(
121                            Field.ASSET_VOCABULARY_ID, assetVocabulary.getVocabularyId());
122                    document.addLocalizedText(
123                            Field.DESCRIPTION, assetVocabulary.getDescriptionMap());
124                    document.addText(Field.NAME, assetVocabulary.getName());
125                    document.addLocalizedText(Field.TITLE, assetVocabulary.getTitleMap());
126    
127                    if (_log.isDebugEnabled()) {
128                            _log.debug("Document " + assetVocabulary + " indexed successfully");
129                    }
130    
131                    return document;
132            }
133    
134            @Override
135            protected Summary doGetSummary(
136                    Document document, Locale locale, String snippet,
137                    PortletRequest portletRequest, PortletResponse portletResponse) {
138    
139                    return null;
140            }
141    
142            @Override
143            protected void doReindex(AssetVocabulary assetVocabulary) throws Exception {
144                    Document document = getDocument(assetVocabulary);
145    
146                    if (document != null) {
147                            SearchEngineUtil.updateDocument(
148                                    getSearchEngineId(), assetVocabulary.getCompanyId(), document,
149                                    isCommitImmediately());
150                    }
151            }
152    
153            @Override
154            protected void doReindex(String className, long classPK) throws Exception {
155                    AssetVocabulary vocabulary =
156                            AssetVocabularyLocalServiceUtil.getVocabulary(classPK);
157    
158                    doReindex(vocabulary);
159            }
160    
161            @Override
162            protected void doReindex(String[] ids) throws Exception {
163                    long companyId = GetterUtil.getLong(ids[0]);
164    
165                    reindexVocabularies(companyId);
166            }
167    
168            protected void reindexVocabularies(final long companyId)
169                    throws PortalException {
170    
171                    final ActionableDynamicQuery actionableDynamicQuery =
172                            AssetVocabularyLocalServiceUtil.getActionableDynamicQuery();
173    
174                    actionableDynamicQuery.setCompanyId(companyId);
175                    actionableDynamicQuery.setPerformActionMethod(
176                            new ActionableDynamicQuery.PerformActionMethod<AssetVocabulary>() {
177    
178                                    @Override
179                                    public void performAction(AssetVocabulary assetVocabulary) {
180                                            try {
181                                                    Document document = getDocument(assetVocabulary);
182    
183                                                    if (document != null) {
184                                                            actionableDynamicQuery.addDocument(document);
185                                                    }
186                                            }
187                                            catch (PortalException pe) {
188                                                    if (_log.isWarnEnabled()) {
189                                                            _log.warn(
190                                                                    "Unable to index asset vocabulary " +
191                                                                            assetVocabulary.getVocabularyId(),
192                                                                    pe);
193                                                    }
194                                            }
195                                    }
196    
197                            });
198                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
199    
200                    actionableDynamicQuery.performActions();
201            }
202    
203            private static final Log _log = LogFactoryUtil.getLog(
204                    AssetVocabularyIndexer.class);
205    
206    }