e3f252e6147c9eba37e747088948c05d16ea4054
[portal.git] / ecomp-portal-FE-common / client / app / services / dashboard / dashboard.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 /**
39  * Created by robertlo on 09/26/2016.
40  */
41 'use strict';
42
43 (function () {
44     class DashboardService {
45         constructor($q, $log, $http, conf, uuid) {
46             this.$q = $q;
47             this.$log = $log;
48             this.$http = $http;
49             this.conf = conf;
50             this.dashboardService = null;
51             this.uuid = uuid;
52         }
53
54         getCommonWidgetData(widgetType) {
55             // this.$log.info('ecomp::dashboard-service::getting news data');
56             let deferred = this.$q.defer();
57             let url = this.conf.api.commonWidget + '?resourceType=' + widgetType;
58                  
59             this.$http({
60                     method: "GET",
61                     url: url,
62                     cache: false,
63                     headers: {
64                         'X-ECOMP-RequestID':this.uuid.generate()
65                     }
66                 })
67                 .then( res => {
68                          // this.$log.info('ecomp::dashboard-service::getting news data',res);
69                     // If response comes back as a redirected HTML page which IS NOT a success
70                     if (Object.keys(res.data).length == 0 || Object.keys(res.data.response) ==null || Object.keys(res.data.response.items) ==null) {
71                         deferred.reject("ecomp::dashboard-service::getNewsData Failed");
72                     } else {
73                         this.userProfile = res.data;
74                         // this.$log.info('ecomp::dashboard-service::getNewsData Succeeded');
75                         deferred.resolve(res.data);
76                     }
77                 })
78                 .catch( status => {
79                     deferred.reject(status);
80                 });
81             return deferred.promise;
82         }
83         
84         saveCommonWidgetData(newData){
85             let deferred = this.$q.defer();
86             //this.$log.info('ecomp::dashboard-service::saveCommonWidgetData');
87             //this.$log.debug('ecomp::dashboard-service::saveCommonWidgetData with:', newData);
88
89             this.$http({
90                 method: "POST",
91                 url: this.conf.api.commonWidget,
92                 data: newData,
93                 cache: false,
94                 headers: {
95                     'X-ECOMP-RequestID':this.uuid.generate()
96                 }
97             }).then( res => {
98                     // If response comes back as a redirected HTML page which IS NOT a success
99                         // this.$log.info(res.data);
100                     if (Object.keys(res.data).length == 0) {
101                         deferred.reject("ecomp::dashboard-service::saveCommonWidgetData Failed");
102                     } else {
103                         // this.$log.info('ecomp::dashboard-service::saveCommonWidgetData Succeeded');
104                         deferred.resolve(res.data);
105                     }
106                 })
107                 .catch( status => {
108                     deferred.reject(status);
109                 });
110             return deferred.promise;
111         }
112         
113         
114         
115         removeCommonWidgetData(widgetToRemove){
116             let deferred = this.$q.defer();
117             // this.$log.info('ecomp::dashboard-service::removeCommonWidgetData');
118             // this.$log.debug('ecomp::dashboard-service::removeCommonWidgetData with:', widgetToRemove);
119             this.$http({
120                 method: "POST",
121                 url: this.conf.api.deleteCommonWidget,
122                 data: widgetToRemove,
123                 cache: false,
124                 headers: {
125                     'X-ECOMP-RequestID':this.uuid.generate()
126                 }
127             }).then( res => {
128                     // If response comes back as a redirected HTML page which IS NOT a success
129                         // this.$log.info(res.data);
130                     if (Object.keys(res.data).length == 0) {
131                         deferred.reject("ecomp::dashboard-service::saveCommonWidgetData Failed");
132                     } else {
133                         // this.$log.info('ecomp::dashboard-service::saveCommonWidgetData Succeeded');
134                         deferred.resolve(res.data);
135                     }
136                 })
137                 .catch( status => {
138                     deferred.reject(status);
139                 });
140             return deferred.promise;
141         }
142
143         getSearchAllByStringResults(searchStr) {
144                 // this.$log.info('ecomp::getSearchAllByStringResults::getting search by string results');
145             let deferred = this.$q.defer();
146             let url = this.conf.api.getSearchAllByStringResults;
147             
148             this.$http({
149                     method: "GET",
150                     url : url,
151                     params : {
152                         'searchString' : searchStr
153                     },
154                     cache: false,
155                     headers: {
156                         'X-ECOMP-RequestID':this.uuid.generate()
157                     }
158                 }).then( res => {
159                     // If response comes back as a redirected HTML page which IS NOT a success
160                     if (Object.keys(res.data).length == 0) {
161                         deferred.reject("ecomp::dashboard-service::getSearchAllByStringResults Failed");
162                     } else {
163                         //this.searchResults = res.data;
164                         // this.$log.info('ecomp::dashboard-service::getSearchAllByStringResults Succeeded');
165                         deferred.resolve(res.data.response);
166                     }
167                 }).catch( status => {
168                     this.$log.error('ecomp::getSearchAllByStringResults error');
169                     deferred.reject(status);
170                 });
171             return deferred.promise;
172                 
173         }
174         
175         getOnlineUserUpdateRate() {
176             let deferred = this.$q.defer();
177             let url = this.conf.api.onlineUserUpdateRate;
178             this.$http({
179                     method: "GET",
180                     url: url,
181                     cache: false,
182                     headers: {
183                         'X-ECOMP-RequestID':this.uuid.generate()
184                     }
185                 }).then( res => {
186                     // If response comes back as a redirected HTML page which IS NOT a success
187                         if (Object.keys(res.data).length == 0) {
188                         deferred.reject("ecomp::dashboard-service::getOnlineUserUpdateRate Failed");
189                     } else {
190                         // this.$log.info('ecomp::dashboard-service::getOnlineUserUpdateRate Succeeded',res);
191                         deferred.resolve(res.data);
192                     }
193                 }).catch( status => {
194                     deferred.reject(status);
195                 });
196             return deferred.promise;
197         }
198     
199     }
200     
201     DashboardService.$inject = ['$q', '$log', '$http', 'conf', 'uuid4'];
202     angular.module('ecompApp').service('dashboardService', DashboardService)
203 })();