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.mztab2.validation.handlers;
017
018import de.isas.mztab2.cvmapping.CvMappingUtils;
019import de.isas.mztab2.cvmapping.RuleEvaluationResult;
020import de.isas.mztab2.cvmapping.SetOperations;
021import de.isas.mztab2.io.serialization.ParameterConverter;
022import de.isas.mztab2.model.Parameter;
023import de.isas.mztab2.model.ValidationMessage;
024import de.isas.mztab2.validation.CvTermValidationHandler;
025import java.util.ArrayList;
026import java.util.List;
027import java.util.Optional;
028import java.util.Set;
029import org.apache.commons.jxpath.Pointer;
030import org.apache.commons.lang3.tuple.Pair;
031import uk.ac.ebi.pride.jmztab2.utils.errors.CrossCheckErrorType;
032import uk.ac.ebi.pride.jmztab2.utils.errors.MZTabError;
033
034/**
035 * Implements support for validating multiple parameters and the intersection
036 * between terms as described by the rule and the actually found ones.
037 *
038 * @author nilshoffmann
039 */
040public class SharedParametersValidationHandler implements CvTermValidationHandler {
041
042    @Override
043    public List<ValidationMessage> handleParameters(
044            RuleEvaluationResult result,
045            boolean errorOnTermNotInRule) {
046        final List<ValidationMessage> messages = new ArrayList<>();
047        Set<String> sharedParameters = SetOperations.intersection(
048                result.getAllowedParameters().
049                        keySet(), result.getFoundParameters().
050                        keySet());
051        for (String paramKey : sharedParameters) {
052            Pair<Pointer, ? extends Parameter> p = result.getFoundParameters().
053                    get(paramKey);
054            Parameter allowedParameter = result.getAllowedParameters().
055                    get(paramKey);
056            //The cv parameter field "{0}" for parameter "{1}" is "{2}" but should be "{3}", as defined in {4}.
057            if (!Optional.ofNullable(p.getValue().
058                    getCvLabel()).
059                    orElse("").
060                    equals(allowedParameter.getCvLabel())) {
061                MZTabError error = new MZTabError(
062                        CrossCheckErrorType.CvTermMalformed,
063                        -1,
064                        Parameter.Properties.cvLabel.getPropertyName(),
065                        new ParameterConverter().convert(p.
066                                getValue()), p.getValue().
067                                getCvLabel(), allowedParameter.getCvLabel(),
068                        CvMappingUtils.niceToString(result.getRule()));
069
070                messages.add(error.toValidationMessage());
071            }
072            if (!Optional.ofNullable(p.getValue().
073                    getCvAccession()).
074                    orElse("").
075                    equals(allowedParameter.getCvAccession())) {
076                MZTabError error = new MZTabError(
077                        CrossCheckErrorType.CvTermMalformed,
078                        -1,
079                        Parameter.Properties.cvAccession.getPropertyName(),
080                        new ParameterConverter().convert(p.
081                                getValue()), p.getValue().
082                                getCvAccession(), allowedParameter.getCvAccession(),
083                        CvMappingUtils.niceToString(result.getRule()));
084
085                messages.add(error.toValidationMessage());
086            }
087            if (!Optional.ofNullable(p.getValue().
088                    getName()).
089                    orElse("").
090                    equals(allowedParameter.getName())) {
091                MZTabError error = new MZTabError(
092                        CrossCheckErrorType.CvTermMalformed,
093                        -1,
094                        Parameter.Properties.name.getPropertyName(),
095                        new ParameterConverter().convert(p.
096                                getValue()), p.getValue().
097                                getName(), allowedParameter.getName(),
098                        CvMappingUtils.niceToString(result.getRule()));
099
100                messages.add(error.toValidationMessage());
101            }
102        }
103        return messages;
104    }
105
106}