1 import { Component, OnDestroy, OnInit } 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>();
26 requiredInputs = new Map<string, {}>();
27 requiredOutputs = new Map<string, {}>();
28 OptionalInputs = new Map<string, {}>();
29 optionalOutputs = new Map<string, {}>();
30 artifactPrefix = false;
31 currentFuncion = new NodeProcess();
32 nodeTemplates = new NodeTemplate('');
33 designerState: DesignerDashboardState;
36 interfaceChildName = '';
40 private designerStore: DesignerStore,
41 private packageCreationStore: PackageCreationStore,
42 private functionStore: FunctionsStore
47 this.designerStore.state$
49 distinctUntilChanged((a: any, b: any) => JSON.stringify(a) === JSON.stringify(b)),
50 takeUntil(this.ngUnsubscribe))
51 .subscribe(designerDashboardState => {
52 this.designerDashboardState = designerDashboardState;
53 this.designerState = designerDashboardState;
54 this.actionName = this.designerState.actionName;
55 const action = this.designerState.template.workflows[this.actionName] as Action;
59 const child = Object.keys(action.steps)[0];
60 this.functionName = action.steps[child].target;
61 console.log(this.designerState.template.node_templates[this.functionName]);
62 // this.currentFuncion = this.designerState.template.node_templates[this.functionName];
63 // reset inouts&outputs
64 this.toNodeProcess(this.designerState.template.node_templates[this.functionName], this.functionName);
65 const type = this.designerState.template.node_templates[this.functionName].type;
66 this.getNodeType(type);
70 this.packageCreationStore.state$
71 .subscribe(cbaPackage => {
72 this.cbaPackage = cbaPackage;
73 console.log('File name =>================== ');
74 console.log(this.cbaPackage.templates.files);
75 this.cbaPackage.templates.files.forEach((value, key) => {
76 console.log('File name => ' + key);
77 const templateAndMapping = new TemplateAndMapping();
78 templateAndMapping.isTemplate = true;
79 const isFromTemplate = true;
80 this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
83 this.cbaPackage.mapping.files.forEach((value, key) => {
84 const templateAndMapping = new TemplateAndMapping();
85 templateAndMapping.isMapping = true;
86 const isFromTemplate = false;
87 this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
94 toNodeProcess(nodeTemplate, functionName) {
95 this.requiredInputs = new Map<string, {}>();
96 this.requiredOutputs = new Map<string, {}>();
97 this.OptionalInputs = new Map<string, {}>();
98 this.optionalOutputs = new Map<string, {}>();
99 console.log(nodeTemplate);
100 this.currentFuncion['instance-name'] = functionName;
101 // tslint:disable-next-line: no-string-literal
102 this.currentFuncion['type'] = nodeTemplate['type'];
103 if (nodeTemplate.interfaces && Object.keys(nodeTemplate.interfaces).length > 0) {
104 const nodeName = Object.keys(nodeTemplate.interfaces)[0];
105 // tslint:disable-next-line: no-string-literal
106 const inputs = nodeTemplate.interfaces[nodeName]['operations']['process']['inputs'];
107 // tslint:disable-next-line: no-string-literal
108 const outputs = nodeTemplate.interfaces[nodeName]['operations']['process']['outputs'];
111 for (const [key, value] of Object.entries(inputs)) {
112 console.log(key + '-' + value);
113 this.currentFuncion.inputs[key] = value;
117 for (const [key, value] of Object.entries(outputs)) {
118 console.log(key + '-' + value);
119 this.currentFuncion.outputs[key] = value;
125 this.ngUnsubscribe.next();
126 this.ngUnsubscribe.complete();
131 // tslint:disable-next-line: variable-name
132 const node_templates = {};
133 const finalFunctionData = this.currentFuncion;
134 // tslint:disable-next-line: no-string-literal
135 const type = finalFunctionData['type'];
136 const instanceName = finalFunctionData['instance-name'];
137 // insert selected templates in nodeTemplates.artifacts
138 this.selectedTemplates.forEach((value, key) => {
142 if (value.isMapping) {
143 this.nodeTemplates.artifacts[key + '-mapping'] = {
144 type: 'artifact-mapping-resource',
145 file: 'Templates/' + key + '-mapping.json'
149 if (value.isTemplate) {
150 this.nodeTemplates.artifacts[key + '-template'] = {
151 type: 'artifact-template-resource',
152 file: 'Templates/' + key + '-template.vtl'
156 // instantiate the final node_template object to save
158 this.nodeTemplates.type = type;
159 node_templates[finalFunctionData['instance-name']] = this.nodeTemplates;
161 delete finalFunctionData['instance-name'];
162 // tslint:disable-next-line: no-string-literal
163 delete finalFunctionData['type'];
165 this.nodeTemplates.interfaces = {
166 [this.interfaceChildName]: {
169 ...finalFunctionData,
175 console.log(finalFunctionData);
176 console.log(node_templates);
177 // tslint:disable-next-line: no-unused-expression
178 this.designerStore.addNodeTemplate(instanceName, type, node_templates[instanceName]);
181 private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
182 const nameOfFile = isFromTemplate ?
183 key.split('/')[1].split('.')[0].split('-template')[0]
184 : key.split('/')[1].split('.')[0].split('-mapping')[0];
185 // const fullName = nameOfFile + ',' + key.split('.');
186 if (this.templateAndMappingMap.has(nameOfFile)) {
187 const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
188 !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
189 this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
191 this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
197 setArtifact(predefined: boolean) {
201 this.currentFuncion.inputs['artifact-prefix-names'] = { get_input: 'template-prefix' };
204 addToInputs(optionalInput) {
205 this.requiredInputs.set(optionalInput, this.OptionalInputs.get(optionalInput));
206 this.OptionalInputs.delete(optionalInput);
209 setTemplate(file: string) {
210 if (this.selectedTemplates.has(file)) {
211 this.selectedTemplates.delete(file);
213 this.selectedTemplates.set(file, this.templateAndMappingMap.get(file));
215 console.log(this.selectedTemplates);
218 getKeys(map: Map<string, any>) {
219 return Array.from(map.keys());
221 getValue(file: string, map: Map<string, any>) {
222 return map.get(file);
225 getObjectKey(object) {
226 // console.log(object);
227 return Object.keys(object);
229 getObjectValue(object) {
230 return Object.values(object);
232 getNodeType(nodeName: string) {
233 this.functionStore.state$
234 .subscribe(state => {
236 console.log(nodeName);
237 const functions = state.serverFunctions;
238 // tslint:disable-next-line: prefer-for-of
239 for (let i = 0; i < functions.length; i++) {
240 if (functions[i].modelName === nodeName) {
241 // tslint:disable: no-string-literal
242 console.log(functions[i].definition['interfaces']);
243 this.getInputFields(functions[i].definition['interfaces'], 'outputs');
244 this.getInputFields(functions[i].definition['interfaces'], 'inputs');
251 getInputFields(interfaces, type) {
253 if (type === 'inputs') {
254 this.requiredInputs = new Map<string, {}>();
255 this.OptionalInputs = new Map<string, {}>();
257 this.requiredOutputs = new Map<string, {}>();
258 this.optionalOutputs = new Map<string, {}>();
261 const nodeName = Object.keys(interfaces)[0];
262 this.interfaceChildName = nodeName;
263 console.log(nodeName + ' ------ ' + type);
264 console.log(interfaces[nodeName]['operations']['process'][type]);
265 const fields = interfaces[nodeName]['operations']['process'][type];
266 this.artifactPrefix = false;
267 for (const [key, value] of Object.entries(fields)) {
268 if (key === 'artifact-prefix-names') {
269 this.artifactPrefix = true;
270 } else if (value['required']) {
271 console.log('This field is required = ' + key);
272 if (type === 'inputs') {
273 this.requiredInputs.set(key, Object.assign({}, value));
275 this.requiredOutputs.set(key, Object.assign({}, value));
278 console.log('This field is Optional ' + key);
279 if (type === 'inputs') {
280 this.OptionalInputs.set(key, Object.assign({}, value));
282 this.optionalOutputs.set(key, Object.assign({}, value));
287 // console.log(this.requiredOutputs);