e808b5a5e3b4c761e7345d0f52980bf7c31b32aa
[portal.git] / ecomp-portal-FE-common / client / app / services / catalog / 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 'use strict';
39
40 (function () {
41     class CatalogService {
42         
43         constructor($q, $log, $http, conf, uuid, utilsService) {
44             this.$q = $q;
45             this.$log = $log;
46             this.$http = $http;
47             this.conf = conf;
48             this.uuid = uuid;
49             this.debug = false;
50             this.utilsService = utilsService;
51         }
52
53         getAppCatalog() {
54             let deferred = this.$q.defer();
55             this.$http({
56                         method: "GET",
57                         url: this.conf.api.appCatalog,
58                     cache: false,
59                     headers: {
60                         'X-ECOMP-RequestID':this.uuid.generate()
61                     }
62                 })
63                 .then( res => {
64                         if (this.debug)
65                                 this.$log.debug('CatalogService::getAppCatalog: result is ' + JSON.stringify(res));
66                     // Res is always JSON, but the data object might be an HTML error page.
67                     if (! this.utilsService.isValidJSON(res.data)) {
68                         var msg = 'CatalogService::getAppCatalog: result data is not JSON';
69                         if (this.debug)
70                                 this.$log.debug(msg);
71                         deferred.reject(msg);
72                     } else {
73                         if (this.debug)
74                                 this.$log.debug('CatalogService::getAppCatalog: success');
75                         deferred.resolve(res.data);
76                     }
77                 })
78                 .catch( status => {
79                         this.$log.error('CatalogService:getAppCatalog failed: ' + status);
80                     deferred.reject(status);
81                 });
82             return deferred.promise;
83         }
84         
85         // Expects an object with fields matching model class AppCatalogSelection:
86         // appId (number), select (boolean), pending (boolean).
87         updateAppCatalog(appData) {
88             let deferred = this.$q.defer();
89             // Validate the request, maybe this is overkill
90             if (appData == null || appData.appId == null || appData.select == null) {
91                 var msg = 'CatalogService::updateAppCatalog: field appId and/or select not found';
92                 this.$log.error(msg);
93                 return deferred.reject(msg);
94             }
95             this.$http({
96                 method: "PUT",
97                 url: this.conf.api.appCatalog,
98                 data: appData,
99                 headers: {
100                     'X-ECOMP-RequestID':this.uuid.generate()
101                 }
102             }).then( res => {
103                     // Detect missing result
104                     if (res == null || res.data == null) {
105                         deferred.reject("CatalogService::updateAppCatalog Failed");
106                     } else {
107                         deferred.resolve(res.data);
108                     }
109                 })
110                 .catch( status => {
111                         this.$log.error('CatalogService:updateAppCatalog failed: ' + status);
112                     deferred.reject(status);
113                 });
114             return deferred.promise;
115         }
116         
117         // Expects an object with fields and used to update records for ep_pers_user_app_man_sort table:
118         // appId (number), select (boolean).
119         updateManualAppSort(appData) {
120             let deferred = this.$q.defer();
121             
122             // Validate the request, maybe this is overkill
123             if (appData == null || appData.appId == null || appData.select == null) {
124                 var msg = 'CatalogService::updateManualAppSort: field appId and/or select not found';
125                 this.$log.error(msg);
126                 return deferred.reject(msg);
127             }
128             this.$http({
129                 method: "PUT",
130                 url: this.conf.api.UpdateUserAppsSortManual,
131                 data: appData,
132                 headers: {
133                     'X-ECOMP-RequestID':this.uuid.generate()
134                 }
135             }).then( res => {
136                     // Detect missing result
137                     if (res == null || res.data == null) {
138                         deferred.reject("CatalogService::updateManualAppSort Failed");
139                     } else {
140                         deferred.resolve(res.data);
141                     }
142                 })
143                 .catch( status => {
144                         this.$log.error('CatalogService:updateManualAppSort failed: ' + status);
145                     deferred.reject(status);
146                 });
147             
148             return deferred.promise;
149         }
150            
151         getuserAppRolesCatalog(item) {
152             let deferred = this.$q.defer();
153             this.$http({
154                         method: "GET",
155                         url: this.conf.api.appCatalogRoles,
156                         params:{appName:item},
157                     cache: false,
158                     headers: {
159                         'X-ECOMP-RequestID':this.uuid.generate()
160                     }
161                 })
162                 .then( res => {
163                         if (this.debug)
164                                 this.$log.debug('CatalogService::getAppCatalog: result is ' + JSON.stringify(res));
165                     // Res is always JSON, but the data object might be an HTML error page.
166                     if (! this.utilsService.isValidJSON(res.data)) {
167                         var msg = 'CatalogService::getAppCatalog: result data is not JSON';
168                         if (this.debug)
169                                 this.$log.debug(msg);
170                         deferred.reject(msg);
171                     } else {
172                         if (this.debug)
173                                 this.$log.debug('CatalogService::getAppCatalog: success');
174                         deferred.resolve(res.data);
175                     }
176                 })
177                 .catch( status => {
178                         this.$log.error('CatalogService:getAppCatalog failed: ' + status);
179                     deferred.reject(status);
180                 });
181             return deferred.promise;
182         }
183         
184         getAppsFullList() {
185             let deferred = this.$q.defer();
186             this.$http({
187                         method: "GET",
188                         url: this.conf.api.appsFullList,
189                     cache: false,
190                     headers: {
191                         'X-ECOMP-RequestID':this.uuid.generate()
192                     }
193                 })
194                 .then( res => {
195                         if (this.debug)
196                                 this.$log.debug('CatalogService::getAppsFullList: result is ' + JSON.stringify(res));
197                     // Res is always JSON, but the data object might be an HTML error page.
198                     if (! this.utilsService.isValidJSON(res.data)) {
199                         var msg = 'CatalogService::getAppsFullList: result data is not JSON';
200                         if (this.debug)
201                                 this.$log.error(msg);
202                         deferred.reject(msg);
203                     } else {
204                         if (this.debug)
205                                 this.$log.debug('CatalogService::getAppsFullList: success');
206                         deferred.resolve(res.data);
207                     }
208                 })
209                 .catch( status => {
210                         this.$log.error('CatalogService:getAppsFullList failed: ' + status);
211                     deferred.reject(status);
212                 });
213             return deferred.promise;
214         }
215
216     }
217     
218     CatalogService.$inject = ['$q', '$log', '$http', 'conf','uuid4', 'utilsService'];
219     angular.module('ecompApp').service('catalogService', CatalogService)
220 })();