e292a6ceef61ce5398a47f2ce55a8e2ac7c5048f
[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     defaultValue:string;
65     description:string;
66     password:boolean;
67     required:boolean;
68     type:string;
69     source:string;
70     parentUniqueId:string;
71     schema:SchemaPropertyGroupModel;
72     componentInstanceId:string;
73     parentValue:string;
74     ownerId:string;
75
76     //instance properties
77     value:string;
78     valueUniqueUid:string;
79     path:Array<string>;
80     rules:Array<Object>;
81     propertiesName:string;
82     input:any;
83
84     //custom properties
85     resourceInstanceUniqueId:string;
86     readonly:boolean;
87     simpleType:string;
88     filterTerm:string;
89     isAlreadySelected:boolean;
90     addOn:string;
91
92
93     constructor(property?:PropertyModel) {
94         super(property);
95         if (property) {
96             // this.constraints = property.constraints;
97             this.source = property.source;
98             this.valueUniqueUid = property.valueUniqueUid;
99             this.path = property.path;
100             this.rules = property.rules;
101             this.resourceInstanceUniqueId = property.resourceInstanceUniqueId;
102             this.readonly = property.readonly;
103             this.simpleType = property.simpleType;
104             this.componentInstanceId = property.componentInstanceId;
105             this.parentValue = property.parentValue;
106             this.ownerId = property.ownerId;
107         }
108
109         if (!this.schema || !this.schema.property) {
110             this.schema = new SchemaPropertyGroupModel(new SchemaProperty());
111         } else {
112             //forcing creating new object, so editing different one than the object in the table
113             this.schema = new SchemaPropertyGroupModel(new SchemaProperty(this.schema.property));
114         }
115         if (property && property.uniqueId) {
116             this.filterTerm = this.name + " " + (this.description || "") + " " + this.type.replace("org.openecomp.datatypes.heat.", "");
117             if (this.schema.property && this.schema.property.type) {
118                 this.filterTerm += " " + this.schema.property.type.replace("org.openecomp.datatypes.heat.", "");
119             }
120         }
121     }
122
123     public convertToServerObject:Function = ():string => {
124         let serverObject = {};
125         let mapData = {
126             "type": this.type,
127             "required": this.required || false,
128             "defaultValue": this.defaultValue != "" && this.defaultValue != "[]" && this.defaultValue != "{}" ? this.defaultValue : null,
129             "description": this.description,
130             "constraints": this.constraints,
131             "isPassword": this.password || false,
132             "schema": this.schema,
133             "name": this.name
134         };
135         serverObject[this.name] = mapData;
136
137         return JSON.stringify(serverObject);
138     };
139
140     public toJSON = ():any => {
141         // if(!this.resourceInstanceUniqueId){
142         //     this.value = undefined;
143         // }
144         let temp = angular.copy(this);
145         temp.readonly = undefined;
146         temp.resourceInstanceUniqueId = undefined;
147         temp.simpleType = undefined;
148         temp.value = temp.value === "{}" || temp.value === "[]" ? undefined : temp.value;
149         temp.defaultValue = temp.defaultValue === "{}" || temp.defaultValue === "[]" ? undefined : temp.defaultValue;
150         temp.rules = undefined; //don't send rules to server until feature is fully supported
151         temp.isAlreadySelected = undefined;
152         temp.addOn = undefined;
153         temp.filterTerm = undefined;
154         return temp;
155     };
156 }