blob: 80af540ef375599c83f81f6004e226ce1c5148ac [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;
44 defaultValue:string;
45 description:string;
46 type:string;
47 schema:SchemaAttributeGroupModel;
48 status:string;
49 value:string;
50 hidden:boolean;
51 parentUniqueId:string;
52 //custom data
53 resourceInstanceUniqueId:string;
54 readonly:boolean;
55 valueUniqueUid:string;
56}
57
58export class AttributeModel implements IAttributeModel {
59
60 //server data
61 uniqueId:string;
62 name:string;
63 defaultValue:string;
64 description:string;
65 type:string;
66 schema:SchemaAttributeGroupModel;
67 status:string;
68 value:string;
69 hidden:boolean;
70 parentUniqueId:string;
71 //custom data
72 resourceInstanceUniqueId:string;
73 readonly:boolean;
74 valueUniqueUid:string;
75
76 constructor(attribute?:AttributeModel) {
77 if (attribute) {
78 this.uniqueId = attribute.uniqueId;
79 this.name = attribute.name;
80 this.defaultValue = attribute.defaultValue;
81 this.description = attribute.description;
82 this.type = attribute.type;
83 this.status = attribute.status;
84 this.schema = attribute.schema;
85 this.value = attribute.value;
86 this.hidden = attribute.hidden;
87 this.parentUniqueId = attribute.parentUniqueId;
88 this.resourceInstanceUniqueId = attribute.resourceInstanceUniqueId;
89 this.readonly = attribute.readonly;
90 this.valueUniqueUid = attribute.valueUniqueUid;
ys969316a9fce2020-01-19 13:50:02 +020091 } else {
92 this.defaultValue = '';
93 this.hidden = false;
Michael Landoed64b5e2017-06-09 03:19:04 +030094 }
95
96 if (!this.schema || !this.schema.property) {
97 this.schema = new SchemaPropertyGroupModel(new SchemaProperty());
98 } else {
99 //forcing creating new object, so editing different one than the object in the table
100 this.schema = new SchemaAttributeGroupModel(new SchemaAttribute(this.schema.property));
101 }
102
103 this.convertValueToView();
104 }
105
ys969316a9fce2020-01-19 13:50:02 +0200106 public convertToServerObject():string {
Michael Landoed64b5e2017-06-09 03:19:04 +0300107 if (this.defaultValue && this.type === 'map') {
108 this.defaultValue = '{' + this.defaultValue + '}';
109 }
110 if (this.defaultValue && this.type === 'list') {
111 this.defaultValue = '[' + this.defaultValue + ']';
112 }
113 this.defaultValue = this.defaultValue != "" && this.defaultValue != "[]" && this.defaultValue != "{}" ? this.defaultValue : null;
114
115 return JSON.stringify(this);
116 };
117
118
119 public convertValueToView() {
120 //unwrapping value {} or [] if type is complex
121 if (this.defaultValue && (this.type === 'map' || this.type === 'list') &&
122 ['[', '{'].indexOf(this.defaultValue.charAt(0)) > -1 &&
123 [']', '}'].indexOf(this.defaultValue.slice(-1)) > -1) {
124 this.defaultValue = this.defaultValue.slice(1, -1);
125 }
126
127 //also for value - for the modal in canvas
128 if (this.value && (this.type === 'map' || this.type === 'list') &&
129 ['[', '{'].indexOf(this.value.charAt(0)) > -1 &&
130 [']', '}'].indexOf(this.value.slice(-1)) > -1) {
131 this.value = this.value.slice(1, -1);
132 }
133 }
134
135 public toJSON = ():any => {
136 if (!this.resourceInstanceUniqueId) {
137 this.value = undefined;
138 }
139 this.readonly = undefined;
140 this.resourceInstanceUniqueId = undefined;
141 return this;
142 };
143}