1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.schemagen.json.impl;
7   
8   import com.fasterxml.jackson.databind.node.ObjectNode;
9   
10  import gov.nist.secauto.metaschema.core.model.IFlagDefinition;
11  import gov.nist.secauto.metaschema.core.model.IFlagInstance;
12  import gov.nist.secauto.metaschema.schemagen.json.IDataTypeJsonSchema;
13  import gov.nist.secauto.metaschema.schemagen.json.IDefineableJsonSchema.IKey;
14  import gov.nist.secauto.metaschema.schemagen.json.IDefinitionJsonSchema;
15  import gov.nist.secauto.metaschema.schemagen.json.IJsonGenerationState;
16  
17  import java.util.Map;
18  
19  import edu.umd.cs.findbugs.annotations.NonNull;
20  
21  public class FlagInstanceJsonProperty
22      extends AbstractNamedInstanceJsonProperty<IFlagInstance> {
23  
24    public FlagInstanceJsonProperty(@NonNull IFlagInstance instance) {
25      super(instance);
26  
27    }
28  
29    @Override
30    public boolean isRequired() {
31      return getInstance().isRequired();
32    }
33  
34    @Override
35    protected void generateMetadata(ObjectNode obj) {
36      IFlagInstance instance = getInstance();
37      MetadataUtils.generateTitle(instance, obj);
38      MetadataUtils.generateDescription(instance, obj);
39      MetadataUtils.generateDefault(instance, obj);
40    }
41  
42    @Override
43    public void gatherDefinitions(
44        @NonNull Map<IKey, IDefinitionJsonSchema<?>> gatheredDefinitions,
45        @NonNull IJsonGenerationState state) {
46      // ensure data type use is registered
47      IDataTypeJsonSchema dataTypeSchema = state.getDataTypeSchemaForDefinition(getInstance().getDefinition());
48      if (dataTypeSchema instanceof IDefinitionJsonSchema) {
49        // this is an extension schema. Use a definition if the restricted definition is
50        // a definition
51        ((IDefinitionJsonSchema<?>) dataTypeSchema).gatherDefinitions(gatheredDefinitions, state);
52      }
53  
54      IFlagDefinition definition = getInstance().getDefinition();
55      if (!state.isInline(definition)) {
56        state.getSchema(IKey.of(definition)).gatherDefinitions(gatheredDefinitions, state);
57      }
58    }
59  
60    @Override
61    protected void generateBody(
62        ObjectNode obj,
63        IJsonGenerationState state) {
64      IFlagInstance instance = getInstance();
65      IFlagDefinition definition = instance.getDefinition();
66      IDataTypeJsonSchema dataTypeSchema = state.getDataTypeSchemaForDefinition(definition);
67      dataTypeSchema.generateSchemaOrRef(obj, state);
68    }
69  }