001    /**
002     * Copyright (c) 2000-2013 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.exception.SystemException;
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.BooleanQueryFactoryUtil;
026    import com.liferay.portal.kernel.search.Document;
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.PortalUtil;
036    import com.liferay.portal.util.PortletKeys;
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    import com.liferay.portlet.asset.service.persistence.AssetVocabularyActionableDynamicQuery;
041    
042    import java.util.ArrayList;
043    import java.util.Collection;
044    import java.util.Locale;
045    
046    import javax.portlet.PortletURL;
047    
048    /**
049     * @author Istvan Andras Dezsi
050     */
051    public class AssetVocabularyIndexer extends BaseIndexer {
052    
053            public static final String[] CLASS_NAMES =
054                    {AssetVocabulary.class.getName()};
055    
056            public static final String PORTLET_ID = PortletKeys.ASSET_CATEGORIES_ADMIN;
057    
058            public AssetVocabularyIndexer() {
059                    setCommitImmediately(true);
060            }
061    
062            public String[] getClassNames() {
063                    return CLASS_NAMES;
064            }
065    
066            public String getPortletId() {
067                    return PORTLET_ID;
068            }
069    
070            @Override
071            public boolean hasPermission(
072                            PermissionChecker permissionChecker, String entryClassName,
073                            long entryClassPK, String actionId)
074                    throws Exception {
075    
076                    AssetVocabulary vocabulary =
077                            AssetVocabularyLocalServiceUtil.getVocabulary(entryClassPK);
078    
079                    return AssetVocabularyPermission.contains(
080                            permissionChecker, vocabulary, ActionKeys.VIEW);
081            }
082    
083            @Override
084            public boolean isFilterSearch() {
085                    return _FILTER_SEARCH;
086            }
087    
088            @Override
089            public boolean isPermissionAware() {
090                    return _PERMISSION_AWARE;
091            }
092    
093            @Override
094            public void postProcessSearchQuery(
095                            BooleanQuery searchQuery, SearchContext searchContext)
096                    throws Exception {
097    
098                    String title = (String)searchContext.getAttribute(Field.TITLE);
099    
100                    if (Validator.isNotNull(title)) {
101                            BooleanQuery localizedQuery = BooleanQueryFactoryUtil.create(
102                                    searchContext);
103    
104                            addSearchLocalizedTerm(
105                                    localizedQuery, searchContext, Field.TITLE, true);
106    
107                            searchQuery.add(localizedQuery, BooleanClauseOccur.SHOULD);
108                    }
109            }
110    
111            @Override
112            protected void doDelete(Object obj) throws Exception {
113                    AssetVocabulary vocabulary = (AssetVocabulary)obj;
114    
115                    deleteDocument(vocabulary.getCompanyId(), vocabulary.getVocabularyId());
116            }
117    
118            @Override
119            protected Document doGetDocument(Object obj) throws Exception {
120                    AssetVocabulary vocabulary = (AssetVocabulary)obj;
121    
122                    if (_log.isDebugEnabled()) {
123                            _log.debug("Indexing vocabulary " + vocabulary);
124                    }
125    
126                    Document document = getBaseModelDocument(PORTLET_ID, vocabulary);
127    
128                    document.addKeyword(
129                            Field.ASSET_VOCABULARY_ID, vocabulary.getVocabularyId());
130    
131                    Locale siteDefaultLocale = PortalUtil.getSiteDefaultLocale(
132                            vocabulary.getGroupId());
133    
134                    addLocalizedField(
135                            document, Field.DESCRIPTION, siteDefaultLocale,
136                            vocabulary.getDescriptionMap());
137    
138                    document.addText(Field.NAME, vocabulary.getName());
139                    addLocalizedField(
140                            document, Field.TITLE, siteDefaultLocale, vocabulary.getTitleMap());
141    
142                    if (_log.isDebugEnabled()) {
143                            _log.debug("Document " + vocabulary + " indexed successfully");
144                    }
145    
146                    return document;
147            }
148    
149            @Override
150            protected Summary doGetSummary(
151                    Document document, Locale locale, String snippet,
152                    PortletURL portletURL) {
153    
154                    return null;
155            }
156    
157            @Override
158            protected void doReindex(Object obj) throws Exception {
159                    AssetVocabulary vocabulary = (AssetVocabulary)obj;
160    
161                    Document document = getDocument(vocabulary);
162    
163                    if (document != null) {
164                            SearchEngineUtil.updateDocument(
165                                    getSearchEngineId(), vocabulary.getCompanyId(), document,
166                                    isCommitImmediately());
167                    }
168            }
169    
170            @Override
171            protected void doReindex(String className, long classPK) throws Exception {
172                    AssetVocabulary vocabulary =
173                            AssetVocabularyLocalServiceUtil.getVocabulary(classPK);
174    
175                    doReindex(vocabulary);
176            }
177    
178            @Override
179            protected void doReindex(String[] ids) throws Exception {
180                    long companyId = GetterUtil.getLong(ids[0]);
181    
182                    reindexVocabularies(companyId);
183            }
184    
185            @Override
186            protected String getPortletId(SearchContext searchContext) {
187                    return PORTLET_ID;
188            }
189    
190            protected void reindexVocabularies(final long companyId)
191                    throws PortalException, SystemException {
192    
193                    final Collection<Document> documents = new ArrayList<Document>();
194    
195                    ActionableDynamicQuery actionableDynamicQuery =
196                            new AssetVocabularyActionableDynamicQuery() {
197    
198                            @Override
199                            protected void performAction(Object object) {
200                                    AssetVocabulary vocabulary = (AssetVocabulary)object;
201    
202                                    try {
203                                            Document document = getDocument(vocabulary);
204    
205                                            if (document != null) {
206                                                    documents.add(document);
207                                            }
208                                    }
209                                    catch (PortalException pe) {
210                                            if (_log.isWarnEnabled()) {
211                                                    _log.warn(
212                                                            "Unable to index asset vocabulary " +
213                                                                    vocabulary.getVocabularyId(),
214                                                            pe);
215                                            }
216                                    }
217                            }
218    
219                    };
220    
221                    actionableDynamicQuery.setCompanyId(companyId);
222    
223                    actionableDynamicQuery.performActions();
224    
225                    SearchEngineUtil.updateDocuments(
226                            getSearchEngineId(), companyId, documents);
227            }
228    
229            private static final boolean _FILTER_SEARCH = true;
230    
231            private static final boolean _PERMISSION_AWARE = true;
232    
233            private static Log _log = LogFactoryUtil.getLog(
234                    AssetVocabularyIndexer.class);
235    
236    }