blob: ae5eac02818b04b19894e02c44c45aeddff644e8 [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";
vasraz26e50292021-02-16 17:37:57 +000023import {SchemaAttribute, SchemaAttributeGroupModel} from "./schema-attribute";
24import {AttributeOutputDetail} from "app/models/attributes-outputs/attribute-output-detail";
25import {AttributeBEModel} from "app/models/attributes-outputs/attribute-be-model";
Michael Landoed64b5e2017-06-09 03:19:04 +030026
27export class AttributesGroup {
vasraz26e50292021-02-16 17:37:57 +000028 constructor(attributesObj?: AttributesGroup) {
29 _.forEach(attributesObj, (attributes: Array<AttributeModel>, instance) => {
30 this[instance] = [];
31 _.forEach(attributes, (attribute: AttributeModel): void => {
32 attribute.resourceInstanceUniqueId = instance;
33 attribute.readonly = true;
34 this[instance].push(new AttributeModel(attribute));
35 });
36 });
37 }
Michael Landoed64b5e2017-06-09 03:19:04 +030038}
39
40export interface IAttributeModel {
41
vasraz26e50292021-02-16 17:37:57 +000042 //server data
43 uniqueId: string;
44 name: string;
45 _default: string;
46 description: string;
47 type: string;
48 schema: SchemaAttributeGroupModel;
49 status: string;
50 value: string;
51 parentUniqueId: string;
52 //custom data
53 resourceInstanceUniqueId: string;
54 readonly: boolean;
55 valueUniqueUid: string;
Michael Landoed64b5e2017-06-09 03:19:04 +030056}
57
vasraz26e50292021-02-16 17:37:57 +000058export class AttributeModel extends AttributeBEModel implements IAttributeModel {
Michael Landoed64b5e2017-06-09 03:19:04 +030059
vasraz26e50292021-02-16 17:37:57 +000060 //server data
61 uniqueId: string;
62 name: string;
63 _default: string;
64 description: string;
65 type: string;
66 schema: SchemaAttributeGroupModel;
67 status: string;
68 value: string;
69 parentUniqueId: string;
70 //custom data
71 resourceInstanceUniqueId: string;
72 readonly: boolean;
73 valueUniqueUid: string;
Michael Landoed64b5e2017-06-09 03:19:04 +030074
vasraz26e50292021-02-16 17:37:57 +000075 getOutputValues: AttributeOutputDetail[];
76 subAttributeOutputPath: string;
77 outputPath: string;
Michael Landoed64b5e2017-06-09 03:19:04 +030078
vasraz26e50292021-02-16 17:37:57 +000079 constructor(attribute?: AttributeModel) {
80 super(attribute);
81 if (attribute) {
82 this.uniqueId = attribute.uniqueId;
83 this.name = attribute.name;
84 this._default = attribute._default;
85 this.description = attribute.description;
86 this.type = attribute.type;
87 this.status = attribute.status;
88 this.schema = attribute.schema;
89 this.value = attribute.value;
90 this.parentUniqueId = attribute.parentUniqueId;
91 this.resourceInstanceUniqueId = attribute.resourceInstanceUniqueId;
92 this.readonly = attribute.readonly;
93 this.valueUniqueUid = attribute.valueUniqueUid;
Michael Landoed64b5e2017-06-09 03:19:04 +030094
vasraz26e50292021-02-16 17:37:57 +000095 this.getOutputValues = attribute.getOutputValues;
96 this.subAttributeOutputPath = attribute.subAttributeOutputPath;
97 this.outputPath = attribute.outputPath;
98 } else {
99 this._default = '';
Michael Landoed64b5e2017-06-09 03:19:04 +0300100 }
101
vasraz26e50292021-02-16 17:37:57 +0000102 if (!this.schema || !this.schema.property) {
103 this.schema = new SchemaAttributeGroupModel(new SchemaAttribute());
104 } else {
105 //forcing creating new object, so editing different one than the object in the table
106 this.schema = new SchemaAttributeGroupModel(new SchemaAttribute(this.schema.property));
Michael Landoed64b5e2017-06-09 03:19:04 +0300107 }
108
vasraz26e50292021-02-16 17:37:57 +0000109 this.convertValueToView();
110 }
111
112 public convertToServerObject(): string {
113 if (this._default && this.type === 'map') {
114 this._default = '{' + this._default + '}';
115 }
116 if (this._default && this.type === 'list') {
117 this._default = '[' + this._default + ']';
118 }
119 this._default = this._default != "" && this._default != "[]" && this._default != "{}" ? this._default : null;
120
121 return JSON.stringify(this);
122 }
123
124
125 public convertValueToView() {
126 //unwrapping value {} or [] if type is complex
127 if (this._default && (this.type === 'map' || this.type === 'list') &&
128 ['[', '{'].indexOf(this._default.charAt(0)) > -1 &&
129 [']', '}'].indexOf(this._default.slice(-1)) > -1) {
130 this._default = this._default.slice(1, -1);
131 }
132
133 //also for value - for the modal in canvas
134 if (this.value && (this.type === 'map' || this.type === 'list') &&
135 ['[', '{'].indexOf(this.value.charAt(0)) > -1 &&
136 [']', '}'].indexOf(this.value.slice(-1)) > -1) {
137 this.value = this.value.slice(1, -1);
138 }
139 }
140
141 public toJSON = (): any => {
142 if (!this.resourceInstanceUniqueId) {
143 this.value = undefined;
144 }
145 this.readonly = undefined;
146 this.resourceInstanceUniqueId = undefined;
147 return this;
148 };
Michael Landoed64b5e2017-06-09 03:19:04 +0300149}