Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / pages / interface-operation / operation-creator / param-row / param-row.component.ts
1 import {Component, Input} from '@angular/core';
2 import {PROPERTY_DATA} from "app/utils";
3 import {DataTypeService} from "app/ng2/services/data-type.service";
4 import {OperationModel, OperationParameter, InputBEModel, DataTypeModel, Capability} from 'app/models';
5 import {DropdownValue} from "app/ng2/components/ui/form-components/dropdown/ui-element-dropdown.component";
6
7 class DropdownValueType extends DropdownValue {
8     type: String;
9
10     constructor(value: string, label: string, type?: String) {
11         super(value, label);
12         if (type) {
13             this.type = type;
14         }
15     }
16 }
17
18 @Component({
19     selector: 'param-row',
20     templateUrl: './param-row.component.html',
21     styleUrls: ['./param-row.component.less']
22 })
23
24 export class ParamRowComponent {
25     @Input() param: OperationParameter;
26     @Input() inputProps: Array<InputBEModel>;
27     @Input() operationOutputs: Array<OperationModel>;
28     @Input() capabilitiesProps: Array<Capability>;
29     @Input() onRemoveParam: Function;
30     @Input() isAssociateWorkflow: boolean;
31     @Input() readonly: boolean;
32     @Input() isInputParam: boolean;
33     @Input() validityChanged: Function;
34
35     propTypeEnum: Array<string> = [];
36     operationOutputCats: Array<{ operationName: string, outputs: Array<DropdownValueType> }> = [];
37     filteredInputProps: Array<DropdownValue> = [];
38     filteredCapabilitiesProps: Array<{capabilityName: string, properties: Array<DropdownValueType>}> = [];
39
40     constructor(private dataTypeService:DataTypeService) {}
41
42     ngOnInit() {
43         if (this.isInputParam) {
44             this.propTypeEnum = _.uniq(
45                 _.map(
46                     _.concat(
47                         this.getPrimitiveSubtypes(),
48                         _.reduce(
49                             this.operationOutputs,
50                             (acc, op) => [...acc, ...op.outputs.listToscaDataDefinition],
51                             []),
52                         _.reduce(
53                             this.capabilitiesProps,
54                             (acc, capab) => [...acc, ...capab.properties],
55                             [])
56                     ),
57                     prop => prop.type
58                 )
59             );
60         } else {
61             const dataTypes: Array<DataTypeModel> = _.toArray(this.dataTypeService.getAllDataTypes());
62             this.propTypeEnum = _.concat(
63                 _.map(
64                     _.filter(
65                         dataTypes,
66                         type => this.isTypePrimitive(type.name)
67                     ),
68                     type => type.name
69                 ).sort(),
70                 _.map(
71                     _.filter(
72                         dataTypes,
73                         type => !this.isTypePrimitive(type.name)
74                     ),
75                     type => type.name
76                 ).sort()
77             );
78         }
79
80         this.onChangeType();
81         this.validityChanged();
82     }
83
84     onChangeName() {
85         this.validityChanged();
86     }
87
88     onChangeType() {
89         if (!this.isInputParam) {
90             this.validityChanged();
91             return;
92         }
93
94         this.filteredInputProps = _.map(
95             _.filter(
96                 this.getPrimitiveSubtypes(),
97                 prop => !this.param.type || prop.type === this.param.type
98             ),
99             prop => new DropdownValue(prop.uniqueId, prop.name)
100         );
101
102         this.operationOutputCats = _.filter(
103             _.map(
104                 this.operationOutputs,
105                 op => {
106                     return {
107                         operationName: `${op.displayType()}.${op.name}`,
108                         outputs: _.map(
109                             _.filter(op.outputs.listToscaDataDefinition, output => !this.param.type || output.type === this.param.type),
110                             output => new DropdownValueType(
111                                 `${op.interfaceType}.${op.name}.${output.name}`,
112                                 output.name,
113                                 output.type
114                             )
115                         )
116                     };
117                 }
118             ),
119             category => category.outputs.length > 0
120         );
121
122         this.filteredCapabilitiesProps = _.filter(
123             _.map(
124                 this.capabilitiesProps,
125                 cap => {
126                     return {
127                         capabilityName: cap.name,
128                         properties: _.map(
129                             _.filter(cap.properties, prop => !this.param.type || prop.type === this.param.type),
130                             prop => new DropdownValueType(
131                                 prop.uniqueId,
132                                 prop.name,
133                                 prop.type
134                             )
135                         )
136                     };
137                 }
138             ),
139             capability => capability.properties.length > 0
140         );
141
142         if (this.param.inputId) {
143             const selProp = this.getSelectedProp();
144             if (selProp && selProp.type === this.param.type) {
145                 this.param.inputId = '-1';
146                 setTimeout(() => this.param.inputId = selProp.uniqueId || selProp.value);
147             } else {
148                 this.param.inputId = null;
149             }
150         }
151
152         this.validityChanged();
153     }
154
155     onChangeProperty() {
156         const newProp = this.getSelectedProp();
157
158         if (!this.param.type) {
159             this.param.type = newProp.type;
160             this.onChangeType();
161         }
162
163         if (!this.param.name) {
164             this.param.name = newProp.name || newProp.label;
165         }
166
167         this.validityChanged();
168     }
169
170     getPrimitiveSubtypes(): Array<InputBEModel> {
171         const flattenedProps: Array<any> = [];
172         const dataTypes = this.dataTypeService.getAllDataTypes();
173
174         _.forEach(this.inputProps, prop => {
175             const type:DataTypeModel = _.find(
176                 _.toArray(dataTypes),
177                 (type: DataTypeModel) => type.name === prop.type
178             );
179             flattenedProps.push(prop);
180             if (!type) {
181                 console.error('Could not find prop in dataTypes: ', prop);
182             } else {
183                 if (type.properties) {
184                     _.forEach(type.properties, subType => {
185                         if (this.isTypePrimitive(subType.type)) {
186                             flattenedProps.push({
187                                 type: subType.type,
188                                 name: `${prop.name}.${subType.name}`,
189                                 uniqueId: `${prop.uniqueId}.${subType.name}`
190                             });
191                         }
192                     });
193                 }
194             }
195         });
196
197         return flattenedProps;
198     }
199
200     getSelectedProp() {
201         return _.find(
202             this.getPrimitiveSubtypes(),
203             prop => this.param.inputId === prop.uniqueId
204         ) || _.find(
205             _.reduce(
206                 this.operationOutputCats,
207                 (acc, cat) => [...acc, ...cat.outputs],
208             []),
209             (out: DropdownValueType) => this.param.inputId === out.value
210             ) || _.find(
211                 _.reduce(
212                     this.filteredCapabilitiesProps,
213                     (acc, cap) => [...acc, ...cap.properties],
214                     []),
215                 (prop: DropdownValueType) => this.param.inputId === prop.value
216         );
217     }
218
219     isTypePrimitive(type: string): boolean {
220         return (
221             type === 'string' ||
222             type === 'integer' ||
223             type === 'float' ||
224             type === 'boolean'
225         );
226     }
227 }