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