Merge "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   viewOnly: boolean = true;
52   options: string;
53   constructor(private formBuilder: FormBuilder, private store: Store<IAppState>,
54     private loader: LoaderService, private dataService: SelectTemplateService) {
55     this.bpState = this.store.select('blueprint');
56     this.CBAMetadataForm = this.formBuilder.group({
57       template_author: ['', Validators.required],
58       author_email: ['', Validators.required],
59       user_groups: ['', Validators.required],
60       template_name: ['', Validators.required],
61       template_version: ['', Validators.required],
62       template_tags: ['', Validators.required]
63     });
64
65   }
66
67   ngOnInit() {
68     this.dataService.currentMessage.subscribe(
69       res => {
70         this.options = res;
71         // this.metdataFormfields(res);
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         if(this.options=='2'){
96           this.CBAMetadataForm = this.formBuilder.group({
97             template_author: ['', Validators.required],
98             author_email: ['', Validators.required],
99             user_groups: [metadatavalues[2], Validators.required],
100             template_name: ['', Validators.required],
101             template_version: ['', Validators.required],
102             template_tags: [metadatavalues[5], Validators.required]
103           });
104         } 
105         else if(this.options=='3'){
106           this.CBAMetadataForm = this.formBuilder.group({
107             template_author: [metadatavalues[0]],
108             author_email: [metadatavalues[1]],
109             user_groups: [metadatavalues[2]],
110             template_name: [metadatavalues[3]],
111             template_version: [metadatavalues[4]],
112             template_tags: [metadatavalues[5]]
113           });
114           this.CBAMetadataForm.disable();
115         }
116           else{
117         this.CBAMetadataForm = this.formBuilder.group({
118           template_author: [metadatavalues[0], Validators.required],
119           author_email: [metadatavalues[1], Validators.required],
120           user_groups: [metadatavalues[2], Validators.required],
121           template_name: [metadatavalues[3], Validators.required],
122           template_version: [metadatavalues[4], Validators.required],
123           template_tags: [metadatavalues[5], Validators.required]
124         });
125       }
126       
127       })
128
129
130   }
131
132   metdataFormfields(options: string) {
133     if (options == '2') {
134       this.CBAMetadataForm.setValue({
135         template_author: ["", Validators.required],
136         author_email: ["", Validators.required],
137         template_name: ["", Validators.required],
138         template_version: ["1.0.0", Validators.required]
139
140       })
141     }
142     if (options == '3') {
143       this.CBAMetadataForm.disable();
144     }
145   }
146
147   UploadMetadata() {
148     this.loader.showLoader();
149     this.metadata = Object.assign({}, this.CBAMetadataForm.value);
150     this.blueprint.metadata = this.metadata;
151     this.filesData.forEach((fileNode) => {
152       if (fileNode.name.includes(this.blueprintName) && fileNode.name == this.entryDefinition) {
153         let tempNodeData = JSON.parse(fileNode.data);
154         tempNodeData.metadata = this.blueprint.metadata;
155         fileNode.data = JSON.stringify(tempNodeData, null, "\t");
156       }
157     });
158     let blueprintState = {
159       blueprint: this.blueprint,
160       name: this.blueprintName,
161       files: this.filesTree,
162       filesData: this.filesData,
163       uploadedFileName: this.uploadedFileName,
164       entryDefinition: this.entryDefinition
165     }
166     this.store.dispatch(new SetBlueprintState(blueprintState));
167     this.loader.hideLoader();
168   }
169 }