001/*
002 *  Copyright 2022 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.plugins.forms.question.validators;
017
018import java.time.ZonedDateTime;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.ametys.runtime.i18n.I18nizableText;
023import org.ametys.runtime.i18n.I18nizableTextParameter;
024import org.ametys.runtime.parameter.Errors;
025
026/**
027 * A date time interval form validator
028 */
029public class DateTimeIntervalFormValidator extends AbstractIntervalFormValidator<ZonedDateTime>
030{
031    /**
032     * The constructor
033     * @param regexp the regexp
034     * @param mandatory mandatory
035     * @param min the min value
036     * @param max the max value
037     */
038    public DateTimeIntervalFormValidator(String regexp, boolean mandatory, ZonedDateTime min, ZonedDateTime max)
039    {
040        super(regexp, mandatory, min, max);
041    }
042
043    @Override
044    protected boolean isLessThan(ZonedDateTime dateToValidate, ZonedDateTime dateMin)
045    {
046        return dateToValidate.isBefore(dateMin);
047    }
048
049    @Override
050    protected boolean isMoreThan(ZonedDateTime dateToValidate, ZonedDateTime dateMax)
051    {
052        return dateToValidate.isAfter(dateMax);
053    }
054
055    @Override
056    protected void addIntervalError(Errors errors, ZonedDateTime min, ZonedDateTime max)
057    {
058        Map<String, I18nizableTextParameter> i18nParams = new HashMap<>();
059        i18nParams.put("startDate", new I18nizableText(min.toString()));
060        i18nParams.put("endDate", new I18nizableText(max.toString()));
061        errors.addError(new I18nizableText("plugin.cms", "PLUGINS_FORMS_ENTRY_VALIDATOR_DATE_INTERVAL_ERROR", i18nParams));
062    }
063}