2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 import { HttpErrorResponse } from '@angular/common/http';
22 import { CollectionViewer, DataSource } from '@angular/cdk/collections';
23 import { Injectable } from '@angular/core';
24 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
25 import { of } from 'rxjs/observable/of';
26 import { Observable } from 'rxjs/Observable';
27 import { catchError, finalize, map } from 'rxjs/operators';
29 import { PolicyType, PolicyTypes, PolicyTypeSchema } from '../interfaces/policy.types';
30 import { PolicyService } from '../services/policy/policy.service';
31 import { NotificationService } from '../services/ui/notification.service';
37 export class PolicyTypeDataSource extends DataSource<PolicyTypeSchema> {
39 policyTypes: PolicyTypeSchema[] = [];
41 policyTypeSubject = new BehaviorSubject<PolicyTypeSchema[]>([]);
43 private loadingSubject = new BehaviorSubject<boolean>(false);
45 public rowCount = 1; // hide footer during intial load
47 constructor(public policySvc: PolicyService,
48 private notificationService: NotificationService) {
52 public getPolicyTypes() {
53 this.policyTypes = [] as PolicyTypeSchema[];
54 this.policySvc.getPolicyTypes()
56 catchError((httpError: HttpErrorResponse) => {
57 this.notificationService.error('Failed to get policy types: ' + httpError.error);
60 finalize(() => this.loadingSubject.next(false))
62 .subscribe((policyType: PolicyTypes) => {
63 this.rowCount = policyType.policytype_ids.length;
64 if (policyType.policytype_ids.length != 0) {
65 policyType.policytype_ids.forEach(policyTypeId => {
66 var policyTypeSchema = {} as PolicyTypeSchema
67 if (policyTypeId === "") {
68 policyTypeSchema.id = '';
69 policyTypeSchema.name = '';
70 policyTypeSchema.schemaObject = '{}';
71 this.policyTypes.push(policyTypeSchema);
74 this.policySvc.getPolicyType(policyTypeId)
76 catchError((httpError: HttpErrorResponse) => {
77 this.notificationService.error('Failed to get policy type: ' + httpError.error);
80 finalize(() => this.loadingSubject.next(false))
82 .subscribe((policyType: PolicyType) => {
83 policyTypeSchema.id = policyTypeId;
84 policyTypeSchema.schemaObject = policyType.policy_schema;
85 policyTypeSchema.name = policyType.policy_schema.title;
86 this.policyTypes.push(policyTypeSchema);
89 this.policyTypeSubject.next(this.policyTypes);
95 connect(collectionViewer: CollectionViewer): Observable<PolicyTypeSchema[]> {
96 return of(this.policyTypeSubject.getValue());
99 disconnect(collectionViewer: CollectionViewer): void {
100 this.policyTypeSubject.complete();