Merge "Updated prerequisites"
[ccsdk/cds.git] / cds-ui / client / src / app / feature-modules / resource-definition / resource-edit / resource-metadata / resource-metadata.component.ts
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP : CDS
4 * ================================================================================
5 * Copyright 2019 TechMahindra
6 *=================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 */
20
21 import { Component, OnInit, EventEmitter, Output  } from '@angular/core';
22 import {FormBuilder, FormGroup, Validators} from '@angular/forms';
23 import { IResources } from 'src/app/common/core/store/models/resources.model';
24 import { IResourcesState } from 'src/app/common/core/store/models/resourcesState.model';
25 import { Observable } from 'rxjs';
26 import { Store } from '@ngrx/store';
27 import { IAppState } from '../../../../common/core/store/state/app.state';
28 import { A11yModule } from '@angular/cdk/a11y';
29 import { LoadResourcesSuccess } from 'src/app/common/core/store/actions/resources.action';
30 import { IPropertyData } from 'src/app/common/core/store/models/propertyData.model';
31 import { IEntrySchema } from 'src/app/common/core/store/models/entrySchema.model';
32
33 @Component({
34   selector: 'app-resource-metadata',
35   templateUrl: './resource-metadata.component.html',
36   styleUrls: ['./resource-metadata.component.scss']
37 })
38 export class ResourceMetadataComponent implements OnInit {
39     entry_schema:IEntrySchema;
40     properties: IPropertyData;
41     ResourceMetadata: FormGroup;
42     resource_name: string;
43     tags: string;
44     rdState: Observable<IResourcesState>;
45     resources: IResources;
46     propertyValues = [];
47     property = [];   
48     @Output() resourcesData = new EventEmitter();
49      
50  constructor(private formBuilder: FormBuilder, private store: Store<IAppState>) { 
51     this.rdState = this.store.select('resources');
52     this.ResourceMetadata = this.formBuilder.group({
53         Resource_Name: ['', Validators.required],
54        _tags: ['', Validators.required],
55        _description : ['', Validators.required],
56        _type: ['', Validators.required],
57        required: ['', Validators.required],
58        entry_schema: ['']
59     });   
60  }
61
62  ngOnInit() {
63     this.rdState.subscribe(
64       resourcesdata => {
65         var resourcesState: IResourcesState = { resources: resourcesdata.resources, isLoadSuccess: resourcesdata.isLoadSuccess, isSaveSuccess: resourcesdata.isSaveSuccess, isUpdateSuccess: resourcesdata.isUpdateSuccess };
66         this.resource_name = resourcesState.resources.name;
67         this.tags = resourcesState.resources.tags;
68         this.resources = resourcesState.resources;
69         this.properties= resourcesState.resources.property;
70         this.propertyValues=  this.checkNested(this.properties);
71         this.ResourceMetadata = this.formBuilder.group({
72         Resource_Name: [this.resource_name, Validators.required],
73          _tags: [this.tags, Validators.required],
74          _description : [ this.propertyValues[0], Validators.required],
75          _type: [ this.propertyValues[1], Validators.required],
76          required: [ JSON.stringify(this.propertyValues[2]), Validators.required],
77          entry_schema: [this.propertyValues[3]]
78       });   
79     })
80  }
81   
82  UploadMetadata() {
83   
84     this.resources.name = this.ResourceMetadata.value.Resource_Name;
85     this.resources.tags = this.ResourceMetadata.value._tags;
86     this.resources.property.description = this.ResourceMetadata.value._description;
87     this.resources.property.type = this.ResourceMetadata.value._type;
88         this.resources.property.required = this.ResourceMetadata.value.required;
89         this.resources.property.entry_schema = this.ResourceMetadata.value.entry_schema;
90         this.resourcesData.emit(this.resources); 
91  }
92    
93  checkNested(obj) {
94   for (let key in obj) {
95     if (obj.hasOwnProperty(key)) {
96       if (typeof obj[key] == "object"){
97          console.log(`Key: ${key}`)
98          this.checkNested(obj[key]);
99       } else {
100          this.property.push(obj[key]);             
101       }   
102      }
103    }
104    return this.property
105  }
106 }