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 | ;
|
16 |
|
17 | const ResourceId = require('./resourceid');
|
18 | const Typed = require('./typed');
|
19 |
|
20 | /**
|
21 | * Identifiable is an entity with a namespace, type and an identifier.
|
22 | * Applications should retrieve instances from {@link Factory}
|
23 | * This class is abstract.
|
24 | * @extends Typed
|
25 | * @abstract
|
26 | * @class
|
27 | * @memberof module:concerto-core
|
28 | */
|
29 | class Identifiable extends Typed {
|
30 | /**
|
31 | * Create an instance.
|
32 | * <p>
|
33 | * <strong>Note: Only to be called by framework code. Applications should
|
34 | * retrieve instances from {@link Factory}</strong>
|
35 | * </p>
|
36 | *
|
37 | * @param {ModelManager} modelManager - The ModelManager for this instance
|
38 | * @param {ClassDeclaration} classDeclaration - The class declaration for this instance.
|
39 | * @param {string} ns - The namespace this instance.
|
40 | * @param {string} type - The type this instance.
|
41 | * @param {string} id - The identifier of this instance.
|
42 | * @private
|
43 | */
|
44 | constructor(modelManager, classDeclaration, ns, type, id) {
|
45 | super(modelManager, classDeclaration, ns, type);
|
46 | this.$identifier = id;
|
47 | }
|
48 |
|
49 | /**
|
50 | * Get the identifier of this instance
|
51 | * @return {string} The identifier for this object
|
52 | */
|
53 | getIdentifier() {
|
54 | return this.$identifier;
|
55 | }
|
56 |
|
57 | /**
|
58 | * Set the identifier of this instance
|
59 | * @param {string} id - the new identifier for this object
|
60 | */
|
61 | setIdentifier(id) {
|
62 | this.$identifier = id;
|
63 | const modelFile = this.$modelManager.getModelFile(this.getNamespace());
|
64 | const typeDeclaration = modelFile.getType(this.getFullyQualifiedType());
|
65 | const idField = typeDeclaration.getIdentifierFieldName();
|
66 | this[idField] = id;
|
67 | }
|
68 |
|
69 | /**
|
70 | * Get the fully qualified identifier of this instance.
|
71 | * (namespace '.' type '#' identifier).
|
72 | * @return {string} the fully qualified identifier of this instance
|
73 | */
|
74 | getFullyQualifiedIdentifier() {
|
75 | return this.getFullyQualifiedType() + '#' + this.$identifier;
|
76 | }
|
77 |
|
78 | /**
|
79 | * Returns the string representation of this class
|
80 | * @return {String} the string representation of the class
|
81 | */
|
82 | toString() {
|
83 | return 'Identifiable {id=' + this.getFullyQualifiedIdentifier() +'}';
|
84 | }
|
85 |
|
86 | /**
|
87 | * Determine if this identifiable is a relationship.
|
88 | * @return {boolean} True if this identifiable is a relationship,
|
89 | * false if not.
|
90 | */
|
91 | isRelationship() {
|
92 | return false;
|
93 | }
|
94 |
|
95 | /**
|
96 | * Determine if this identifiable is a resource.
|
97 | * @return {boolean} True if this identifiable is a resource,
|
98 | * false if not.
|
99 | */
|
100 | isResource() {
|
101 | return false;
|
102 | }
|
103 |
|
104 | /**
|
105 | * Returns a URI representation of a reference to this identifiable
|
106 | * @return {String} the URI for the identifiable
|
107 | */
|
108 | toURI() {
|
109 | const resourceId = new ResourceId(this.getNamespace(), this.getType(), this.getIdentifier());
|
110 | const result = resourceId.toURI();
|
111 | //console.log( '***** URI for ' + this.toString() + ' is ' + result );
|
112 | return result;
|
113 | }
|
114 | }
|
115 |
|
116 | module.exports = Identifiable;
|