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