001
014
015 package com.liferay.portlet.assetpublisher.util;
016
017 import com.liferay.portal.kernel.search.BaseIndexer;
018 import com.liferay.portal.kernel.search.BooleanClauseOccur;
019 import com.liferay.portal.kernel.search.BooleanQuery;
020 import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
021 import com.liferay.portal.kernel.search.Document;
022 import com.liferay.portal.kernel.search.DocumentImpl;
023 import com.liferay.portal.kernel.search.Field;
024 import com.liferay.portal.kernel.search.Indexer;
025 import com.liferay.portal.kernel.search.IndexerPostProcessor;
026 import com.liferay.portal.kernel.search.SearchContext;
027 import com.liferay.portal.kernel.search.Summary;
028 import com.liferay.portal.kernel.util.Validator;
029 import com.liferay.portal.util.PortalUtil;
030 import com.liferay.portal.util.PropsValues;
031 import com.liferay.portlet.asset.model.AssetCategory;
032 import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
033 import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
034
035 import java.util.ArrayList;
036 import java.util.List;
037 import java.util.Locale;
038
039 import javax.portlet.PortletURL;
040
041
044 public class AssetSearcher extends BaseIndexer {
045
046 public static Indexer getInstance() {
047 return new AssetSearcher();
048 }
049
050 public AssetSearcher() {
051 setFilterSearch(true);
052 setPermissionAware(true);
053 }
054
055 public String[] getClassNames() {
056 long[] classNameIds = _assetEntryQuery.getClassNameIds();
057
058 String[] classNames = new String[classNameIds.length];
059
060 for (int i = 0; i < classNames.length; i++) {
061 long classNameId = classNameIds[i];
062
063 classNames[i] = PortalUtil.getClassName(classNameId);
064 }
065
066 return classNames;
067 }
068
069 @Override
070 public IndexerPostProcessor[] getIndexerPostProcessors() {
071 throw new UnsupportedOperationException();
072 }
073
074 public String getPortletId() {
075 return null;
076 }
077
078 @Override
079 public void registerIndexerPostProcessor(
080 IndexerPostProcessor indexerPostProcessor) {
081
082 throw new UnsupportedOperationException();
083 }
084
085 public void setAssetEntryQuery(AssetEntryQuery assetEntryQuery) {
086 _assetEntryQuery = assetEntryQuery;
087 }
088
089 protected void addSearchAllCategories(
090 BooleanQuery contextQuery, SearchContext searchContext)
091 throws Exception {
092
093 long[] allCategoryIds = _assetEntryQuery.getAllCategoryIds();
094
095 if (allCategoryIds.length == 0) {
096 return;
097 }
098
099 BooleanQuery categoryIdsQuery = BooleanQueryFactoryUtil.create(
100 searchContext);
101
102 for (long allCategoryId : allCategoryIds) {
103 AssetCategory assetCategory =
104 AssetCategoryLocalServiceUtil.fetchAssetCategory(allCategoryId);
105
106 if (assetCategory == null) {
107 continue;
108 }
109
110 List<Long> categoryIds = new ArrayList<Long>();
111
112 if (PropsValues.ASSET_CATEGORIES_SEARCH_HIERARCHICAL) {
113 categoryIds = AssetCategoryLocalServiceUtil.getSubcategoryIds(
114 allCategoryId);
115 }
116
117 if (categoryIds.isEmpty()) {
118 categoryIds.add(allCategoryId);
119 }
120
121 BooleanQuery categoryIdQuery = BooleanQueryFactoryUtil.create(
122 searchContext);
123
124 for (long categoryId : categoryIds) {
125 categoryIdQuery.addTerm(Field.ASSET_CATEGORY_IDS, categoryId);
126 }
127
128 categoryIdsQuery.add(categoryIdQuery, BooleanClauseOccur.MUST);
129 }
130
131 contextQuery.add(categoryIdsQuery, BooleanClauseOccur.MUST);
132 }
133
134 protected void addSearchAllTags(
135 BooleanQuery contextQuery, SearchContext searchContext)
136 throws Exception {
137
138 long[] allTagIds = _assetEntryQuery.getAllTagIds();
139
140 if (allTagIds.length == 0) {
141 return;
142 }
143
144 BooleanQuery tagIdsQuery = BooleanQueryFactoryUtil.create(
145 searchContext);
146
147 for (long tagId : allTagIds) {
148 tagIdsQuery.addRequiredTerm(Field.ASSET_TAG_IDS, tagId);
149 }
150
151 contextQuery.add(tagIdsQuery, BooleanClauseOccur.MUST);
152 }
153
154 protected void addSearchAnyCategories(
155 BooleanQuery contextQuery, SearchContext searchContext)
156 throws Exception {
157
158 long[] anyCategoryIds = _assetEntryQuery.getAnyCategoryIds();
159
160 if (anyCategoryIds.length == 0) {
161 return;
162 }
163
164 BooleanQuery categoryIdsQuery = BooleanQueryFactoryUtil.create(
165 searchContext);
166
167 for (long anyCategoryId : anyCategoryIds) {
168 AssetCategory assetCategory =
169 AssetCategoryLocalServiceUtil.fetchAssetCategory(anyCategoryId);
170
171 if (assetCategory == null) {
172 continue;
173 }
174
175 List<Long> categoryIds = new ArrayList<Long>();
176
177 if (PropsValues.ASSET_CATEGORIES_SEARCH_HIERARCHICAL) {
178 categoryIds = AssetCategoryLocalServiceUtil.getSubcategoryIds(
179 anyCategoryId);
180 }
181
182 if (categoryIds.isEmpty()) {
183 categoryIds.add(anyCategoryId);
184 }
185
186 for (long categoryId : categoryIds) {
187 categoryIdsQuery.addTerm(Field.ASSET_CATEGORY_IDS, categoryId);
188 }
189 }
190
191 contextQuery.add(categoryIdsQuery, BooleanClauseOccur.MUST);
192 }
193
194 protected void addSearchAnyTags(
195 BooleanQuery contextQuery, SearchContext searchContext)
196 throws Exception {
197
198 long[] anyTagIds = _assetEntryQuery.getAnyTagIds();
199
200 if (anyTagIds.length == 0) {
201 return;
202 }
203
204 BooleanQuery tagIdsQuery = BooleanQueryFactoryUtil.create(
205 searchContext);
206
207 for (long tagId : anyTagIds) {
208 tagIdsQuery.addTerm(Field.ASSET_TAG_IDS, tagId);
209 }
210
211 contextQuery.add(tagIdsQuery, BooleanClauseOccur.MUST);
212 }
213
214 @Override
215 protected void addSearchAssetCategoryIds(
216 BooleanQuery contextQuery, SearchContext searchContext)
217 throws Exception {
218
219 addSearchAllCategories(contextQuery, searchContext);
220 addSearchAnyCategories(contextQuery, searchContext);
221 addSearchNotAnyCategories(contextQuery, searchContext);
222 addSearchNotAllCategories(contextQuery, searchContext);
223 }
224
225 @Override
226 protected void addSearchAssetTagNames(
227 BooleanQuery contextQuery, SearchContext searchContext)
228 throws Exception {
229
230 addSearchAllTags(contextQuery, searchContext);
231 addSearchAnyTags(contextQuery, searchContext);
232 addSearchNotAllTags(contextQuery, searchContext);
233 addSearchNotAnyTags(contextQuery, searchContext);
234 }
235
236 @Override
237 protected void addSearchKeywords(
238 BooleanQuery searchQuery, SearchContext searchContext)
239 throws Exception {
240
241 String keywords = searchContext.getKeywords();
242
243 if (Validator.isNull(keywords)) {
244 return;
245 }
246
247 super.addSearchKeywords(searchQuery, searchContext);
248
249 String field = DocumentImpl.getLocalizedName(
250 searchContext.getLocale(), "localized_title");
251
252 searchQuery.addTerm(field, keywords, true);
253 }
254
255 @Override
256 protected void addSearchLayout(
257 BooleanQuery contextQuery, SearchContext searchContext)
258 throws Exception {
259
260 String layoutUuid = (String)searchContext.getAttribute(
261 Field.LAYOUT_UUID);
262
263 if (Validator.isNotNull(layoutUuid)) {
264 contextQuery.addRequiredTerm(Field.LAYOUT_UUID, layoutUuid);
265 }
266 }
267
268 protected void addSearchNotAllCategories(
269 BooleanQuery contextQuery, SearchContext searchContext)
270 throws Exception {
271
272 long[] notAllCategoryIds = _assetEntryQuery.getNotAllCategoryIds();
273
274 if (notAllCategoryIds.length == 0) {
275 return;
276 }
277
278 BooleanQuery categoryIdsQuery = BooleanQueryFactoryUtil.create(
279 searchContext);
280
281 for (long notAllCategoryId : notAllCategoryIds) {
282 AssetCategory assetCategory =
283 AssetCategoryLocalServiceUtil.fetchAssetCategory(
284 notAllCategoryId);
285
286 if (assetCategory == null) {
287 continue;
288 }
289
290 List<Long> categoryIds = new ArrayList<Long>();
291
292 if (PropsValues.ASSET_CATEGORIES_SEARCH_HIERARCHICAL) {
293 categoryIds = AssetCategoryLocalServiceUtil.getSubcategoryIds(
294 notAllCategoryId);
295 }
296
297 if (categoryIds.isEmpty()) {
298 categoryIds.add(notAllCategoryId);
299 }
300
301 BooleanQuery categoryIdQuery = BooleanQueryFactoryUtil.create(
302 searchContext);
303
304 for (long categoryId : categoryIds) {
305 categoryIdQuery.addTerm(Field.ASSET_CATEGORY_IDS, categoryId);
306 }
307
308 categoryIdsQuery.add(categoryIdQuery, BooleanClauseOccur.MUST);
309 }
310
311 contextQuery.add(categoryIdsQuery, BooleanClauseOccur.MUST_NOT);
312 }
313
314 protected void addSearchNotAllTags(
315 BooleanQuery contextQuery, SearchContext searchContext)
316 throws Exception {
317
318 long[] notAllTagIds = _assetEntryQuery.getNotAllTagIds();
319
320 if (notAllTagIds.length == 0) {
321 return;
322 }
323
324 BooleanQuery tagIdsQuery = BooleanQueryFactoryUtil.create(
325 searchContext);
326
327 for (long tagId : notAllTagIds) {
328 tagIdsQuery.addRequiredTerm(Field.ASSET_TAG_IDS, tagId);
329 }
330
331 contextQuery.add(tagIdsQuery, BooleanClauseOccur.MUST_NOT);
332 }
333
334 protected void addSearchNotAnyCategories(
335 BooleanQuery contextQuery, SearchContext searchContext)
336 throws Exception {
337
338 long[] notAnyCategoryIds = _assetEntryQuery.getNotAnyCategoryIds();
339
340 if (notAnyCategoryIds.length == 0) {
341 return;
342 }
343
344 BooleanQuery categoryIdsQuery = BooleanQueryFactoryUtil.create(
345 searchContext);
346
347 for (long notAnyCategoryId : notAnyCategoryIds) {
348 AssetCategory assetCategory =
349 AssetCategoryLocalServiceUtil.fetchAssetCategory(
350 notAnyCategoryId);
351
352 if (assetCategory == null) {
353 continue;
354 }
355
356 List<Long> categoryIds = new ArrayList<Long>();
357
358 if (PropsValues.ASSET_CATEGORIES_SEARCH_HIERARCHICAL) {
359 categoryIds = AssetCategoryLocalServiceUtil.getSubcategoryIds(
360 notAnyCategoryId);
361 }
362
363 if (categoryIds.isEmpty()) {
364 categoryIds.add(notAnyCategoryId);
365 }
366
367 for (long categoryId : categoryIds) {
368 categoryIdsQuery.addTerm(Field.ASSET_CATEGORY_IDS, categoryId);
369 }
370 }
371
372 contextQuery.add(categoryIdsQuery, BooleanClauseOccur.MUST_NOT);
373 }
374
375 protected void addSearchNotAnyTags(
376 BooleanQuery contextQuery, SearchContext searchContext)
377 throws Exception {
378
379 long[] notAnyTagIds = _assetEntryQuery.getNotAnyTagIds();
380
381 if (notAnyTagIds.length == 0) {
382 return;
383 }
384
385 BooleanQuery tagIdsQuery = BooleanQueryFactoryUtil.create(
386 searchContext);
387
388 for (long tagId : _assetEntryQuery.getNotAnyTagIds()) {
389 tagIdsQuery.addTerm(Field.ASSET_TAG_IDS, tagId);
390 }
391
392 contextQuery.add(tagIdsQuery, BooleanClauseOccur.MUST_NOT);
393 }
394
395 @Override
396 protected void doDelete(Object obj) throws Exception {
397 throw new UnsupportedOperationException();
398 }
399
400 @Override
401 protected Document doGetDocument(Object obj) throws Exception {
402 throw new UnsupportedOperationException();
403 }
404
405 @Override
406 protected Summary doGetSummary(
407 Document document, Locale locale, String snippet,
408 PortletURL portletURL)
409 throws Exception {
410
411 throw new UnsupportedOperationException();
412 }
413
414 @Override
415 protected void doReindex(Object obj) throws Exception {
416 throw new UnsupportedOperationException();
417 }
418
419 @Override
420 protected void doReindex(String className, long classPK) throws Exception {
421 throw new UnsupportedOperationException();
422 }
423
424 @Override
425 protected void doReindex(String[] ids) throws Exception {
426 throw new UnsupportedOperationException();
427 }
428
429 @Override
430 protected String getPortletId(SearchContext searchContext) {
431 return null;
432 }
433
434 private AssetEntryQuery _assetEntryQuery;
435
436 }