Implement Attributes/Outputs BE (part 2)
[sdc.git] / catalog-ui / src / app / models / attributes-outputs / attribute-be-model.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2021 Nordix Foundation. 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 import {PROPERTY_DATA, PROPERTY_TYPES} from 'app/utils/constants';
22 import {ToscaPresentationData} from '../tosca-presentation';
23 import {AttributeOutputDetail} from "app/models/attributes-outputs/attribute-output-detail";
24 import {SchemaAttribute, SchemaAttributeGroupModel} from "../schema-attribute";
25
26 export enum DerivedAttributeType {
27   SIMPLE,
28   LIST,
29   MAP,
30   COMPLEX
31 }
32
33 export class AttributeBEModel {
34
35   constraints: any[];
36   defaultValue: string;
37   description: string;
38   fromDerived: boolean;
39   getOutputValues: AttributeOutputDetail[];
40   name: string;
41   origName: string;
42   parentUniqueId: string;
43   password: boolean;
44   required: boolean;
45   schema: SchemaAttributeGroupModel;
46   schemaType: string;
47   type: string;
48   uniqueId: string;
49   value: string;
50   parentAttributeType: string;
51   subAttributeOutputPath: string;
52   outputPath: string;
53   toscaPresentation: ToscaPresentationData;
54
55   constructor(attribute?: AttributeBEModel) {
56     if (attribute) {
57       this.constraints = attribute.constraints;
58       this.defaultValue = attribute.defaultValue;
59       this.description = attribute.description;
60       this.fromDerived = attribute.fromDerived;
61       this.name = attribute.name;
62       this.origName = attribute.origName;
63       this.parentUniqueId = attribute.parentUniqueId;
64       this.password = attribute.password;
65       this.required = attribute.required;
66       this.schema = attribute.schema;
67       this.schemaType = attribute.schemaType;
68       this.type = attribute.type;
69       this.uniqueId = attribute.uniqueId;
70       this.value = attribute.value;
71       this.getOutputValues = attribute.getOutputValues;
72       this.parentAttributeType = attribute.parentAttributeType;
73       this.subAttributeOutputPath = attribute.subAttributeOutputPath;
74       this.toscaPresentation = attribute.toscaPresentation;
75       this.outputPath = attribute.outputPath;
76     }
77
78     if (!this.schema || !this.schema.property) {
79       this.schema = new SchemaAttributeGroupModel(new SchemaAttribute());
80     } else { // forcing creating new object, so editing different one than the object in the table
81       this.schema = new SchemaAttributeGroupModel(new SchemaAttribute(this.schema.property));
82     }
83   }
84
85   public toJSON = (): any => {
86     const temp = angular.copy(this);
87     temp.value = temp.value === '{}' || temp.value === '[]' ? undefined : temp.value;
88     temp.defaultValue = temp.defaultValue === '{}' || temp.defaultValue === '[]' ? undefined : temp.defaultValue;
89     return temp;
90   }
91
92   public getDerivedAttributeType = () => {
93     if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.type) > -1) {
94       return DerivedAttributeType.SIMPLE;
95     } else if (this.type === PROPERTY_TYPES.LIST) {
96       return DerivedAttributeType.LIST;
97     } else if (this.type === PROPERTY_TYPES.MAP) {
98       return DerivedAttributeType.MAP;
99     } else {
100       return DerivedAttributeType.COMPLEX;
101     }
102   }
103 }