c87fb511629d5a17cfad4bcd1f319372aa922fc7
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. All rights reserved.
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, Input, ViewEncapsulation} from "@angular/core";
22 import {GabService, IServerResponse} from "../../../services/gab.service";
23 import {PathsAndNamesDefinition} from "../../../../models/paths-and-names";
24
25 const COLUMN_PREFIX: string = 'col';
26
27 @Component({
28     selector: 'gab',
29     templateUrl: './generic-artifact-browser.component.html',
30     styleUrls:['./generic-artifact-browser.component.less'],
31     encapsulation: ViewEncapsulation.None
32 })
33 export class GenericArtifactBrowserComponent {
34     @Input()
35     pathsandnames: PathsAndNamesDefinition[];
36     @Input()
37     artifactid: string;
38     @Input()
39     resourceid: string;
40
41     columns: ColumnDefinition[];
42     rows: any[];
43     selectedRows: any[];
44     isLoading: boolean;
45     ready: boolean;
46
47     constructor(private gabService: GabService) {
48     }
49
50     ngOnInit() {
51         this.ready = false;
52         this.isLoading = true;
53         this.columns = [];
54         let paths: string[] = this.pathsandnames.map(item => item.path);
55         this.gabService.getArtifact(this.artifactid, this.resourceid, paths)
56         .subscribe(
57             response => {
58             let typedServerResponse:IServerResponse = <IServerResponse>response.json();
59             this.normalizeDataForNgxDatatable(typedServerResponse.data);
60             },
61             err => console.log(err),
62             () => {
63                 this.ready = true;
64                 this.isLoading = false;
65             }
66         );
67     }
68
69     private normalizeDataForNgxDatatable(data: [{ [key: string]: string }]) {
70         let result: NormalizationResult = this.getNormalizationResult(data, this.pathsandnames);
71         this.rows = result.rows;
72         this.columns = result.columns;
73     }
74
75     private getNormalizationResult(data: [{ [key: string]: string }],
76                                    pathsAndNames: PathsAndNamesDefinition[]): NormalizationResult {
77         //Prepare column names and column data property names
78         let mappingsPathToProp = new Map<string,string>();
79         let columnsDefinitions = this.normalizeColumns(pathsAndNames, mappingsPathToProp);
80
81         //Convert rows from { "string": "string" } to { prop : "string" } format
82         //This is required by NgxDatatable component
83         let arrayOfRows = this.normalizeRows(data, mappingsPathToProp);
84
85         return new NormalizationResult(arrayOfRows, columnsDefinitions);
86     }
87
88     private normalizeColumns(pathsAndNames: PathsAndNamesDefinition[], mappingsPathToProp: Map<string,string>) {
89         let columnsDefinitions: ColumnDefinition[] = [];
90         let index: number = 1;
91
92         pathsAndNames.forEach(function (col) {
93             let columnDataPropertyName: string = COLUMN_PREFIX + index;
94             mappingsPathToProp.set(col.path, columnDataPropertyName);
95             let cell: ColumnDefinition = new ColumnDefinition(col.friendlyName, columnDataPropertyName);
96             columnsDefinitions.push(cell);
97             index += 1;
98         });
99         return columnsDefinitions;
100     }
101
102     private normalizeRows(data: [{ [key: string]: string }], mappingsPathToProp: Map<string,string>) {
103         let arrayOfRows = [];
104         data.forEach(function (col) {
105             let row = {};
106             for (let key in col) {
107                 if (col.hasOwnProperty(key)) {
108                     let columnNameAsProp = mappingsPathToProp.get(key);
109                     row[columnNameAsProp] = col[key];
110                 }
111             }
112             arrayOfRows.push(row);
113         });
114         return arrayOfRows;
115     }
116 }
117
118 class NormalizationResult {
119     constructor(public rows: any[], public columns: ColumnDefinition[]) {}
120 }
121
122 export class ColumnDefinition {
123     constructor(public name: string, public prop: string) {}
124 }
125