001/*
002 *  Copyright 2014 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.contenttype.validation;
017
018import java.time.LocalDate;
019import java.time.format.DateTimeFormatter;
020import java.time.format.FormatStyle;
021import java.util.HashMap;
022import java.util.Map;
023
024import org.apache.avalon.framework.configuration.Configuration;
025import org.apache.avalon.framework.configuration.ConfigurationException;
026
027import org.ametys.cms.contenttype.ContentValidator;
028import org.ametys.runtime.i18n.I18nizableText;
029import org.ametys.runtime.i18n.I18nizableTextParameter;
030import org.ametys.runtime.model.ElementDefinition;
031import org.ametys.runtime.model.type.ElementType;
032import org.ametys.runtime.model.type.ModelItemTypeConstants;
033import org.ametys.runtime.parameter.Errors;
034
035/**
036 * This implementation of {@link ContentValidator} validates the content by comparing one or more couple of date values.
037 * For each couple of values, the identified end date should be greater than or equal to the identified start date.
038 * 
039 * The start and end dates to compare are identified in validator's configuration by the metadata path as follows:
040 * <startDate path="path/to/attribute/start"/>
041 * <endDate path="path/to/attribute/end"/>
042 * 
043 * The path of attribute can contain one or more repeaters.
044 */
045public class DateIntervalValidator extends AbstractIntervalValidator<LocalDate>
046{
047    @Override
048    protected String configureMinPath(Configuration configuration) throws ConfigurationException
049    {
050        return configuration.getChild("startDate").getAttribute("path");
051    }
052    
053    @Override
054    protected String configureMaxPath(Configuration configuration) throws ConfigurationException
055    {
056        return configuration.getChild("endDate").getAttribute("path");
057    }
058
059    @Override
060    protected boolean isLessThan(LocalDate date1, LocalDate date2)
061    {
062        return date1.isBefore(date2);
063    }
064    
065    @Override
066    protected boolean isSupportedType(ElementType type)
067    {
068        return type.getId().equals(ModelItemTypeConstants.DATE_TYPE_ID);
069    }
070    
071    @Override
072    protected void addIntervalError(Errors errors, ElementDefinition minDefinition, ElementDefinition maxDefinition, LocalDate startDate, LocalDate endDate)
073    {
074        Map<String, I18nizableTextParameter> i18nParams = new HashMap<>();
075        i18nParams.put("startDate", new I18nizableText(startDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT))));
076        i18nParams.put("endDate", new I18nizableText(endDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT))));
077        i18nParams.put("startLabel", minDefinition.getLabel());
078        i18nParams.put("endLabel", maxDefinition.getLabel());
079        errors.addError(new I18nizableText("plugin.cms", "PLUGINS_CMS_CONTENT_VALIDATOR_DATE_INTERVAL_ERROR", i18nParams));
080    }
081
082    @Override
083    protected void addErrorEmpty(Errors errors, ElementDefinition minDefinition, ElementDefinition maxDefinition, I18nizableText emptyLabel)
084    {
085        Map<String, I18nizableTextParameter> i18nParams = new HashMap<>();
086        i18nParams.put("emptyLabel", emptyLabel);
087        i18nParams.put("startLabel", minDefinition.getLabel());
088        i18nParams.put("endLabel", maxDefinition.getLabel());
089        errors.addError(new I18nizableText("plugin.cms", "PLUGINS_CMS_CONTENT_VALIDATOR_DATE_INTERVAL_NULL_ERROR", i18nParams));
090    }
091}