summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/pages/interface-operation/operation-creator/operation-creator.component.ts
blob: 8d26055feb5409bb17d21a162e054e84ca67e694 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import * as _ from "lodash";
import {Component} from '@angular/core';

import {Subscription} from "rxjs/Subscription";

import {TranslateService} from "app/ng2/shared/translator/translate.service";
import {WorkflowServiceNg2} from 'app/ng2/services/workflow.service';
import {InputModel, OperationModel, OperationParameter} from 'app/models';

import {DropdownValue} from "app/ng2/components/ui/form-components/dropdown/ui-element-dropdown.component";

@Component({
    selector: 'operation-creator',
    templateUrl: './operation-creator.component.html',
    styleUrls:['./operation-creator.component.less'],
    providers: [TranslateService]
})

export class OperationCreatorComponent {

    input: any;
    operation: OperationModel;

    workflows: Array<DropdownValue> = [];
    workflowVersions: Array<DropdownValue> = [];
    inputProperties: Array<DropdownValue> = [];
    inputPropertyTypes: {};

    inputParameters: Array<OperationParameter> = [];
    noAssignInputParameters: Array<OperationParameter> = [];
    assignInputParameters: { [key: string]: { [key: string]: Array<OperationParameter>; }; } = {};

    isAssociateWorkflow: boolean = false;
    isEditMode: boolean = false;
    isLoading: boolean = false;

    propertyTooltipText: String;

    constructor(private workflowServiceNg2: WorkflowServiceNg2, private translateService: TranslateService) {
        this.translateService.languageChangedObservable.subscribe(lang => {
            this.propertyTooltipText = this.translateService.translate("OPERATION_PROPERTY_TOOLTIP_TEXT");
        });
    }

    ngOnInit() {

        this.inputProperties = _.map(this.input.inputProperties,
            (input: InputModel) => new DropdownValue(input.uniqueId, input.name)
        );

        this.inputPropertyTypes = {};
        _.forEach(this.input.inputProperties, (input: InputModel) => {
            this.inputPropertyTypes[input.uniqueId] = input.type;
        });

        const inputOperation = <OperationModel>this.input.operation;
        this.operation = new OperationModel(inputOperation || {});

        const buildInputParams = () => {
            if (inputOperation.inputParams) {
                this.inputParameters = [];
                _.forEach(inputOperation.inputParams.listToscaDataDefinition, (input: OperationParameter) => {
                    this.addParam(input);
                });
            }
        }

        this.isLoading = true;
        this.workflowServiceNg2.getWorkflows().subscribe(workflows => {
            this.isLoading = false;

            this.workflows = _.map(workflows, (workflow: any) => {
                return new DropdownValue(workflow.id, workflow.name);
            });

            if (inputOperation) {
                if (inputOperation.workflowVersionId) {
                    this.isAssociateWorkflow = true;
                    this.onSelectWorkflow(inputOperation.workflowVersionId).add(buildInputParams);
                } else {
                    this.inputParameters = this.noAssignInputParameters;
                    this.isAssociateWorkflow = false;
                    buildInputParams();
                }

                if (inputOperation.uniqueId) {
                    this.isEditMode = true;
                }
            }
        });


    }

    onSelectWorkflow(selectedVersionId?: string): Subscription {

        this.operation.workflowVersionId = selectedVersionId || null;
        if (!this.assignInputParameters[this.operation.workflowId]) {
            this.assignInputParameters[this.operation.workflowId] = {};
        }

        this.isLoading = true;
        return this.workflowServiceNg2.getWorkflowVersions(this.operation.workflowId).subscribe((versions: Array<any>) => {
            this.isLoading = false;

            this.workflowVersions = _.map(
                _.filter(versions, version => version.state === this.workflowServiceNg2.VERSION_STATE_CERTIFIED),
                (version: any) => {
                    if (!this.assignInputParameters[this.operation.workflowId][version.id]) {
                        this.assignInputParameters[this.operation.workflowId][version.id] = _.map(version.inputs, (input: any) => {
                            return new OperationParameter({
                                name: input.name,
                                type: input.type && input.type.toLowerCase(),
                                property: null,
                                mandatory: input.mandatory,
                            });
                        });
                    }
                    return new DropdownValue(version.id, `v. ${version.name}`);
                }
            );

            if (!selectedVersionId && versions.length) {
                this.operation.workflowVersionId = _.last(versions.sort()).id;
            }
            this.changeWorkflowVersion();
        });

    }

    changeWorkflowVersion() {
        this.inputParameters = this.assignInputParameters[this.operation.workflowId][this.operation.workflowVersionId];
    }

    toggleAssociateWorkflow() {

        if (!this.isAssociateWorkflow) {
            this.inputParameters = this.noAssignInputParameters;
        } else {
            if (!this.operation.workflowId || !this.operation.workflowVersionId) {
                this.inputParameters = [];
            } else {
                this.inputParameters = this.assignInputParameters[this.operation.workflowId][this.operation.workflowVersionId];
            }
        }

    }

    addParam(param?: OperationParameter): void {
        this.inputParameters.push(new OperationParameter(param));
    }

    isParamsValid(): boolean {

        for (let ctr=0; ctr<this.inputParameters.length; ctr++) {
            if (!this.inputParameters[ctr].name || !this.inputParameters[ctr].property) {
                return false;
            }
        }
        return true;

    }

    onRemoveParam = (param: OperationParameter): void => {
        let index = _.indexOf(this.inputParameters, param);
        this.inputParameters.splice(index, 1);
    }

    createInputParamList(): void {
        this.operation.createInputParamsList(this.inputParameters);
    }

    checkFormValidForSubmit(): boolean {
        return this.operation.operationType &&
            (!this.isAssociateWorkflow || this.operation.workflowVersionId) &&
            this.isParamsValid();
    }

}