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.ClassNameLocalServiceUtil;
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 @Override
046 public void validate(
047 long groupId, String className, long[] categoryIds,
048 String[] entryNames)
049 throws PortalException, SystemException {
050
051 List<AssetVocabulary> vocabularies =
052 AssetVocabularyLocalServiceUtil.getGroupVocabularies(
053 groupId, false);
054
055 Group group = GroupLocalServiceUtil.getGroup(groupId);
056
057 if (!group.isCompany()) {
058 try {
059 Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
060 group.getCompanyId());
061
062 vocabularies = ListUtil.copy(vocabularies);
063
064 vocabularies.addAll(
065 AssetVocabularyLocalServiceUtil.getGroupVocabularies(
066 companyGroup.getGroupId()));
067 }
068 catch (NoSuchGroupException nsge) {
069 }
070 }
071
072 long classNameId = ClassNameLocalServiceUtil.getClassNameId(className);
073
074 for (AssetVocabulary vocabulary : vocabularies) {
075 validate(classNameId, categoryIds, vocabulary);
076 }
077 }
078
079 protected void validate(
080 long classNameId, long[] categoryIds, AssetVocabulary vocabulary)
081 throws PortalException, SystemException {
082
083 UnicodeProperties settingsProperties =
084 vocabulary.getSettingsProperties();
085
086 long[] selectedClassNameIds = StringUtil.split(
087 settingsProperties.getProperty("selectedClassNameIds"), 0L);
088
089 if (selectedClassNameIds.length == 0) {
090 return;
091 }
092
093 if ((selectedClassNameIds[0] !=
094 AssetCategoryConstants.ALL_CLASS_NAME_IDS) &&
095 !ArrayUtil.contains(selectedClassNameIds, classNameId)) {
096
097 return;
098 }
099
100 String className = PortalUtil.getClassName(classNameId);
101
102 AssetRendererFactory assetRendererFactory =
103 AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
104 className);
105
106 if ((assetRendererFactory == null) ||
107 !assetRendererFactory.isCategorizable()) {
108
109 return;
110 }
111
112 long[] requiredClassNameIds = StringUtil.split(
113 settingsProperties.getProperty("requiredClassNameIds"), 0L);
114
115 List<AssetCategory> categories =
116 AssetCategoryLocalServiceUtil.getVocabularyCategories(
117 vocabulary.getVocabularyId(), QueryUtil.ALL_POS,
118 QueryUtil.ALL_POS, null);
119
120 if ((requiredClassNameIds.length > 0) &&
121 ((requiredClassNameIds[0] ==
122 AssetCategoryConstants.ALL_CLASS_NAME_IDS) ||
123 ArrayUtil.contains(requiredClassNameIds, classNameId))) {
124
125 boolean found = false;
126
127 for (AssetCategory category : categories) {
128 if (ArrayUtil.contains(categoryIds, category.getCategoryId())) {
129 found = true;
130
131 break;
132 }
133 }
134
135 if (!found && !categories.isEmpty()) {
136 throw new AssetCategoryException(
137 vocabulary, AssetCategoryException.AT_LEAST_ONE_CATEGORY);
138 }
139 }
140
141 if (!vocabulary.isMultiValued()) {
142 boolean duplicate = false;
143
144 for (AssetCategory category : categories) {
145 if (ArrayUtil.contains(categoryIds, category.getCategoryId())) {
146 if (!duplicate) {
147 duplicate = true;
148 }
149 else {
150 throw new AssetCategoryException(
151 vocabulary,
152 AssetCategoryException.TOO_MANY_CATEGORIES);
153 }
154 }
155 }
156 }
157 }
158
159 }