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

fr.ird.observe.validation.ValidatorsMap Maven / Gradle / Ivy

There is a newer version: 4.34
Show newest version
/*
 * #%L
 * ObServe Toolkit :: Common Validation
 * %%
 * Copyright (C) 2017 - 2019 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%
 */
package fr.ird.observe.validation;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import fr.ird.observe.dto.data.DataDto;
import fr.ird.observe.dto.referential.ReferentialDto;
import fr.ird.observe.spi.DtoModelHelper;
import org.nuiton.validator.NuitonValidatorScope;
import org.nuiton.validator.bean.simple.SimpleBeanValidator;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Un dictionnaire de validateurs ordonnees par le type de leur bean.
 *
 * @author Tony Chemit - [email protected]
 * @since 2.1
 */
public class ValidatorsMap {

    private final Map, SimpleBeanValidator> delegate;

    private ValidatorsMap() {
        delegate = new HashMap<>();
    }

    public static Builder forData(String context, Iterable scopes) {
        return new Builder(context, true, scopes);
    }

    public static Builder forReferential(String context, Iterable scopes) {
        return new Builder(context, false, scopes);
    }

    public NuitonValidatorScope[] getScopes() {
        return delegate.values().stream().flatMap(s -> s.getScopes().stream()).distinct().toArray(NuitonValidatorScope[]::new);
    }

    public  SimpleBeanValidator getValidator(Class klass) {
        //noinspection unchecked
        return (SimpleBeanValidator) get(klass);
    }

    public int size() {
        return delegate.size();
    }

    public boolean isEmpty() {
        return delegate.isEmpty();
    }

    public boolean containsKey(Class key) {
        return delegate.containsKey(key);
    }

    public SimpleBeanValidator get(Class key) {
        return delegate.get(key);
    }

    public SimpleBeanValidator put(Class key, SimpleBeanValidator value) {
        return delegate.put(key, value);
    }

    public Collection> values() {
        return delegate.values();
    }

    public Set> keySet() {
        return delegate.keySet();
    }

    public static class Builder {

        private final String context;
        private final NuitonValidatorScope[] scopes;
        private final boolean data;

        private Builder(String context, boolean data, Iterable scopes) {
            this.context = context;
            this.data = data;
            this.scopes = Iterables.toArray(scopes, NuitonValidatorScope.class);
        }

        public ValidatorsMap build() {
            ValidatorsMap result = new ValidatorsMap();
            for (Class type : getDtoTypes()) {
                SimpleBeanValidator validator = getValidator(type);
                if (validator == null) {
                    continue;
                }
                result.put(type, validator);
            }
            return result;
        }

        private ImmutableSet> getDtoTypes() {
            ImmutableSet.Builder> typesBuilder = ImmutableSet.builder();
            if (data) {

                for (Class type : DtoModelHelper.getMainDataClasses()) {
                    typesBuilder.add(type);
                }
            } else {
                for (Class type : DtoModelHelper.getReferentialClasses()) {
                    typesBuilder.add(type);
                }
            }
            return typesBuilder.build();
        }

        private  SimpleBeanValidator getValidator(Class type) {

            SimpleBeanValidator validator = SimpleBeanValidator.newValidator(type, context, scopes);
            Set effectiveScopes = validator.getEffectiveScopes();
            if (effectiveScopes.isEmpty()) {

                validator = null;
            }
            return validator;

        }
    }
}