001/*
002 *  Copyright 2018 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.cms.content.referencetable;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020
021import org.ametys.cms.data.ContentValue;
022import org.ametys.cms.repository.Content;
023import org.ametys.cms.workflow.NeedAllValues;
024import org.ametys.plugins.repository.AmetysObjectResolver;
025import org.ametys.runtime.i18n.I18nizableText;
026import org.ametys.runtime.parameter.DefaultValidator;
027import org.ametys.runtime.parameter.ValidationResult;
028
029/**
030 * This validator does not allow a set of values only containing candidates. At
031 * least one content is expected
032 */
033@NeedAllValues
034public class CandidatesValidator extends DefaultValidator
035{
036    /** Ametys resolver */
037    protected AmetysObjectResolver _resolver;
038    
039    /** The helper component for hierarchical reference tables */
040    protected HierarchicalReferenceTablesHelper _hierarchicalReferenceTablesHelper;
041    
042    @Override
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        super.service(manager);
046        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
047        _hierarchicalReferenceTablesHelper = (HierarchicalReferenceTablesHelper) manager.lookup(HierarchicalReferenceTablesHelper.ROLE);
048    }
049    
050    @Override
051    protected ValidationResult validateSingleValue(Object value)
052    {
053        ValidationResult result = super.validateSingleValue(value);
054        
055        ContentValue contentValue = (ContentValue) value;
056        Content content = contentValue != null ? contentValue.getContentIfExists().orElse(null) : null;
057        
058        if (content != null && _hierarchicalReferenceTablesHelper.isCandidate(content))
059        {
060            if (getLogger().isDebugEnabled())
061            {
062                getLogger().debug("The validator refused a value because it contained only candidates");
063            }
064            
065            result.addError(new I18nizableText("plugin.cms", "PLUGINS_CMS_HIERARCHICAL_REFERENCE_TABLES_VALIDATOR_SINGLE_VALUE_REJECTED_MSG"));
066        }
067        
068        return result;
069    }
070    
071    @Override
072    protected ValidationResult validateArrayValues(Object[] values)
073    {
074        ValidationResult result = super.validateArrayValues(values);
075        
076        if (values != null && values.length > 0 && _hasOnlyCandidates((ContentValue[]) values))
077        {
078            if (getLogger().isDebugEnabled())
079            {
080                getLogger().debug("The validator refused a value because it contained only candidates");
081            }
082            
083            result.addError(new I18nizableText("plugin.cms", "PLUGINS_CMS_HIERARCHICAL_REFERENCE_TABLES_VALIDATOR_ARRAY_VALUES_REJECTED_MSG"));
084        }
085        
086        return result;
087    }
088    
089    /**
090     * Indicates if the array of contents contains only candidates
091     * @param contents the list of content
092     * @return true if it contains only candidates
093     */
094    protected boolean _hasOnlyCandidates(ContentValue[] contents)
095    {
096        for (ContentValue contentValue : contents)
097        {
098            Content content = contentValue.getContentIfExists().orElse(null);
099            
100            if (content != null && !_hierarchicalReferenceTablesHelper.isCandidate(content))
101            {
102                return false;
103            }
104        }
105        
106        return true;
107    }
108}