Improved error printouts
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy-control / policy-type.datasource.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
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 { CollectionViewer, DataSource } from '@angular/cdk/collections';
22 import { HttpErrorResponse } from '@angular/common/http';
23 import { MatSort } from '@angular/material';
24 import { Observable } from 'rxjs/Observable';
25 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
26 import { merge } from 'rxjs';
27 import { of } from 'rxjs/observable/of';
28 import { catchError, finalize, map } from 'rxjs/operators';
29 import { PolicyType } from '../interfaces/policy.types';
30 import { PolicyService } from '../services/policy/policy.service';
31 import { NotificationService } from '../services/ui/notification.service';
32
33 export class PolicyTypeDataSource extends DataSource<PolicyType> {
34
35     private policyTypeSubject = new BehaviorSubject<PolicyType[]>([]);
36
37     private loadingSubject = new BehaviorSubject<boolean>(false);
38
39     public loading$ = this.loadingSubject.asObservable();
40
41     public rowCount = 1; // hide footer during intial load
42
43     constructor(private policySvc: PolicyService,
44         private sort: MatSort,
45         private notificationService: NotificationService) {
46         super();
47     }
48
49     loadTable() {
50         this.loadingSubject.next(true);
51         this.policySvc.getPolicyTypes()
52             .pipe(
53                 catchError((her: HttpErrorResponse) => {
54                     this.notificationService.error('Failed to get policy types: ' + her.statusText + ', ' + her.error);
55                     return of([]);
56                 }),
57                 finalize(() => this.loadingSubject.next(false))
58             )
59             .subscribe((types: PolicyType[]) => {
60                 this.rowCount = types.length;
61                 for (let i = 0; i < types.length; i++) {
62                     const policyType = types[i];
63                     try {
64                         policyType.schemaObject = JSON.parse(policyType.schema);
65                     } catch (jsonError) {
66                         console.error('Could not parse schema: ' + policyType.schema);
67                         policyType.schemaObject = { description: 'Incorrect schema: ' + jsonError };
68                     }
69                 }
70                 this.policyTypeSubject.next(types);
71             });
72     }
73
74     connect(collectionViewer: CollectionViewer): Observable<PolicyType[]> {
75         const dataMutations = [
76             this.policyTypeSubject.asObservable(),
77             this.sort.sortChange
78         ];
79         return merge(...dataMutations).pipe(map(() => {
80             return this.getSortedData([...this.policyTypeSubject.getValue()]);
81         }));
82     }
83
84     disconnect(collectionViewer: CollectionViewer): void {
85         this.policyTypeSubject.complete();
86         this.loadingSubject.complete();
87     }
88
89     private getSortedData(data: PolicyType[]) {
90         if (!this.sort.active || this.sort.direction === '') {
91             return data;
92         }
93
94         return data.sort((a, b) => {
95             const isAsc = this.sort.direction === 'asc';
96             switch (this.sort.active) {
97                 case 'name': return compare(a.name, b.name, isAsc);
98                 default: return 0;
99             }
100         });
101     }
102 }
103
104 function compare(a: any, b: any, isAsc: boolean) {
105     return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
106 }