added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / shared / services / model.service.ts
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 import { HttpClient, HttpHeaders } from "@angular/common/http";\r
18 import { AppGlobals } from "../../app.global";\r
19 import { ParamsService } from "./params.service";\r
20 import { Observable, observable, from } from "rxjs";\r
21 import { CookieService } from "ngx-cookie-service";\r
22 import { FeathersService } from "./feathers.service";\r
23 import { Injectable } from "@angular/core";\r
24 \r
25 Injectable({\r
26     providedIn: 'root'\r
27 })\r
28 export class ModelService {\r
29 \r
30     protected path;\r
31     protected http: HttpClient;\r
32     protected Params: ParamsService;\r
33     protected cookie: CookieService;\r
34     protected feathers: FeathersService;\r
35     private authenticated: Boolean = false;\r
36 \r
37     constructor(endpoint: String, http: HttpClient, Params: ParamsService, cookie: CookieService, feathers: FeathersService) {\r
38         this.http = http;\r
39         this.Params = Params;\r
40         this.path = AppGlobals.baseAPIUrl + endpoint;\r
41         this.cookie = cookie;\r
42         this.feathers = feathers;\r
43     }\r
44 \r
45     checkAuth(): Observable<Object>{\r
46         return this.feathers.auth;\r
47     }\r
48 \r
49     call(method, data?, path?){\r
50         if(!path){\r
51             path = this.path;\r
52         }\r
53         return new Observable(observer => {\r
54             var init = null;\r
55             if(data.params && data.params.events){\r
56                 delete data.params.events;\r
57                 this.feathers.service(path)\r
58                     .on('created', data => {\r
59                         if(init){\r
60                             if(init.data){\r
61                                 (init.data as Array<Object>).unshift(data);\r
62                                 observer.next(init);\r
63                             }else{\r
64                                 (init as Array<Object>).unshift(data);\r
65                                 observer.next(init);\r
66                             }\r
67                         }\r
68                     })\r
69                     .on('removed', data => {\r
70                         if(init){\r
71                             if(init.data){\r
72                                 init.data = (init.data as Array<Object>).filter(item => item['_id'] != data._id);\r
73                                 observer.next(init);\r
74                             }else{\r
75                                 init  = (init as Array<Object>).filter(item => item['_id'] != data._id);\r
76                                 observer.next(init);\r
77                             }\r
78                         }\r
79                     })\r
80                     .on('updated', data => {\r
81                         if(init){\r
82                             if(init.data){\r
83                                 (init.data as Array<Object>).forEach((elem, val) => {\r
84                                     if(elem['_id'] == data._id){\r
85                                         (init.data as Array<Object>).splice(val, 1, data);\r
86                                         return;\r
87                                     }\r
88                                 })\r
89                                 observer.next(init);\r
90                             }else{\r
91                                 (init as Array<Object>).forEach((elem, val) => {\r
92                                     if(elem['_id'] == data._id){\r
93                                         (init as Array<Object>).splice(val, 1, data);\r
94                                         return;\r
95                                     }\r
96                                 })\r
97                                 observer.next(init);\r
98                             }\r
99                         }\r
100                     });\r
101                 \r
102             }\r
103             this.checkAuth().subscribe(res => {\r
104                 if(data.data){\r
105                     \r
106                     //UPDATE & PATCH\r
107                     if(method == 'update' || method == 'patch'){\r
108                         let id = data.data._id;\r
109                         delete data.data._id;\r
110                         this.feathers.service(path)[method](id, data.data, {query: data.params}).then(result =>{\r
111                             if(!init){\r
112                                 init = result;\r
113                             }\r
114                             observer.next(result)\r
115                         }).catch(err => {\r
116                             observer.error(err)}\r
117                             );\r
118                     }else{\r
119                         this.feathers.service(path)[method](data.data, {query: data.params}).then(result =>{\r
120                             if(!init){\r
121                                 init = result;\r
122                             }\r
123                             observer.next(result)\r
124                         }).catch(err => {\r
125                             observer.error(err)\r
126                         });\r
127                     }\r
128                 }else{\r
129                     this.feathers.service(path)[method]({query: data.params}).then(result =>{\r
130                         if(!init){\r
131                             init = result;\r
132                         }\r
133                         observer.next(result)\r
134                     }).catch(err => observer.error(err));\r
135                 }\r
136 \r
137             }, err => {\r
138                 \r
139                 this.feathers.authenticate().subscribe(res => {\r
140                     observer.next(this.call(method, data, path));\r
141                 })\r
142             });\r
143         })  \r
144     }\r
145 \r
146     on(event){\r
147         return new Observable(observer => {\r
148             this.feathers.service(this.path).on(event, (data) => {\r
149                 observer.next(data);\r
150             });\r
151         })\r
152     }\r
153 \r
154     // sfind(params = []): Observable<Object> {\r
155     //     return this.http.get(this.path + this.Params.toString(params), this.getHttpOptions());\r
156     // }\r
157 \r
158     find(params?): Observable<Object> {\r
159 \r
160         return this.call('find', {params: params})\r
161     }\r
162 \r
163     // sget(id, params = []): Observable<Object> {\r
164     //     return from(this.http.get(this.path + '/' + id + this.Params.toString(params), this.getHttpOptions()));\r
165     // }\r
166 \r
167     get(id, params?): Observable<Object> {\r
168         return this.call('get', {data: id, params: params})\r
169     }\r
170 \r
171     // create(data, params = []): Observable<Object> {\r
172     //     return this.http.post(this.path + this.Params.toString(params), data, this.getHttpOptions());\r
173     // }\r
174 \r
175     create(data, params?): Observable<Object> {\r
176         return this.call('create', {data: data, params: params})\r
177     }\r
178 \r
179     // update(data, params = []): Observable<Object> {\r
180     //     return this.http.put(this.path + '/' + data._id + this.Params.toString(params), data, this.getHttpOptions());\r
181     // }\r
182 \r
183     update(data, params?): Observable<Object> {\r
184         return this.call('update', {data: data, params: params})\r
185     }\r
186 \r
187     // patch(data, params = []): Observable<Object> {\r
188     //     return this.http.patch(this.path + '/' + data._id + this.Params.toString(params), data, this.getHttpOptions());\r
189     // }\r
190 \r
191     patch(data, params?): Observable<Object> {\r
192         return this.call('patch', {data: data, params: params})\r
193     }\r
194 \r
195     // delete(id, params = []): Observable<Object> {\r
196     //     return this.http.delete(this.path + '/' + id + this.Params.toString(params), this.getHttpOptions());\r
197     // }\r
198 \r
199     delete(id, params?): Observable<Object> {\r
200         return this.call('remove', {data: id, params: params})\r
201     }\r
202 \r
203     protected getHttpOptions() {\r
204         return {\r
205             headers: new HttpHeaders({\r
206                 'Authorization': 'Bearer ' + JSON.parse(this.cookie.get('access_token'))\r
207             })\r
208         };\r
209     }\r
210 \r
211 }