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