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