1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.core.model.constraint.impl;
7   
8   import java.util.List;
9   import java.util.Map;
10  import java.util.Set;
11  
12  import dev.metaschema.core.datatype.markup.MarkupLine;
13  import dev.metaschema.core.datatype.markup.MarkupMultiline;
14  import dev.metaschema.core.metapath.IMetapathExpression;
15  import dev.metaschema.core.model.IAttributable;
16  import dev.metaschema.core.model.ISource;
17  import dev.metaschema.core.model.constraint.ConstraintInitializationException;
18  import dev.metaschema.core.model.constraint.IConstraint;
19  import dev.metaschema.core.model.constraint.IIndexHasKeyConstraint;
20  import dev.metaschema.core.model.constraint.IKeyField;
21  import edu.umd.cs.findbugs.annotations.NonNull;
22  import edu.umd.cs.findbugs.annotations.Nullable;
23  
24  /**
25   * Represents a key reference constraint.
26   * <p>
27   * Uses a set of key fields to build a key to match against an index. A Match is
28   * successful if the resulting key matches an entry in the index.
29   */
30  public final class DefaultIndexHasKeyConstraint
31      extends AbstractKeyConstraint
32      implements IIndexHasKeyConstraint {
33    @NonNull
34    private final String indexName;
35  
36    /**
37     * Construct a key reference constraint.
38     *
39     * @param id
40     *          the optional identifier for the constraint
41     * @param formalName
42     *          the constraint's formal indexName or {@code null} if not provided
43     * @param description
44     *          the constraint's semantic description or {@code null} if not
45     *          provided
46     * @param source
47     *          information about the constraint source
48     * @param level
49     *          the significance of a violation of this constraint
50     * @param target
51     *          the Metapath expression identifying the nodes the constraint targets
52     * @param properties
53     *          a collection of associated properties
54     * @param indexName
55     *          a reference to the indexName of the index
56     * @param keyFields
57     *          a list of key fields associated with the constraint
58     * @param message
59     *          an optional message to emit when the constraint is violated
60     * @param remarks
61     *          optional remarks describing the intent of the constraint
62     */
63    @SuppressWarnings("PMD.ExcessiveParameterList")
64    public DefaultIndexHasKeyConstraint(
65        @Nullable String id,
66        @Nullable String formalName,
67        @Nullable MarkupLine description,
68        @NonNull ISource source,
69        @NonNull Level level,
70        @NonNull IMetapathExpression target,
71        @NonNull Map<IAttributable.Key, Set<String>> properties,
72        @NonNull String indexName,
73        @NonNull List<IKeyField> keyFields,
74        @Nullable String message,
75        @Nullable MarkupMultiline remarks) {
76      super(id, formalName, description, source, level, target, properties, keyFields, message, remarks);
77      if (indexName.isBlank()) {
78        throw new ConstraintInitializationException(
79            String.format("The index indexName must be a non-blank string in the constraint %s in '%s'",
80                IConstraint.getConstraintIdentity(this),
81                source.getLocationHint()));
82      }
83      this.indexName = indexName;
84    }
85  
86    @Override
87    public String getIndexName() {
88      return indexName;
89    }
90  
91  }