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