Fixed defect CCSDK-1610
[ccsdk/cds.git] / cds-ui / client / src / app / feature-modules / blueprint / select-template / metadata / metadata.component.ts
1 /*
2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2018 IBM Intellectual Property. All rights reserved.
5 ===================================================================
6
7 Unless otherwise specified, all software contained herein is licensed
8 under the Apache License, Version 2.0 (the License);
9 you may not use this software except in compliance with the License.
10 You may obtain a copy of the License at
11
12     http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19 ============LICENSE_END============================================
20 */
21
22 import { Component, OnInit } from '@angular/core';
23 import { FormBuilder, FormGroup, Validators } from '@angular/forms';
24 import { Observable } from 'rxjs';
25 import { Store } from '@ngrx/store';
26
27 import { IAppState } from '../../../../common/core/store/state/app.state';
28 import { IBlueprintState } from 'src/app/common/core/store/models/blueprintState.model';
29 import { IBlueprint } from 'src/app/common/core/store/models/blueprint.model';
30 import { IMetaData } from '../../../../common/core/store/models/metadata.model';
31 import { SetBlueprintState } from 'src/app/common/core/store/actions/blueprint.action';
32 import { LoaderService } from '../../../../common/core/services/loader.service';
33 import { SelectTemplateService } from 'src/app/feature-modules/blueprint/select-template/select-template.service';
34 @Component({
35   selector: 'app-metadata',
36   templateUrl: './metadata.component.html',
37   styleUrls: ['./metadata.component.scss']
38 })
39 export class MetadataComponent implements OnInit {
40   CBAMetadataForm: FormGroup;
41   metadata: IMetaData;
42   bpState: Observable<IBlueprintState>;
43   blueprint: IBlueprint;
44   filesTree: any = [];
45   filesData: any = [];
46   selectedFile: string;
47   zipFolder: any;
48   blueprintName: string;
49   uploadedFileName: string;
50   entryDefinition: string;
51   
52   constructor(private formBuilder: FormBuilder, private store: Store<IAppState>,
53     private loader: LoaderService, private dataService: SelectTemplateService) {
54     this.bpState = this.store.select('blueprint');
55     this.CBAMetadataForm = this.formBuilder.group({
56       template_author: ['', Validators.required],
57       author_email: ['', Validators.required],
58       user_groups: ['', Validators.required],
59       template_name: ['', Validators.required],
60       template_version: ['', Validators.required],
61       template_tags: ['', Validators.required]
62     });
63
64   }
65
66   ngOnInit() {
67     this.dataService.currentMessage.subscribe(
68       res => {
69         let options = res;
70         console.log(options + " data from service ngoninit" + res);
71       }
72     );
73     
74     this.bpState.subscribe(
75       blueprintdata => {
76         var blueprintState: IBlueprintState = { blueprint: blueprintdata.blueprint, isLoadSuccess: blueprintdata.isLoadSuccess, isSaveSuccess: blueprintdata.isSaveSuccess, isUpdateSuccess: blueprintdata.isUpdateSuccess };
77         this.blueprint = blueprintState.blueprint;
78         this.filesTree = blueprintdata.files;
79         this.filesData = blueprintdata.filesData;
80         this.blueprintName = blueprintdata.name;
81         this.uploadedFileName = blueprintdata.uploadedFileName;
82         this.entryDefinition = blueprintdata.entryDefinition;
83
84         var blueprintState: IBlueprintState = { blueprint: blueprintdata.blueprint, isLoadSuccess: blueprintdata.isLoadSuccess, isSaveSuccess: blueprintdata.isSaveSuccess, isUpdateSuccess: blueprintdata.isUpdateSuccess };
85         this.metadata = blueprintState.blueprint.metadata;
86         this.blueprint = blueprintState.blueprint;
87         let metadatavalues = [];
88         for (let key in this.metadata) {
89           if (this.metadata.hasOwnProperty(key)) {
90             metadatavalues.push(this.metadata[key]);
91           }
92         }
93         let temp_author = metadatavalues[0];
94         console.log(temp_author);
95         this.CBAMetadataForm = this.formBuilder.group({
96           template_author: [metadatavalues[0], Validators.required],
97           author_email: [metadatavalues[1], Validators.required],
98           user_groups: [metadatavalues[2], Validators.required],
99           template_name: [metadatavalues[3], Validators.required],
100           template_version: [metadatavalues[4], Validators.required],
101           template_tags: [metadatavalues[5], Validators.required]
102         });
103       })
104   }
105   
106   UploadMetadata() {
107     this.loader.showLoader();
108     this.metadata = Object.assign({}, this.CBAMetadataForm.value);
109     this.blueprint.metadata = this.metadata;
110     this.filesData.forEach((fileNode) => {
111       if (fileNode.name.includes(this.blueprintName) && fileNode.name == this.entryDefinition) {
112         let tempNodeData = JSON.parse(fileNode.data);
113         tempNodeData.metadata = this.blueprint.metadata;
114         fileNode.data = JSON.stringify(tempNodeData, null, "\t");
115       }
116     });
117     let blueprintState = {
118       blueprint: this.blueprint,
119       name: this.blueprintName,
120       files: this.filesTree,
121       filesData: this.filesData,
122       uploadedFileName: this.uploadedFileName,
123       entryDefinition: this.entryDefinition
124     }
125     this.store.dispatch(new SetBlueprintState(blueprintState));
126     this.loader.hideLoader();
127   }
128 }