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 (typeof value === 'object') {
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;
158 this.ngUnsubscribe.next();
159 this.ngUnsubscribe.complete();
163 this.finalTemplates = new Map(this.selectedTemplates);
166 this.selectedTemplates = new Map<string, TemplateAndMapping>();
170 this.nodeTemplates = new NodeTemplate('');
171 // tslint:disable-next-line: variable-name
172 const node_templates = {};
173 const finalFunctionData = this.currentFuncion;
174 // tslint:disable-next-line: no-string-literal
175 const type = finalFunctionData['type'];
176 const instanceName = finalFunctionData['instance-name'];
177 // insert selected templates in nodeTemplates.artifacts
178 this.selectedTemplates.forEach((value, key) => {
181 console.log(finalFunctionData.inputs['artifact-prefix-names']);
183 if (finalFunctionData.inputs['artifact-prefix-names'] === undefined) {
184 finalFunctionData.inputs['artifact-prefix-names'] = [key];
186 Array.isArray(finalFunctionData.inputs['artifact-prefix-names']) &&
187 !finalFunctionData.inputs['artifact-prefix-names'].includes(key)
189 finalFunctionData.inputs['artifact-prefix-names'].push(key);
192 if (value.isMapping) {
193 this.nodeTemplates.artifacts[key + '-mapping'] = {
194 type: 'artifact-mapping-resource',
195 file: 'Templates/' + key + '-mapping.json'
199 if (value.isTemplate) {
200 this.nodeTemplates.artifacts[key + '-template'] = {
201 type: 'artifact-template-velocity',
202 file: 'Templates/' + key + '-template.vtl'
206 // instantiate the final node_template object to save
208 this.nodeTemplates.type = type;
209 delete this.nodeTemplates.properties;
210 node_templates[finalFunctionData['instance-name']] = this.nodeTemplates;
212 delete finalFunctionData['instance-name'];
213 // tslint:disable-next-line: no-string-literal
214 delete finalFunctionData['type'];
216 if (finalFunctionData.outputs === {} || Object.keys(finalFunctionData.outputs).length <= 0) {
217 delete finalFunctionData.outputs;
220 this.nodeTemplates.interfaces = {
221 [this.interfaceChildName]: {
224 ...finalFunctionData,
229 console.log(finalFunctionData);
230 console.log(node_templates);
231 // save function to store
232 // tslint:disable-next-line: no-unused-expression
233 this.designerStore.addNodeTemplate(instanceName, type, node_templates[instanceName]);
234 // create a new package
235 this.saveEvent.emit('save');
238 private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
239 const nameOfFile = isFromTemplate ?
240 key.split('/')[1].split('.')[0].split('-template')[0]
241 : key.split('/')[1].split('.')[0].split('-mapping')[0];
242 // const fullName = nameOfFile + ',' + key.split('.');
243 if (this.templateAndMappingMap.has(nameOfFile)) {
244 const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
245 !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
246 this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
248 this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
253 setArtifact(predefined: boolean) {
255 this.currentFuncion.inputs['artifact-prefix-names'] = [];
257 this.currentFuncion.inputs['artifact-prefix-names'] = { get_input: 'template-prefix' };
260 addToInputs(optionalInput) {
261 this.requiredInputs.set(optionalInput, this.OptionalInputs.get(optionalInput));
262 this.OptionalInputs.delete(optionalInput);
265 setTemplate(file: string) {
266 if (this.selectedTemplates.has(file)) {
267 this.selectedTemplates.delete(file);
269 this.selectedTemplates.set(file, this.templateAndMappingMap.get(file));
271 console.log(this.selectedTemplates);
274 getKeys(map: Map<string, any>) {
275 return Array.from(map.keys());
277 getValue(file: string, map: Map<string, any>) {
278 return map.get(file);
281 getObjectKey(object) {
282 // console.log(object);
283 return Object.keys(object);
285 getObjectValue(object) {
286 return Object.values(object);
289 getNodeType(nodeName: string) {
290 this.functionStore.state$
291 .subscribe(state => {
293 console.log(nodeName);
294 const functions = state.serverFunctions;
295 // tslint:disable-next-line: prefer-for-of
296 for (let i = 0; i < functions.length; i++) {
297 if (functions[i].modelName === nodeName) {
298 // tslint:disable: no-string-literal
299 console.log(functions[i].definition['interfaces']);
300 this.getInputFields(functions[i].definition['interfaces'], 'outputs');
301 this.getInputFields(functions[i].definition['interfaces'], 'inputs');
308 getInputFields(interfaces, type) {
310 if (type === 'inputs') {
311 this.requiredInputs = new Map<string, {}>();
312 this.OptionalInputs = new Map<string, {}>();
314 this.requiredOutputs = new Map<string, {}>();
315 this.optionalOutputs = new Map<string, {}>();
318 const nodeName = Object.keys(interfaces)[0];
319 this.interfaceChildName = nodeName;
320 console.log(nodeName + ' ------ ' + type);
321 console.log(interfaces[nodeName]['operations']['process'][type]);
322 const fields = interfaces[nodeName]['operations']['process'][type];
323 this.artifactPrefix = false;
324 for (const [key, value] of Object.entries(fields)) {
325 if (key === 'artifact-prefix-names') {
326 this.artifactPrefix = true;
327 // in edit&view mode need to check first if this input||output already exists
328 } else if (key in this.currentFuncion.inputs) {
329 this.requiredInputs.set(key, Object.assign({}, value));
330 } else if (key in this.currentFuncion.outputs) {
331 this.requiredOutputs.set(key, Object.assign({}, value));
332 } else if (value['required']) {
333 console.log('This field is required = ' + key);
334 if (type === 'inputs') {
335 this.requiredInputs.set(key, Object.assign({}, value));
337 this.requiredOutputs.set(key, Object.assign({}, value));
340 console.log('This field is Optional ' + key);
341 if (type === 'inputs') {
342 this.OptionalInputs.set(key, Object.assign({}, value));
344 this.optionalOutputs.set(key, Object.assign({}, value));
349 // console.log(this.requiredOutputs);