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.Errors;
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 void validateSingleValue(Object value, Errors errors)
052    {
053        super.validateSingleValue(value, errors);
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            errors.addError(new I18nizableText("plugin.cms", "PLUGINS_CMS_HIERARCHICAL_REFERENCE_TABLES_VALIDATOR_SINGLE_VALUE_REJECTED_MSG"));
066        }
067    }
068    
069    @Override
070    protected void validateArrayValues(Object[] values, Errors errors)
071    {
072        super.validateArrayValues(values, errors);
073        
074        if (values != null && values.length > 0 && _hasOnlyCandidates((ContentValue[]) values))
075        {
076            if (getLogger().isDebugEnabled())
077            {
078                getLogger().debug("The validator refused a value because it contained only candidates");
079            }
080            
081            errors.addError(new I18nizableText("plugin.cms", "PLUGINS_CMS_HIERARCHICAL_REFERENCE_TABLES_VALIDATOR_ARRAY_VALUES_REJECTED_MSG"));
082        }
083    }
084    
085    /**
086     * Indicates if the array of contents contains only candidates
087     * @param contents the list of content
088     * @return true if it contains only candidates
089     */
090    protected boolean _hasOnlyCandidates(ContentValue[] contents)
091    {
092        for (ContentValue contentValue : contents)
093        {
094            Content content = contentValue.getContentIfExists().orElse(null);
095            
096            if (content != null && !_hierarchicalReferenceTablesHelper.isCandidate(content))
097            {
098                return false;
099            }
100        }
101        
102        return true;
103    }
104}