Support list of map properties in composition
[sdc.git] / catalog-ui / src / app / ng2 / pages / properties-assignment / input-list / input-list.component.ts
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 import {Component} from '@angular/core';
21 import {
22     ComponentMetadata, DataTypeModel, PropertyBEModel
23 } from 'app/models';
24 import {TopologyTemplateService} from "../../../services/component-services/topology-template.service";
25 import {WorkspaceService} from "../../workspace/workspace.service";
26 import {PropertiesService} from "../../../services/properties.service";
27 import {PROPERTY_DATA} from "../../../../utils/constants";
28 import {DataTypeService} from "../../../services/data-type.service";
29
30 @Component({
31     selector: 'input-list',
32     templateUrl: './input-list.component.html',
33     styleUrls: ['./input-list.component.less'],
34 })
35
36 export class InputListComponent {
37
38     selectInputValue;
39     isLoading: boolean;
40     propertyType: string;
41     inputs: Array<PropertyBEModel> = [];
42
43     private dataTypeProperties: Array<PropertyBEModel> = [];
44     private componentMetadata: ComponentMetadata;
45
46     constructor(private topologyTemplateService: TopologyTemplateService,
47                 private workspaceService: WorkspaceService,
48                 private propertiesService: PropertiesService,
49                 private dataTypeService: DataTypeService) {
50     }
51
52     ngOnInit() {
53         this.componentMetadata = this.workspaceService.metadata;
54         this.propertyType = this.propertiesService.getCheckedPropertyType();
55         this.loadInputValues(this.propertyType);
56     }
57
58     private loadInputValues(propertyType: string): void {
59         this.isLoading = true;
60         this.topologyTemplateService.getComponentInputsValues(this.componentMetadata.componentType, this.componentMetadata.uniqueId)
61         .subscribe((response) => {
62             response.inputs.forEach((inputProperty: any) => {
63                 if (propertyType === inputProperty.type) {
64                     this.inputs.push(inputProperty);
65                 } else if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(inputProperty.type) === -1 && inputProperty.type !== propertyType) {
66                     this.buildInputDataForComplexType(inputProperty, propertyType);
67                 }
68             });
69         }, () => {
70             //error ignored
71         }, () => {
72             this.isLoading = false;
73         });
74     }
75
76     private buildInputDataForComplexType(inputProperty: PropertyBEModel, propertyType: string) {
77         let dataTypeFound: DataTypeModel = this.dataTypeService.getDataTypeByModelAndTypeName(this.componentMetadata.model, inputProperty.type);
78         if (dataTypeFound && dataTypeFound.properties) {
79             dataTypeFound.properties.forEach(dataTypeProperty => {
80                 let inputData = inputProperty.name + "->" + dataTypeProperty.name;
81                 dataTypeProperty.name = inputData;
82                 if (this.dataTypeProperties.indexOf(dataTypeProperty) === -1 && dataTypeProperty.type === propertyType) {
83                     this.inputs.push(dataTypeProperty);
84                 }
85             });
86         }
87     }
88 }