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.dao.orm.IndexableActionableDynamicQuery;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.search.BaseIndexer;
023    import com.liferay.portal.kernel.search.BooleanClauseOccur;
024    import com.liferay.portal.kernel.search.BooleanQuery;
025    import com.liferay.portal.kernel.search.Document;
026    import com.liferay.portal.kernel.search.Field;
027    import com.liferay.portal.kernel.search.IndexWriterHelperUtil;
028    import com.liferay.portal.kernel.search.SearchContext;
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                    setDefaultSelectedFieldNames(
056                            Field.ASSET_VOCABULARY_ID, Field.COMPANY_ID, Field.GROUP_ID,
057                            Field.UID);
058                    setFilterSearch(true);
059                    setPermissionAware(true);
060            }
061    
062            @Override
063            public String getClassName() {
064                    return CLASS_NAME;
065            }
066    
067            @Override
068            public boolean hasPermission(
069                            PermissionChecker permissionChecker, String entryClassName,
070                            long entryClassPK, String actionId)
071                    throws Exception {
072    
073                    AssetVocabulary vocabulary =
074                            AssetVocabularyLocalServiceUtil.getVocabulary(entryClassPK);
075    
076                    return AssetVocabularyPermission.contains(
077                            permissionChecker, vocabulary, ActionKeys.VIEW);
078            }
079    
080            @Override
081            public void postProcessSearchQuery(
082                            BooleanQuery searchQuery, BooleanFilter fullQueryBooleanFilter,
083                            SearchContext searchContext)
084                    throws Exception {
085    
086                    String title = (String)searchContext.getAttribute(Field.TITLE);
087    
088                    if (Validator.isNotNull(title)) {
089                            BooleanQuery localizedQuery = new BooleanQueryImpl();
090    
091                            addSearchLocalizedTerm(
092                                    localizedQuery, searchContext, Field.TITLE, true);
093    
094                            searchQuery.add(localizedQuery, BooleanClauseOccur.SHOULD);
095                    }
096            }
097    
098            @Override
099            protected void doDelete(AssetVocabulary assetVocabulary) throws Exception {
100                    deleteDocument(
101                            assetVocabulary.getCompanyId(), assetVocabulary.getVocabularyId());
102            }
103    
104            @Override
105            protected Document doGetDocument(AssetVocabulary assetVocabulary)
106                    throws Exception {
107    
108                    if (_log.isDebugEnabled()) {
109                            _log.debug("Indexing asset vocabulary " + assetVocabulary);
110                    }
111    
112                    Document document = getBaseModelDocument(CLASS_NAME, assetVocabulary);
113    
114                    document.addKeyword(
115                            Field.ASSET_VOCABULARY_ID, assetVocabulary.getVocabularyId());
116                    document.addLocalizedText(
117                            Field.DESCRIPTION, assetVocabulary.getDescriptionMap());
118                    document.addText(Field.NAME, assetVocabulary.getName());
119                    document.addLocalizedText(Field.TITLE, assetVocabulary.getTitleMap());
120    
121                    if (_log.isDebugEnabled()) {
122                            _log.debug("Document " + assetVocabulary + " indexed successfully");
123                    }
124    
125                    return document;
126            }
127    
128            @Override
129            protected Summary doGetSummary(
130                    Document document, Locale locale, String snippet,
131                    PortletRequest portletRequest, PortletResponse portletResponse) {
132    
133                    return null;
134            }
135    
136            @Override
137            protected void doReindex(AssetVocabulary assetVocabulary) throws Exception {
138                    Document document = getDocument(assetVocabulary);
139    
140                    IndexWriterHelperUtil.updateDocument(
141                            getSearchEngineId(), assetVocabulary.getCompanyId(), document,
142                            isCommitImmediately());
143            }
144    
145            @Override
146            protected void doReindex(String className, long classPK) throws Exception {
147                    AssetVocabulary vocabulary =
148                            AssetVocabularyLocalServiceUtil.getVocabulary(classPK);
149    
150                    doReindex(vocabulary);
151            }
152    
153            @Override
154            protected void doReindex(String[] ids) throws Exception {
155                    long companyId = GetterUtil.getLong(ids[0]);
156    
157                    reindexVocabularies(companyId);
158            }
159    
160            protected void reindexVocabularies(final long companyId)
161                    throws PortalException {
162    
163                    final IndexableActionableDynamicQuery indexableActionableDynamicQuery =
164                            AssetVocabularyLocalServiceUtil.
165                                    getIndexableActionableDynamicQuery();
166    
167                    indexableActionableDynamicQuery.setCompanyId(companyId);
168                    indexableActionableDynamicQuery.setPerformActionMethod(
169                            new ActionableDynamicQuery.PerformActionMethod<AssetVocabulary>() {
170    
171                                    @Override
172                                    public void performAction(AssetVocabulary assetVocabulary) {
173                                            try {
174                                                    Document document = getDocument(assetVocabulary);
175    
176                                                    indexableActionableDynamicQuery.addDocuments(document);
177                                            }
178                                            catch (PortalException pe) {
179                                                    if (_log.isWarnEnabled()) {
180                                                            _log.warn(
181                                                                    "Unable to index asset vocabulary " +
182                                                                            assetVocabulary.getVocabularyId(),
183                                                                    pe);
184                                                    }
185                                            }
186                                    }
187    
188                            });
189                    indexableActionableDynamicQuery.setSearchEngineId(getSearchEngineId());
190    
191                    indexableActionableDynamicQuery.performActions();
192            }
193    
194            private static final Log _log = LogFactoryUtil.getLog(
195                    AssetVocabularyIndexer.class);
196    
197    }