All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.ird.observe.validation.validators.TemperatureBoundFieldValidator Maven / Gradle / Ivy

There is a newer version: 4.34
Show newest version
package fr.ird.observe.validation.validators;

/*-
 * #%L
 * ObServe Toolkit :: Common Validation
 * %%
 * Copyright (C) 2017 - 2020 IRD, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;
import io.ultreia.java4all.jaxx.JaxxWidgetsTemperatureI18nEnumHelper;
import org.nuiton.jaxx.widgets.temperature.TemperatureEditorModel;
import org.nuiton.jaxx.widgets.temperature.TemperatureFormat;

import java.util.LinkedHashMap;
import java.util.Map;

import static io.ultreia.java4all.i18n.I18n.n;

/**
 * To validate that the temperature is in correct bound.
 * 

* Created by tchemit on 22/09/2018. * * @author Tony Chemit - [email protected] */ @SuppressWarnings("unused") public class TemperatureBoundFieldValidator extends FieldValidatorSupport { /** * Default temperature format (min and max are based on this format). */ private TemperatureFormat defaultTemperatureFormat; /** * Min temperature. */ private Float min; /** * Max temperature. */ private Float max; private Map temperatureResults = new LinkedHashMap<>(); public void setDefaultTemperatureFormat(String defaultTemperatureFormat) { this.defaultTemperatureFormat = TemperatureFormat.valueOf(defaultTemperatureFormat); } public void setMin(String min) { this.min = Float.valueOf(min); } public void setMax(String max) { this.max = Float.valueOf(max); } public Map getTemperatureResults() { return temperatureResults; } @Override public void validate(Object object) throws ValidationException { if (min == null) { throw new ValidationException("No parameter 'min' filled"); } if (max == null) { throw new ValidationException("No parameter 'max' filled"); } if (min >= max) { throw new ValidationException(String.format("No parameter 'min' (%s) is greater than 'max' (%s)", min, max)); } String fieldName = getFieldName(); if (fieldName == null) { throw new ValidationException("No parameter 'fieldName' filled"); } Float temperature = (Float) getFieldValue(fieldName, object); if (temperature == null) { return; } temperatureResults.clear(); @SuppressWarnings("unchecked") Map map = (Map) getFieldValue("temperatureEditorModels", object); TemperatureFormat temperatureFormat = defaultTemperatureFormat; if (map != null) { TemperatureEditorModel temperatureEditorModel = map.get(fieldName); //FIXME une validation est lancé dans l'ui alors que l'on ne devrait pas et on a pas ce composant alors if (temperatureEditorModel != null) { temperatureFormat = temperatureEditorModel.getFormat(); } } if (temperature < min || temperature > max) { float min; float max; if (temperatureFormat.equals(defaultTemperatureFormat)) { min = this.min; max = this.max; } else { min = defaultTemperatureFormat.convert(this.min, temperatureFormat); max = defaultTemperatureFormat.convert(this.max, temperatureFormat); } String fieldBoundName = fieldName + "Bound"; temperatureResults.put(fieldBoundName, new TemperatureResult(min, max, JaxxWidgetsTemperatureI18nEnumHelper.getLabel(temperatureFormat))); if (getDefaultMessage().isEmpty()) { String defaultMessage = String.format("%1$s##${%2$s.min}##${%2$s.max}##${%2$s.format}", n("observe.validation.temperature.bound"), "temperatureResults." + fieldBoundName); setDefaultMessage(defaultMessage); } ValidatorUtil.addFieldError(this, stack, getFieldName(), object); } } @Override public String getValidatorType() { return "temperatureBound"; } static class TemperatureResult { private final float min; private final float max; private final String format; TemperatureResult(float min, float max, String format) { this.min = min; this.max = max; this.format = format; } public float getMin() { return min; } public float getMax() { return max; } public String getFormat() { return format; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy