aboutsummaryrefslogtreecommitdiffstats
path: root/cds-ui/designer-client/src/app/modules/feature-modules/packages/designer/functions-attribute/functions-attribute.component.ts
blob: 8780621ea9405ed83d417a1a8f491179242bd0e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import { Component, EventEmitter, OnDestroy, OnInit, Output } from '@angular/core';
import { DesignerStore } from '../designer.store';
import { PackageCreationStore } from '../../package-creation/package-creation.store';
import { Subject } from 'rxjs';
import { distinctUntilChanged, takeUntil } from 'rxjs/operators';
import { CBAPackage } from '../../package-creation/mapping-models/CBAPacakge.model';
import { TemplateAndMapping } from '../../package-creation/template-mapping/TemplateAndMapping';
import { FunctionsStore } from '../functions.store';
import { NodeProcess, NodeTemplate } from '../model/desinger.nodeTemplate.model';
import { DesignerDashboardState } from '../model/designer.dashboard.state';
import { Action } from '../action-attributes/models/Action';

@Component({
    selector: 'app-functions-attribute',
    templateUrl: './functions-attribute.component.html',
    styleUrls: ['./functions-attribute.component.css']
})
export class FunctionsAttributeComponent implements OnInit, OnDestroy {

    ngUnsubscribe = new Subject();
    designerDashboardState: DecodeSuccessCallback;
    cbaPackage: CBAPackage;
    templateAndMappingMap = new Map<string, TemplateAndMapping>();
    selectedTemplates = new Map<string, TemplateAndMapping>();
    finalTemplates = new Map<string, TemplateAndMapping>();
    fileToDelete: string;
    requiredInputs = new Map<string, {}>();
    requiredOutputs = new Map<string, {}>();
    OptionalInputs = new Map<string, {}>();
    optionalOutputs = new Map<string, {}>();
    artifactPrefix = false;
    currentFuncion = new NodeProcess();
    nodeTemplates = new NodeTemplate('');
    designerState: DesignerDashboardState;
    actionName = '';
    functionName = '';
    interfaceChildName = '';
    @Output() saveEvent = new EventEmitter<string>();


    constructor(
        private designerStore: DesignerStore,
        private packageCreationStore: PackageCreationStore,
        private functionStore: FunctionsStore
    ) {
    }

    ngOnInit() {
        this.designerStore.state$.subscribe(designerDashboardState => {
            this.designerState = designerDashboardState;
            this.actionName = this.designerState.actionName;
            const action = this.designerState.template.workflows[this.actionName] as Action;
            this.currentFuncion = new NodeProcess();
            try {
                console.log(action);
                if (action) {
                    // this.designerState.functionName
                    const child = Object.keys(action.steps)[0];
                    this.functionName = this.designerState.functionName;
                    console.log(this.designerState.template.node_templates);
                    console.log(this.designerState);
                    console.log(this.designerState.template.node_templates[this.functionName]);
                    //  this.currentFuncion = this.designerState.template.node_templates[this.functionName];
                    // reset inouts&outputs
                    this.requiredInputs = new Map<string, {}>();
                    this.requiredOutputs = new Map<string, {}>();
                    this.OptionalInputs = new Map<string, {}>();
                    this.optionalOutputs = new Map<string, {}>();
                    this.toNodeProcess(this.designerState.template.node_templates[this.functionName], this.functionName);
                    const type = this.designerState.template.node_templates[this.functionName].type;
                    this.getNodeType(type);
                    this.onInitMapping();
                }
            } catch (e) { }
        });

        this.packageCreationStore.state$
            .subscribe(cbaPackage => {
                this.cbaPackage = cbaPackage;
                console.log('File name =>================== ');
                console.log(this.cbaPackage.templates.files);
                this.cbaPackage.templates.files.forEach((value, key) => {
                    console.log('File name => ' + key);
                    const templateAndMapping = new TemplateAndMapping();
                    templateAndMapping.isTemplate = true;
                    const isFromTemplate = true;
                    this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
                });

                this.cbaPackage.mapping.files.forEach((value, key) => {
                    const templateAndMapping = new TemplateAndMapping();
                    templateAndMapping.isMapping = true;
                    const isFromTemplate = false;
                    this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
                });
            });

    }

    onInitMapping() {
        // selectedTemplates , templateAndMappingMap
        // this.selectedTemplates = new Map<string, TemplateAndMapping>();
        try {
            const functionMap = this.designerState.template.node_templates[this.functionName].artifacts;
            console.log(this.templateAndMappingMap);

            Object.keys(functionMap).forEach((file) => {
                const filename = file.substring(0, file.lastIndexOf('-'));
                console.log(filename);
                if (this.templateAndMappingMap.has(filename)) {
                    this.selectedTemplates.set(filename, this.templateAndMappingMap.get(filename));
                    this.finalTemplates.set(filename, this.templateAndMappingMap.get(filename));
                }
            });


        } catch (e) { }
    }

    init() {
        this.selectedTemplates = new Map(this.finalTemplates);
    }

    toNodeProcess(nodeTemplate, functionName) {
        console.log(nodeTemplate);
        this.currentFuncion['instance-name'] = functionName;
        // tslint:disable-next-line: no-string-literal
        this.currentFuncion['type'] = nodeTemplate['type'];
        if (nodeTemplate.interfaces && Object.keys(nodeTemplate.interfaces).length > 0) {
            const nodeName = Object.keys(nodeTemplate.interfaces)[0];
            // console.log(Object.keys(nodeTemplate.interfaces));
            // tslint:disable-next-line: no-string-literal
            const inputs = nodeTemplate.interfaces[nodeName]['operations']['process']['inputs'];
            // tslint:disable-next-line: no-string-literal
            const outputs = nodeTemplate.interfaces[nodeName]['operations']['process']['outputs'];

            // console.log(inputs);

            if (inputs) {
                for (const [key, value] of Object.entries(inputs)) {
                    console.log(key + ' - ' + value);
                    if (this.isValidJson(value)) {
                        this.currentFuncion.inputs[key] = JSON.stringify(value);
                    } else {
                        this.currentFuncion.inputs[key] = value;
                    }
                }
            }
            if (outputs) {
                for (const [key, value] of Object.entries(outputs)) {
                    console.log(key + '-' + value);
                    this.currentFuncion.outputs[key] = value;
                }
            }
        }
    }

    isValidJson(val) {
        try {
            JSON.parse(val);
            return true;
        } catch (e) { }
        return false;
    }

    jsonToStr(json) {
        return JSON.stringify(json);
    }

    bind(key, e) {
        const val = JSON.parse(e.target.value);
        this.currentFuncion.inputs[key] = {
            ...val
        };
    }
    ngOnDestroy() {
        this.ngUnsubscribe.next();
        this.ngUnsubscribe.complete();
    }

    addTemplates() {
        this.finalTemplates = new Map(this.selectedTemplates);
    }
    cancel() {
        this.selectedTemplates = new Map<string, TemplateAndMapping>();
    }

    saveFunctionData() {
        this.nodeTemplates = new NodeTemplate('');
        // tslint:disable-next-line: variable-name
        const node_templates = {};
        const finalFunctionData = this.currentFuncion;
        // tslint:disable-next-line: no-string-literal
        const type = finalFunctionData['type'];
        const instanceName = finalFunctionData['instance-name'];
        // insert selected templates in nodeTemplates.artifacts
        this.selectedTemplates.forEach((value, key) => {
            console.log(key);
            console.log(value);
            console.log(finalFunctionData.inputs['artifact-prefix-names']);

            if (finalFunctionData.inputs['artifact-prefix-names'] === undefined) {
                finalFunctionData.inputs['artifact-prefix-names'] = [key];
            } else if (
                Array.isArray(finalFunctionData.inputs['artifact-prefix-names']) &&
                !finalFunctionData.inputs['artifact-prefix-names'].includes(key)
            ) {
                finalFunctionData.inputs['artifact-prefix-names'].push(key);
            }

            if (value.isMapping) {
                this.nodeTemplates.artifacts[key + '-mapping'] = {
                    type: 'artifact-mapping-resource',
                    file: 'Templates/' + key + '-mapping.json'
                };
            }

            if (value.isTemplate) {
                this.nodeTemplates.artifacts[key + '-template'] = {
                    type: 'artifact-template-velocity',
                    file: 'Templates/' + key + '-template.vtl'
                };
            }
        });
        // instantiate the final node_template object to save

        this.nodeTemplates.type = type;
        delete this.nodeTemplates.properties;
        node_templates[finalFunctionData['instance-name']] = this.nodeTemplates;

        delete finalFunctionData['instance-name'];
        // tslint:disable-next-line: no-string-literal
        delete finalFunctionData['type'];

        if (finalFunctionData.outputs === {} || Object.keys(finalFunctionData.outputs).length <= 0) {
            delete finalFunctionData.outputs;
        }

        this.nodeTemplates.interfaces = {
            [this.interfaceChildName]: {
                operations: {
                    process: {
                        ...finalFunctionData,
                    }
                }
            }
        };
        console.log(finalFunctionData);
        console.log(node_templates);
        // save function to store
        // tslint:disable-next-line: no-unused-expression
        this.designerStore.addNodeTemplate(instanceName, type, node_templates[instanceName]);
        // create a new package
        this.saveEvent.emit('save');
    }
    // Template logic
    private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
        const nameOfFile = isFromTemplate ?
            key.split('/')[1].split('.')[0].split('-template')[0]
            : key.split('/')[1].split('.')[0].split('-mapping')[0];
        // const fullName = nameOfFile + ',' + key.split('.');
        if (this.templateAndMappingMap.has(nameOfFile)) {
            const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
            !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
            this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
        } else {
            this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
        }

    }

    setArtifact(predefined: boolean) {
        if (predefined) {
            this.currentFuncion.inputs['artifact-prefix-names'] = [];
        } else {
            this.currentFuncion.inputs['artifact-prefix-names'] = { get_input: 'template-prefix' };
        }
    }
    addToInputs(optionalInput) {
        this.requiredInputs.set(optionalInput, this.OptionalInputs.get(optionalInput));
        this.OptionalInputs.delete(optionalInput);
    }

    setTemplate(file: string) {
        if (this.selectedTemplates.has(file)) {
            this.selectedTemplates.delete(file);
        } else {
            this.selectedTemplates.set(file, this.templateAndMappingMap.get(file));
        }
        console.log(this.selectedTemplates);
    }

    getKeys(map: Map<string, any>) {
        return Array.from(map.keys());
    }
    getValue(file: string, map: Map<string, any>) {
        return map.get(file);
    }

    getObjectKey(object) {
        // console.log(object);
        return Object.keys(object);
    }
    getObjectValue(object) {
        return Object.values(object);
    }

    getNodeType(nodeName: string) {
        this.functionStore.state$
            .subscribe(state => {
                console.log(state);
                console.log(nodeName);
                const functions = state.serverFunctions;
                // tslint:disable-next-line: prefer-for-of
                for (let i = 0; i < functions.length; i++) {
                    if (functions[i].modelName === nodeName) {
                        // tslint:disable: no-string-literal
                        console.log(functions[i].definition['interfaces']);
                        this.getInputFields(functions[i].definition['interfaces'], 'outputs');
                        this.getInputFields(functions[i].definition['interfaces'], 'inputs');
                        break;
                    }
                }
            });
    }

    getInputFields(interfaces, type) {

        if (type === 'inputs') {
            this.requiredInputs = new Map<string, {}>();
            this.OptionalInputs = new Map<string, {}>();
        } else {
            this.requiredOutputs = new Map<string, {}>();
            this.optionalOutputs = new Map<string, {}>();

        }
        const nodeName = Object.keys(interfaces)[0];
        this.interfaceChildName = nodeName;
        console.log(nodeName + ' ------ ' + type);
        console.log(interfaces[nodeName]['operations']['process'][type]);
        const fields = interfaces[nodeName]['operations']['process'][type];
        this.artifactPrefix = false;
        for (const [key, value] of Object.entries(fields)) {
            if (key === 'artifact-prefix-names') {
                this.artifactPrefix = true;
                // in edit&view mode need to check first if this input||output already exists
            } else if (key in this.currentFuncion.inputs) {
                this.requiredInputs.set(key, Object.assign({}, value));
            } else if (key in this.currentFuncion.outputs) {
                this.requiredOutputs.set(key, Object.assign({}, value));
            } else if (value['required']) {
                console.log('This field is required = ' + key);
                if (type === 'inputs') {
                    this.requiredInputs.set(key, Object.assign({}, value));
                } else {
                    this.requiredOutputs.set(key, Object.assign({}, value));
                }
            } else {
                console.log('This field is Optional ' + key);
                if (type === 'inputs') {
                    this.OptionalInputs.set(key, Object.assign({}, value));
                } else {
                    this.optionalOutputs.set(key, Object.assign({}, value));
                }
            }
        }

        // console.log(this.requiredOutputs);
    }


}