001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.asset.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.ListUtil;
019    import com.liferay.portal.model.Group;
020    import com.liferay.portal.service.ClassNameLocalServiceUtil;
021    import com.liferay.portal.service.GroupLocalServiceUtil;
022    import com.liferay.portal.util.PortalUtil;
023    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
024    import com.liferay.portlet.asset.exception.AssetCategoryException;
025    import com.liferay.portlet.asset.model.AssetRendererFactory;
026    import com.liferay.portlet.asset.model.AssetVocabulary;
027    import com.liferay.portlet.asset.service.AssetVocabularyLocalServiceUtil;
028    
029    import java.util.List;
030    
031    /**
032     * @author Juan Fern??ndez
033     */
034    public class BaseAssetEntryValidator implements AssetEntryValidator {
035    
036            @Override
037            public void validate(
038                            long groupId, String className, long classTypePK,
039                            long[] categoryIds, String[] entryNames)
040                    throws PortalException {
041    
042                    List<AssetVocabulary> vocabularies =
043                            AssetVocabularyLocalServiceUtil.getGroupVocabularies(
044                                    groupId, false);
045    
046                    Group group = GroupLocalServiceUtil.getGroup(groupId);
047    
048                    if (!group.isCompany()) {
049                            Group companyGroup = GroupLocalServiceUtil.fetchCompanyGroup(
050                                    group.getCompanyId());
051    
052                            if (companyGroup != null) {
053                                    vocabularies = ListUtil.copy(vocabularies);
054    
055                                    vocabularies.addAll(
056                                            AssetVocabularyLocalServiceUtil.getGroupVocabularies(
057                                                    companyGroup.getGroupId()));
058                            }
059                    }
060    
061                    long classNameId = ClassNameLocalServiceUtil.getClassNameId(className);
062    
063                    for (AssetVocabulary vocabulary : vocabularies) {
064                            validate(classNameId, classTypePK, categoryIds, vocabulary);
065                    }
066            }
067    
068            protected boolean isAssetCategorizable(long classNameId) {
069                    String className = PortalUtil.getClassName(classNameId);
070    
071                    AssetRendererFactory<?> assetRendererFactory =
072                            AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
073                                    className);
074    
075                    if ((assetRendererFactory == null) ||
076                            !assetRendererFactory.isCategorizable()) {
077    
078                            return false;
079                    }
080    
081                    return true;
082            }
083    
084            protected void validate(
085                            long classNameId, long classTypePK, final long[] categoryIds,
086                            AssetVocabulary vocabulary)
087                    throws PortalException {
088    
089                    if (!vocabulary.isAssociatedToClassNameIdAndClassTypePK(
090                                    classNameId, classTypePK)) {
091    
092                            return;
093                    }
094    
095                    if (!isAssetCategorizable(classNameId)) {
096                            return;
097                    }
098    
099                    if (vocabulary.isMissingRequiredCategory(
100                                    classNameId, classTypePK, categoryIds)) {
101    
102                            throw new AssetCategoryException(
103                                    vocabulary, AssetCategoryException.AT_LEAST_ONE_CATEGORY);
104                    }
105    
106                    if (!vocabulary.isMultiValued() &&
107                            vocabulary.hasMoreThanOneCategorySelected(categoryIds)) {
108    
109                            throw new AssetCategoryException(
110                                    vocabulary, AssetCategoryException.TOO_MANY_CATEGORIES);
111                    }
112            }
113    
114    }