001
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.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.search.filter.BooleanFilter;
032 import com.liferay.portal.kernel.search.generic.BooleanQueryImpl;
033 import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
034 import com.liferay.portal.kernel.util.GetterUtil;
035 import com.liferay.portal.kernel.util.Validator;
036 import com.liferay.portal.security.permission.ActionKeys;
037 import com.liferay.portal.security.permission.PermissionChecker;
038 import com.liferay.portlet.asset.model.AssetVocabulary;
039 import com.liferay.portlet.asset.service.AssetVocabularyLocalServiceUtil;
040 import com.liferay.portlet.asset.service.permission.AssetVocabularyPermission;
041
042 import java.util.Locale;
043
044 import javax.portlet.PortletRequest;
045 import javax.portlet.PortletResponse;
046
047
050 @OSGiBeanProperties
051 public class AssetVocabularyIndexer extends BaseIndexer<AssetVocabulary> {
052
053 public static final String CLASS_NAME = AssetVocabulary.class.getName();
054
055 public AssetVocabularyIndexer() {
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(AssetVocabulary assetVocabulary) throws Exception {
101 Document document = new DocumentImpl();
102
103 document.addUID(CLASS_NAME, assetVocabulary.getVocabularyId());
104
105 SearchEngineUtil.deleteDocument(
106 getSearchEngineId(), assetVocabulary.getCompanyId(),
107 document.get(Field.UID), isCommitImmediately());
108 }
109
110 @Override
111 protected Document doGetDocument(AssetVocabulary assetVocabulary)
112 throws Exception {
113
114 if (_log.isDebugEnabled()) {
115 _log.debug("Indexing asset vocabulary " + assetVocabulary);
116 }
117
118 Document document = getBaseModelDocument(CLASS_NAME, assetVocabulary);
119
120 document.addKeyword(
121 Field.ASSET_VOCABULARY_ID, assetVocabulary.getVocabularyId());
122 document.addLocalizedText(
123 Field.DESCRIPTION, assetVocabulary.getDescriptionMap());
124 document.addText(Field.NAME, assetVocabulary.getName());
125 document.addLocalizedText(Field.TITLE, assetVocabulary.getTitleMap());
126
127 if (_log.isDebugEnabled()) {
128 _log.debug("Document " + assetVocabulary + " indexed successfully");
129 }
130
131 return document;
132 }
133
134 @Override
135 protected Summary doGetSummary(
136 Document document, Locale locale, String snippet,
137 PortletRequest portletRequest, PortletResponse portletResponse) {
138
139 return null;
140 }
141
142 @Override
143 protected void doReindex(AssetVocabulary assetVocabulary) throws Exception {
144 Document document = getDocument(assetVocabulary);
145
146 if (document != null) {
147 SearchEngineUtil.updateDocument(
148 getSearchEngineId(), assetVocabulary.getCompanyId(), document,
149 isCommitImmediately());
150 }
151 }
152
153 @Override
154 protected void doReindex(String className, long classPK) throws Exception {
155 AssetVocabulary vocabulary =
156 AssetVocabularyLocalServiceUtil.getVocabulary(classPK);
157
158 doReindex(vocabulary);
159 }
160
161 @Override
162 protected void doReindex(String[] ids) throws Exception {
163 long companyId = GetterUtil.getLong(ids[0]);
164
165 reindexVocabularies(companyId);
166 }
167
168 protected void reindexVocabularies(final long companyId)
169 throws PortalException {
170
171 final IndexableActionableDynamicQuery indexableActionableDynamicQuery =
172 AssetVocabularyLocalServiceUtil.
173 getIndexableActionableDynamicQuery();
174
175 indexableActionableDynamicQuery.setCompanyId(companyId);
176 indexableActionableDynamicQuery.setPerformActionMethod(
177 new ActionableDynamicQuery.PerformActionMethod<AssetVocabulary>() {
178
179 @Override
180 public void performAction(AssetVocabulary assetVocabulary) {
181 try {
182 Document document = getDocument(assetVocabulary);
183
184 if (document != null) {
185 indexableActionableDynamicQuery.addDocuments(
186 document);
187 }
188 }
189 catch (PortalException pe) {
190 if (_log.isWarnEnabled()) {
191 _log.warn(
192 "Unable to index asset vocabulary " +
193 assetVocabulary.getVocabularyId(),
194 pe);
195 }
196 }
197 }
198
199 });
200 indexableActionableDynamicQuery.setSearchEngineId(getSearchEngineId());
201
202 indexableActionableDynamicQuery.performActions();
203 }
204
205 private static final Log _log = LogFactoryUtil.getLog(
206 AssetVocabularyIndexer.class);
207
208 }