2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 import { AttributeBEModel } from 'app/models/attributes-outputs/attribute-be-model';
23 import { AttributeOutputDetail } from 'app/models/attributes-outputs/attribute-output-detail';
24 import * as _ from 'lodash';
25 import { SchemaAttribute, SchemaAttributeGroupModel } from './schema-attribute';
27 export class AttributesGroup {
28 constructor(attributesObj?: AttributesGroup) {
29 _.forEach(attributesObj, (attributes: AttributeModel[], instance) => {
31 _.forEach(attributes, (attribute: AttributeModel): void => {
32 attribute.resourceInstanceUniqueId = instance;
33 attribute.readonly = true;
34 this[instance].push(new AttributeModel(attribute));
40 export interface IAttributeModel {
48 schema: SchemaAttributeGroupModel;
51 parentUniqueId: string;
53 resourceInstanceUniqueId: string;
55 valueUniqueUid: string;
58 export class AttributeModel extends AttributeBEModel implements IAttributeModel {
66 schema: SchemaAttributeGroupModel;
69 parentUniqueId: string;
71 resourceInstanceUniqueId: string;
73 valueUniqueUid: string;
75 getOutputValues: AttributeOutputDetail[];
76 subAttributeOutputPath: string;
79 constructor(attribute?: AttributeModel) {
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;
95 this.getOutputValues = attribute.getOutputValues;
96 this.subAttributeOutputPath = attribute.subAttributeOutputPath;
97 this.outputPath = attribute.outputPath;
102 if (!this.schema || !this.schema.property) {
103 this.schema = new SchemaAttributeGroupModel(new SchemaAttribute());
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));
110 public convertToServerObject(): string {
111 if (this._default && this.type === 'map') {
112 this._default = '{' + this._default + '}';
114 if (this._default && this.type === 'list') {
115 this._default = '[' + this._default + ']';
117 this._default = this._default != '' && this._default != '[]' && this._default != '{}' ? this._default : null;
119 return JSON.stringify(this);
122 public convertValueToView() {
123 // unwrapping value {} or [] if type is complex
124 if (this._default && (this.type === 'map' || this.type === 'list') &&
125 ['[', '{'].indexOf(this._default.charAt(0)) > -1 &&
126 [']', '}'].indexOf(this._default.slice(-1)) > -1) {
127 this._default = this._default.slice(1, -1);
130 // also for value - for the modal in canvas
131 if (this.value && (this.type === 'map' || this.type === 'list') &&
132 ['[', '{'].indexOf(this.value.charAt(0)) > -1 &&
133 [']', '}'].indexOf(this.value.slice(-1)) > -1) {
134 this.value = this.value.slice(1, -1);
138 public toJSON = (): any => {
139 if (!this.resourceInstanceUniqueId) {
140 this.value = undefined;
142 this.readonly = undefined;
143 this.resourceInstanceUniqueId = undefined;