001/*
002 *  Copyright 2010 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.workflow;
017
018import java.util.List;
019import java.util.Map;
020import java.util.stream.Collectors;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.cms.repository.Content;
026import org.ametys.core.util.I18nUtils;
027import org.ametys.plugins.repository.AmetysRepositoryException;
028import org.ametys.plugins.workflow.EnhancedCondition;
029import org.ametys.runtime.i18n.I18nizableText;
030import org.ametys.runtime.parameter.ValidationResult;
031
032import com.opensymphony.module.propertyset.PropertySet;
033import com.opensymphony.workflow.WorkflowException;
034
035/**
036 * OSWorkflow condition to check all content metadata are valid
037 */
038public class ValidateContentCondition extends AbstractContentWorkflowComponent implements EnhancedCondition
039{
040    /** The i18n utils */
041    protected I18nUtils _i18nUtils;
042    
043    @Override
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        super.service(smanager);
047        _i18nUtils = (I18nUtils) smanager.lookup(I18nUtils.ROLE);
048    }
049    
050    @Override
051    public boolean passesCondition(Map transientVars, Map args, PropertySet ps) throws WorkflowException
052    {
053        List<ConditionFailure> conditionFailures = getConditionFailures(transientVars);
054        try
055        {
056            Content content = getContent(transientVars);
057            ValidationResult result = _contentHelper.validateContent(content);
058            if (result.hasErrors())
059            {
060                if (conditionFailures != null)
061                {
062                    String errorMsg = result.getErrors().stream().map(error -> _i18nUtils.translate(error, "en")).collect(Collectors.joining(", "));
063                    conditionFailures.add(new ConditionFailure(
064                            errorMsg,
065                            ValidateContentCondition.class.getName()
066                            ));
067                }
068                
069                return false;
070            }
071            
072            return true;
073        }
074        catch (AmetysRepositoryException e)
075        {
076            if (conditionFailures != null)
077            {
078                conditionFailures.add(new ConditionFailure(
079                        "Validate metadata condition failed (" + e.getMessage() + ")",
080                        ValidateContentCondition.class.getName()
081                ));
082            }
083            _logger.error("Cannot check condition ("  + e.getMessage() + "). Assuming it is false.", e);
084            return false; 
085        }
086    }
087    
088    @Override
089    public I18nizableText getLabel()
090    {
091        return new I18nizableText("plugin.cms", "PLUGINS_CMS_VALIDATE_CONTENT_CONDITION_LABEL");
092    }
093}