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 * as _ from "lodash";
23 import {SchemaAttributeGroupModel, SchemaAttribute} from "./schema-attribute";
24 import {SchemaPropertyGroupModel, SchemaProperty} from "./aschema-property";
26 export class AttributesGroup {
27 constructor(attributesObj?:AttributesGroup) {
28 _.forEach(attributesObj, (attributes:Array<AttributeModel>, instance) => {
30 _.forEach(attributes, (attribute:AttributeModel):void => {
31 attribute.resourceInstanceUniqueId = instance;
32 attribute.readonly = true;
33 this[instance].push(new AttributeModel(attribute));
39 export interface IAttributeModel {
47 schema:SchemaAttributeGroupModel;
50 parentUniqueId:string;
52 resourceInstanceUniqueId:string;
54 valueUniqueUid:string;
57 export class AttributeModel implements IAttributeModel {
65 schema:SchemaAttributeGroupModel;
68 parentUniqueId:string;
70 resourceInstanceUniqueId:string;
72 valueUniqueUid:string;
74 constructor(attribute?:AttributeModel) {
76 this.uniqueId = attribute.uniqueId;
77 this.name = attribute.name;
78 this._default = attribute._default;
79 this.description = attribute.description;
80 this.type = attribute.type;
81 this.status = attribute.status;
82 this.schema = attribute.schema;
83 this.value = attribute.value;
84 this.parentUniqueId = attribute.parentUniqueId;
85 this.resourceInstanceUniqueId = attribute.resourceInstanceUniqueId;
86 this.readonly = attribute.readonly;
87 this.valueUniqueUid = attribute.valueUniqueUid;
92 if (!this.schema || !this.schema.property) {
93 this.schema = new SchemaPropertyGroupModel(new SchemaProperty());
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));
99 this.convertValueToView();
102 public convertToServerObject():string {
103 if (this._default && this.type === 'map') {
104 this._default = '{' + this._default + '}';
106 if (this._default && this.type === 'list') {
107 this._default = '[' + this._default + ']';
109 this._default = this._default != "" && this._default != "[]" && this._default != "{}" ? this._default : null;
111 return JSON.stringify(this);
115 public convertValueToView() {
116 //unwrapping value {} or [] if type is complex
117 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);
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);
131 public toJSON = ():any => {
132 if (!this.resourceInstanceUniqueId) {
133 this.value = undefined;
135 this.readonly = undefined;
136 this.resourceInstanceUniqueId = undefined;