1 import {Component, OnInit} from '@angular/core';
2 import {InputActionAttribute, OutputActionAttribute} from './models/InputActionAttribute';
3 import {DesignerStore} from '../designer.store';
4 import {DesignerDashboardState} from '../model/designer.dashboard.state';
5 import {Action} from './models/Action';
6 import {FunctionsStore} from '../functions.store';
7 import {FunctionsState} from '../model/functions.state';
8 import {PackageCreationStore} from '../../package-creation/package-creation.store';
9 import {CBAPackage} from '../../package-creation/mapping-models/CBAPacakge.model';
12 selector: 'app-action-attributes',
13 templateUrl: './action-attributes.component.html',
14 styleUrls: ['./action-attributes.component.css']
16 export class ActionAttributesComponent implements OnInit {
22 actionAttributesSideBar: boolean;
23 inputActionAttribute = new InputActionAttribute();
24 outputActionAttribute = new OutputActionAttribute();
25 isInputOtherType: boolean;
26 isOutputOtherType: boolean;
30 designerState: DesignerDashboardState;
31 isFunctionAttributeActive = false;
32 functions: FunctionsState;
34 suggestedInputs: string[] = [];
35 suggestedOutputs: string[] = [];
37 tempInputs: string[] = [];
38 tempOutputs: string[] = [];
39 currentInterfaceName: string;
40 functionAndAttributesInput: Map<string, string[]> = new Map<string, string[]>();
41 private currentTargetFunctionName: any;
42 private functionAndAttributesOutput: Map<string, string[]> = new Map<string, string[]>();
43 suggestedAttributes: string[] = [];
44 selectedFunctionName = '';
45 selectedAttributeName = '';
46 isNotComponentResourceResolution = true;
47 currentArtifacts: string[] = [];
48 isParametersHidden = true;
49 cbaPackage: CBAPackage;
50 suggestedMappingParameters: string[] = [];
51 selectedParameterList: string[] = [];
52 currentSuggestedArtifact: string;
53 suggestedDeletedInput: any = {};
54 suggestedEditedAttribute: any = {};
56 constructor(private designerStore: DesignerStore,
57 private functionsStore: FunctionsStore,
58 private packageCreationStore: PackageCreationStore) {
63 console.log('is paramters hidden' + this.isParametersHidden);
64 console.log('is artifacts hidden' + this.isNotComponentResourceResolution);
65 this.designerStore.state$.subscribe(designerState => {
66 this.designerState = designerState;
67 if (this.designerState && this.designerState.actionName) {
68 this.actionName = this.designerState.actionName;
69 console.log(this.actionName);
70 const action = this.designerState.template.workflows[this.actionName] as Action;
72 const steps = Object.keys(action.steps);
73 this.isFunctionAttributeActive = steps && steps.length > 0;
75 this.suggestedOutputs = [];
76 this.suggestedInputs = [];
80 const namesOfInput = Object.keys(action.inputs);
81 this.inputs = this.extractFields(namesOfInput, action.inputs);
85 const namesOfOutput = Object.keys(action.outputs);
86 this.outputs = this.extractFields(namesOfOutput, action.outputs);
91 this.functionsStore.state$.subscribe(functions => {
92 this.functions = functions;
95 this.packageCreationStore.state$
96 .subscribe(cbaPackage => {
97 this.cbaPackage = cbaPackage;
103 private extractFields(namesOfOutput: string[], container: {}) {
105 for (const nameOutput of namesOfOutput) {
106 const fieldAttribute = new OutputActionAttribute();
107 fieldAttribute.name = nameOutput;
108 fieldAttribute.description = container[nameOutput].description;
109 fieldAttribute.required = container[nameOutput].required;
110 fieldAttribute.type = container[nameOutput].type;
111 fieldAttribute.value = container[nameOutput].value;
112 console.log(fieldAttribute.value);
113 const insertedOutputActionAttribute = Object.assign({}, fieldAttribute);
114 fields.push(insertedOutputActionAttribute);
119 addInput(input: InputActionAttribute) {
121 if (input && input.type && input.name) {
122 const insertedInputActionAttribute = Object.assign({}, input);
123 if (!this.newInputs.some(obj => obj.name === input.name)) {
124 this.newInputs.push(insertedInputActionAttribute);
129 addOutput(output: OutputActionAttribute) {
131 if (output && output.type && output.name) {
132 const insertedOutputActionAttribute = Object.assign({}, output);
133 this.newOutputs.push(insertedOutputActionAttribute);
137 setInputType(type: string) {
138 this.inputActionAttribute.type = type;
139 this.isInputOtherType = this.checkIfTypeIsOther(type);
142 setInputRequired(isRequired) {
143 this.inputActionAttribute.required = isRequired;
146 setOutputRequired(isRequired) {
147 this.outputActionAttribute.required = isRequired;
150 setOutputType(type: string) {
151 this.outputActionAttribute.type = type;
152 this.isOutputOtherType = this.checkIfTypeIsOther(type);
155 checkIfTypeIsOther(type) {
156 return type.includes('Other');
160 this.addInput(this.inputActionAttribute);
161 if (this.selectedFunctionName && this.selectedAttributeName) {
162 console.log(this.getValue());
163 this.outputActionAttribute.value =
166 this.addOutput(this.outputActionAttribute);
167 this.clearFormInputs();
168 this.storeOutputs(this.newOutputs);
169 this.storeInputs((this.newInputs));
170 this.newInputs.forEach(input => {
171 if (!this.inputs.includes(input.name)) {
172 this.inputs.push(input);
176 this.newOutputs.forEach(output => {
177 if (!this.outputs.includes(output)) {
178 this.outputs.push(output);
182 this.newOutputs = [];
186 let value = '["' + this.selectedFunctionName + '", "' + '","' + this.selectedAttributeName;
188 if (!this.isParametersHidden) {
189 let currentSelected = '';
190 for (const selectedParameter of this.selectedParameterList) {
191 currentSelected += '"' + selectedParameter + '",';
193 value += '","' + this.currentSuggestedArtifact + '",'
194 + currentSelected.substr(0, currentSelected.length - 2) + '';
195 } else if (!this.isNotComponentResourceResolution && this.currentSuggestedArtifact) {
196 value += '","' + this.currentSuggestedArtifact + '';
200 return value += '"]';
203 public clearFormInputs() {
204 console.log('trying to clear ');
205 this.inputActionAttribute = new InputActionAttribute();
206 this.outputActionAttribute = new OutputActionAttribute();
207 this.outputOtherType = '';
208 this.inputOtherType = '';
211 private storeInputs(InputActionAttributes: InputActionAttribute[]) {
214 InputActionAttributes.forEach(input => {
215 inputs += this.appendAttributes(input);
218 if (inputs.length > 0) {
219 this.writeAttribute(inputs, 'inputs');
223 private storeOutputs(OutputActionAttributes: OutputActionAttribute[]) {
225 OutputActionAttributes.forEach(output => {
226 outputs += this.appendOutputAttributes(output);
228 if (outputs.length > 0) {
229 this.writeAttribute(outputs, 'outputs');
234 private appendAttributes(inputActionAttribute: InputActionAttribute) {
235 const entrySchema: string = this.getEntrySchema(inputActionAttribute);
236 const input = '"' + inputActionAttribute.name + '" : {\n' +
237 ' "required" : ' + inputActionAttribute.required + ',\n' +
238 ' "type" : "' + inputActionAttribute.type + '",\n' +
239 ' "description" : "' + inputActionAttribute.description + '"';
240 return input + entrySchema;
243 private getEntrySchema(inputActionAttribute: InputActionAttribute) {
244 return inputActionAttribute.type.includes('list') ?
245 ',\n\t' + '"entry_schema" : {\n' +
246 ' "type" : "string"\n' +
252 setInputAndOutputs(targetName) {
253 console.log(targetName);
254 const nodeTemplate = this.designerState.template.node_templates[targetName];
255 console.log(this.designerState.template.node_templates);
256 console.log(nodeTemplate);
257 /* tslint:disable:no-string-literal */
258 console.log(nodeTemplate['type']);
259 this.functions.serverFunctions
260 /* tslint:disable:no-string-literal */
261 .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
262 .forEach(currentFunction => {
263 console.log(currentFunction);
264 /* tslint:disable:no-string-literal */
265 if (currentFunction['definition'] && currentFunction['definition']['interfaces']) {
266 const interfaces = Object.keys(currentFunction['definition']['interfaces']);
267 if (interfaces && interfaces.length > 0) {
268 const interfaceName = interfaces[0];
269 console.log(interfaceName);
270 this.currentInterfaceName = interfaceName;
272 if (!this.functionAndAttributesInput.has(targetName)) {
273 this.currentTargetFunctionName = targetName;
274 this.functionAndAttributesInput.set(targetName, []);
277 if (!this.functionAndAttributesOutput.has(targetName)) {
278 this.currentTargetFunctionName = targetName;
279 this.functionAndAttributesOutput.set(targetName, []);
282 if (nodeTemplate['interfaces'] &&
283 nodeTemplate['interfaces'][interfaceName]['operations'] &&
284 nodeTemplate['interfaces'][interfaceName]['operations']['process']
287 if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['inputs']) {
288 /* tslint:disable:no-string-literal */
289 this.suggestedInputs = Object.keys(nodeTemplate['interfaces']
290 [interfaceName]['operations']['process']['inputs']);
291 this.filterTwoCollections(this.suggestedInputs, this.inputs);
294 if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['outputs']) {
295 /* tslint:disable:no-string-literal */
296 this.suggestedOutputs = Object.keys(nodeTemplate['interfaces']
297 [interfaceName]['operations']['process']['outputs']);
298 this.filterTwoCollections(this.suggestedOutputs, this.outputs);
305 console.log(nodeTemplate);
308 private filterTwoCollections(suggestedInputs: string[], inputs: any[]) {
309 inputs.forEach(input => {
310 if (suggestedInputs.includes(input.name)) {
311 this.deleteAttribute(suggestedInputs, input.name);
317 this.setInputAndOutputs(
318 this.designerState.template.workflows[this.actionName]['steps'][this.steps[0]]['target']
322 addTempInput(suggestedInput: string) {
323 this.addAttribute(this.tempInputs, suggestedInput);
324 this.deleteAttribute(this.suggestedInputs, suggestedInput);
325 this.addAttribute(this.functionAndAttributesInput.get(this.currentTargetFunctionName), suggestedInput);
328 addTempOutput(suggestedOutput: string) {
329 this.addAttribute(this.tempOutputs, suggestedOutput);
330 this.deleteAttribute(this.suggestedOutputs, suggestedOutput);
331 this.addAttribute(this.functionAndAttributesOutput.get(this.currentTargetFunctionName), suggestedOutput);
334 deleteAttribute(container: string[], suggestedAttribute: string) {
335 if (container && suggestedAttribute && container.includes(suggestedAttribute)) {
336 const index: number = container.indexOf(suggestedAttribute);
339 container.splice(index, 1);
344 addAttribute(container: string[], suggestedAttribute: string) {
345 if (container && suggestedAttribute && !container.includes(suggestedAttribute)) {
346 container.push(suggestedAttribute);
351 submitTempAttributes() {
352 this.writeSelectedAttribute(this.functionAndAttributesInput, 'inputs');
353 this.writeSelectedAttribute(this.functionAndAttributesOutput, 'outputs');
356 private writeSelectedAttribute(map: Map<string, string[]>, attributeType: string) {
357 map.forEach((value, key) => {
358 const nodeTemplate = this.getNodeTemplate(key);
359 this.functions.serverFunctions
360 /* tslint:disable:no-string-literal */
361 .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
362 .forEach(currentFunction => {
364 if (currentFunction['definition'] && currentFunction['definition']['interfaces']
365 [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
366 ['operations']['process'][attributeType]) {
367 let newAttributes = '';
368 const attributes = currentFunction['definition'] && currentFunction['definition']['interfaces']
369 [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
370 ['operations']['process'][attributeType];
371 value.forEach(attribute => {
372 newAttributes += '"' + attribute + '": ' + this.convertToString(attributes[attribute]) + ',';
374 if (value.length > 0) {
375 this.writeAttribute(newAttributes, attributeType);
382 private writeAttribute(newAttributes: string, attributeType: string) {
383 newAttributes = this.removeTheLastComma(newAttributes);
384 console.log(newAttributes);
385 let originalAttributes = this.convertToString(this.designerState.template.workflows[this.actionName]
387 this.createAttributeTypeIfNotExisted(originalAttributes, attributeType);
388 originalAttributes = this.convertToString(this.designerState.template.workflows[this.actionName]
390 if (originalAttributes.length > 2) {
391 this.designerState.template.workflows[this.actionName][attributeType] =
392 this.convertToObject(originalAttributes.substr(0, originalAttributes.length - 1)
393 + ',' + newAttributes + '}');
395 this.designerState.template.workflows[this.actionName][attributeType] =
396 this.convertToObject(originalAttributes.substr(0, originalAttributes.length - 1)
397 + newAttributes + '}');
402 private removeTheLastComma(newInputs: string): string {
403 if (newInputs.endsWith(',')) {
404 newInputs = newInputs.substr(0, newInputs.length - 1);
409 private convertToString = object => JSON.stringify(object);
411 private convertToObject = stringValue => JSON.parse(stringValue);
413 private getNodeTemplate = (value: string) => this.designerState.template.node_templates[value];
416 getAttributesAndOutputs(functionName: string) {
417 this.suggestedAttributes = [];
418 console.log(functionName);
420 const nodeTemplate = this.designerState.template.node_templates[functionName];
421 if (nodeTemplate['type'].includes('component-resource-resolution')) {
422 this.isNotComponentResourceResolution = false;
423 this.isParametersHidden = true;
425 this.isNotComponentResourceResolution = true;
426 this.isParametersHidden = true;
428 /* tslint:disable:no-string-literal */
429 console.log(nodeTemplate['type']);
430 this.functions.serverFunctions
431 /* tslint:disable:no-string-literal */
432 .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
433 .forEach(currentFunction => {
434 if (currentFunction.definition['attributes']) {
435 Object.keys(currentFunction.definition['attributes']).forEach(attribute => {
436 this.suggestedAttributes.push(attribute);
439 console.log(this.suggestedAttributes);
441 this.selectedFunctionName = functionName;
447 addTempOutputAttr(suggestedOutputAndAttribute: string) {
449 this.selectedAttributeName = suggestedOutputAndAttribute;
450 this.currentArtifacts = [];
451 const nodeTemplate = this.designerState.template.node_templates[this.selectedFunctionName];
452 if (nodeTemplate['artifacts']
454 Object.keys(nodeTemplate['artifacts']).forEach(key => {
455 const mappingName = key.split('-')[0];
456 if (!this.currentArtifacts.includes(mappingName)) {
457 this.currentArtifacts.push(mappingName);
461 console.log('happend');
466 private appendOutputAttributes(outputActionAttribute: OutputActionAttribute) {
467 const entrySchema: string = this.getEntrySchema(outputActionAttribute);
468 const output = '"' + outputActionAttribute.name + '" : {\n' +
469 ' "required" : ' + outputActionAttribute.required + ',\n' +
470 ' "type" : "' + outputActionAttribute.type + '",\n' +
471 ' "description" : "' + outputActionAttribute.description + '",\n' +
472 ' "value\" :' + '{\n' +
473 ' "get_attribute" : ' + outputActionAttribute.value + '\n' +
476 return output + entrySchema;
480 addArtifactFile(suggestedArtifact: string) {
481 this.currentSuggestedArtifact = suggestedArtifact;
482 this.isParametersHidden = !this.selectedAttributeName.includes('assignment-map');
483 if (!this.isParametersHidden) {
484 this.suggestedMappingParameters = this.getSuggestedMappingParameters(suggestedArtifact);
488 private getSuggestedMappingParameters(suggestedArtifact: string) {
489 const suggestedMappingParameters = [];
491 this.cbaPackage.mapping.files.forEach(((value, key) => {
492 if (key.includes(suggestedArtifact)) {
494 JSON.parse(value).forEach(value2 => {
495 suggestedMappingParameters.push(value2['name']);
499 return suggestedMappingParameters;
502 addSuggestedMappingParameter(suggestedMappingParameter: string) {
503 this.addAttribute(this.selectedParameterList, suggestedMappingParameter);
504 this.deleteAttribute(this.suggestedMappingParameters, suggestedMappingParameter);
508 editAttribute(input: any) {
509 this.suggestedEditedAttribute = input;
512 private createAttributeTypeIfNotExisted(originalAttributes: string, attributeType: string) {
513 if (!originalAttributes) {
514 this.designerState.template.workflows[this.actionName][attributeType] = {};
518 private checkIfTypeIsList(type: string) {
519 return type.includes('list');
522 markDeletedInput(input: any) {
523 this.suggestedDeletedInput = input;
526 deleteActionAttribute() {
527 delete this.designerState.template.workflows[this.actionName]
528 ['inputs'][this.suggestedDeletedInput.name];
529 this.deleteAttribute(this.inputs, this.suggestedDeletedInput);
531 delete this.designerState.template.workflows[this.actionName]
532 ['outputs'][this.suggestedDeletedInput.name];
533 this.deleteAttribute(this.outputs, this.suggestedDeletedInput);