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 Field = require('../introspect/field');
|
18 | const Moment = require('moment-mini');
|
19 |
|
20 | /**
|
21 | * Object is an instance with a namespace and a type.
|
22 | *
|
23 | * This class is abstract.
|
24 | * @abstract
|
25 | * @class
|
26 | * @memberof module:concerto-core
|
27 | */
|
28 | class Typed {
|
29 | /**
|
30 | * Create an instance.
|
31 | * <p>
|
32 | * <strong>Note: Only to be called by framework code. Applications should
|
33 | * retrieve instances from {@link Factory}</strong>
|
34 | * </p>
|
35 | *
|
36 | * @param {ModelManager} modelManager - The ModelManager for this instance
|
37 | * @param {ClassDeclaration} classDeclaration - The class declaration for this instance.
|
38 | * @param {string} ns - The namespace this instance.
|
39 | * @param {string} type - The type this instance.
|
40 | * @private
|
41 | */
|
42 | constructor(modelManager, classDeclaration, ns, type) {
|
43 | this.$modelManager = modelManager;
|
44 | this.$classDeclaration = classDeclaration;
|
45 | this.$namespace = ns;
|
46 | this.$type = type;
|
47 | }
|
48 |
|
49 | /**
|
50 | * Visitor design pattern
|
51 | * @param {Object} visitor - the visitor
|
52 | * @param {Object} parameters - the parameter
|
53 | * @return {Object} the result of visiting or null
|
54 | * @private
|
55 | */
|
56 | accept(visitor,parameters) {
|
57 | return visitor.visit(this, parameters);
|
58 | }
|
59 |
|
60 | /**
|
61 | * Get the ModelManager for this instance
|
62 | * @return {ModelManager} The ModelManager for this object
|
63 | * @private
|
64 | */
|
65 | getModelManager() {
|
66 | return this.$modelManager;
|
67 | }
|
68 |
|
69 | /**
|
70 | * Get the type of the instance (a short name, not including namespace).
|
71 | * @return {string} The type of this object
|
72 | */
|
73 | getType() {
|
74 | return this.$type;
|
75 | }
|
76 |
|
77 | /**
|
78 | * Get the fully-qualified type name of the instance (including namespace).
|
79 | * @return {string} The fully-qualified type name of this object
|
80 | */
|
81 | getFullyQualifiedType() {
|
82 | return this.$classDeclaration.getFullyQualifiedName();
|
83 | }
|
84 |
|
85 | /**
|
86 | * Get the namespace of the instance.
|
87 | * @return {string} The namespace of this object
|
88 | */
|
89 | getNamespace() {
|
90 | return this.$namespace;
|
91 | }
|
92 |
|
93 | /**
|
94 | * Returns the class declaration for this instance object.
|
95 | *
|
96 | * @return {ClassDeclaration} - the class declaration for this instance
|
97 | * @private
|
98 | */
|
99 | getClassDeclaration() {
|
100 | return this.$classDeclaration;
|
101 | }
|
102 |
|
103 | /**
|
104 | * Sets a property on this Resource
|
105 | * @param {string} propName - the name of the field
|
106 | * @param {string} value - the value of the property
|
107 | */
|
108 | setPropertyValue(propName, value) {
|
109 | this[propName] = value;
|
110 | }
|
111 |
|
112 | /**
|
113 | * Adds a value to an array property on this Resource
|
114 | * @param {string} propName - the name of the field
|
115 | * @param {string} value - the value of the property
|
116 | */
|
117 | addArrayValue(propName, value) {
|
118 | if(this[propName]) {
|
119 | this[propName].push(value);
|
120 | }
|
121 | else {
|
122 | this[propName] = [value];
|
123 | }
|
124 | }
|
125 |
|
126 | /**
|
127 | * Sets the fields to their default values, based on the model
|
128 | * @private
|
129 | */
|
130 | assignFieldDefaults() {
|
131 | let classDeclaration = this.getClassDeclaration();
|
132 | let fields = classDeclaration.getProperties();
|
133 |
|
134 | for (let n = 0; n < fields.length; n++) {
|
135 | let field = fields[n];
|
136 | if (field instanceof Field) {
|
137 | let defaultValue = field.getDefaultValue();
|
138 |
|
139 | if (defaultValue) {
|
140 | if (field.getType() === 'String') {
|
141 | this.setPropertyValue(field.getName(), defaultValue);
|
142 | } else if (field.getType() === 'Integer') {
|
143 | this.setPropertyValue(field.getName(), parseInt(defaultValue));
|
144 | } else if (field.getType() === 'Long') {
|
145 | this.setPropertyValue(field.getName(), parseInt(defaultValue));
|
146 | } else if (field.getType() === 'Double') {
|
147 | this.setPropertyValue(field.getName(), parseFloat(defaultValue));
|
148 | } else if (field.getType() === 'Boolean') {
|
149 | this.setPropertyValue(field.getName(), (defaultValue === 'true'));
|
150 | } else if (field.getType() === 'DateTime') {
|
151 | const dateTime = Moment.parseZone(defaultValue);
|
152 | this.setPropertyValue(field.getName(), dateTime);
|
153 | } else {
|
154 | // following precident set in jsonpopulator.js - if we get this far the field should be an enum
|
155 | this.setPropertyValue(field.getName(), defaultValue);
|
156 | }
|
157 | }
|
158 | }
|
159 | }
|
160 | }
|
161 |
|
162 | /**
|
163 | * Check to see if this instance is an instance of the specified fully qualified
|
164 | * type name.
|
165 | * @param {String} fqt The fully qualified type name.
|
166 | * @returns {boolean} True if this instance is an instance of the specified fully
|
167 | * qualified type name, false otherwise.
|
168 | */
|
169 | instanceOf(fqt) {
|
170 | // Check to see if this is an exact instance of the specified type.
|
171 | const classDeclaration = this.getClassDeclaration();
|
172 | if (classDeclaration.getFullyQualifiedName() === fqt) {
|
173 | return true;
|
174 | }
|
175 | // Now walk the class hierachy looking to see if it's an instance of the specified type.
|
176 | let superTypeDeclaration = classDeclaration.getSuperTypeDeclaration();
|
177 | while (superTypeDeclaration) {
|
178 | if (superTypeDeclaration.getFullyQualifiedName() === fqt) {
|
179 | return true;
|
180 | }
|
181 | superTypeDeclaration = superTypeDeclaration.getSuperTypeDeclaration();
|
182 | }
|
183 | return false;
|
184 | }
|
185 |
|
186 | /**
|
187 | * Overriden to prevent people accidentally converting a resource to JSON
|
188 | * without using the Serializer.
|
189 | * @private
|
190 | */
|
191 | toJSON() {
|
192 | throw new Error('Use Serializer.toJSON to convert resource instances to JSON objects.');
|
193 | }
|
194 | }
|
195 |
|
196 | module.exports = Typed;
|