aboutsummaryrefslogtreecommitdiffstats
path: root/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/package-creation.component.ts
blob: 1d2340534e006182ed405348c2077444179508dc (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
/*
============LICENSE_START==========================================
===================================================================
Copyright (C) 2019 Orange. All rights reserved.
===================================================================

Unless otherwise specified, all software contained herein is licensed
under the Apache License, Version 2.0 (the License);
you may not use this software except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
============LICENSE_END============================================
*/

import { Component, ElementRef, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { FilesContent, FolderNodeElement, MetaDataTabModel } from './mapping-models/metadata/MetaDataTab.model';

import * as JSZip from 'jszip';
import { PackageCreationStore } from './package-creation.store';
import { CBAPackage, Definition } from './mapping-models/CBAPacakge.model';
import { PackageCreationModes } from './creationModes/PackageCreationModes';
import { PackageCreationBuilder } from './creationModes/PackageCreationBuilder';
import { PackageCreationUtils } from './package-creation.utils';
import { MetadataTabComponent } from './metadata-tab/metadata-tab.component';
import { Router } from '@angular/router';
import { ToastrService } from 'ngx-toastr';
import { TourService } from 'ngx-tour-md-menu';
import { PackageCreationService } from './package-creation.service';
import { ComponentCanDeactivate } from '../../../../common/core/canDactivate/ComponentCanDeactivate';
import { DesignerStore } from '../designer/designer.store';
import { NgxUiLoaderService } from 'ngx-ui-loader';


@Component({
    selector: 'app-package-creation',
    templateUrl: './package-creation.component.html',
    styleUrls: ['./package-creation.component.css']
})
export class PackageCreationComponent extends ComponentCanDeactivate implements OnInit, OnDestroy {


    // adding initial referencing to designer mode


    constructor(
        private packageCreationStore: PackageCreationStore,
        private packageCreationService: PackageCreationService,
        private packageCreationUtils: PackageCreationUtils,
        private router: Router,
        private tourService: TourService,
        private toastService: ToastrService,
        private ngxService: NgxUiLoaderService,
        private designerStore: DesignerStore) {

        super();
    }

    counter = 0;
    modes: object[] = [
        { name: 'Designer Mode', style: 'mode-icon icon-designer-mode' },
        { name: 'Scripting Mode', style: 'mode-icon icon-scripting-mode' }];
    metaDataTab: MetaDataTabModel = new MetaDataTabModel();
    folder: FolderNodeElement = new FolderNodeElement();
    zipFile: JSZip = new JSZip();
    filesData: any = [];
    definition: Definition = new Definition();
    isSaveEnabled = false;

    @ViewChild(MetadataTabComponent, { static: false })
    metadataTabComponent: MetadataTabComponent;

    @ViewChild('nameit', { static: true })
    elementRef: ElementRef;
    versionPattern = '^(\\d+\\.)?(\\d+\\.)?(\\*|\\d+)$';
    metadataClasses = 'nav-item nav-link active complete';
    private cbaPackage: CBAPackage;

    ngOnInit() {
        this.elementRef.nativeElement.focus();
        const regexp = RegExp(this.versionPattern);
        this.packageCreationStore.state$.subscribe(cbaPackage => {
            console.log(cbaPackage);
            console.log('abbaaaas' + cbaPackage.metaData.name);
            this.cbaPackage = cbaPackage;
            if (cbaPackage && cbaPackage.metaData && cbaPackage.metaData.description
                && cbaPackage.metaData.name && cbaPackage.metaData.version &&
                regexp.test(cbaPackage.metaData.version)) {
                this.isSaveEnabled = true;
                if (!this.metadataClasses.includes('complete')) {
                    console.log('added');
                    this.metadataClasses += 'complete';
                }
                console.log('perhaps it is been added');
            } else {
                this.isSaveEnabled = false;
                this.metadataClasses = this.metadataClasses.replace('complete', '');
            }
        });
    }

    openTourGuide(step: string) {
        // this.tourService.goto(step);
    }

    saveBluePrint() {
        this.ngxService.start();
        console.log(this.cbaPackage);
        FilesContent.clear();
        let packageCreationModes: PackageCreationModes;
        this.cbaPackage = PackageCreationModes.mapModeType(this.cbaPackage);
        this.cbaPackage.metaData = PackageCreationModes.setEntryPoint(this.cbaPackage.metaData);
        packageCreationModes = PackageCreationBuilder.getCreationMode(this.cbaPackage);

        // this.cbaPackage.templateTopology.content = this.designerStore.state.sourceContent;
        packageCreationModes.execute(this.cbaPackage, this.packageCreationUtils);
        this.filesData.push(this.folder.TREE_DATA);
        this.saveBluePrintToDataBase();


    }


    saveBluePrintToDataBase() {
        this.create();
        this.zipFile.generateAsync({ type: 'blob' })
            .then(blob => {
                this.packageCreationService.savePackage(blob).subscribe(
                    bluePrintDetailModels => {
                        if (bluePrintDetailModels) {
                            const id = bluePrintDetailModels.toString().split('id')[1].split(':')[1].split('"')[1];
                            this.toastService.success('Package Updated Successfully ');
                            this.isSaveEnabled = false;
                            this.router.navigate(['/packages/package/' + id]);
                        }
                    }, error => {
                        // this.toastService.error('Error occure during editng process ' + error.message);
                        console.log('Error -' + error.message);
                    }, () => {
                        this.ngxService.stop();
                    });
            });
    }


    create() {
        FilesContent.getMapOfFilesNamesAndContent().forEach((value, key) => {
            this.zipFile.folder(key.split('/')[0]);
            this.zipFile.file(key, value);
        });

    }


    goBackToDashBorad() {
        this.router.navigate(['/packages']);
    }

    saveMetaData() {
        console.log('executed change');
        this.metadataTabComponent.saveMetaDataToStore();

    }

    canDeactivate(): boolean {
        return this.isSaveEnabled;
    }

    ngOnDestroy(): void {

    }
}