blob: 92bfba51fb62cfc81c990f4b1c33723f2aeb4fb1 [file] [log] [blame]
Radek Krejcid23f0df2017-08-31 16:34:49 +02001import { Injectable } from '@angular/core';
Radek Krejci6be087d2018-02-14 08:53:20 +01002import { Http, Headers, Response, RequestOptions, URLSearchParams } from '@angular/http';
Radek Krejcid23f0df2017-08-31 16:34:49 +02003import { Observable } from 'rxjs/Observable';
4import 'rxjs/add/operator/catch';
5import 'rxjs/add/operator/map';
6
Radek Krejci6be087d2018-02-14 08:53:20 +01007import { Schema } from '../inventory/schema';
Radek Krejcid23f0df2017-08-31 16:34:49 +02008
9@Injectable()
10export class SchemasService {
Radek Krejci6be087d2018-02-14 08:53:20 +010011 public schemas: Schema[];
12 public activeSchema: string;
Radek Krejcid23f0df2017-08-31 16:34:49 +020013
Radek Krejci6be087d2018-02-14 08:53:20 +010014 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 Krejcid23f0df2017-08-31 16:34:49 +020022
Radek Krejci6be087d2018-02-14 08:53:20 +010023 storeData() {
24 localStorage.setItem('schemas', JSON.stringify(this.schemas));
25 }
Radek Krejcic58f3472017-09-08 16:17:01 +020026
Radek Krejci6be087d2018-02-14 08:53:20 +010027 loadData() {
28 this.schemas = JSON.parse(localStorage.getItem('schemas'));
29 }
30
31 getActiveSchema(key: string = this.activeSchema): Schema {
32 if (!key) {
33 return null;
34 }
35 for (let i = this.schemas.length; i > 0; i--) {
36 if (this.schemas[i - 1].key == key) {
37 return this.schemas[i - 1];
38 }
39 }
40 return null;
41 }
42
43 changeActiveSchema(key: string): Schema {
44 let result = this.getActiveSchema(key);
45 if (result) {
46 this.activeSchema = key;
47 localStorage.setItem('activeSession', this.activeSchema);
48 }
49 return result;
50 }
51
52 getSchemas() {
53 return this.http.get( '/netopeer/inventory/schemas' )
54 .map(( resp: Response ) => resp.json()).toPromise();
55 }
56
57 show(schema: Schema) {
58 let newSchema = true;
59 for (let i in this.schemas) {
60 if (this.schemas[i].key == schema.key) {
61 schema = this.schemas[i];
62 newSchema = false;
63 break;
64 }
65 }
66
67 if (!('data' in schema)) {
68 let params = new URLSearchParams();
69 params.set('key', schema.key);
70 let options = new RequestOptions({ search: params });
71 this.http.get('/netopeer/inventory/schema', options)
72 .map((resp: Response) => resp.json()).toPromise().then(result => {
73 console.log(result)
74 if (result['success']) {
75 schema['data'] = result['data'];
76 this.storeData();
77 console.log(this.schemas)
78 }
79 });
80 }
81
82 if (newSchema) {
83 this.schemas.push(schema);
84 this.storeData();
85 }
86 }
87
88 addSchema( schema: File ) {
89 let headers = new Headers( { 'specific-content-type': '' } );
90 let options = new RequestOptions( { headers: headers } );
91 let input = new FormData();
92 input.append( "schema", schema );
93 return this.http.post( '/netopeer/inventory/schemas', input, options )
94 .map(( resp: Response ) => resp.json() )
95 .catch(( err: Response | any ) => Observable.throw( err ) );
96 }
97
98 rmSchema( schema: Schema ) {
99 let options = new RequestOptions( { body: schema.key } );
100 return this.http.delete( '/netopeer/inventory/schemas', options )
101 .map(( resp: Response ) => resp.json() )
102 .catch(( err: Response | any ) => Observable.throw( err ) );
103 }
Radek Krejcid23f0df2017-08-31 16:34:49 +0200104}