1 import * as _ from "lodash";
2 import {Component, Input, ComponentRef, Inject} from '@angular/core';
3 import {Component as IComponent} from 'app/models/components/component';
5 import {SdcConfigToken, ISdcConfig} from "app/ng2/config/sdc-config.config";
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';
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';
15 import {OperationCreatorComponent, OperationCreatorInput} from './operation-creator/operation-creator.component';
18 selector: 'interface-operation',
19 templateUrl: './interface-operation.page.component.html',
20 styleUrls: ['interface-operation.page.component.less'],
21 providers: [ModalService]
24 export class InterfaceOperationComponent {
26 modalInstance: ComponentRef<ModalComponent>;
27 operationList: Array<OperationModel> = [];
28 openOperation: OperationModel;
29 enableWorkflowAssociation: boolean;
30 inputs: Array<InputBEModel>;
33 @Input() component: IComponent;
34 @Input() readonly: boolean;
37 @Inject(SdcConfigToken) sdcConfig: ISdcConfig,
38 private ComponentServiceNg2: ComponentServiceNg2,
39 private WorkflowServiceNg2: WorkflowServiceNg2,
40 private ModalServiceNg2: ModalService,
42 this.enableWorkflowAssociation = sdcConfig.enableWorkflowAssociation;
46 this.isLoading = true;
47 this.ComponentServiceNg2.getInterfaceOperations(this.component).subscribe((response: ComponentGenericResponse) => {
49 this.isLoading = false;
51 let {interfaceOperations} = response;
52 this.component.interfaceOperations = interfaceOperations;
53 this.operationList = _.toArray(interfaceOperations).sort((a, b) => a.operationType.localeCompare(b.operationType));
55 this.ComponentServiceNg2.getComponentInputs(this.component).subscribe((response: ComponentGenericResponse) => {
56 if (this.component.interfaceOperations) {
57 this.isLoading = false;
59 this.inputs = response.inputs;
63 getDisabled = (): boolean => {
64 return !this.modalInstance.instance.dynamicContent.instance.checkFormValidForSubmit();
67 onEditOperation = (operation?: OperationModel): void => {
70 modalTitle: 'Create a New Operation',
71 saveBtnText: 'Create',
72 submitCallback: this.createOperation,
75 modalTitle: 'Edit Operation',
77 submitCallback: this.updateOperation,
81 const modalData = operation ? modalMap.edit : modalMap.create;
83 if (this.openOperation) {
84 if (operation ? operation.uniqueId === this.openOperation.uniqueId : !this.openOperation.uniqueId) {
85 operation = this.openOperation;
89 const cancelButton: ButtonModel = new ButtonModel(
93 this.openOperation = null;
94 this.ModalServiceNg2.closeCurrentModal();
98 const saveButton: ButtonModel = new ButtonModel(
99 modalData.saveBtnText,
102 this.modalInstance.instance.dynamicContent.instance.createInputParamList();
103 this.ModalServiceNg2.closeCurrentModal();
105 const {operation, isAssociateWorkflow} = this.modalInstance.instance.dynamicContent.instance;
106 this.openOperation = {...operation};
108 if (!this.enableWorkflowAssociation && !isAssociateWorkflow) {
109 operation.workflowId = null;
110 operation.workflowVersionId = null;
113 modalData.submitCallback(operation);
118 const modalModel: ModalModel = new ModalModel(
120 modalData.modalTitle,
122 [saveButton, cancelButton],
126 this.modalInstance = this.ModalServiceNg2.createCustomModal(modalModel);
128 let input: OperationCreatorInput = {
130 inputProperties: this.inputs,
131 enableWorkflowAssociation: this.enableWorkflowAssociation,
132 readonly: this.readonly,
133 isService: this.component.isService()
136 this.ModalServiceNg2.addDynamicContentToModal(
138 OperationCreatorComponent,
142 this.modalInstance.instance.open();
145 onRemoveOperation = (event: Event, operation: OperationModel): void => {
146 event.stopPropagation();
148 const confirmCallback = () => {
149 this.ModalServiceNg2.closeCurrentModal();
150 this.ComponentServiceNg2
151 .deleteInterfaceOperation(this.component, operation)
153 const index = _.findIndex(this.operationList, el => el.uniqueId === operation.uniqueId);
154 this.operationList.splice(index, 1);
155 this.component.interfaceOperations = this.operationList;
159 this.modalInstance = this.ModalServiceNg2.createActionModal(
160 operation.operationType,
161 'Are you sure you want to delete this operation?',
167 this.modalInstance.instance.open();
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));
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();
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;
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();