1 import * as _ from "lodash";
2 import {Component, ViewChild} from '@angular/core';
4 import {Subscription} from "rxjs/Subscription";
6 import {TranslateService} from "app/ng2/shared/translator/translate.service";
7 import {WorkflowServiceNg2} from 'app/ng2/services/workflow.service';
8 import {InterfaceModel, OperationModel, OperationParameter, InputBEModel, RadioButtonModel, WORKFLOW_ASSOCIATION_OPTIONS} from 'app/models';
10 import {IDropDownOption} from "sdc-ui/lib/angular/form-elements/dropdown/dropdown-models";
11 import {Tabs, Tab} from "app/ng2/components/ui/tabs/tabs.component";
12 import {DropdownValue} from "app/ng2/components/ui/form-components/dropdown/ui-element-dropdown.component";
14 export class DropDownOption implements IDropDownOption {
18 constructor(value: string, label?: string) {
20 this.label = label || value;
24 class TypedDropDownOption extends DropDownOption {
27 constructor(value: string, label?: string, type?: number) {
33 export interface OperationCreatorInput {
34 inputOperation: OperationModel,
35 interfaces: Array<InterfaceModel>,
36 inputProperties: Array<InputBEModel>,
37 enableWorkflowAssociation: boolean,
40 interfaceTypes: { [interfaceType: string]: Array<string> },
41 validityChangedCallback: Function
45 selector: 'operation-creator',
46 templateUrl: './operation-creator.component.html',
47 styleUrls: ['./operation-creator.component.less'],
48 providers: [TranslateService]
51 export class OperationCreatorComponent {
53 input: OperationCreatorInput;
54 inputOperation: OperationModel;
55 interfaces: Array<InterfaceModel>;
56 operation: OperationModel;
57 interfaceNames: Array<TypedDropDownOption> = [];
58 interfaceTypes: { [interfaceType: string]: Array<string> };
59 operationNames: Array<TypedDropDownOption> = [];
60 validityChangedCallback: Function;
62 workflows: Array<DropdownValue> = [];
63 workflowVersions: Array<DropdownValue> = [];
64 inputProperties: Array<InputBEModel> = [];
65 archivedWorkflowId: string = '&';
67 inputParameters: Array<OperationParameter> = [];
68 noAssignInputParameters: Array<OperationParameter> = [];
69 assignInputParameters: { [key: string]: { [key: string]: Array<OperationParameter>; }; } = {};
71 outputParameters: Array<OperationParameter> = [];
72 noAssignOutputParameters: Array<OperationParameter> = [];
73 assignOutputParameters: { [key: string]: { [key: string]: Array<OperationParameter>; }; } = {};
75 tableParameters: Array<OperationParameter> = [];
77 associationOptions: Array<DropdownValue> = [];
78 workflowAssociationType: string;
80 enableWorkflowAssociation: boolean;
81 isEditMode: boolean = false;
82 isLoading: boolean = false;
85 propertyTooltipText: String;
87 TYPE_INPUT = 'Inputs';
88 TYPE_OUTPUT = 'Outputs';
90 INTERFACE_OTHER_HEADER = 'Local Interfaces';
91 INTERFACE_OTHER = 'Local';
93 @ViewChild('propertyInputTabs') propertyInputTabs: Tabs;
96 constructor(private workflowServiceNg2: WorkflowServiceNg2, private translateService: TranslateService) {
97 this.translateService.languageChangedObservable.subscribe(lang => {
98 this.propertyTooltipText = this.translateService.translate("OPERATION_PROPERTY_TOOLTIP_TEXT");
100 this.associationOptions = [
101 new DropDownOption(WORKFLOW_ASSOCIATION_OPTIONS.NONE, this.translateService.translate("NO_WORKFLOW_ASSOCIATION")),
102 new DropDownOption(WORKFLOW_ASSOCIATION_OPTIONS.EXISTING, this.translateService.translate("EXISTING_WORKFLOW_ASSOCIATION"))
105 this.workflowAssociationType = this.operation.workflowAssociationType || WORKFLOW_ASSOCIATION_OPTIONS.NONE;
108 this.currentTab = this.TYPE_INPUT;
111 createInterfaceDropdown(type: string) {
113 const lastDot = label.lastIndexOf('.');
115 label = label.substr(lastDot + 1);
117 return new TypedDropDownOption(type, label);
121 this.interfaceNames = _.map(
122 _.keys(this.interfaceTypes),
123 type => this.createInterfaceDropdown(type)
125 this.interfaceNames.unshift(new TypedDropDownOption('Existing Interfaces', 'Existing Interfaces', 1));
126 this.interfaceNames = this.interfaceNames.concat([
127 new TypedDropDownOption(' ', ' ', 3),
128 new TypedDropDownOption(this.INTERFACE_OTHER_HEADER, this.INTERFACE_OTHER_HEADER, 1),
129 new TypedDropDownOption(this.INTERFACE_OTHER)
132 const inputOperation = this.inputOperation;
133 this.operation = new OperationModel(inputOperation || {});
134 this.onSelectInterface(new DropDownOption(this.operation.interfaceType));
135 this.validityChanged();
137 if (this.enableWorkflowAssociation) {
138 this.isLoading = true;
139 this.workflowServiceNg2.getWorkflows().subscribe(workflows => {
140 this.isLoading = false;
141 this.workflows = _.map(
145 if (workflow.archiving === this.workflowServiceNg2.WF_STATE_ACTIVE) {
148 if (workflow.archiving === this.workflowServiceNg2.WF_STATE_ARCHIVED &&
149 workflow.id === this.operation.workflowId) {
150 this.archivedWorkflowId = workflow.id;
156 (workflow: any) => new DropdownValue(workflow.id, workflow.name)
158 this.reconstructOperation();
161 this.reconstructOperation();
165 reconstructOperation = () => {
166 const inputOperation = this.inputOperation;
167 if (inputOperation) {
168 if (this.enableWorkflowAssociation && inputOperation.workflowVersionId && this.isUsingExistingWF(inputOperation)) {
169 const sub = this.onSelectWorkflow(new DropDownOption(inputOperation.workflowId), inputOperation.workflowVersionId);
177 this.inputParameters = this.noAssignInputParameters;
178 this.outputParameters = this.noAssignOutputParameters;
183 if (inputOperation.uniqueId) {
184 this.isEditMode = true;
188 this.validityChanged();
191 buildParams = () => {
192 if (this.inputOperation.outputs) {
193 this.currentTab = this.TYPE_OUTPUT;
196 [...this.inputOperation.outputs.listToscaDataDefinition].sort((a, b) => a.name.localeCompare(b.name)),
197 (output: OperationParameter) => {
198 this.addParam(output);
202 this.currentTab = this.TYPE_INPUT;
204 if (this.inputOperation.inputs) {
206 [...this.inputOperation.inputs.listToscaDataDefinition].sort((a, b) => a.name.localeCompare(b.name)),
207 (input: OperationParameter) => {
208 this.addParam(input);
214 isInterfaceOther(): boolean {
215 return this.operation.interfaceType === this.INTERFACE_OTHER;
218 onSelectInterface(interf: IDropDownOption) {
219 if (interf && this.operation.interfaceType !== interf.value) {
220 this.operation.name = null;
222 this.operation.interfaceType = interf && interf.value;
223 this.operationNames = !this.operation.interfaceType ? [] : (
225 this.interfaceTypes[this.operation.interfaceType],
227 const existingOp = _.find(
230 interf => interf.type === this.operation.interfaceType
232 op => op.name === name
234 const ddType = (existingOp && existingOp.uniqueId !== this.operation.uniqueId) ? 2 : 0;
235 return new TypedDropDownOption(name, name, ddType);
239 this.validityChanged();
242 onSelectOperationName(name: IDropDownOption) {
244 this.operation.name = name.value;
246 this.validityChanged();
250 this.validityChanged();
253 get descriptionValue() {
254 return this.operation.description;
257 set descriptionValue(v) {
258 this.operation.description = v;
259 this.validityChanged();
262 onSelectWorkflow(workflowId: DropDownOption, selectedVersionId?: string): Subscription {
264 if (_.isUndefined(workflowId) || workflowId.value === this.operation.workflowId) {
267 this.operation.workflowId = workflowId.value;
268 if (!this.assignInputParameters[this.operation.workflowId]) {
269 this.assignInputParameters[this.operation.workflowId] = {};
270 this.assignOutputParameters[this.operation.workflowId] = {};
273 this.isLoading = true;
274 this.validityChanged();
275 return this.workflowServiceNg2.getWorkflowVersions(this.operation.workflowId).subscribe((versions: Array<any>) => {
276 this.isLoading = false;
278 this.workflowVersions = _.map(
280 versions, version => version.state === this.workflowServiceNg2.VERSION_STATE_CERTIFIED
281 ).sort((a, b) => a.name.localeCompare(b.name)),
283 if (!this.assignInputParameters[this.operation.workflowId][version.id] && version.id !== selectedVersionId) {
284 this.assignInputParameters[this.operation.workflowId][version.id] = _.map(version.inputs, (input: OperationParameter) => {
285 return new OperationParameter({...input, type: input.type.toLowerCase()});
287 .sort((a, b) => a.name.localeCompare(b.name));
289 this.assignOutputParameters[this.operation.workflowId][version.id] = _.map(version.outputs, (output: OperationParameter) => {
290 return new OperationParameter({...output, type: output.type.toLowerCase()});
292 .sort((a, b) => a.name.localeCompare(b.name));
294 return new DropdownValue(version.id, `V ${version.name}`);
298 if (selectedVersionId) {
299 this.assignInputParameters[this.operation.workflowId][selectedVersionId] = [];
300 this.assignOutputParameters[this.operation.workflowId][selectedVersionId] = [];
302 if (!selectedVersionId && this.workflowVersions.length) {
303 this.operation.workflowVersionId = _.last(this.workflowVersions).value;
306 this.changeWorkflowVersion(new DropDownOption(this.operation.workflowVersionId));
307 this.validityChanged();
312 changeWorkflowVersion(versionId: DropDownOption) {
314 if (_.isUndefined(versionId)) {
318 this.operation.workflowVersionId = versionId.value;
319 this.inputParameters = this.assignInputParameters[this.operation.workflowId][this.operation.workflowVersionId];
320 this.outputParameters = this.assignOutputParameters[this.operation.workflowId][this.operation.workflowVersionId];
322 this.validityChanged();
326 toggleAssociateWorkflow(type: DropDownOption) {
328 if (_.isUndefined(type)) {
332 this.operation.workflowAssociationType = type.value;
333 this.workflowAssociationType = this.operation.workflowAssociationType;
335 if (!this.isUsingExistingWF()) {
336 this.inputParameters = this.noAssignInputParameters;
337 this.outputParameters = this.noAssignOutputParameters;
339 if (!this.operation.workflowId || !this.operation.workflowVersionId) {
340 this.inputParameters = [];
341 this.outputParameters = [];
343 this.inputParameters = this.assignInputParameters[this.operation.workflowId][this.operation.workflowVersionId];
344 this.outputParameters = this.assignOutputParameters[this.operation.workflowId][this.operation.workflowVersionId];
349 this.validityChanged();
353 tabChanged = (event) => {
355 this.currentTab = event.title;
362 switch (this.currentTab) {
363 case this.TYPE_INPUT:
364 this.tableParameters = this.inputParameters;
366 case this.TYPE_OUTPUT:
367 this.tableParameters = this.outputParameters;
373 addParam(param?: OperationParameter): void {
374 this.validityChanged();
375 this.tableParameters.push(new OperationParameter(param));
378 canAdd = (): boolean => {
381 if (this.currentTab === this.TYPE_INPUT) {
382 _.forEach(this.inputParameters, param => {
383 if (!param.name || !param.inputId) {
388 _.forEach(this.outputParameters, param => {
389 if (!param.name || !param.type) {
399 isParamsValid = (): boolean => {
402 _.forEach(this.inputParameters, param => {
403 if (!param.name || !param.inputId) {
407 _.forEach(this.outputParameters, param => {
408 if (!param.name || !param.type) {
417 onRemoveParam = (param: OperationParameter): void => {
418 let index = _.indexOf(this.tableParameters, param);
419 this.tableParameters.splice(index, 1);
420 this.validityChanged();
423 createParamLists = () => {
424 this.operation.createInputsList(this.inputParameters);
425 this.operation.createOutputsList(this.outputParameters);
428 isUsingExistingWF = (operation?: OperationModel): boolean => {
429 operation = operation || this.operation;
430 return operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.EXISTING;
433 shouldCreateWF = (operation?: OperationModel): boolean => {
434 operation = operation || this.operation;
435 return operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.NEW;
438 checkFormValidForSubmit = (): boolean => {
439 return this.operation.name &&
440 (!this.isUsingExistingWF() || this.operation.workflowVersionId) &&
441 this.isParamsValid();
444 validityChanged = () => {
445 let validState = this.checkFormValidForSubmit();
446 this.validityChangedCallback(validState);