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.INamedModelInstanceAbsolute;
11  import gov.nist.secauto.metaschema.schemagen.json.IDefineableJsonSchema.IKey;
12  import gov.nist.secauto.metaschema.schemagen.json.IDefinitionJsonSchema;
13  import gov.nist.secauto.metaschema.schemagen.json.IJsonGenerationState;
14  import gov.nist.secauto.metaschema.schemagen.json.impl.builder.IModelInstanceBuilder;
15  
16  import java.util.Map;
17  
18  import edu.umd.cs.findbugs.annotations.NonNull;
19  
20  public class NamedModelInstanceJsonProperty
21      extends AbstractNamedInstanceJsonProperty<INamedModelInstanceAbsolute>
22      implements IGroupableModelInstanceJsonProperty<INamedModelInstanceAbsolute> {
23  
24    private final IModelInstanceBuilder<?> collectionBuilder;
25  
26    public NamedModelInstanceJsonProperty(
27        @NonNull INamedModelInstanceAbsolute instance,
28        @NonNull IJsonGenerationState state) {
29      super(instance);
30      this.collectionBuilder = IModelInstanceBuilder.newCollectionBuilder(instance);
31      this.collectionBuilder.addItemType(instance);
32  
33      // // register definition
34      // // REFACTOR: handle discriminator?
35      // state.getSchema(IKey.of(instance.getDefinition(),
36      // instance.getJsonKeyFlagName(), null, null));
37    }
38  
39    protected IModelInstanceBuilder<?> getCollectionBuilder() {
40      return collectionBuilder;
41    }
42  
43    // @Override
44    // public void gatherDefinitions(SortedSet<IDefineableJsonSchema> schemaMap,
45    // IJsonGenerationState state) {
46    // for (IModelInstanceBuilder.IType type : collectionBuilder.getTypes()) {
47    // IJsonSchema schema = type.getJsonSchema(state);
48    // schema.gatherDefinitions(schemaMap, state);
49    // }
50    // }
51  
52    @Override
53    protected void generateBody(
54        ObjectNode obj,
55        IJsonGenerationState state) {
56      getCollectionBuilder().build(
57          obj,
58          state);
59    }
60  
61    @Override
62    protected void generateMetadata(ObjectNode obj) {
63      INamedModelInstanceAbsolute instance = getInstance();
64      MetadataUtils.generateTitle(instance, obj);
65      MetadataUtils.generateDescription(instance, obj);
66    }
67  
68    @Override
69    public void gatherDefinitions(
70        @NonNull Map<IKey, IDefinitionJsonSchema<?>> gatheredDefinitions,
71        @NonNull IJsonGenerationState state) {
72      for (IModelInstanceBuilder.IType type : collectionBuilder.getTypes()) {
73        type.gatherDefinitions(gatheredDefinitions, state);
74      }
75    }
76  }