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 this.getNodeType(this.functionName);
69 this.packageCreationStore.state$
70 .subscribe(cbaPackage => {
71 this.cbaPackage = cbaPackage;
72 console.log('File name =>================== ');
73 console.log(this.cbaPackage.templates.files);
74 this.cbaPackage.templates.files.forEach((value, key) => {
75 console.log('File name => ' + key);
76 const templateAndMapping = new TemplateAndMapping();
77 templateAndMapping.isTemplate = true;
78 const isFromTemplate = true;
79 this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
82 this.cbaPackage.mapping.files.forEach((value, key) => {
83 const templateAndMapping = new TemplateAndMapping();
84 templateAndMapping.isMapping = true;
85 const isFromTemplate = false;
86 this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
93 toNodeProcess(nodeTemplate, functionName) {
94 this.requiredInputs = new Map<string, {}>();
95 this.requiredOutputs = new Map<string, {}>();
96 this.OptionalInputs = new Map<string, {}>();
97 this.optionalOutputs = new Map<string, {}>();
98 console.log(nodeTemplate);
99 this.currentFuncion['instance-name'] = functionName;
100 // tslint:disable-next-line: no-string-literal
101 this.currentFuncion['type'] = nodeTemplate['type'];
102 if (Object.keys(nodeTemplate.interfaces).length > 0) {
103 const nodeName = Object.keys(nodeTemplate.interfaces)[0];
104 // tslint:disable-next-line: no-string-literal
105 const inputs = nodeTemplate.interfaces[nodeName]['operations']['process']['inputs'];
106 // tslint:disable-next-line: no-string-literal
107 const outputs = nodeTemplate.interfaces[nodeName]['operations']['process']['outputs'];
110 for (const [key, value] of Object.entries(inputs)) {
111 console.log(key + '-' + value);
112 this.currentFuncion.inputs[key] = value;
116 for (const [key, value] of Object.entries(outputs)) {
117 console.log(key + '-' + value);
118 this.currentFuncion.outputs[key] = value;
124 this.ngUnsubscribe.next();
125 this.ngUnsubscribe.complete();
128 displayFunctionData() {
130 // tslint:disable-next-line: variable-name
131 const node_templates = {};
132 // tslint:disable-next-line: no-string-literal
133 const type = this.currentFuncion['type'];
134 const instanceName = this.currentFuncion['instance-name'];
135 // insert selected templates in nodeTemplates.artifacts
136 this.selectedTemplates.forEach((value, key) => {
140 if (value.isMapping) {
141 this.nodeTemplates.artifacts[key + '-mapping'] = {
142 type: 'artifact-mapping-resource',
143 file: 'Templates/' + key + '-mapping.json'
147 if (value.isTemplate) {
148 this.nodeTemplates.artifacts[key + '-template'] = {
149 type: 'artifact-template-resource',
150 file: 'Templates/' + key + '-template.vtl'
154 // instantiate the final node_template object to save
156 this.nodeTemplates.type = type;
157 node_templates[this.currentFuncion['instance-name']] = this.nodeTemplates;
159 delete this.currentFuncion['instance-name'];
160 // tslint:disable-next-line: no-string-literal
161 delete this.currentFuncion['type'];
163 this.nodeTemplates.interfaces = {
164 [this.interfaceChildName]: {
167 ...this.currentFuncion,
173 console.log(this.currentFuncion);
174 console.log(node_templates);
175 // tslint:disable-next-line: no-unused-expression
176 this.designerStore.addNodeTemplate(instanceName, type, node_templates[instanceName]);
179 private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
180 const nameOfFile = isFromTemplate ?
181 key.split('/')[1].split('.')[0].split('-template')[0]
182 : key.split('/')[1].split('.')[0].split('-mapping')[0];
183 // const fullName = nameOfFile + ',' + key.split('.');
184 if (this.templateAndMappingMap.has(nameOfFile)) {
185 const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
186 !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
187 this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
189 this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
195 setArtifact(predefined: boolean) {
199 this.currentFuncion.inputs['artifact-prefix-names'] = { get_input: 'template-prefix' };
202 addToInputs(optionalInput) {
203 this.requiredInputs.set(optionalInput, this.OptionalInputs.get(optionalInput));
204 this.OptionalInputs.delete(optionalInput);
207 setTemplate(file: string) {
208 if (this.selectedTemplates.has(file)) {
209 this.selectedTemplates.delete(file);
211 this.selectedTemplates.set(file, this.templateAndMappingMap.get(file));
213 console.log(this.selectedTemplates);
216 getKeys(map: Map<string, any>) {
217 return Array.from(map.keys());
219 getValue(file: string, map: Map<string, any>) {
220 return map.get(file);
223 getObjectKey(object) {
224 // console.log(object);
225 return Object.keys(object);
227 getObjectValue(object) {
228 return Object.values(object);
230 getNodeType(nodeName: string) {
231 this.functionStore.state$
232 .subscribe(state => {
234 const functions = state.serverFunctions;
235 // tslint:disable-next-line: prefer-for-of
236 for (let i = 0; i < functions.length; i++) {
237 if (functions[i].modelName === nodeName) {
238 // tslint:disable: no-string-literal
239 console.log(functions[i].definition['interfaces']);
240 this.getInputFields(functions[i].definition['interfaces'], 'outputs');
241 this.getInputFields(functions[i].definition['interfaces'], 'inputs');
248 getInputFields(interfaces, type) {
250 if (type === 'inputs') {
251 this.requiredInputs = new Map<string, {}>();
252 this.OptionalInputs = new Map<string, {}>();
254 this.requiredOutputs = new Map<string, {}>();
255 this.optionalOutputs = new Map<string, {}>();
258 const nodeName = Object.keys(interfaces)[0];
259 this.interfaceChildName = nodeName;
260 console.log(interfaces[nodeName]['operations']['process'][type]);
261 const fields = interfaces[nodeName]['operations']['process'][type];
262 this.artifactPrefix = false;
263 for (const [key, value] of Object.entries(fields)) {
264 if (key === 'artifact-prefix-names') {
265 this.artifactPrefix = true;
266 } else if (value['required']) {
267 console.log('This field is required = ' + key);
268 if (type === 'inputs') {
269 this.requiredInputs.set(key, Object.assign({}, value));
271 this.requiredOutputs.set(key, Object.assign({}, value));
274 console.log('This field is Optional ' + key);
275 if (type === 'inputs') {
276 this.OptionalInputs.set(key, Object.assign({}, value));
278 this.optionalOutputs.set(key, Object.assign({}, value));
283 // console.log(this.requiredOutputs);