1 import { Component, EventEmitter, OnDestroy, OnInit, Output } from '@angular/core';
2 import { DesignerStore } from '../designer.store';
3 import { PackageCreationStore } from '../../package-creation/package-creation.store';
4 import { Subject } from 'rxjs';
5 import { distinctUntilChanged, takeUntil } from 'rxjs/operators';
6 import { CBAPackage } from '../../package-creation/mapping-models/CBAPacakge.model';
7 import { TemplateAndMapping } from '../../package-creation/template-mapping/TemplateAndMapping';
8 import { FunctionsStore } from '../functions.store';
9 import { NodeProcess, NodeTemplate } from '../model/desinger.nodeTemplate.model';
10 import { DesignerDashboardState } from '../model/designer.dashboard.state';
11 import { Action } from '../action-attributes/models/Action';
14 selector: 'app-functions-attribute',
15 templateUrl: './functions-attribute.component.html',
16 styleUrls: ['./functions-attribute.component.css']
18 export class FunctionsAttributeComponent implements OnInit, OnDestroy {
20 ngUnsubscribe = new Subject();
21 designerDashboardState: DecodeSuccessCallback;
22 cbaPackage: CBAPackage;
23 templateAndMappingMap = new Map<string, TemplateAndMapping>();
24 selectedTemplates = new Map<string, TemplateAndMapping>();
25 finalTemplates = new Map<string, TemplateAndMapping>();
27 requiredInputs = new Map<string, {}>();
28 requiredOutputs = new Map<string, {}>();
29 OptionalInputs = new Map<string, {}>();
30 optionalOutputs = new Map<string, {}>();
31 artifactPrefix = false;
32 currentFuncion = new NodeProcess();
33 nodeTemplates = new NodeTemplate('');
34 designerState: DesignerDashboardState;
37 interfaceChildName = '';
38 @Output() saveEvent = new EventEmitter<string>();
42 private designerStore: DesignerStore,
43 private packageCreationStore: PackageCreationStore,
44 private functionStore: FunctionsStore
49 this.designerStore.state$.subscribe(designerDashboardState => {
50 this.designerState = designerDashboardState;
51 this.actionName = this.designerState.actionName;
52 const action = this.designerState.template.workflows[this.actionName] as Action;
53 this.currentFuncion = new NodeProcess();
57 // this.designerState.functionName
58 const child = Object.keys(action.steps)[0];
59 this.functionName = this.designerState.functionName;
60 console.log(this.designerState.template.node_templates);
61 console.log(this.designerState);
62 console.log(this.designerState.template.node_templates[this.functionName]);
63 // this.currentFuncion = this.designerState.template.node_templates[this.functionName];
64 // reset inouts&outputs
65 this.requiredInputs = new Map<string, {}>();
66 this.requiredOutputs = new Map<string, {}>();
67 this.OptionalInputs = new Map<string, {}>();
68 this.optionalOutputs = new Map<string, {}>();
69 this.toNodeProcess(this.designerState.template.node_templates[this.functionName], this.functionName);
70 const type = this.designerState.template.node_templates[this.functionName].type;
71 this.getNodeType(type);
77 this.packageCreationStore.state$
78 .subscribe(cbaPackage => {
79 this.cbaPackage = cbaPackage;
80 console.log('File name =>================== ');
81 console.log(this.cbaPackage.templates.files);
82 this.cbaPackage.templates.files.forEach((value, key) => {
83 console.log('File name => ' + key);
84 const templateAndMapping = new TemplateAndMapping();
85 templateAndMapping.isTemplate = true;
86 const isFromTemplate = true;
87 this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
90 this.cbaPackage.mapping.files.forEach((value, key) => {
91 const templateAndMapping = new TemplateAndMapping();
92 templateAndMapping.isMapping = true;
93 const isFromTemplate = false;
94 this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
101 // selectedTemplates , templateAndMappingMap
102 // this.selectedTemplates = new Map<string, TemplateAndMapping>();
104 const functionMap = this.designerState.template.node_templates[this.functionName].artifacts;
105 console.log(this.templateAndMappingMap);
107 Object.keys(functionMap).forEach((file) => {
108 const filename = file.substring(0, file.lastIndexOf('-'));
109 console.log(filename);
110 if (this.templateAndMappingMap.has(filename)) {
111 this.selectedTemplates.set(filename, this.templateAndMappingMap.get(filename));
112 this.finalTemplates.set(filename, this.templateAndMappingMap.get(filename));
121 this.selectedTemplates = new Map(this.finalTemplates);
124 toNodeProcess(nodeTemplate, functionName) {
125 console.log(nodeTemplate);
126 this.currentFuncion['instance-name'] = functionName;
127 // tslint:disable-next-line: no-string-literal
128 this.currentFuncion['type'] = nodeTemplate['type'];
129 if (nodeTemplate.interfaces && Object.keys(nodeTemplate.interfaces).length > 0) {
130 const nodeName = Object.keys(nodeTemplate.interfaces)[0];
131 // console.log(Object.keys(nodeTemplate.interfaces));
132 // tslint:disable-next-line: no-string-literal
133 const inputs = nodeTemplate.interfaces[nodeName]['operations']['process']['inputs'];
134 // tslint:disable-next-line: no-string-literal
135 const outputs = nodeTemplate.interfaces[nodeName]['operations']['process']['outputs'];
137 // console.log(inputs);
140 for (const [key, value] of Object.entries(inputs)) {
141 console.log(key + ' - ' + value);
142 if (this.isValidJson(value)) {
143 this.currentFuncion.inputs[key] = JSON.stringify(value);
145 this.currentFuncion.inputs[key] = value;
150 for (const [key, value] of Object.entries(outputs)) {
151 console.log(key + '-' + value);
152 this.currentFuncion.outputs[key] = value;
167 return JSON.stringify(json);
171 const val = JSON.parse(e.target.value);
172 this.currentFuncion.inputs[key] = {
177 this.ngUnsubscribe.next();
178 this.ngUnsubscribe.complete();
182 this.finalTemplates = new Map(this.selectedTemplates);
185 this.selectedTemplates = new Map<string, TemplateAndMapping>();
189 this.nodeTemplates = new NodeTemplate('');
190 // tslint:disable-next-line: variable-name
191 const node_templates = {};
192 const finalFunctionData = this.currentFuncion;
193 // tslint:disable-next-line: no-string-literal
194 const type = finalFunctionData['type'];
195 const instanceName = finalFunctionData['instance-name'];
196 // insert selected templates in nodeTemplates.artifacts
197 this.selectedTemplates.forEach((value, key) => {
200 console.log(finalFunctionData.inputs['artifact-prefix-names']);
202 if (finalFunctionData.inputs['artifact-prefix-names'] === undefined) {
203 finalFunctionData.inputs['artifact-prefix-names'] = [key];
205 Array.isArray(finalFunctionData.inputs['artifact-prefix-names']) &&
206 !finalFunctionData.inputs['artifact-prefix-names'].includes(key)
208 finalFunctionData.inputs['artifact-prefix-names'].push(key);
211 if (value.isMapping) {
212 this.nodeTemplates.artifacts[key + '-mapping'] = {
213 type: 'artifact-mapping-resource',
214 file: 'Templates/' + key + '-mapping.json'
218 if (value.isTemplate) {
219 this.nodeTemplates.artifacts[key + '-template'] = {
220 type: 'artifact-template-velocity',
221 file: 'Templates/' + key + '-template.vtl'
225 // instantiate the final node_template object to save
227 this.nodeTemplates.type = type;
228 delete this.nodeTemplates.properties;
229 node_templates[finalFunctionData['instance-name']] = this.nodeTemplates;
231 delete finalFunctionData['instance-name'];
232 // tslint:disable-next-line: no-string-literal
233 delete finalFunctionData['type'];
235 if (finalFunctionData.outputs === {} || Object.keys(finalFunctionData.outputs).length <= 0) {
236 delete finalFunctionData.outputs;
239 this.nodeTemplates.interfaces = {
240 [this.interfaceChildName]: {
243 ...finalFunctionData,
248 console.log(finalFunctionData);
249 console.log(node_templates);
250 // save function to store
251 // tslint:disable-next-line: no-unused-expression
252 this.designerStore.addNodeTemplate(instanceName, type, node_templates[instanceName]);
253 // create a new package
254 this.saveEvent.emit('save');
257 private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
258 const nameOfFile = isFromTemplate ?
259 key.split('/')[1].split('.')[0].split('-template')[0]
260 : key.split('/')[1].split('.')[0].split('-mapping')[0];
261 // const fullName = nameOfFile + ',' + key.split('.');
262 if (this.templateAndMappingMap.has(nameOfFile)) {
263 const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
264 !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
265 this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
267 this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
272 setArtifact(predefined: boolean) {
274 this.currentFuncion.inputs['artifact-prefix-names'] = [];
276 this.currentFuncion.inputs['artifact-prefix-names'] = { get_input: 'template-prefix' };
279 addToInputs(optionalInput) {
280 this.requiredInputs.set(optionalInput, this.OptionalInputs.get(optionalInput));
281 this.OptionalInputs.delete(optionalInput);
284 setTemplate(file: string) {
285 if (this.selectedTemplates.has(file)) {
286 this.selectedTemplates.delete(file);
288 this.selectedTemplates.set(file, this.templateAndMappingMap.get(file));
290 console.log(this.selectedTemplates);
293 getKeys(map: Map<string, any>) {
294 return Array.from(map.keys());
296 getValue(file: string, map: Map<string, any>) {
297 return map.get(file);
300 getObjectKey(object) {
301 // console.log(object);
302 return Object.keys(object);
304 getObjectValue(object) {
305 return Object.values(object);
308 getNodeType(nodeName: string) {
309 this.functionStore.state$
310 .subscribe(state => {
312 console.log(nodeName);
313 const functions = state.serverFunctions;
314 // tslint:disable-next-line: prefer-for-of
315 for (let i = 0; i < functions.length; i++) {
316 if (functions[i].modelName === nodeName) {
317 // tslint:disable: no-string-literal
318 console.log(functions[i].definition['interfaces']);
319 this.getInputFields(functions[i].definition['interfaces'], 'outputs');
320 this.getInputFields(functions[i].definition['interfaces'], 'inputs');
327 getInputFields(interfaces, type) {
329 if (type === 'inputs') {
330 this.requiredInputs = new Map<string, {}>();
331 this.OptionalInputs = new Map<string, {}>();
333 this.requiredOutputs = new Map<string, {}>();
334 this.optionalOutputs = new Map<string, {}>();
337 const nodeName = Object.keys(interfaces)[0];
338 this.interfaceChildName = nodeName;
339 console.log(nodeName + ' ------ ' + type);
340 console.log(interfaces[nodeName]['operations']['process'][type]);
341 const fields = interfaces[nodeName]['operations']['process'][type];
342 this.artifactPrefix = false;
343 for (const [key, value] of Object.entries(fields)) {
344 if (key === 'artifact-prefix-names') {
345 this.artifactPrefix = true;
346 // in edit&view mode need to check first if this input||output already exists
347 } else if (key in this.currentFuncion.inputs) {
348 this.requiredInputs.set(key, Object.assign({}, value));
349 } else if (key in this.currentFuncion.outputs) {
350 this.requiredOutputs.set(key, Object.assign({}, value));
351 } else if (value['required']) {
352 console.log('This field is required = ' + key);
353 if (type === 'inputs') {
354 this.requiredInputs.set(key, Object.assign({}, value));
356 this.requiredOutputs.set(key, Object.assign({}, value));
359 console.log('This field is Optional ' + key);
360 if (type === 'inputs') {
361 this.OptionalInputs.set(key, Object.assign({}, value));
363 this.optionalOutputs.set(key, Object.assign({}, value));
368 // console.log(this.requiredOutputs);