001
014
015 package com.liferay.portlet.asset.util;
016
017 import com.liferay.portal.NoSuchGroupException;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.util.ListUtil;
020 import com.liferay.portal.model.Group;
021 import com.liferay.portal.service.ClassNameLocalServiceUtil;
022 import com.liferay.portal.service.GroupLocalServiceUtil;
023 import com.liferay.portal.util.PortalUtil;
024 import com.liferay.portlet.asset.AssetCategoryException;
025 import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
026 import com.liferay.portlet.asset.model.AssetRendererFactory;
027 import com.liferay.portlet.asset.model.AssetVocabulary;
028 import com.liferay.portlet.asset.service.AssetVocabularyLocalServiceUtil;
029
030 import java.util.List;
031
032
035 public class BaseAssetEntryValidator implements AssetEntryValidator {
036
037 @Override
038 public void validate(
039 long groupId, String className, long classTypePK,
040 long[] categoryIds, String[] entryNames)
041 throws PortalException {
042
043 List<AssetVocabulary> vocabularies =
044 AssetVocabularyLocalServiceUtil.getGroupVocabularies(
045 groupId, false);
046
047 Group group = GroupLocalServiceUtil.getGroup(groupId);
048
049 if (!group.isCompany()) {
050 try {
051 Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
052 group.getCompanyId());
053
054 vocabularies = ListUtil.copy(vocabularies);
055
056 vocabularies.addAll(
057 AssetVocabularyLocalServiceUtil.getGroupVocabularies(
058 companyGroup.getGroupId()));
059 }
060 catch (NoSuchGroupException nsge) {
061 }
062 }
063
064 long classNameId = ClassNameLocalServiceUtil.getClassNameId(className);
065
066 for (AssetVocabulary vocabulary : vocabularies) {
067 validate(classNameId, classTypePK, categoryIds, vocabulary);
068 }
069 }
070
071 protected boolean isAssetCategorizable(long classNameId) {
072 String className = PortalUtil.getClassName(classNameId);
073
074 AssetRendererFactory assetRendererFactory =
075 AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
076 className);
077
078 if ((assetRendererFactory == null) ||
079 !assetRendererFactory.isCategorizable()) {
080
081 return false;
082 }
083
084 return true;
085 }
086
087 protected void validate(
088 long classNameId, long classTypePK, final long[] categoryIds,
089 AssetVocabulary vocabulary)
090 throws PortalException {
091
092 if (!vocabulary.isAssociatedToClassNameIdAndClassTypePK(
093 classNameId, classTypePK)) {
094
095 return;
096 }
097
098 if (!isAssetCategorizable(classNameId)) {
099 return;
100 }
101
102 if (vocabulary.isMissingRequiredCategory(
103 classNameId, classTypePK, categoryIds)) {
104
105 throw new AssetCategoryException(
106 vocabulary, AssetCategoryException.AT_LEAST_ONE_CATEGORY);
107 }
108
109 if (!vocabulary.isMultiValued() &&
110 vocabulary.hasMoreThanOneCategorySelected(categoryIds)) {
111
112 throw new AssetCategoryException(
113 vocabulary, AssetCategoryException.TOO_MANY_CATEGORIES);
114 }
115 }
116
117 }