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.model.Parameter;
020import de.isas.mztab2.model.ValidationMessage;
021import info.psidev.cvmapping.CvMappingRule;
022import java.util.ArrayList;
023import java.util.Arrays;
024import java.util.List;
025import lombok.extern.slf4j.Slf4j;
026import org.apache.commons.jxpath.Pointer;
027import org.apache.commons.lang3.tuple.Pair;
028import uk.ac.ebi.pride.jmztab2.utils.errors.CrossCheckErrorType;
029import uk.ac.ebi.pride.jmztab2.utils.errors.MZTabError;
030
031/**
032 * Implements handling of rules that have no cv parameter match. Depending on
033 * the requirement level, this may raise an Info, Warning or Error level
034 * message.
035 *
036 * @author nilshoffmann
037 */
038@Slf4j
039public class EmptyRuleHandler {
040
041    public List<ValidationMessage> handleRule(CvMappingRule rule,
042            List<Pair<Pointer, Parameter>> selection) {
043        if (selection.isEmpty()) {
044            log.debug(
045                    "Evaluating rule " + rule.getId() + " on " + rule.
046                    getCvElementPath() + " did not yield any selected elements!");
047            switch (rule.getRequirementLevel()) {
048                case MAY:
049                    return Arrays.asList(new MZTabError(
050                            CrossCheckErrorType.RulePointerObjectNullOptional, -1,
051                            rule.getCvElementPath(), rule.getId(), "optional",
052                            CvMappingUtils.niceToString(rule)).
053                            toValidationMessage());
054                case SHOULD:
055                    return Arrays.asList(new MZTabError(
056                            CrossCheckErrorType.RulePointerObjectNullRecommended, -1,
057                            rule.getCvElementPath(), rule.getId(), "recommended",
058                            CvMappingUtils.niceToString(rule)).
059                            toValidationMessage());
060                case MUST:
061                    //The object "{0}" accessed by {1} is {2}, but was null or empty. Allowed terms are defined in {3}
062                    return Arrays.asList(new MZTabError(
063                            CrossCheckErrorType.RulePointerObjectNullRequired, -1,
064                            rule.getCvElementPath(), rule.getId(), "required",
065                            CvMappingUtils.niceToString(rule)).
066                            toValidationMessage());
067                default:
068                    throw new IllegalArgumentException(
069                            "Unknown requirement level value: " + rule.
070                                    getRequirementLevel() + "! Supported are: " + Arrays.
071                                    toString(CvMappingRule.RequirementLevel.
072                                            values()));
073
074            }
075        } else {
076            return new ArrayList<>();
077        }
078
079    }
080
081}