Fix bugs in attribute outputs page
[sdc.git] / catalog-ui / src / app / models / properties.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 {SchemaPropertyGroupModel, SchemaProperty} from "./schema-property";
24 import {InputPropertyBase} from "./input-property-base";
25 import {PropertyBEModel} from "./properties-inputs/property-be-model";
26
27 export class PropertiesGroup {
28     constructor(propertiesObj?:PropertiesGroup) {
29         _.forEach(propertiesObj, (properties:Array<PropertyModel>, instance) => {
30             this[instance] = [];
31             _.forEach(properties, (property:PropertyModel):void => {
32                 property.resourceInstanceUniqueId = instance;
33                 property.readonly = true;
34                 this[instance].push(new PropertyModel(property));
35             });
36         });
37     }
38 }
39
40 export interface IPropertyModel extends InputPropertyBase {
41
42     //server data
43     //constraints:Array<Object>;
44     source:string;
45
46     //instance properties
47     valueUniqueUid:string;
48     path:Array<string>;
49     rules:Array<Object>;
50     propertiesName:string;
51     input:any;
52
53     //custom properties
54     resourceInstanceUniqueId:string;
55     readonly:boolean;
56     simpleType:string;
57 }
58
59 export class PropertyModel extends PropertyBEModel implements IPropertyModel {
60     //server data
61     uniqueId:string;
62     name:string;
63     constraints:Array<Object>;
64     propertyConstraints:Array<string>;
65     defaultValue:string;
66     description:string;
67     password:boolean;
68     required:boolean;
69     type:string;
70     source:string;
71     parentUniqueId:string;
72     schema:SchemaPropertyGroupModel;
73     componentInstanceId:string;
74     parentValue:string;
75     ownerId:string;
76
77     //instance properties
78     value:string;
79     valueUniqueUid:string;
80     path:Array<string>;
81     rules:Array<Object>;
82     propertiesName:string;
83     input:any;
84
85     //custom properties
86     resourceInstanceUniqueId:string;
87     readonly:boolean;
88     simpleType:string;
89     filterTerm:string;
90     isAlreadySelected:boolean;
91     addOn:string;
92     propertyView: boolean = false;
93
94     constructor(property?:PropertyModel) {
95         super(property);
96         if (property) {
97             if (property.propertyConstraints) {
98                 this.constraints = new Array();
99                 property.propertyConstraints.forEach((constraint: any) => {
100                     this.constraints.push(JSON.parse(constraint));
101                 });
102             }
103             this.propertyConstraints = property.propertyConstraints;
104             this.source = property.source;
105             this.valueUniqueUid = property.valueUniqueUid;
106             this.path = property.path;
107             this.rules = property.rules;
108             this.resourceInstanceUniqueId = property.resourceInstanceUniqueId;
109             this.readonly = property.readonly;
110             this.simpleType = property.simpleType;
111             this.componentInstanceId = property.componentInstanceId;
112             this.parentValue = property.parentValue;
113             this.ownerId = property.ownerId;
114             this.propertyView = property.propertyView;
115         }
116
117         if (!this.schema || !this.schema.property) {
118             this.schema = new SchemaPropertyGroupModel(new SchemaProperty());
119         } else {
120             //forcing creating new object, so editing different one than the object in the table
121             this.schema = new SchemaPropertyGroupModel(new SchemaProperty(this.schema.property));
122         }
123         if (property && property.uniqueId) {
124             this.filterTerm = this.name + " " + (this.description || "") + " " + this.type.replace("org.openecomp.datatypes.heat.", "");
125             if (this.schema.property && this.schema.property.type) {
126                 this.filterTerm += " " + this.schema.property.type.replace("org.openecomp.datatypes.heat.", "");
127             }
128         }
129     }
130
131     public convertToServerObject:Function = ():string => {
132         let serverObject = {};
133         let mapData = {
134             "type": this.type,
135             "required": this.required || false,
136             "defaultValue": this.defaultValue != "" && this.defaultValue != "[]" && this.defaultValue != "{}" ? this.defaultValue : null,
137             "description": this.description,
138             "constraints": this.constraints,
139             "metadata": this.metadata,
140             "propertyConstraints": this.propertyConstraints,
141             "isPassword": this.password || false,
142             "schema": this.schema,
143             "name": this.name
144         };
145         serverObject[this.name] = mapData;
146
147         return JSON.stringify(serverObject);
148     };
149
150     public toJSON = ():any => {
151         // if(!this.resourceInstanceUniqueId){
152         //     this.value = undefined;
153         // }
154         let temp = angular.copy(this);
155         temp.readonly = undefined;
156         temp.resourceInstanceUniqueId = undefined;
157         temp.simpleType = undefined;
158         temp.value = temp.value === "{}" || temp.value === "[]" ? undefined : temp.value;
159         temp.defaultValue = temp.defaultValue === "{}" || temp.defaultValue === "[]" ? undefined : temp.defaultValue;
160         temp.rules = undefined; //don't send rules to server until feature is fully supported
161         temp.isAlreadySelected = undefined;
162         temp.addOn = undefined;
163         temp.filterTerm = undefined;
164         return temp;
165     };
166 }