Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 1 | import { Injectable } from '@angular/core'; |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 2 | import { Http, Headers, Response, RequestOptions, URLSearchParams } from '@angular/http'; |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 3 | import { Observable } from 'rxjs/Observable'; |
| 4 | import 'rxjs/add/operator/catch'; |
| 5 | import 'rxjs/add/operator/map'; |
| 6 | |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 7 | import { Schema } from '../inventory/schema'; |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 8 | |
| 9 | @Injectable() |
| 10 | export class SchemasService { |
Radek Krejci | 5a1571a | 2018-02-16 13:45:53 +0100 | [diff] [blame^] | 11 | public schemas; |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 12 | public activeSchema: string; |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 13 | |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 14 | constructor( private http: Http ) { |
| 15 | this.loadData(); |
| 16 | this.activeSchema = localStorage.getItem('activeSchema'); |
| 17 | if (!this.activeSchema) { |
| 18 | this.activeSchema = ""; |
| 19 | this.schemas = []; |
| 20 | } |
| 21 | } |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 22 | |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 23 | storeData() { |
| 24 | localStorage.setItem('schemas', JSON.stringify(this.schemas)); |
| 25 | } |
Radek Krejci | c58f347 | 2017-09-08 16:17:01 +0200 | [diff] [blame] | 26 | |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 27 | loadData() { |
| 28 | this.schemas = JSON.parse(localStorage.getItem('schemas')); |
| 29 | } |
| 30 | |
Radek Krejci | 5a1571a | 2018-02-16 13:45:53 +0100 | [diff] [blame^] | 31 | schemasKeys() { |
| 32 | if (this.schemas) { |
| 33 | return Object.keys(this.schemas); |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 34 | } |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 35 | } |
| 36 | |
Radek Krejci | 5a1571a | 2018-02-16 13:45:53 +0100 | [diff] [blame^] | 37 | getSchemaKey(schema: Schema) { |
| 38 | if (!schema) { |
| 39 | return null |
| 40 | } else if ('revision' in schema) { |
| 41 | return schema.name + '@' + schema.revision + '.yang'; |
| 42 | } else { |
| 43 | return schema.name + '.yang'; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | getActiveSchema(key: string = this.activeSchema): Schema { |
| 48 | if (key in this.schemas) { |
| 49 | return this.schemas[key]; |
| 50 | } else { |
| 51 | return null; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | changeActiveSchemaKey(key: string): Schema { |
| 56 | if (key && (key in this.schemas)) { |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 57 | this.activeSchema = key; |
| 58 | localStorage.setItem('activeSession', this.activeSchema); |
| 59 | } |
Radek Krejci | 5a1571a | 2018-02-16 13:45:53 +0100 | [diff] [blame^] | 60 | return this.schemas[key]; |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | getSchemas() { |
| 64 | return this.http.get( '/netopeer/inventory/schemas' ) |
| 65 | .map(( resp: Response ) => resp.json()).toPromise(); |
| 66 | } |
| 67 | |
Radek Krejci | 5a1571a | 2018-02-16 13:45:53 +0100 | [diff] [blame^] | 68 | show(key: string, schema: Schema) { |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 69 | let newSchema = true; |
Radek Krejci | 5a1571a | 2018-02-16 13:45:53 +0100 | [diff] [blame^] | 70 | |
| 71 | if (key in this.schemas) { |
| 72 | newSchema = false; |
| 73 | schema = this.schemas[key]; |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | if (!('data' in schema)) { |
| 77 | let params = new URLSearchParams(); |
Radek Krejci | 5a1571a | 2018-02-16 13:45:53 +0100 | [diff] [blame^] | 78 | params.set('key', key); |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 79 | let options = new RequestOptions({ search: params }); |
| 80 | this.http.get('/netopeer/inventory/schema', options) |
| 81 | .map((resp: Response) => resp.json()).toPromise().then(result => { |
| 82 | console.log(result) |
| 83 | if (result['success']) { |
| 84 | schema['data'] = result['data']; |
| 85 | this.storeData(); |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 86 | } |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | if (newSchema) { |
Radek Krejci | 5a1571a | 2018-02-16 13:45:53 +0100 | [diff] [blame^] | 91 | this.schemas[key] = schema; |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 92 | this.storeData(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | addSchema( schema: File ) { |
| 97 | let headers = new Headers( { 'specific-content-type': '' } ); |
| 98 | let options = new RequestOptions( { headers: headers } ); |
| 99 | let input = new FormData(); |
| 100 | input.append( "schema", schema ); |
| 101 | return this.http.post( '/netopeer/inventory/schemas', input, options ) |
| 102 | .map(( resp: Response ) => resp.json() ) |
| 103 | .catch(( err: Response | any ) => Observable.throw( err ) ); |
| 104 | } |
| 105 | |
Radek Krejci | 5a1571a | 2018-02-16 13:45:53 +0100 | [diff] [blame^] | 106 | rmSchema(key: string) { |
| 107 | let options = new RequestOptions( { body: JSON.stringify(key) } ); |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 108 | return this.http.delete( '/netopeer/inventory/schemas', options ) |
| 109 | .map(( resp: Response ) => resp.json() ) |
| 110 | .catch(( err: Response | any ) => Observable.throw( err ) ); |
| 111 | } |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 112 | } |