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