a6966cd7994de698bc63578e56dc30fb3b547740
[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
68       if (attribute.schemaType) {
69         this.schemaType = attribute.schemaType;
70       }
71
72       this.type = attribute.type;
73       this.uniqueId = attribute.uniqueId;
74       this.value = attribute.value;
75       this.getOutputValues = attribute.getOutputValues;
76       this.parentAttributeType = attribute.parentAttributeType;
77       this.subAttributeOutputPath = attribute.subAttributeOutputPath;
78       this.toscaPresentation = attribute.toscaPresentation;
79       this.outputPath = attribute.outputPath;
80     }
81
82     if (!this.schema || !this.schema.property) {
83       this.schema = new SchemaAttributeGroupModel(new SchemaAttribute());
84     } else { // forcing creating new object, so editing different one than the object in the table
85       this.schema = new SchemaAttributeGroupModel(new SchemaAttribute(this.schema.property));
86     }
87   }
88
89   public toJSON = (): any => {
90     const temp = angular.copy(this);
91     temp.value = temp.value === '{}' || temp.value === '[]' ? undefined : temp.value;
92     temp.defaultValue = temp.defaultValue === '{}' || temp.defaultValue === '[]' ? undefined : temp.defaultValue;
93     return temp;
94   }
95
96   public getDerivedAttributeType = () => {
97     if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.type) > -1) {
98       return DerivedAttributeType.SIMPLE;
99     } else if (this.type === PROPERTY_TYPES.LIST) {
100       return DerivedAttributeType.LIST;
101     } else if (this.type === PROPERTY_TYPES.MAP) {
102       return DerivedAttributeType.MAP;
103     } else {
104       return DerivedAttributeType.COMPLEX;
105     }
106   }
107 }