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 {
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(Object obj) throws Exception {
101                    AssetVocabulary vocabulary = (AssetVocabulary)obj;
102    
103                    Document document = new DocumentImpl();
104    
105                    document.addUID(CLASS_NAME, vocabulary.getVocabularyId());
106    
107                    SearchEngineUtil.deleteDocument(
108                            getSearchEngineId(), vocabulary.getCompanyId(),
109                            document.get(Field.UID), isCommitImmediately());
110            }
111    
112            @Override
113            protected Document doGetDocument(Object obj) throws Exception {
114                    AssetVocabulary vocabulary = (AssetVocabulary)obj;
115    
116                    if (_log.isDebugEnabled()) {
117                            _log.debug("Indexing vocabulary " + vocabulary);
118                    }
119    
120                    Document document = getBaseModelDocument(CLASS_NAME, vocabulary);
121    
122                    document.addKeyword(
123                            Field.ASSET_VOCABULARY_ID, vocabulary.getVocabularyId());
124                    document.addLocalizedText(
125                            Field.DESCRIPTION, vocabulary.getDescriptionMap());
126                    document.addText(Field.NAME, vocabulary.getName());
127                    document.addLocalizedText(Field.TITLE, vocabulary.getTitleMap());
128    
129                    if (_log.isDebugEnabled()) {
130                            _log.debug("Document " + vocabulary + " indexed successfully");
131                    }
132    
133                    return document;
134            }
135    
136            @Override
137            protected Summary doGetSummary(
138                    Document document, Locale locale, String snippet,
139                    PortletRequest portletRequest, PortletResponse portletResponse) {
140    
141                    return null;
142            }
143    
144            @Override
145            protected void doReindex(Object obj) throws Exception {
146                    AssetVocabulary vocabulary = (AssetVocabulary)obj;
147    
148                    Document document = getDocument(vocabulary);
149    
150                    if (document != null) {
151                            SearchEngineUtil.updateDocument(
152                                    getSearchEngineId(), vocabulary.getCompanyId(), document,
153                                    isCommitImmediately());
154                    }
155            }
156    
157            @Override
158            protected void doReindex(String className, long classPK) throws Exception {
159                    AssetVocabulary vocabulary =
160                            AssetVocabularyLocalServiceUtil.getVocabulary(classPK);
161    
162                    doReindex(vocabulary);
163            }
164    
165            @Override
166            protected void doReindex(String[] ids) throws Exception {
167                    long companyId = GetterUtil.getLong(ids[0]);
168    
169                    reindexVocabularies(companyId);
170            }
171    
172            protected void reindexVocabularies(final long companyId)
173                    throws PortalException {
174    
175                    final ActionableDynamicQuery actionableDynamicQuery =
176                            AssetVocabularyLocalServiceUtil.getActionableDynamicQuery();
177    
178                    actionableDynamicQuery.setCompanyId(companyId);
179                    actionableDynamicQuery.setPerformActionMethod(
180                            new ActionableDynamicQuery.PerformActionMethod() {
181    
182                                    @Override
183                                    public void performAction(Object object) {
184                                            AssetVocabulary assetVocabulary = (AssetVocabulary)object;
185    
186                                            try {
187                                                    Document document = getDocument(assetVocabulary);
188    
189                                                    if (document != null) {
190                                                            actionableDynamicQuery.addDocument(document);
191                                                    }
192                                            }
193                                            catch (PortalException pe) {
194                                                    if (_log.isWarnEnabled()) {
195                                                            _log.warn(
196                                                                    "Unable to index asset vocabulary " +
197                                                                            assetVocabulary.getVocabularyId(),
198                                                                    pe);
199                                                    }
200                                            }
201                                    }
202    
203                            });
204                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
205    
206                    actionableDynamicQuery.performActions();
207            }
208    
209            private static final Log _log = LogFactoryUtil.getLog(
210                    AssetVocabularyIndexer.class);
211    
212    }