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

cdc.mf.checks.atts.cardinality.AbstractCardinalityMustBeValid Maven / Gradle / Ivy

The newest version!
package cdc.mf.checks.atts.cardinality;

import cdc.issues.IssueSeverity;
import cdc.issues.checks.CheckContext;
import cdc.issues.checks.CheckResult;
import cdc.issues.checks.SnapshotManager;
import cdc.issues.locations.Location;
import cdc.issues.rules.Rule;
import cdc.issues.rules.RuleDescription;
import cdc.mf.checks.IssueDescription;
import cdc.mf.checks.MfAbstractRuleChecker;
import cdc.mf.model.MfCardinality;
import cdc.mf.model.MfCardinalityItem;

/**
 * Check that if an {@link MfCardinalityItem} has a well formed cardinality, it is valid.
 *
 * @author Damien Carbonne
 */
public abstract class AbstractCardinalityMustBeValid extends MfAbstractRuleChecker {
    public static final IssueSeverity SEVERITY = IssueSeverity.MAJOR;

    protected static String describe() {
        return RuleDescription.format("All well formed {%wrap} must be valid.",
                                      N_CARDINALITIES);
    }

    protected AbstractCardinalityMustBeValid(SnapshotManager manager,
                                             Rule rule) {
        super(manager,
              MfCardinalityItem.class,
              rule);
    }

    @Override
    protected final String getHeader(MfCardinalityItem object) {
        return getTheCardinalityOfHeader(object);
    }

    @Override
    public CheckResult check(CheckContext context,
                             MfCardinalityItem object,
                             Location location) {
        final MfCardinality cardinality = object.getCardinality();
        if (cardinality != null
                && !cardinality.isMalformed()
                && !cardinality.isValid()) {
            final IssueDescription.Builder description = IssueDescription.builder();

            // TODO better explanation
            description.header(getHeader(object))
                       .value(cardinality.getText())
                       .violation("is invalid")
                       .justifications("min..max or minmax",
                                       "0 <= min < Infinity",
                                       "min <= max <= Infinity",
                                       "0 < max",
                                       "Infinity = *",
                                       "1 = 1..1",
                                       "n = n..n",
                                       "* = O..*");

            add(issue().description(description)
                       .location(location)
                       .build());
            return CheckResult.FAILURE;
        } else {
            return CheckResult.SUCCESS;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy