001
014
015 package com.liferay.portlet.asset.util;
016
017 import com.liferay.portal.NoSuchGroupException;
018 import com.liferay.portal.kernel.dao.orm.QueryUtil;
019 import com.liferay.portal.kernel.exception.PortalException;
020 import com.liferay.portal.kernel.exception.SystemException;
021 import com.liferay.portal.kernel.util.ArrayUtil;
022 import com.liferay.portal.kernel.util.ListUtil;
023 import com.liferay.portal.kernel.util.StringUtil;
024 import com.liferay.portal.kernel.util.UnicodeProperties;
025 import com.liferay.portal.model.Group;
026 import com.liferay.portal.service.ClassNameServiceUtil;
027 import com.liferay.portal.service.GroupLocalServiceUtil;
028 import com.liferay.portal.util.PortalUtil;
029 import com.liferay.portlet.asset.AssetCategoryException;
030 import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
031 import com.liferay.portlet.asset.model.AssetCategory;
032 import com.liferay.portlet.asset.model.AssetCategoryConstants;
033 import com.liferay.portlet.asset.model.AssetRendererFactory;
034 import com.liferay.portlet.asset.model.AssetVocabulary;
035 import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
036 import com.liferay.portlet.asset.service.AssetVocabularyLocalServiceUtil;
037
038 import java.util.List;
039
040
043 public class BaseAssetEntryValidator implements AssetEntryValidator {
044
045 public void validate(
046 long groupId, String className, long[] categoryIds,
047 String[] entryNames)
048 throws PortalException, SystemException {
049
050 List<AssetVocabulary> vocabularies =
051 AssetVocabularyLocalServiceUtil.getGroupVocabularies(
052 groupId, false);
053
054 Group group = GroupLocalServiceUtil.getGroup(groupId);
055
056 if (!group.isCompany()) {
057 try {
058 Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
059 group.getCompanyId());
060
061 vocabularies = ListUtil.copy(vocabularies);
062
063 vocabularies.addAll(
064 AssetVocabularyLocalServiceUtil.getGroupVocabularies(
065 companyGroup.getGroupId()));
066 }
067 catch (NoSuchGroupException nsge) {
068 }
069 }
070
071 long classNameId = ClassNameServiceUtil.getClassNameId(className);
072
073 for (AssetVocabulary vocabulary : vocabularies) {
074 validate(classNameId, categoryIds, vocabulary);
075 }
076 }
077
078 protected void validate(
079 long classNameId, long[] categoryIds, AssetVocabulary vocabulary)
080 throws PortalException, SystemException {
081
082 UnicodeProperties settingsProperties =
083 vocabulary.getSettingsProperties();
084
085 long[] selectedClassNameIds = StringUtil.split(
086 settingsProperties.getProperty("selectedClassNameIds"), 0L);
087
088 if (selectedClassNameIds.length == 0) {
089 return;
090 }
091
092 if ((selectedClassNameIds[0] !=
093 AssetCategoryConstants.ALL_CLASS_NAME_IDS) &&
094 !ArrayUtil.contains(selectedClassNameIds, classNameId)) {
095
096 return;
097 }
098
099 String className = PortalUtil.getClassName(classNameId);
100
101 AssetRendererFactory assetRendererFactory =
102 AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
103 className);
104
105 if ((assetRendererFactory == null) ||
106 !assetRendererFactory.isCategorizable()) {
107
108 return;
109 }
110
111 long[] requiredClassNameIds = StringUtil.split(
112 settingsProperties.getProperty("requiredClassNameIds"), 0L);
113
114 List<AssetCategory> categories =
115 AssetCategoryLocalServiceUtil.getVocabularyCategories(
116 vocabulary.getVocabularyId(), QueryUtil.ALL_POS,
117 QueryUtil.ALL_POS, null);
118
119 if ((requiredClassNameIds.length > 0) &&
120 ((requiredClassNameIds[0] ==
121 AssetCategoryConstants.ALL_CLASS_NAME_IDS) ||
122 ArrayUtil.contains(requiredClassNameIds, classNameId))) {
123
124 boolean found = false;
125
126 for (AssetCategory category : categories) {
127 if (ArrayUtil.contains(categoryIds, category.getCategoryId())) {
128 found = true;
129
130 break;
131 }
132 }
133
134 if (!found && !categories.isEmpty()) {
135 throw new AssetCategoryException(
136 vocabulary, AssetCategoryException.AT_LEAST_ONE_CATEGORY);
137 }
138 }
139
140 if (!vocabulary.isMultiValued()) {
141 boolean duplicate = false;
142
143 for (AssetCategory category : categories) {
144 if (ArrayUtil.contains(categoryIds, category.getCategoryId())) {
145 if (!duplicate) {
146 duplicate = true;
147 }
148 else {
149 throw new AssetCategoryException(
150 vocabulary,
151 AssetCategoryException.TOO_MANY_CATEGORIES);
152 }
153 }
154 }
155 }
156 }
157
158 }