Provide user to specify the ouput name while declaring the atrributes
[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   outputName: string;
55
56   constructor(attribute?: AttributeBEModel) {
57     if (attribute) {
58       this.constraints = attribute.constraints;
59       this.defaultValue = attribute.defaultValue;
60       this.description = attribute.description;
61       this.fromDerived = attribute.fromDerived;
62       this.name = attribute.name;
63       this.origName = attribute.origName;
64       this.parentUniqueId = attribute.parentUniqueId;
65       this.password = attribute.password;
66       this.required = attribute.required;
67       this.schema = attribute.schema;
68
69       if (attribute.schemaType) {
70         this.schemaType = attribute.schemaType;
71       }
72
73       this.type = attribute.type;
74       this.uniqueId = attribute.uniqueId;
75       this.value = attribute.value;
76       this.getOutputValues = attribute.getOutputValues;
77       this.parentAttributeType = attribute.parentAttributeType;
78       this.subAttributeOutputPath = attribute.subAttributeOutputPath;
79       this.toscaPresentation = attribute.toscaPresentation;
80       this.outputPath = attribute.outputPath;
81       this.outputName = attribute.outputName;
82     }
83
84     if (!this.schema || !this.schema.property) {
85       this.schema = new SchemaAttributeGroupModel(new SchemaAttribute());
86     } else { // forcing creating new object, so editing different one than the object in the table
87       this.schema = new SchemaAttributeGroupModel(new SchemaAttribute(this.schema.property));
88     }
89   }
90
91   public toJSON = (): any => {
92     const temp = angular.copy(this);
93     temp.value = temp.value === '{}' || temp.value === '[]' ? undefined : temp.value;
94     temp.defaultValue = temp.defaultValue === '{}' || temp.defaultValue === '[]' ? undefined : temp.defaultValue;
95     return temp;
96   }
97
98   public getDerivedAttributeType = () => {
99     if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.type) > -1) {
100       return DerivedAttributeType.SIMPLE;
101     } else if (this.type === PROPERTY_TYPES.LIST) {
102       return DerivedAttributeType.LIST;
103     } else if (this.type === PROPERTY_TYPES.MAP) {
104       return DerivedAttributeType.MAP;
105     } else {
106       return DerivedAttributeType.COMPLEX;
107     }
108   }
109 }