Add new code new version
[sdc.git] / dox-sequence-diagram-ui / src / main / webapp / lib / ecomp / asdc / sequencer / model / Metamodels.js
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16
17 import Common from '../common/Common';
18 import Metamodel from './Metamodel';
19
20 /**
21  * A simple lookup for schemas by ID.
22  */
23 export default class Metamodels {
24
25   // ///////////////////////////////////////////////////////////////////////////////////////////////
26
27   /**
28    * Construct metamodels from provided JSON definitions.
29    * @param metamodels JSON metamodel definitions.
30    */
31   constructor(metamodels) {
32
33     Common.assertType(metamodels, 'Array');
34
35     this.lookup = {};
36
37     // Save each metamodel. It's up to the Metamodel class to make sense of
38     // potentially nonsense metamodel definitions.
39
40     for (const json of metamodels) {
41       const metamodel = new Metamodel(json);
42       this.lookup[metamodel.getId()] = metamodel;
43     }
44
45     // Set (or override) the default metamodel with the inlined one.
46
47     this.lookup.$ = Metamodel.getDefault();
48     Common.assertInstanceOf(this.lookup.$, Metamodel);
49   }
50
51   // ///////////////////////////////////////////////////////////////////////////////////////////////
52
53   /**
54    * Get Metamodel by its @id.
55    * @param id identifier.
56    * @returns Metamodel, or undefined if no matching metamodel found.
57    */
58   getMetamodel(id) {
59     return this.lookup[id];
60   }
61
62   // ///////////////////////////////////////////////////////////////////////////////////////////////
63
64   /**
65    * Get the default (permissive) metamodel.
66    * @returns default Metamodel.
67    */
68   getDefault() {
69     return this.lookup.$;
70   }
71
72   // ///////////////////////////////////////////////////////////////////////////////////////////////
73
74   /**
75    * Get metamodel by its @id, falling back to the default.
76    * @param id identifier.
77    * @returns matching metamodel, or default.
78    */
79   getMetamodelOrDefault(id) {
80     const metamodel = this.getMetamodel(id);
81     if (metamodel) {
82       return metamodel;
83     }
84     return this.getDefault();
85   }
86
87 }