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