536be26cd0c7ef2d3cc52d0acf824fec774dc8ff
[ccsdk/cds.git] /
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 } 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   
49  constructor(private formBuilder: FormBuilder, private store: Store<IAppState>) { 
50     this.rdState = this.store.select('resources');
51     this.ResourceMetadata = this.formBuilder.group({
52         Resource_Name: ['', Validators.required],
53        _tags: ['', Validators.required],
54        _description : ['', Validators.required],
55        _type: ['', Validators.required],
56        required: ['', Validators.required],
57        entry_schema: ['']
58     });   
59  }
60
61  ngOnInit() {
62     this.rdState.subscribe(
63       resourcesdata => {
64         var resourcesState: IResourcesState = { resources: resourcesdata.resources, isLoadSuccess: resourcesdata.isLoadSuccess, isSaveSuccess: resourcesdata.isSaveSuccess, isUpdateSuccess: resourcesdata.isUpdateSuccess };
65         this.resource_name = resourcesState.resources.name;
66         this.tags = resourcesState.resources.tags;
67         this.resources = resourcesState.resources;
68         this.properties= resourcesState.resources.property;
69         this.propertyValues=  this.checkNested(this.properties);
70         this.ResourceMetadata = this.formBuilder.group({
71        Resource_Name: [this.resource_name, Validators.required],
72         _tags: [this.tags, Validators.required],
73         _description : [ this.propertyValues[0], Validators.required],
74         _type: [ this.propertyValues[1], Validators.required],
75         required: [ this.propertyValues[2], Validators.required],
76         entry_schema: [this.propertyValues[3]]
77       });   
78     })
79  }
80     
81  checkNested(obj) {
82   for (let key in obj) {
83     if (obj.hasOwnProperty(key)) {
84       if (typeof obj[key] == "object"){
85          console.log(`Key: ${key}`)
86          this.checkNested(obj[key]);
87       } else {
88          this.property.push(obj[key]);             
89       }   
90      }
91    }
92    return this.property
93  }
94 }