b3e06c33cfd981107629d5b678482755002c0868
[sdc.git] / catalog-ui / src / app / ng2 / pages / interface-operation / interface-operation.page.component.ts
1 import * as _ from "lodash";
2 import {Component, Input, ComponentRef, Inject} from '@angular/core';
3 import {Component as IComponent} from 'app/models/components/component';
4
5 import {ModalComponent} from 'app/ng2/components/ui/modal/modal.component';
6 import {ModalService} from 'app/ng2/services/modal.service';
7 import {ModalModel, ButtonModel, InputModel, OperationModel, CreateOperationResponse} from 'app/models';
8
9 import {ComponentServiceNg2} from 'app/ng2/services/component-services/component.service';
10 import {ComponentGenericResponse} from 'app/ng2/services/responses/component-generic-response';
11 import {WorkflowServiceNg2} from 'app/ng2/services/workflow.service';
12
13 import {OperationCreatorComponent} from './operation-creator/operation-creator.component';
14
15 @Component({
16     selector: 'interface-operation',
17     templateUrl: './interface-operation.page.component.html',
18     styleUrls: ['interface-operation.page.component.less'],
19     providers: [ModalService]
20 })
21
22 export class InterfaceOperationComponent {
23
24     modalInstance: ComponentRef<ModalComponent>;
25     operationList: Array<OperationModel> = [];
26     openOperation: OperationModel;
27
28     @Input() component: IComponent;
29     @Input() readonly: boolean;
30
31     constructor(
32         @Inject('$state') private $state:ng.ui.IStateService,
33         private ComponentServiceNg2: ComponentServiceNg2,
34         private WorkflowServiceNg2: WorkflowServiceNg2,
35         private ModalServiceNg2: ModalService,
36     ) {}
37
38     ngOnInit(): void {
39         this.ComponentServiceNg2.getInterfaceOperations(this.component).subscribe((response: ComponentGenericResponse) => {
40             let {interfaceOperations} = response;
41             this.component.interfaceOperations = interfaceOperations;
42             this.operationList = _.toArray(interfaceOperations);
43         });
44     }
45
46     getDisabled = (): boolean => {
47         return !this.modalInstance.instance.dynamicContent.instance.checkFormValidForSubmit();
48     }
49
50     onEditOperation = (operation: OperationModel): void => {
51         this.ComponentServiceNg2
52             .getInterfaceOperation(this.component, operation)
53             .subscribe(op => this.onAddOperation(op));
54     }
55
56     onAddOperation = (operation?: OperationModel): void => {
57         const modalMap = {
58             create: {
59                 modalTitle: 'Create a New Operation',
60                 saveBtnText: 'Create',
61                 submitCallback: this.createOperation,
62             },
63             edit: {
64                 modalTitle: 'Edit Operation',
65                 saveBtnText: 'Save',
66                 submitCallback: this.updateOperation,
67             }
68         };
69
70         const modalData = operation ? modalMap.edit : modalMap.create;
71
72         if (this.openOperation) {
73             if (operation ? operation.uniqueId === this.openOperation.uniqueId : !this.openOperation.uniqueId) {
74                 operation = this.openOperation;
75             }
76         }
77
78         this.ComponentServiceNg2.getComponentInputs(this.component).subscribe((response: ComponentGenericResponse) => {
79
80             const cancelButton: ButtonModel = new ButtonModel(
81                 'Cancel',
82                 'outline white',
83                 () => {
84                     this.openOperation = null;
85                     this.ModalServiceNg2.closeCurrentModal();
86                 },
87             );
88
89             const saveButton: ButtonModel = new ButtonModel(
90                 modalData.saveBtnText,
91                 'blue',
92                 () => {
93                     this.modalInstance.instance.dynamicContent.instance.createInputParamList();
94                     this.ModalServiceNg2.closeCurrentModal();
95
96                     const {operation, isAssociateWorkflow} = this.modalInstance.instance.dynamicContent.instance;
97                     this.openOperation = {...operation};
98
99                     if (!isAssociateWorkflow) {
100                         operation.workflowId = null;
101                         operation.workflowVersionId = null;
102                     }
103
104                     modalData.submitCallback(operation);
105                 },
106                 this.getDisabled,
107             );
108
109             const modalModel: ModalModel = new ModalModel(
110                 'l',
111                 modalData.modalTitle,
112                 '',
113                 [saveButton, cancelButton],
114                 'standard',
115             );
116
117             this.modalInstance = this.ModalServiceNg2.createCustomModal(modalModel);
118
119             this.ModalServiceNg2.addDynamicContentToModal(
120                 this.modalInstance,
121                 OperationCreatorComponent,
122                 {
123                     operation,
124                     inputProperties: response.inputs,
125                 },
126             );
127
128             this.modalInstance.instance.open();
129         });
130     }
131
132     onRemoveOperation = (event: Event, operation: OperationModel): void => {
133         event.stopPropagation();
134
135         const confirmCallback = () => {
136             this.ModalServiceNg2.closeCurrentModal();
137             this.ComponentServiceNg2
138                 .deleteInterfaceOperation(this.component, operation)
139                 .subscribe(() => {
140                     const index = _.findIndex(this.operationList, el => el.uniqueId === operation.uniqueId);
141                     this.operationList.splice(index, 1);
142                     this.component.interfaceOperations = this.operationList;
143                 });
144         }
145
146         this.modalInstance = this.ModalServiceNg2.createActionModal(
147             operation.operationType,
148             'Are you sure you want to delete this operation?',
149             'Delete',
150             confirmCallback,
151             'Cancel',
152         );
153
154         this.modalInstance.instance.open();
155     }
156
157     private createOperation = (operation: OperationModel): any => {
158         this.ComponentServiceNg2.createInterfaceOperation(this.component, operation).subscribe((response: CreateOperationResponse) => {
159             this.openOperation = null;
160             this.operationList.push(new OperationModel(response));
161             if (response.workflowId) {
162                 const resourceId = this.component.uuid;
163                 const operationId = response.uniqueId;
164                 const workflowId = response.workflowId;
165                 const versionId = response.workflowVersionId;
166                 const artifactId = response.artifactUUID;
167                 this.WorkflowServiceNg2.associateWorkflowArtifact(resourceId, operationId, workflowId, versionId, artifactId).subscribe();
168             }
169         });
170     }
171
172     private updateOperation = (operation: OperationModel): any => {
173         this.ComponentServiceNg2.updateInterfaceOperation(this.component, operation).subscribe(newOperation => {
174             this.openOperation = null;
175             const index = _.findIndex(this.operationList, el => el.uniqueId === operation.uniqueId);
176             this.operationList.splice(index, 1, newOperation);
177             this.component.interfaceOperations = this.operationList;
178         });
179     }
180
181 }