Fix import VFC with attributes
[sdc.git] / catalog-ui / src / app / models / attributes.ts
1 /*-
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
21 'use strict';
22 import * as _ from "lodash";
23 import {SchemaAttributeGroupModel, SchemaAttribute} from "./schema-attribute";
24 import {SchemaPropertyGroupModel, SchemaProperty} from "./aschema-property";
25
26 export 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
39 export interface IAttributeModel {
40
41     //server data
42     uniqueId:string;
43     name:string;
44     _default:string;
45     description:string;
46     type:string;
47     schema:SchemaAttributeGroupModel;
48     status:string;
49     value:string;
50     parentUniqueId:string;
51     //custom data
52     resourceInstanceUniqueId:string;
53     readonly:boolean;
54     valueUniqueUid:string;
55 }
56
57 export class AttributeModel implements IAttributeModel {
58
59     //server data
60     uniqueId:string;
61     name:string;
62     _default:string;
63     description:string;
64     type:string;
65     schema:SchemaAttributeGroupModel;
66     status:string;
67     value:string;
68     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;
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;
88         } else {
89             this._default = '';
90         }
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
102     public convertToServerObject():string {
103         if (this._default && this.type === 'map') {
104             this._default = '{' + this._default + '}';
105         }
106         if (this._default && this.type === 'list') {
107             this._default = '[' + this._default + ']';
108         }
109         this._default = this._default != "" && this._default != "[]" && this._default != "{}" ? this._default : null;
110
111         return JSON.stringify(this);
112     };
113
114
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);
121         }
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 }