Replace ecomp references
[portal.git] / ecomp-portal-FE-common / client / app / services / widgets / widgets.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 /**
39  * Created by doritrieur on 12/3/15.
40  */
41 'use strict';
42
43 (function () {
44     class WidgetsService {
45         constructor($q, $log, $http, conf, uuid, utilsService) {
46             this.$q = $q;
47             this.$log = $log;
48             this.$http = $http;
49             this.conf = conf;
50             this.uuid = uuid;
51             this.utilsService = utilsService;
52         }
53
54         getUserWidgets() {
55             let deferred = this.$q.defer();
56             this.$log.info('WidgetsService::getUserWidgets');
57             this.$http({
58                 method: 'GET',
59                 url: this.conf.api.widgets,
60                 cache: false,
61                 headers: {
62                     'X-Widgets-Type': 'all',
63                     'X-ECOMP-RequestID':this.uuid.generate()
64                 }
65             }).then(res => {
66                 // If response comes back as a redirected HTML page which IS NOT a success
67                 if (this.utilsService.isValidJSON(res) == false) {
68                         var msg = 'WidgetsService::getUserWidgets Failed';
69                         this.$log.error(msg);
70                         deferred.reject(msg);
71                 } else {
72                         // this.$log.info('WidgetsService::getUserWidgets Succeeded');
73                         deferred.resolve(res.data);
74                 }
75             })
76             .catch(status => {
77                 deferred.reject(status);
78             });
79             return deferred.promise;
80         }
81
82         getManagedWidgets() {
83             let deferred = this.$q.defer();
84             this.$log.info('WidgetsService::getManagedWidgets');
85             this.$http({
86                 method: 'GET',
87                 url: this.conf.api.widgets,
88                 cache: false,
89                 headers: {
90                     'X-Widgets-Type': 'managed',
91                     'X-ECOMP-RequestID':this.uuid.generate()
92                 }
93             }).then(res => {
94                     // If response comes back as a redirected HTML page which IS NOT a success
95                     if (this.utilsService.isValidJSON(res)== false) {
96                         deferred.reject('WidgetsService::getManagedWidgets Failed');
97                     } else if(Object.keys(res).length == 0 && this.utilsService.isValidJSON(res)) {
98                         deferred.resolve(null);
99                     } else {
100                         this.$log.info('WidgetsService::getManagedWidgets Succeeded');
101                         deferred.resolve(res.data);
102                     }
103                 })
104                 .catch(status => {
105                     deferred.reject(status);
106                 });
107             return deferred.promise;
108         }
109
110         createWidget(widgetData) {
111             let deferred = this.$q.defer();
112             this.$log.info('WidgetsService::createWidget');
113             this.$http({
114                 method: 'POST',
115                 url: this.conf.api.widgets,
116                 cache: false,
117                 data: widgetData,
118                 headers: {
119                     'X-ECOMP-RequestID':this.uuid.generate()
120                 }
121             }).then(res => {
122                     // If response comes back as a redirected HTML page which IS NOT a success
123                     if (this.utilsService.isValidJSON(res)== false) {
124                         deferred.reject('WidgetsService::createWidget Failed');
125                     } else {
126                         this.$log.info('WidgetsService::createWidget Succeeded');
127                         deferred.resolve(res.data);
128                     }
129                 })
130                 .catch(errRes => {
131                     deferred.reject(errRes);
132                 });
133             return deferred.promise;
134         }
135
136         updateWidget(widgetId, widgetData) {
137             let deferred = this.$q.defer();
138             this.$log.info('WidgetsService::updateWidget');
139             let url = this.conf.api.widgets + '/' + widgetId;
140             this.$http({
141                 method: 'PUT',
142                 url: url,
143                 cache: false,
144                 data: widgetData,
145                 headers: {
146                     'X-ECOMP-RequestID':this.uuid.generate()
147                 }
148             }).then(res => {
149                     // If response comes back as a redirected HTML page which IS NOT a success
150                     if (this.utilsService.isValidJSON(res)== false) {
151                         deferred.reject('WidgetsService::updateWidget Failed');
152                     } else {
153                         this.$log.info('WidgetsService::updateWidget Succeeded');
154                         deferred.resolve(res.data);
155                     }
156                 })
157                 .catch(errRes => {
158                     deferred.reject(errRes);
159                 });
160             return deferred.promise;
161         }
162
163         deleteWidget(widgetId) {
164             let deferred = this.$q.defer();
165             this.$log.info('WidgetsService::deleteWidget');
166             let url = this.conf.api.widgets + '/' + widgetId;
167             this.$http({
168                 method: 'DELETE',
169                 url: url,
170                 cache: false,
171                 headers: {
172                     'X-ECOMP-RequestID':this.uuid.generate()
173                 }
174             }).then(res => {
175                     // If response comes back as a redirected HTML page which IS NOT a success
176                     if (this.utilsService.isValidJSON(res)== false) {
177                         deferred.reject('WidgetsService::deleteWidget Failed');
178                     } else {
179                         this.$log.info('WidgetsService::deleteWidget Succeeded');
180                         deferred.resolve(res.data);
181                     }
182                 })
183                 .catch(status => {
184                     deferred.reject(status);
185                 });
186             return deferred.promise;
187         }
188
189         checkIfWidgetUrlExists(urlToValidate) {
190             let deferred = this.$q.defer();
191             this.$log.info('WidgetsService::checkIfWidgetUrlExists:' + urlToValidate);
192             let reqBody = {
193                 url: urlToValidate
194             };
195             this.$http({
196                 method: 'POST',
197                 url: this.conf.api.widgetsValidation,
198                 cache: false,
199                 data: reqBody,
200                 headers: {
201                     'X-ECOMP-RequestID':this.uuid.generate()
202                 }
203             }).then(() => {
204                 //if exists return true
205                     deferred.resolve(true);
206                 })
207                 .catch(response => {
208                     if (this.utilsService.isValidJSON(res)== false) {
209                         deferred.reject('WidgetsService::checkIfWidgetUrlExists Failed');
210                     } else {
211                         if (response.data.status === 404) {
212                             deferred.resolve(false);
213                         } else {
214                             deferred.reject(response.data);
215                         }
216                     }
217                 });
218             return deferred.promise;
219         }
220
221     }
222     WidgetsService.$inject = ['$q', '$log', '$http', 'conf','uuid4', 'utilsService'];
223     angular.module('ecompApp').service('widgetsService', WidgetsService)
224 })();