001
014
015 package com.liferay.taglib.util;
016
017 import com.liferay.portal.kernel.util.ArrayUtil;
018 import com.liferay.portal.kernel.util.GetterUtil;
019 import com.liferay.portal.kernel.util.StringBundler;
020 import com.liferay.portal.kernel.util.StringPool;
021 import com.liferay.portal.kernel.util.StringUtil;
022 import com.liferay.portal.kernel.util.Validator;
023 import com.liferay.portal.theme.ThemeDisplay;
024 import com.liferay.portlet.asset.model.AssetCategory;
025 import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
026
027 import java.util.ArrayList;
028 import java.util.List;
029
030
033 public class AssetCategoryUtil {
034
035 public static final String CATEGORY_SEPARATOR = "_CATEGORY_";
036
037 public static long[] filterCategoryIds(
038 long vocabularyId, long[] categoryIds) {
039
040 List<Long> filteredCategoryIds = new ArrayList<>();
041
042 for (long categoryId : categoryIds) {
043 AssetCategory category =
044 AssetCategoryLocalServiceUtil.fetchCategory(categoryId);
045
046 if (category == null) {
047 continue;
048 }
049
050 if (category.getVocabularyId() == vocabularyId) {
051 filteredCategoryIds.add(category.getCategoryId());
052 }
053 }
054
055 return ArrayUtil.toArray(
056 filteredCategoryIds.toArray(new Long[filteredCategoryIds.size()]));
057 }
058
059 public static String[] getCategoryIdsTitles(
060 String categoryIds, String categoryNames, long vocabularyId,
061 ThemeDisplay themeDisplay) {
062
063 if (Validator.isNotNull(categoryIds)) {
064 long[] categoryIdsArray = GetterUtil.getLongValues(
065 StringUtil.split(categoryIds));
066
067 if (vocabularyId > 0) {
068 categoryIdsArray = filterCategoryIds(
069 vocabularyId, categoryIdsArray);
070 }
071
072 categoryIds = StringPool.BLANK;
073 categoryNames = StringPool.BLANK;
074
075 if (categoryIdsArray.length > 0) {
076 StringBundler categoryIdsSb = new StringBundler(
077 categoryIdsArray.length * 2);
078 StringBundler categoryNamesSb = new StringBundler(
079 categoryIdsArray.length * 2);
080
081 for (long categoryId : categoryIdsArray) {
082 AssetCategory category =
083 AssetCategoryLocalServiceUtil.fetchCategory(categoryId);
084
085 if (category == null) {
086 continue;
087 }
088
089 category = category.toEscapedModel();
090
091 categoryIdsSb.append(categoryId);
092 categoryIdsSb.append(StringPool.COMMA);
093
094 categoryNamesSb.append(
095 category.getTitle(themeDisplay.getLocale()));
096 categoryNamesSb.append(CATEGORY_SEPARATOR);
097 }
098
099 if (categoryIdsSb.index() > 0) {
100 categoryIdsSb.setIndex(categoryIdsSb.index() - 1);
101 categoryNamesSb.setIndex(categoryNamesSb.index() - 1);
102
103 categoryIds = categoryIdsSb.toString();
104 categoryNames = categoryNamesSb.toString();
105 }
106 }
107 }
108
109 return new String[] {categoryIds, categoryNames};
110 }
111
112 }