blob: a51358cdc4225d5eeabab4bfaa9b06f268c8a236 [file] [log] [blame]
Michael Landodd603392017-07-12 00:54:52 +03001/*-
2 * ============LICENSE_START=======================================================
3 * SDC
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 */
20
Michael Landoed64b5e2017-06-09 03:19:04 +030021'use strict';
Michael Landoa5445102018-03-04 14:53:33 +020022import * as _ from "lodash";
Michael Landoed64b5e2017-06-09 03:19:04 +030023import {SchemaAttributeGroupModel, SchemaAttribute} from "./schema-attribute";
24import {SchemaPropertyGroupModel, SchemaProperty} from "./aschema-property";
25
26export class AttributesGroup {
27 constructor(attributesObj?:AttributesGroup) {
28 _.forEach(attributesObj, (attributes:Array<AttributeModel>, instance) => {
29 this[instance] = [];
30 _.forEach(attributes, (attribute:AttributeModel):void => {
31 attribute.resourceInstanceUniqueId = instance;
32 attribute.readonly = true;
33 this[instance].push(new AttributeModel(attribute));
34 });
35 });
36 }
37}
38
39export interface IAttributeModel {
40
41 //server data
42 uniqueId:string;
43 name:string;
aribeiro2f74a4a2020-11-17 10:52:41 +000044 _default:string;
Michael Landoed64b5e2017-06-09 03:19:04 +030045 description:string;
46 type:string;
47 schema:SchemaAttributeGroupModel;
48 status:string;
49 value:string;
Michael Landoed64b5e2017-06-09 03:19:04 +030050 parentUniqueId:string;
51 //custom data
52 resourceInstanceUniqueId:string;
53 readonly:boolean;
54 valueUniqueUid:string;
55}
56
57export class AttributeModel implements IAttributeModel {
58
59 //server data
60 uniqueId:string;
61 name:string;
aribeiro2f74a4a2020-11-17 10:52:41 +000062 _default:string;
Michael Landoed64b5e2017-06-09 03:19:04 +030063 description:string;
64 type:string;
65 schema:SchemaAttributeGroupModel;
66 status:string;
67 value:string;
Michael Landoed64b5e2017-06-09 03:19:04 +030068 parentUniqueId:string;
69 //custom data
70 resourceInstanceUniqueId:string;
71 readonly:boolean;
72 valueUniqueUid:string;
73
74 constructor(attribute?:AttributeModel) {
75 if (attribute) {
76 this.uniqueId = attribute.uniqueId;
77 this.name = attribute.name;
aribeiro2f74a4a2020-11-17 10:52:41 +000078 this._default = attribute._default;
Michael Landoed64b5e2017-06-09 03:19:04 +030079 this.description = attribute.description;
80 this.type = attribute.type;
81 this.status = attribute.status;
82 this.schema = attribute.schema;
83 this.value = attribute.value;
Michael Landoed64b5e2017-06-09 03:19:04 +030084 this.parentUniqueId = attribute.parentUniqueId;
85 this.resourceInstanceUniqueId = attribute.resourceInstanceUniqueId;
86 this.readonly = attribute.readonly;
87 this.valueUniqueUid = attribute.valueUniqueUid;
ys969316a9fce2020-01-19 13:50:02 +020088 } else {
aribeiro2f74a4a2020-11-17 10:52:41 +000089 this._default = '';
Michael Landoed64b5e2017-06-09 03:19:04 +030090 }
91
92 if (!this.schema || !this.schema.property) {
93 this.schema = new SchemaPropertyGroupModel(new SchemaProperty());
94 } else {
95 //forcing creating new object, so editing different one than the object in the table
96 this.schema = new SchemaAttributeGroupModel(new SchemaAttribute(this.schema.property));
97 }
98
99 this.convertValueToView();
100 }
101
ys969316a9fce2020-01-19 13:50:02 +0200102 public convertToServerObject():string {
aribeiro2f74a4a2020-11-17 10:52:41 +0000103 if (this._default && this.type === 'map') {
104 this._default = '{' + this._default + '}';
Michael Landoed64b5e2017-06-09 03:19:04 +0300105 }
aribeiro2f74a4a2020-11-17 10:52:41 +0000106 if (this._default && this.type === 'list') {
107 this._default = '[' + this._default + ']';
Michael Landoed64b5e2017-06-09 03:19:04 +0300108 }
aribeiro2f74a4a2020-11-17 10:52:41 +0000109 this._default = this._default != "" && this._default != "[]" && this._default != "{}" ? this._default : null;
Michael Landoed64b5e2017-06-09 03:19:04 +0300110
111 return JSON.stringify(this);
112 };
113
114
115 public convertValueToView() {
116 //unwrapping value {} or [] if type is complex
aribeiro2f74a4a2020-11-17 10:52:41 +0000117 if (this._default && (this.type === 'map' || this.type === 'list') &&
118 ['[', '{'].indexOf(this._default.charAt(0)) > -1 &&
119 [']', '}'].indexOf(this._default.slice(-1)) > -1) {
120 this._default = this._default.slice(1, -1);
Michael Landoed64b5e2017-06-09 03:19:04 +0300121 }
122
123 //also for value - for the modal in canvas
124 if (this.value && (this.type === 'map' || this.type === 'list') &&
125 ['[', '{'].indexOf(this.value.charAt(0)) > -1 &&
126 [']', '}'].indexOf(this.value.slice(-1)) > -1) {
127 this.value = this.value.slice(1, -1);
128 }
129 }
130
131 public toJSON = ():any => {
132 if (!this.resourceInstanceUniqueId) {
133 this.value = undefined;
134 }
135 this.readonly = undefined;
136 this.resourceInstanceUniqueId = undefined;
137 return this;
138 };
139}