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