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.isCategorizable()) {
106 return;
107 }
108
109 long[] requiredClassNameIds = StringUtil.split(
110 settingsProperties.getProperty("requiredClassNameIds"), 0L);
111
112 List<AssetCategory> categories =
113 AssetCategoryLocalServiceUtil.getVocabularyCategories(
114 vocabulary.getVocabularyId(), QueryUtil.ALL_POS,
115 QueryUtil.ALL_POS, null);
116
117 if ((requiredClassNameIds.length > 0) &&
118 ((requiredClassNameIds[0] ==
119 AssetCategoryConstants.ALL_CLASS_NAME_IDS) ||
120 ArrayUtil.contains(requiredClassNameIds, classNameId))) {
121
122 boolean found = false;
123
124 for (AssetCategory category : categories) {
125 if (ArrayUtil.contains(categoryIds, category.getCategoryId())) {
126 found = true;
127
128 break;
129 }
130 }
131
132 if (!found && !categories.isEmpty()) {
133 throw new AssetCategoryException(
134 vocabulary, AssetCategoryException.AT_LEAST_ONE_CATEGORY);
135 }
136 }
137
138 if (!vocabulary.isMultiValued()) {
139 boolean duplicate = false;
140
141 for (AssetCategory category : categories) {
142 if (ArrayUtil.contains(categoryIds, category.getCategoryId())) {
143 if (!duplicate) {
144 duplicate = true;
145 }
146 else {
147 throw new AssetCategoryException(
148 vocabulary,
149 AssetCategoryException.TOO_MANY_CATEGORIES);
150 }
151 }
152 }
153 }
154 }
155
156 }