[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-FE-common / client / app / services / widgets-catalog / widgets-catalog.service.js
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
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  * ================================================================================
19  */
20 'use strict';
21
22 (function () {
23         class WidgetsCatalogService {
24         constructor($q, $log, $http, conf,uuid,base64Service, beReaderService, utilsService) {
25             this.$q = $q;
26             this.$log = $log;
27             this.$http = $http;
28             this.conf = conf;
29             this.uuid = uuid;
30             this.base64Service = base64Service;
31             this.beReaderService = beReaderService;
32             this.utilsService = utilsService;
33         }
34         
35         getUserWidgets(loginName) {
36                  let deferred = this.$q.defer();
37                  this.$http({
38                          method: "GET",
39                      url: this.conf.api.widgetCommon + '/widgetCatalog' + '/' + loginName,
40                      cache: false,
41                      headers: {
42                          'X-Widgets-Type': 'all',
43                          'X-ECOMP-RequestID':this.uuid.generate()
44                      }
45              }).then(res => {
46                         if (res == null || res.data == null) {
47                      deferred.reject("WidgetsCatalogService::getUserWidgets Failed");
48                  } else {
49                      deferred.resolve(res.data);
50                  }
51              })
52              .catch(status => {
53                  deferred.reject(status);
54                  });
55              return deferred.promise;
56         }
57
58         getManagedWidgets() {           
59             let deferred = this.$q.defer();
60             let url = this.conf.api.widgetCommon + '/widgetCatalog';
61             this.$http({
62                 method: "GET",
63                 url: url,
64                 cache: false,
65                 headers: {
66                         'X-Widgets-Type': 'all',
67                         'X-ECOMP-RequestID':this.uuid.generate()
68                 }
69             }).then(res => {
70                         if (res == null || res.data == null) {
71                      deferred.reject("WidgetsCatalogService::getManagedWidgets Failed");
72                  } else {
73                      deferred.resolve(res.data);
74                  }
75              })
76              .catch(status => {
77                  deferred.reject(status);
78              });  
79             return deferred.promise;
80         }
81         
82         createWidget(newWidget, file) {
83                 console.log(newWidget);
84                 let deferred = this.$q.defer();
85                 var formData = new FormData();
86                 formData.append('file', file);
87                 this.$http({
88                 method: "POST",
89                 url: this.conf.api.widgetCommon + '/widgetCatalog',
90                 transformRequest: angular.identity,  
91                 data: formData,
92                 headers:{  
93                         'Content-Type': undefined,
94                         'X-Widgets-Type': 'all',
95                         'X-ECOMP-RequestID':this.uuid.generate()
96                 },
97                 params:{
98                         'newWidget': newWidget
99                 }
100             }).then(res => {
101                 if (res == null || res.data == null) {
102                     deferred.reject("WidgetsCatalogService::getManagedWidgets Failed");
103                 } else {
104                     deferred.resolve(res.data);
105                 }
106             })
107             .catch(errRes => {
108                 deferred.reject(errRes);
109             });
110             return deferred.promise;
111         }
112         
113         updateWidgetWithFile(file, widgetId, newWidget){
114                 console.log(widgetId);
115                 let deferred = this.$q.defer();
116             var formData = new FormData();
117                 formData.append('file', file);
118                 let url = this.conf.api.widgetCommon + '/widgetCatalog/' + widgetId;
119                 this.$http({
120                 method: 'POST',
121                 url: url, 
122                 transformRequest: angular.identity,  
123                 data: formData,
124                 headers: {        
125                         'Content-Type': undefined,
126                         'X-Widgets-Type': 'all',
127                         'X-ECOMP-RequestID':this.uuid.generate()
128                 },
129                         params:{
130                                 'newWidget': newWidget
131                         }
132             })
133             .then(res => {
134                if (res == null || res.data == null)
135                    deferred.reject("WidgetsCatalogService::saveWidgetFile Failed");
136                else 
137                    deferred.resolve(res.data);
138             })
139             .catch(status => {
140                  deferred.reject(status);
141              });  
142                 return deferred.promise;
143         }
144         
145         updateWidget(widgetId, widgetData) {
146             let deferred = this.$q.defer();
147             let url = this.conf.api.widgetCommon  + '/widgetCatalog' + '/' + widgetId;
148             this.$http({
149                 method: 'PUT',
150                 url: url,
151                 cache: false,
152                 data: widgetData,
153                 headers: {
154                         'X-Widgets-Type': 'all',
155                         'X-ECOMP-RequestID':this.uuid.generate()
156                 }
157             })
158             .then(res => {
159                 deferred.resolve(res.data);
160             })
161             .catch(errRes => {
162                 deferred.reject(errRes);
163             });
164            
165             return deferred.promise;
166         }
167         
168         
169         deleteWidgetFile(widgetName) {
170                 let deferred = this.$q.defer();
171             this.$log.info('WidgetsCatalogService::deleteWidgetCatalog');
172             let url = this.conf.api.widgetCommon + '/doUpload' + '/' + widgetName;
173             this.$http({
174                 method: "DELETE",
175                 url: url,
176                 cache: false,
177                 headers:{  
178                         'X-Widgets-Type': 'all',
179                         'X-ECOMP-RequestID':this.uuid.generate()
180                 }
181             }).then(res => {
182                     deferred.resolve(res.data); 
183                 })
184                 .catch(status => {
185                         deferred.reject(status);
186                 });
187            
188             return deferred.promise;
189         }
190         
191         deleteWidget(widgetId) {
192                 let deferred = this.$q.defer();
193             this.$log.info('WidgetsCatalogService::deleteWidgetCatalog');
194             let url = this.conf.api.widgetCommon + '/widgetCatalog'  + '/' + widgetId;
195             this.$http({
196                 method: "DELETE",
197                 url: url,
198                 cache: false,
199                 headers:{  
200                         'X-Widgets-Type': 'all',
201                         'X-ECOMP-RequestID':this.uuid.generate()
202                 }
203             }).then(res => {
204                     deferred.resolve(res.data); 
205                 })
206                 .catch(status => {
207                         deferred.reject(status);
208                 });
209            
210             return deferred.promise;
211         }
212         
213         downloadWidgetFile(widgetId) {
214                 let deferred = this.$q.defer();
215             this.$log.info('WidgetsCatalogService::downloadWidgetFile');
216             let url = this.conf.api.widgetCommon + '/download/' + widgetId;
217             console.log(url);
218             this.$http({
219                 method: "GET",
220                 url: url,
221                 cache: false,
222                 responseType: "arraybuffer",
223                 headers:{  
224                         'X-Widgets-Type': 'all',
225                         'X-ECOMP-RequestID':this.uuid.generate()
226                 }
227             }).then(res => {
228                     deferred.resolve(res.data); 
229                 })
230                 .catch(status => {
231                         deferred.reject(status);
232                 });
233            
234             return deferred.promise;
235         }
236         
237         updateWidgetCatalog(appData){
238                 let deferred = this.$q.defer();
239             // Validate the request, maybe this is overkill
240             if (appData == null || appData.widgetId == null || appData.select == null) {
241                 var msg = 'WidgetCatalogService::updateAppCatalog: field appId and/or select not found';
242                 this.$log.error(msg);
243                 return deferred.reject(msg);
244             }
245             this.$http({
246                 method: "PUT",
247                 url: this.conf.api.widgetCatalogSelection,
248                 data: appData,
249                 headers: {
250                          'X-Widgets-Type': 'all',
251                          'X-ECOMP-RequestID':this.uuid.generate()
252                 }
253             }).then( res => {
254                     // Detect non-JSON
255                     if (res == null || res.data == null) {
256                         deferred.reject("WidgetCatalogService::updateAppCatalog Failed");
257                     } else {
258                         deferred.resolve(res.data);
259                     }
260                 })
261                 .catch( status => {
262                         this.$log.error('WidgetCatalogService:updateAppCatalog failed: ' + status);
263                     deferred.reject(status);
264                 });
265             return deferred.promise;
266         }
267         
268         getServiceJSON(serviceId){
269                 let deferred = this.$q.defer();
270             this.$log.info('WidgetsCatalogService::getServiceJSON');
271             let url = this.conf.api.microserviceProxy + "/" + serviceId;
272             console.log(url);
273             this.$http({
274                 method: "GET",
275                 url: url,
276                 cache: false,
277                 headers:{  
278                         'X-Widgets-Type': 'all',
279                         'X-ECOMP-RequestID':this.uuid.generate()
280                 }
281             }).then(res => {
282                         if (res == null || res == null) {
283                                         this.$log.error('WidgetCatalogService::getServiceJSON Failed: Result or result.data is null');
284                             deferred.reject("WidgetCatalogService::getServiceJSON Failed: Result or result.data is null");
285                         } else{
286                                 deferred.resolve(res.data);
287                         }
288                         
289                 })
290                 .catch(status => {
291                         deferred.reject(status);
292                 });
293            
294             return deferred.promise;
295         }
296         
297         getWidgetCatalogParameters(widgetId){
298                 let deferred = this.$q.defer();
299             this.$log.info('WidgetsCatalogService::getWidgetCatalogParameters');
300             let url = this.conf.api.widgetCommon + "/parameters/" + widgetId;
301             console.log(url);
302             this.$http({
303                 method: "GET",
304                 url: url,
305                 cache: false,
306                 headers:{  
307                         'X-Widgets-Type': 'all',
308                         'X-ECOMP-RequestID':this.uuid.generate()
309                 }
310             }).then(res => {
311                         if (res == null || res.data == null) {
312                                         this.$log.error('WidgetCatalogService::getWidgetCatalogParameters Failed: Result or result.data is null');
313                             deferred.reject("WidgetCatalogService::getWidgetCatalogParameters Failed: Result or result.data is null");
314                         } else {
315                             deferred.resolve(res.data);
316                         }
317                 })
318                 .catch(status => {
319                         deferred.reject(status);
320                 });
321            
322             return deferred.promise;
323         }
324         
325         
326         saveWidgetParameter(widgetParamObject){
327                 let deferred = this.$q.defer();
328             this.$log.info('WidgetsCatalogService::saveWidgetParameter');
329             let url = this.conf.api.widgetCommon + "/parameters";
330             this.$http({
331                 method: "POST",
332                 url: url,
333                 cache: false,
334                 data: widgetParamObject,
335                 headers:{  
336                         'X-Widgets-Type': 'all',
337                         'X-ECOMP-RequestID':this.uuid.generate()
338                 }
339             }).then(res => {
340                         if (res == null || res.data == null) {
341                                         this.$log.error('WidgetCatalogService::getWidgetCatalogParameters Failed: Result or result.data is null');
342                             deferred.reject("WidgetCatalogService::getWidgetCatalogParameters Failed: Result or result.data is null");
343                         } else {
344                             deferred.resolve(res.data);
345                         }
346                 })
347                 .catch(status => {
348                         deferred.reject(status);
349                 });
350            
351             return deferred.promise;
352         }
353         
354     }
355     
356     WidgetsCatalogService.$inject = ['$q', '$log', '$http', 'conf','uuid4','base64Service', 'beReaderService', 'utilsService'];
357     angular.module('ecompApp').service('widgetsCatalogService', WidgetsCatalogService)
358 })();