001/*
002 * Copyright 2018 Leibniz-Institut für Analytische Wissenschaften – ISAS – e.V..
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 de.isas.lipidomics.mztab2.validation.validators;
017
018import de.isas.lipidomics.mztab2.validation.constraints.CheckParameter;
019import de.isas.mztab2.model.Parameter;
020import java.util.Optional;
021import javax.validation.ConstraintValidator;
022import javax.validation.ConstraintValidatorContext;
023
024/**
025 * Base interface for parameter validation with default validation method.
026 *
027 * @author nilshoffmann
028 */
029public interface ParameterValidator extends ConstraintValidator<CheckParameter, Parameter> {
030
031    @Override
032    default boolean isValid(Parameter parameter,
033            ConstraintValidatorContext context) {
034        Optional<Parameter> optional = Optional.ofNullable(parameter);
035        if (optional.isPresent()) {
036            Parameter p = optional.get();
037            // a cv parameter must have non-null cvLabel, non-null cvAccession and non-null name and optionally a value
038            // a user parameter must have null cvVlabel, null cvAccession and non-null name and optionally a value
039            if ((p.getCvLabel() != null && !p.getCvLabel().isEmpty()) && (p.getCvAccession() != null && !p.getCvAccession().isEmpty()) && (p.getName() != null && !p.getName().isEmpty())) {
040                return true;
041            } else if ((p.getCvLabel() == null || p.getCvLabel().isEmpty()) && (p.getCvAccession() == null || p.getCvAccession().isEmpty()) && (p.getName() != null && !p.getName().isEmpty())) {
042                return true;
043            }
044            return false;
045        }
046        //parameter may be optional
047        return true;
048
049    }
050}