001
014
015 package com.liferay.portlet.asset.util;
016
017 import com.liferay.asset.kernel.AssetRendererFactoryRegistryUtil;
018 import com.liferay.asset.kernel.model.AssetCategory;
019 import com.liferay.asset.kernel.model.AssetCategoryProperty;
020 import com.liferay.asset.kernel.model.AssetEntry;
021 import com.liferay.asset.kernel.model.AssetRendererFactory;
022 import com.liferay.asset.kernel.model.AssetTag;
023 import com.liferay.asset.kernel.model.AssetVocabulary;
024 import com.liferay.asset.kernel.model.ClassType;
025 import com.liferay.asset.kernel.model.ClassTypeReader;
026 import com.liferay.asset.kernel.service.AssetCategoryLocalServiceUtil;
027 import com.liferay.asset.kernel.service.AssetCategoryPropertyLocalServiceUtil;
028 import com.liferay.asset.kernel.service.AssetEntryLocalServiceUtil;
029 import com.liferay.asset.kernel.service.AssetTagLocalServiceUtil;
030 import com.liferay.asset.kernel.service.persistence.AssetEntryQuery;
031 import com.liferay.dynamic.data.mapping.kernel.DDMStructure;
032 import com.liferay.dynamic.data.mapping.kernel.DDMStructureManager;
033 import com.liferay.dynamic.data.mapping.kernel.DDMStructureManagerUtil;
034 import com.liferay.portal.kernel.exception.PortalException;
035 import com.liferay.portal.kernel.log.Log;
036 import com.liferay.portal.kernel.log.LogFactoryUtil;
037 import com.liferay.portal.kernel.model.Group;
038 import com.liferay.portal.kernel.model.Layout;
039 import com.liferay.portal.kernel.model.LayoutTypePortletConstants;
040 import com.liferay.portal.kernel.model.Portlet;
041 import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
042 import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
043 import com.liferay.portal.kernel.portlet.LiferayWindowState;
044 import com.liferay.portal.kernel.search.BaseModelSearchResult;
045 import com.liferay.portal.kernel.search.Document;
046 import com.liferay.portal.kernel.search.DocumentImpl;
047 import com.liferay.portal.kernel.search.Field;
048 import com.liferay.portal.kernel.search.Hits;
049 import com.liferay.portal.kernel.search.Indexer;
050 import com.liferay.portal.kernel.search.SearchContext;
051 import com.liferay.portal.kernel.search.SearchContextFactory;
052 import com.liferay.portal.kernel.search.Sort;
053 import com.liferay.portal.kernel.search.SortFactoryUtil;
054 import com.liferay.portal.kernel.security.permission.ActionKeys;
055 import com.liferay.portal.kernel.security.permission.PermissionChecker;
056 import com.liferay.portal.kernel.security.permission.comparator.ModelResourceComparator;
057 import com.liferay.portal.kernel.service.GroupLocalServiceUtil;
058 import com.liferay.portal.kernel.service.PortletLocalServiceUtil;
059 import com.liferay.portal.kernel.theme.PortletDisplay;
060 import com.liferay.portal.kernel.theme.ThemeDisplay;
061 import com.liferay.portal.kernel.util.ArrayUtil;
062 import com.liferay.portal.kernel.util.CharPool;
063 import com.liferay.portal.kernel.util.GetterUtil;
064 import com.liferay.portal.kernel.util.HttpUtil;
065 import com.liferay.portal.kernel.util.ListUtil;
066 import com.liferay.portal.kernel.util.LocaleUtil;
067 import com.liferay.portal.kernel.util.ParamUtil;
068 import com.liferay.portal.kernel.util.PortalUtil;
069 import com.liferay.portal.kernel.util.PredicateFilter;
070 import com.liferay.portal.kernel.util.StringPool;
071 import com.liferay.portal.kernel.util.StringUtil;
072 import com.liferay.portal.kernel.util.Validator;
073 import com.liferay.portal.kernel.util.WebKeys;
074 import com.liferay.portlet.asset.service.permission.AssetCategoryPermission;
075 import com.liferay.portlet.asset.service.permission.AssetVocabularyPermission;
076
077 import java.io.Serializable;
078
079 import java.util.ArrayList;
080 import java.util.Collections;
081 import java.util.HashMap;
082 import java.util.HashSet;
083 import java.util.List;
084 import java.util.Locale;
085 import java.util.Map;
086 import java.util.Set;
087 import java.util.TimeZone;
088 import java.util.TreeMap;
089
090 import javax.portlet.PortletMode;
091 import javax.portlet.PortletURL;
092
093 import javax.servlet.http.HttpServletRequest;
094
095
099 public class AssetUtil {
100
101 public static final int ASSET_ENTRY_ABSTRACT_LENGTH = 200;
102
103 public static final String CLASSNAME_SEPARATOR = "_CLASSNAME_";
104
105 public static final char[] INVALID_CHARACTERS = new char[] {
106 CharPool.AMPERSAND, CharPool.APOSTROPHE, CharPool.AT,
107 CharPool.BACK_SLASH, CharPool.CLOSE_BRACKET, CharPool.CLOSE_CURLY_BRACE,
108 CharPool.COLON, CharPool.COMMA, CharPool.EQUAL, CharPool.GREATER_THAN,
109 CharPool.FORWARD_SLASH, CharPool.LESS_THAN, CharPool.NEW_LINE,
110 CharPool.OPEN_BRACKET, CharPool.OPEN_CURLY_BRACE, CharPool.PERCENT,
111 CharPool.PIPE, CharPool.PLUS, CharPool.POUND, CharPool.PRIME,
112 CharPool.QUESTION, CharPool.QUOTE, CharPool.RETURN, CharPool.SEMICOLON,
113 CharPool.SLASH, CharPool.STAR, CharPool.TILDE
114 };
115
116 public static Set<String> addLayoutTags(
117 HttpServletRequest request, List<AssetTag> tags) {
118
119 Set<String> layoutTags = getLayoutTagNames(request);
120
121 for (AssetTag tag : tags) {
122 layoutTags.add(tag.getName());
123 }
124
125 return layoutTags;
126 }
127
128 public static void addPortletBreadcrumbEntries(
129 long assetCategoryId, HttpServletRequest request,
130 PortletURL portletURL)
131 throws Exception {
132
133 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
134 WebKeys.THEME_DISPLAY);
135
136 AssetCategory assetCategory = AssetCategoryLocalServiceUtil.getCategory(
137 assetCategoryId);
138
139 List<AssetCategory> ancestorCategories = assetCategory.getAncestors();
140
141 Collections.reverse(ancestorCategories);
142
143 for (AssetCategory ancestorCategory : ancestorCategories) {
144 portletURL.setParameter(
145 "categoryId", String.valueOf(ancestorCategory.getCategoryId()));
146
147 PortalUtil.addPortletBreadcrumbEntry(
148 request, ancestorCategory.getTitle(themeDisplay.getLocale()),
149 portletURL.toString());
150 }
151
152 portletURL.setParameter("categoryId", String.valueOf(assetCategoryId));
153
154 PortalUtil.addPortletBreadcrumbEntry(
155 request, assetCategory.getTitle(themeDisplay.getLocale()),
156 portletURL.toString());
157 }
158
159 public static String checkViewURL(
160 AssetEntry assetEntry, boolean viewInContext, String viewURL,
161 String currentURL, ThemeDisplay themeDisplay) {
162
163 if (Validator.isNull(viewURL)) {
164 return viewURL;
165 }
166
167 viewURL = HttpUtil.setParameter(
168 viewURL, "inheritRedirect", viewInContext);
169
170 Layout layout = themeDisplay.getLayout();
171
172 String assetEntryLayoutUuid = assetEntry.getLayoutUuid();
173
174 if (!viewInContext ||
175 (Validator.isNotNull(assetEntryLayoutUuid) &&
176 !assetEntryLayoutUuid.equals(layout.getUuid()))) {
177
178 viewURL = HttpUtil.setParameter(viewURL, "redirect", currentURL);
179 }
180
181 return viewURL;
182 }
183
184 public static long[] filterCategoryIds(
185 PermissionChecker permissionChecker, long[] categoryIds)
186 throws PortalException {
187
188 List<Long> viewableCategoryIds = new ArrayList<>();
189
190 for (long categoryId : categoryIds) {
191 AssetCategory category =
192 AssetCategoryLocalServiceUtil.fetchCategory(categoryId);
193
194 if ((category != null) &&
195 AssetCategoryPermission.contains(
196 permissionChecker, categoryId, ActionKeys.VIEW)) {
197
198 viewableCategoryIds.add(categoryId);
199 }
200 }
201
202 return ArrayUtil.toArray(
203 viewableCategoryIds.toArray(new Long[viewableCategoryIds.size()]));
204 }
205
206 public static List<AssetVocabulary> filterVocabularies(
207 List<AssetVocabulary> vocabularies, String className,
208 final long classTypePK) {
209
210 final long classNameId = PortalUtil.getClassNameId(className);
211
212 PredicateFilter<AssetVocabulary> predicateFilter =
213 new PredicateFilter<AssetVocabulary>() {
214
215 @Override
216 public boolean filter(AssetVocabulary assetVocabulary) {
217 return
218 assetVocabulary.isAssociatedToClassNameIdAndClassTypePK(
219 classNameId, classTypePK);
220 }
221
222 };
223
224 return ListUtil.filter(vocabularies, predicateFilter);
225 }
226
227 public static long[] filterVocabularyIds(
228 PermissionChecker permissionChecker, long[] vocabularyIds)
229 throws PortalException {
230
231 List<Long> viewableVocabularyIds = new ArrayList<>();
232
233 for (long vocabularyId : vocabularyIds) {
234 if (AssetVocabularyPermission.contains(
235 permissionChecker, vocabularyId, ActionKeys.VIEW)) {
236
237 viewableVocabularyIds.add(vocabularyId);
238 }
239 }
240
241 return ArrayUtil.toArray(
242 viewableVocabularyIds.toArray(
243 new Long[viewableVocabularyIds.size()]));
244 }
245
246 public static PortletURL getAddPortletURL(
247 LiferayPortletRequest liferayPortletRequest,
248 LiferayPortletResponse liferayPortletResponse, long groupId,
249 String className, long classTypeId, long[] allAssetCategoryIds,
250 String[] allAssetTagNames, String redirect)
251 throws Exception {
252
253 ThemeDisplay themeDisplay =
254 (ThemeDisplay)liferayPortletRequest.getAttribute(
255 WebKeys.THEME_DISPLAY);
256
257 AssetRendererFactory<?> assetRendererFactory =
258 AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
259 className);
260
261 if ((assetRendererFactory == null) ||
262 !assetRendererFactory.hasAddPermission(
263 themeDisplay.getPermissionChecker(), groupId, classTypeId)) {
264
265 return null;
266 }
267
268 if (groupId > 0) {
269 Group group = GroupLocalServiceUtil.fetchGroup(groupId);
270
271 liferayPortletRequest.setAttribute(
272 WebKeys.ASSET_RENDERER_FACTORY_GROUP, group);
273 }
274
275 PortletURL addPortletURL = assetRendererFactory.getURLAdd(
276 liferayPortletRequest, liferayPortletResponse, classTypeId);
277
278 if (addPortletURL == null) {
279 return null;
280 }
281
282 if (redirect != null) {
283 addPortletURL.setParameter("redirect", redirect);
284 }
285
286 String referringPortletResource = ParamUtil.getString(
287 liferayPortletRequest, "portletResource");
288
289 if (Validator.isNotNull(referringPortletResource)) {
290 addPortletURL.setParameter(
291 "referringPortletResource", referringPortletResource);
292 }
293 else {
294 PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
295
296 addPortletURL.setParameter(
297 "referringPortletResource", portletDisplay.getId());
298
299 if (allAssetCategoryIds != null) {
300 Map<Long, String> assetVocabularyAssetCategoryIds =
301 new HashMap<>();
302
303 for (long assetCategoryId : allAssetCategoryIds) {
304 AssetCategory assetCategory =
305 AssetCategoryLocalServiceUtil.fetchAssetCategory(
306 assetCategoryId);
307
308 if (assetCategory == null) {
309 continue;
310 }
311
312 long assetVocabularyId = assetCategory.getVocabularyId();
313
314 if (assetVocabularyAssetCategoryIds.containsKey(
315 assetVocabularyId)) {
316
317 String assetCategoryIds =
318 assetVocabularyAssetCategoryIds.get(
319 assetVocabularyId);
320
321 assetVocabularyAssetCategoryIds.put(
322 assetVocabularyId,
323 assetCategoryIds + StringPool.COMMA +
324 assetCategoryId);
325 }
326 else {
327 assetVocabularyAssetCategoryIds.put(
328 assetVocabularyId, String.valueOf(assetCategoryId));
329 }
330 }
331
332 for (Map.Entry<Long, String> entry :
333 assetVocabularyAssetCategoryIds.entrySet()) {
334
335 long assetVocabularyId = entry.getKey();
336 String assetCategoryIds = entry.getValue();
337
338 addPortletURL.setParameter(
339 "assetCategoryIds_" + assetVocabularyId,
340 assetCategoryIds);
341 }
342 }
343
344 if (allAssetTagNames != null) {
345 addPortletURL.setParameter(
346 "assetTagNames", StringUtil.merge(allAssetTagNames));
347 }
348 }
349
350 addPortletURL.setPortletMode(PortletMode.VIEW);
351 addPortletURL.setWindowState(LiferayWindowState.POP_UP);
352
353 return addPortletURL;
354 }
355
356
362 @Deprecated
363 public static PortletURL getAddPortletURL(
364 LiferayPortletRequest liferayPortletRequest,
365 LiferayPortletResponse liferayPortletResponse, String className,
366 long classTypeId, long[] allAssetCategoryIds,
367 String[] allAssetTagNames, String redirect)
368 throws Exception {
369
370 ThemeDisplay themeDisplay =
371 (ThemeDisplay)liferayPortletRequest.getAttribute(
372 WebKeys.THEME_DISPLAY);
373
374 return getAddPortletURL(
375 liferayPortletRequest, liferayPortletResponse,
376 themeDisplay.getScopeGroupId(), className, classTypeId,
377 allAssetCategoryIds, allAssetTagNames, redirect);
378 }
379
380 public static Map<String, PortletURL> getAddPortletURLs(
381 LiferayPortletRequest liferayPortletRequest,
382 LiferayPortletResponse liferayPortletResponse, long groupId,
383 long[] classNameIds, long[] classTypeIds,
384 long[] allAssetCategoryIds, String[] allAssetTagNames,
385 String redirect)
386 throws Exception {
387
388 ThemeDisplay themeDisplay =
389 (ThemeDisplay)liferayPortletRequest.getAttribute(
390 WebKeys.THEME_DISPLAY);
391
392 Map<String, PortletURL> addPortletURLs = new TreeMap<>(
393 new ModelResourceComparator(themeDisplay.getLocale()));
394
395 for (long classNameId : classNameIds) {
396 String className = PortalUtil.getClassName(classNameId);
397
398 AssetRendererFactory<?> assetRendererFactory =
399 AssetRendererFactoryRegistryUtil.
400 getAssetRendererFactoryByClassName(className);
401
402 if (Validator.isNull(assetRendererFactory.getPortletId())) {
403 continue;
404 }
405
406 Portlet portlet = PortletLocalServiceUtil.getPortletById(
407 themeDisplay.getCompanyId(),
408 assetRendererFactory.getPortletId());
409
410 if (!portlet.isActive()) {
411 continue;
412 }
413
414 ClassTypeReader classTypeReader =
415 assetRendererFactory.getClassTypeReader();
416
417 List<ClassType> classTypes = classTypeReader.getAvailableClassTypes(
418 PortalUtil.getCurrentAndAncestorSiteGroupIds(groupId),
419 themeDisplay.getLocale());
420
421 if (classTypes.isEmpty()) {
422 PortletURL addPortletURL = getAddPortletURL(
423 liferayPortletRequest, liferayPortletResponse, groupId,
424 className, 0, allAssetCategoryIds, allAssetTagNames,
425 redirect);
426
427 if (addPortletURL != null) {
428 addPortletURLs.put(className, addPortletURL);
429 }
430 }
431
432 for (ClassType classType : classTypes) {
433 long classTypeId = classType.getClassTypeId();
434
435 if (ArrayUtil.contains(classTypeIds, classTypeId) ||
436 (classTypeIds.length == 0)) {
437
438 PortletURL addPortletURL = getAddPortletURL(
439 liferayPortletRequest, liferayPortletResponse, groupId,
440 className, classTypeId, allAssetCategoryIds,
441 allAssetTagNames, redirect);
442
443 if (addPortletURL != null) {
444 String mesage =
445 className + CLASSNAME_SEPARATOR +
446 classType.getName();
447
448 addPortletURLs.put(mesage, addPortletURL);
449 }
450 }
451 }
452 }
453
454 return addPortletURLs;
455 }
456
457
463 @Deprecated
464 public static Map<String, PortletURL> getAddPortletURLs(
465 LiferayPortletRequest liferayPortletRequest,
466 LiferayPortletResponse liferayPortletResponse, long[] classNameIds,
467 long[] classTypeIds, long[] allAssetCategoryIds,
468 String[] allAssetTagNames, String redirect)
469 throws Exception {
470
471 ThemeDisplay themeDisplay =
472 (ThemeDisplay)liferayPortletRequest.getAttribute(
473 WebKeys.THEME_DISPLAY);
474
475 return getAddPortletURLs(
476 liferayPortletRequest, liferayPortletResponse,
477 themeDisplay.getScopeGroupId(), classNameIds, classTypeIds,
478 allAssetCategoryIds, allAssetTagNames, redirect);
479 }
480
481 public static String getAddURLPopUp(
482 long groupId, long plid, PortletURL addPortletURL, String portletId,
483 boolean addDisplayPageParameter, Layout layout) {
484
485 addPortletURL.setParameter(
486 "hideDefaultSuccessMessage", Boolean.TRUE.toString());
487 addPortletURL.setParameter("groupId", String.valueOf(groupId));
488 addPortletURL.setParameter("showHeader", Boolean.FALSE.toString());
489
490 String addPortletURLString = addPortletURL.toString();
491
492 addPortletURLString = HttpUtil.addParameter(
493 addPortletURLString, "refererPlid", plid);
494
495 if (addDisplayPageParameter && (layout != null)) {
496 String namespace = PortalUtil.getPortletNamespace(portletId);
497
498 addPortletURLString = HttpUtil.addParameter(
499 addPortletURLString, namespace + "layoutUuid",
500 layout.getUuid());
501 }
502
503 return addPortletURLString;
504 }
505
506 public static List<AssetEntry> getAssetEntries(Hits hits) {
507 List<AssetEntry> assetEntries = new ArrayList<>();
508
509 if (hits.getDocs() == null) {
510 return assetEntries;
511 }
512
513 for (Document document : hits.getDocs()) {
514 String className = GetterUtil.getString(
515 document.get(Field.ENTRY_CLASS_NAME));
516 long classPK = GetterUtil.getLong(
517 document.get(Field.ENTRY_CLASS_PK));
518
519 try {
520 AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry(
521 className, classPK);
522
523 assetEntries.add(assetEntry);
524 }
525 catch (Exception e) {
526 }
527 }
528
529 return assetEntries;
530 }
531
532 public static String getAssetKeywords(String className, long classPK) {
533 List<AssetTag> tags = AssetTagLocalServiceUtil.getTags(
534 className, classPK);
535 List<AssetCategory> categories =
536 AssetCategoryLocalServiceUtil.getCategories(className, classPK);
537
538 StringBuffer sb = new StringBuffer();
539
540 sb.append(ListUtil.toString(tags, AssetTag.NAME_ACCESSOR));
541
542 if (!tags.isEmpty()) {
543 sb.append(StringPool.COMMA);
544 }
545
546 sb.append(ListUtil.toString(categories, AssetCategory.NAME_ACCESSOR));
547
548 return sb.toString();
549 }
550
551 public static String getClassName(String className) {
552 int pos = className.indexOf(AssetUtil.CLASSNAME_SEPARATOR);
553
554 if (pos != -1) {
555 className = className.substring(0, pos);
556 }
557
558 return className;
559 }
560
561 public static String getClassNameMessage(String className, Locale locale) {
562 String message = null;
563
564 int pos = className.indexOf(AssetUtil.CLASSNAME_SEPARATOR);
565
566 if (pos != -1) {
567 message = className.substring(
568 pos + AssetUtil.CLASSNAME_SEPARATOR.length());
569
570 className = className.substring(0, pos);
571 }
572
573 AssetRendererFactory<?> assetRendererFactory =
574 AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
575 className);
576
577 if (pos == -1) {
578 message = assetRendererFactory.getTypeName(locale);
579 }
580
581 return message;
582 }
583
584 public static String getDefaultAssetPublisherId(Layout layout) {
585 return layout.getTypeSettingsProperty(
586 LayoutTypePortletConstants.DEFAULT_ASSET_PUBLISHER_PORTLET_ID,
587 StringPool.BLANK);
588 }
589
590 public static Set<String> getLayoutTagNames(HttpServletRequest request) {
591 Set<String> tagNames = (Set<String>)request.getAttribute(
592 WebKeys.ASSET_LAYOUT_TAG_NAMES);
593
594 if (tagNames == null) {
595 tagNames = new HashSet<>();
596
597 request.setAttribute(WebKeys.ASSET_LAYOUT_TAG_NAMES, tagNames);
598 }
599
600 return tagNames;
601 }
602
603 public static boolean hasSubtype(
604 String subtypeClassName, Map<String, PortletURL> addPortletURLs) {
605
606 for (Map.Entry<String, PortletURL> entry : addPortletURLs.entrySet()) {
607 String className = entry.getKey();
608
609 if (className.startsWith(subtypeClassName) &&
610 !className.equals(subtypeClassName)) {
611
612 return true;
613 }
614 }
615
616 return false;
617 }
618
619 public static boolean isDefaultAssetPublisher(
620 Layout layout, String portletId, String portletResource) {
621
622 String defaultAssetPublisherPortletId = getDefaultAssetPublisherId(
623 layout);
624
625 if (Validator.isNull(defaultAssetPublisherPortletId)) {
626 return false;
627 }
628
629 if (defaultAssetPublisherPortletId.equals(portletId) ||
630 defaultAssetPublisherPortletId.equals(portletResource)) {
631
632 return true;
633 }
634
635 return false;
636 }
637
638 public static boolean isValidWord(String word) {
639 if (Validator.isNull(word)) {
640 return false;
641 }
642
643 char[] wordCharArray = word.toCharArray();
644
645 for (char c : wordCharArray) {
646 for (char invalidChar : INVALID_CHARACTERS) {
647 if (c == invalidChar) {
648 if (_log.isDebugEnabled()) {
649 _log.debug(
650 "Word " + word + " is not valid because " + c +
651 " is not allowed");
652 }
653
654 return false;
655 }
656 }
657 }
658
659 return true;
660 }
661
662 public static Hits search(
663 HttpServletRequest request, AssetEntryQuery assetEntryQuery,
664 int start, int end)
665 throws Exception {
666
667 SearchContext searchContext = SearchContextFactory.getInstance(request);
668
669 return search(searchContext, assetEntryQuery, start, end);
670 }
671
672 public static Hits search(
673 SearchContext searchContext, AssetEntryQuery assetEntryQuery,
674 int start, int end)
675 throws Exception {
676
677 AssetSearcher assetSearcher = getAssetSearcher(
678 searchContext, assetEntryQuery, start, end);
679
680 return assetSearcher.search(searchContext);
681 }
682
683 public static BaseModelSearchResult<AssetEntry> searchAssetEntries(
684 AssetEntryQuery assetEntryQuery, long[] assetCategoryIds,
685 String[] assetTagNames, Map<String, Serializable> attributes,
686 long companyId, String keywords, Layout layout, Locale locale,
687 long scopeGroupId, TimeZone timeZone, long userId, int start,
688 int end)
689 throws Exception {
690
691 SearchContext searchContext = SearchContextFactory.getInstance(
692 assetCategoryIds, assetTagNames, attributes, companyId, keywords,
693 layout, locale, scopeGroupId, timeZone, userId);
694
695 return searchAssetEntries(searchContext, assetEntryQuery, start, end);
696 }
697
698 public static BaseModelSearchResult<AssetEntry> searchAssetEntries(
699 HttpServletRequest request, AssetEntryQuery assetEntryQuery,
700 int start, int end)
701 throws Exception {
702
703 SearchContext searchContext = SearchContextFactory.getInstance(request);
704
705 return searchAssetEntries(searchContext, assetEntryQuery, start, end);
706 }
707
708 public static BaseModelSearchResult<AssetEntry> searchAssetEntries(
709 SearchContext searchContext, AssetEntryQuery assetEntryQuery,
710 int start, int end)
711 throws Exception {
712
713 AssetSearcher assetSearcher = getAssetSearcher(
714 searchContext, assetEntryQuery, start, end);
715
716 Hits hits = assetSearcher.search(searchContext);
717
718 List<AssetEntry> assetEntries = getAssetEntries(hits);
719
720 return new BaseModelSearchResult<>(assetEntries, hits.getLength());
721 }
722
723 public static String substituteCategoryPropertyVariables(
724 long groupId, long categoryId, String s) {
725
726 String result = s;
727
728 AssetCategory category = null;
729
730 if (categoryId > 0) {
731 category = AssetCategoryLocalServiceUtil.fetchCategory(categoryId);
732 }
733
734 if (category != null) {
735 List<AssetCategoryProperty> categoryProperties =
736 AssetCategoryPropertyLocalServiceUtil.getCategoryProperties(
737 categoryId);
738
739 for (AssetCategoryProperty categoryProperty : categoryProperties) {
740 result = StringUtil.replace(
741 result, "[$" + categoryProperty.getKey() + "$]",
742 categoryProperty.getValue());
743 }
744 }
745
746 return StringUtil.stripBetween(result, "[$", "$]");
747 }
748
749 public static String toWord(String text) {
750 if (Validator.isNull(text)) {
751 return text;
752 }
753
754 char[] textCharArray = text.toCharArray();
755
756 for (int i = 0; i < textCharArray.length; i++) {
757 char c = textCharArray[i];
758
759 for (char invalidChar : INVALID_CHARACTERS) {
760 if (c == invalidChar) {
761 textCharArray[i] = CharPool.SPACE;
762
763 break;
764 }
765 }
766 }
767
768 return new String(textCharArray);
769 }
770
771 protected static AssetSearcher getAssetSearcher(
772 SearchContext searchContext, AssetEntryQuery assetEntryQuery,
773 int start, int end)
774 throws Exception {
775
776 Indexer<?> searcher = AssetSearcher.getInstance();
777
778 AssetSearcher assetSearcher = (AssetSearcher)searcher;
779
780 assetSearcher.setAssetEntryQuery(assetEntryQuery);
781
782 Layout layout = assetEntryQuery.getLayout();
783
784 if (layout != null) {
785 searchContext.setAttribute(Field.LAYOUT_UUID, layout.getUuid());
786 }
787
788 String ddmStructureFieldName = (String)assetEntryQuery.getAttribute(
789 "ddmStructureFieldName");
790 Serializable ddmStructureFieldValue = assetEntryQuery.getAttribute(
791 "ddmStructureFieldValue");
792
793 if (Validator.isNotNull(ddmStructureFieldName) &&
794 Validator.isNotNull(ddmStructureFieldValue)) {
795
796 searchContext.setAttribute(
797 "ddmStructureFieldName", ddmStructureFieldName);
798 searchContext.setAttribute(
799 "ddmStructureFieldValue", ddmStructureFieldValue);
800 }
801
802 String paginationType = GetterUtil.getString(
803 assetEntryQuery.getPaginationType(), "more");
804
805 if (!paginationType.equals("none") &&
806 !paginationType.equals("simple")) {
807
808 searchContext.setAttribute("paginationType", paginationType);
809 }
810
811 searchContext.setClassTypeIds(assetEntryQuery.getClassTypeIds());
812 searchContext.setEnd(end);
813 searchContext.setGroupIds(assetEntryQuery.getGroupIds());
814
815 if (Validator.isNotNull(assetEntryQuery.getKeywords())) {
816 searchContext.setLike(true);
817 }
818
819 searchContext.setSorts(
820 getSorts(assetEntryQuery, searchContext.getLocale()));
821 searchContext.setStart(start);
822
823 return assetSearcher;
824 }
825
826 protected static String getDDMFormFieldType(String sortField)
827 throws PortalException {
828
829 String[] sortFields = sortField.split(
830 DDMStructureManager.STRUCTURE_INDEXER_FIELD_SEPARATOR);
831
832 long ddmStructureId = GetterUtil.getLong(sortFields[2]);
833 String fieldName = sortFields[3];
834
835 DDMStructure ddmStructure = DDMStructureManagerUtil.getStructure(
836 ddmStructureId);
837
838 return ddmStructure.getFieldType(fieldName);
839 }
840
841 protected static String getOrderByCol(
842 String sortField, String fieldType, int sortType, Locale locale) {
843
844 if (sortField.startsWith(
845 DDMStructureManager.STRUCTURE_INDEXER_FIELD_PREFIX)) {
846
847 sortField = sortField.concat(StringPool.UNDERLINE).concat(
848 LocaleUtil.toLanguageId(locale));
849
850 if (!fieldType.equals("ddm-date") &&
851 ((sortType == Sort.DOUBLE_TYPE) ||
852 (sortType == Sort.FLOAT_TYPE) || (sortType == Sort.INT_TYPE) ||
853 (sortType == Sort.LONG_TYPE))) {
854
855 sortField = sortField.concat(StringPool.UNDERLINE).concat(
856 "Number");
857 }
858
859 sortField = DocumentImpl.getSortableFieldName(sortField);
860 }
861 else if (sortField.equals("modifiedDate")) {
862 sortField = Field.MODIFIED_DATE;
863 }
864 else if (sortField.equals("title")) {
865 sortField = DocumentImpl.getSortableFieldName(
866 "localized_title_".concat(LocaleUtil.toLanguageId(locale)));
867 }
868
869 return sortField;
870 }
871
872 protected static Sort getSort(
873 String orderByType, String sortField, Locale locale)
874 throws Exception {
875
876 String ddmFormFieldType = sortField;
877
878 if (ddmFormFieldType.startsWith(
879 DDMStructureManager.STRUCTURE_INDEXER_FIELD_PREFIX)) {
880
881 ddmFormFieldType = getDDMFormFieldType(ddmFormFieldType);
882 }
883
884 int sortType = getSortType(ddmFormFieldType);
885
886 return SortFactoryUtil.getSort(
887 AssetEntry.class, sortType,
888 getOrderByCol(sortField, ddmFormFieldType, sortType, locale),
889 !sortField.startsWith(
890 DDMStructureManager.STRUCTURE_INDEXER_FIELD_PREFIX),
891 orderByType);
892 }
893
894 protected static Sort[] getSorts(
895 AssetEntryQuery assetEntryQuery, Locale locale)
896 throws Exception {
897
898 Sort sort1 = getSort(
899 assetEntryQuery.getOrderByType1(), assetEntryQuery.getOrderByCol1(),
900 locale);
901 Sort sort2 = getSort(
902 assetEntryQuery.getOrderByType2(), assetEntryQuery.getOrderByCol2(),
903 locale);
904
905 return new Sort[] {sort1, sort2};
906 }
907
908 protected static int getSortType(String fieldType) {
909 int sortType = Sort.STRING_TYPE;
910
911 if (fieldType.equals(Field.CREATE_DATE) ||
912 fieldType.equals(Field.EXPIRATION_DATE) ||
913 fieldType.equals(Field.PUBLISH_DATE) ||
914 fieldType.equals("ddm-date") || fieldType.equals("modifiedDate")) {
915
916 sortType = Sort.LONG_TYPE;
917 }
918 else if (fieldType.equals(Field.PRIORITY) ||
919 fieldType.equals(Field.RATINGS) ||
920 fieldType.equals("ddm-decimal") ||
921 fieldType.equals("ddm-number")) {
922
923 sortType = Sort.DOUBLE_TYPE;
924 }
925 else if (fieldType.equals(Field.VIEW_COUNT) ||
926 fieldType.equals("ddm-integer")) {
927
928 sortType = Sort.INT_TYPE;
929 }
930
931 return sortType;
932 }
933
934 private static final Log _log = LogFactoryUtil.getLog(AssetUtil.class);
935
936 }