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