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.ValidationMessage;
023import de.isas.mztab2.validation.CvTermValidationHandler;
024import info.psidev.cvmapping.CvMappingRule;
025import java.util.ArrayList;
026import java.util.Arrays;
027import java.util.List;
028import java.util.Set;
029import uk.ac.ebi.pride.jmztab2.utils.errors.CrossCheckErrorType;
030import uk.ac.ebi.pride.jmztab2.utils.errors.MZTabError;
031import uk.ac.ebi.pride.jmztab2.utils.errors.MZTabErrorType;
032
033/**
034 * Implements 'AND' logic, meaning all alternative possible terms or term roots for
035 * an object must occur.
036 * @author nilshoffmann
037 */
038public class AndValidationHandler implements CvTermValidationHandler {
039
040    @Override
041    public List<ValidationMessage> handleParameters(RuleEvaluationResult result,
042        boolean errorOnTermNotInRule) {
043        final List<ValidationMessage> messages = new ArrayList<>();
044        // all defined terms or children thereof need to appear
045        Set<String> unmatchedRuleParams = SetOperations.complement(
046            result.getAllowedParameters().
047                keySet(), result.getFoundParameters().
048                keySet());
049        if (!unmatchedRuleParams.isEmpty()) {
050            for (String s : unmatchedRuleParams) {
051                MZTabErrorType errorType = null;
052                switch (result.getRule().
053                    getRequirementLevel()) {
054                    case MAY:
055                        errorType = CrossCheckErrorType.CvTermOptional;
056                        break;
057                    case SHOULD:
058                        errorType = CrossCheckErrorType.CvTermRecommended;
059                        break;
060                    case MUST:
061                        errorType = CrossCheckErrorType.CvTermRequired;
062                        break;
063                    default:
064                        throw new IllegalArgumentException(
065                            "Unknown requirement level value: " + result.
066                                getRule().
067                                getRequirementLevel() + "! Supported are: " + Arrays.
068                                toString(CvMappingRule.RequirementLevel.
069                                    values()));
070                }
071                MZTabError error = new MZTabError(errorType, -1,
072                    new ParameterConverter().convert(result.
073                        getAllowedParameters().
074                        get(
075                            s)), result.getRule().
076                        getCvElementPath(), CvMappingUtils.
077                        niceToString(
078                            result.getRule()));
079                messages.add(error.toValidationMessage());
080            }
081        }
082        return messages;
083    }
084}