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