UNPKG

3.52 kBJavaScriptView Raw
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15'use strict';
16
17const Identifiable = require('./identifiable');
18const ModelUtil = require('../modelutil');
19const ResourceId = require('./resourceid');
20
21/**
22 * A Relationship is a typed pointer to an instance. I.e the relationship
23 * with namespace = 'org.example', type = 'Vehicle' and id = 'ABC' creates
24 * a pointer that points at an instance of org.example.Vehicle with the id
25 * ABC.
26 *
27 * Applications should retrieve instances from {@link Factory}
28 *
29 * @extends Identifiable
30 * @see See {@link Identifiable}
31 * @class
32 * @memberof module:concerto-core
33 */
34class Relationship extends Identifiable {
35 /**
36 * Create an asset. Use the Factory to create instances.
37 * <p>
38 * <strong>Note: Only to be called by framework code. Applications should
39 * retrieve instances from {@link Factory}</strong>
40 * </p>
41 *
42 * @param {ModelManager} modelManager - The ModelManager for this instance
43 * @param {ClassDeclaration} classDeclaration - The class declaration for this instance.
44 * @param {string} ns - The namespace this instance.
45 * @param {string} type - The type this instance.
46 * @param {string} id - The identifier of this instance.
47 * @private
48 */
49 constructor(modelManager, classDeclaration, ns, type, id) {
50 super(modelManager, classDeclaration, ns, type, id);
51 // we use this metatag to identify the instance as a relationship
52 this.$class = 'Relationship';
53 }
54
55 /**
56 * Returns the string representation of this class
57 * @return {String} the string representation of the class
58 */
59 toString() {
60 return 'Relationship {id=' + this.getFullyQualifiedIdentifier() + '}';
61 }
62
63 /**
64 * Determine if this identifiable is a relationship.
65 * @return {boolean} True if this identifiable is a relationship,
66 * false if not.
67 */
68 isRelationship() {
69 return true;
70 }
71
72 /**
73 * Contructs a Relationship instance from a URI representation (created using toURI).
74 * @param {ModelManager} modelManager - the model manager to bind the relationship to
75 * @param {String} uriAsString - the URI as a string, generated using Identifiable.toURI()
76 * @param {String} [defaultNamespace] - default namespace to use for backwards compatability (optional)
77 * @param {String} [defaultType] - default type to use for backwards compatability (optional)
78 * @return {Relationship} the relationship
79 */
80 static fromURI(modelManager, uriAsString, defaultNamespace, defaultType) {
81 const resourceId = ResourceId.fromURI(uriAsString, defaultNamespace, defaultType);
82 let fqt = ModelUtil.getFullyQualifiedName(resourceId.namespace, resourceId.type);
83 let classDeclaration = modelManager.getType(fqt);
84 let relationship = new Relationship(modelManager, classDeclaration, resourceId.namespace, resourceId.type, resourceId.id);
85 return relationship;
86 }
87}
88
89module.exports = Relationship;