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