Operation WF association UI
[sdc.git] / catalog-ui / src / app / ng2 / pages / interface-operation / interface-operation.page.component.ts
1 import * as _ from "lodash";
2 import {Component, Input, Output, 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 {Observable} from "rxjs/Observable";
8
9 import {ModalComponent} from 'app/ng2/components/ui/modal/modal.component';
10 import {ModalService} from 'app/ng2/services/modal.service';
11 import {ModalModel, ButtonModel, InputBEModel, OperationModel, CreateOperationResponse, WORKFLOW_ASSOCIATION_OPTIONS} from 'app/models';
12
13 import {ComponentServiceNg2} from 'app/ng2/services/component-services/component.service';
14 import {ComponentGenericResponse} from 'app/ng2/services/responses/component-generic-response';
15 import {WorkflowServiceNg2} from 'app/ng2/services/workflow.service';
16
17 import {OperationCreatorComponent, OperationCreatorInput} from './operation-creator/operation-creator.component';
18
19 @Component({
20     selector: 'interface-operation',
21     templateUrl: './interface-operation.page.component.html',
22     styleUrls: ['interface-operation.page.component.less'],
23     providers: [ModalService]
24 })
25
26 export class InterfaceOperationComponent {
27
28     modalInstance: ComponentRef<ModalComponent>;
29     operationList: Array<OperationModel> = [];
30     openOperation: OperationModel;
31     enableWorkflowAssociation: boolean;
32     inputs: Array<InputBEModel>;
33     isLoading: boolean;
34
35     @Input() component: IComponent;
36     @Input() readonly: boolean;
37     @Input() enableMenuItems: Function;
38     @Input() disableMenuItems: Function;
39
40     constructor(
41         @Inject(SdcConfigToken) private sdcConfig: ISdcConfig,
42         @Inject("$state") private $state: ng.ui.IStateService,
43         private ComponentServiceNg2: ComponentServiceNg2,
44         private WorkflowServiceNg2: WorkflowServiceNg2,
45         private ModalServiceNg2: ModalService,
46     ) {
47         this.enableWorkflowAssociation = sdcConfig.enableWorkflowAssociation;
48     }
49
50     ngOnInit(): void {
51         this.isLoading = true;
52         Observable.forkJoin(
53             this.ComponentServiceNg2.getInterfaceOperations(this.component),
54             this.ComponentServiceNg2.getComponentInputs(this.component)
55         ).subscribe((response) => {
56             this.isLoading = false;
57             this.component.interfaceOperations = response[0].interfaceOperations;
58             this.operationList = _.toArray(response[0].interfaceOperations).sort((a, b) => a.operationType.localeCompare(b.operationType));
59             this.inputs = response[1].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.createParamLists();
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 && operation.workflowAssociationType === 'EXISTING') {
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             } else if (operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.NEW) {
184                 this.$state.go('workspace.plugins', { path: 'workflowDesigner' });
185             }
186         });
187     }
188
189     private updateOperation = (operation: OperationModel): any => {
190         this.ComponentServiceNg2.updateInterfaceOperation(this.component, operation).subscribe(newOperation => {
191             this.openOperation = null;
192             const index = _.findIndex(this.operationList, el => el.uniqueId === operation.uniqueId);
193             this.operationList.splice(index, 1, newOperation);
194             this.component.interfaceOperations = this.operationList;
195
196             if (newOperation.workflowId) {
197                 const resourceId = this.component.uuid;
198                 const operationId = newOperation.uniqueId;
199                 const workflowId = newOperation.workflowId;
200                 const versionId = newOperation.workflowVersionId;
201                 const artifactId = newOperation.artifactUUID;
202                 this.WorkflowServiceNg2.associateWorkflowArtifact(resourceId, operationId, workflowId, versionId, artifactId).subscribe();
203             }
204         });
205     }
206
207 }