711e5b3c49077a652eddde85b5f6b4e3c3bc4141
[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 { 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';
26
27 export class AttributesGroup {
28   constructor(attributesObj?: AttributesGroup) {
29     _.forEach(attributesObj, (attributes: 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   }
38 }
39
40 export interface IAttributeModel {
41
42   // 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;
56 }
57
58 export class AttributeModel extends AttributeBEModel implements IAttributeModel {
59
60   // 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;
74
75   getOutputValues: AttributeOutputDetail[];
76   subAttributeOutputPath: string;
77   outputPath: string;
78
79   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;
94
95       this.getOutputValues = attribute.getOutputValues;
96       this.subAttributeOutputPath = attribute.subAttributeOutputPath;
97       this.outputPath = attribute.outputPath;
98     } else {
99       this._default = '';
100     }
101
102     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));
107     }
108
109     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   public convertValueToView() {
125     // unwrapping value {} or [] if type is complex
126     if (this._default && (this.type === 'map' || this.type === 'list') &&
127         ['[', '{'].indexOf(this._default.charAt(0)) > -1 &&
128         [']', '}'].indexOf(this._default.slice(-1)) > -1) {
129       this._default = this._default.slice(1, -1);
130     }
131
132     // also for value - for the modal in canvas
133     if (this.value && (this.type === 'map' || this.type === 'list') &&
134         ['[', '{'].indexOf(this.value.charAt(0)) > -1 &&
135         [']', '}'].indexOf(this.value.slice(-1)) > -1) {
136       this.value = this.value.slice(1, -1);
137     }
138   }
139
140   public toJSON = (): any => {
141     if (!this.resourceInstanceUniqueId) {
142       this.value = undefined;
143     }
144     this.readonly = undefined;
145     this.resourceInstanceUniqueId = undefined;
146
147     return this;
148   }
149 }