[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / client / app / services / dashboard / dashboard.service.js
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ================================================================================\r
19  */\r
20 /**\r
21  * Created by robertlo on 09/26/2016.\r
22  */\r
23 'use strict';\r
24 \r
25 (function () {\r
26     class DashboardService {\r
27         constructor($q, $log, $http, conf, uuid) {\r
28             this.$q = $q;\r
29             this.$log = $log;\r
30             this.$http = $http;\r
31             this.conf = conf;\r
32             this.dashboardService = null;\r
33             this.uuid = uuid;\r
34         }\r
35 \r
36         getCommonWidgetData(widgetType) {\r
37             // this.$log.info('ecomp::dashboard-service::getting news data');\r
38             let deferred = this.$q.defer();\r
39             let url = this.conf.api.commonWidget + '?resourceType=' + widgetType;\r
40                  \r
41             this.$http({\r
42                     method: "GET",\r
43                     url: url,\r
44                     cache: false,\r
45                     headers: {\r
46                         'X-ECOMP-RequestID':this.uuid.generate()\r
47                     }\r
48                 })\r
49                 .then( res => {\r
50                          // this.$log.info('ecomp::dashboard-service::getting news data',res);\r
51                     // If response comes back as a redirected HTML page which IS NOT a success\r
52                     if (Object.keys(res.data).length == 0 || Object.keys(res.data.response) ==null || Object.keys(res.data.response.items) ==null) {\r
53                         deferred.reject("ecomp::dashboard-service::getNewsData Failed");\r
54                     } else {\r
55                         this.userProfile = res.data;\r
56                         // this.$log.info('ecomp::dashboard-service::getNewsData Succeeded');\r
57                         deferred.resolve(res.data);\r
58                     }\r
59                 })\r
60                 .catch( status => {\r
61                     deferred.reject(status);\r
62                 });\r
63             return deferred.promise;\r
64         }\r
65         \r
66         saveCommonWidgetData(newData){\r
67             let deferred = this.$q.defer();\r
68             //this.$log.info('ecomp::dashboard-service::saveCommonWidgetData');\r
69             //this.$log.debug('ecomp::dashboard-service::saveCommonWidgetData with:', newData);\r
70 \r
71             this.$http({\r
72                 method: "POST",\r
73                 url: this.conf.api.commonWidget,\r
74                 data: newData,\r
75                 cache: false,\r
76                 headers: {\r
77                     'X-ECOMP-RequestID':this.uuid.generate()\r
78                 }\r
79             }).then( res => {\r
80                     // If response comes back as a redirected HTML page which IS NOT a success\r
81                         // this.$log.info(res.data);\r
82                     if (Object.keys(res.data).length == 0) {\r
83                         deferred.reject("ecomp::dashboard-service::saveCommonWidgetData Failed");\r
84                     } else {\r
85                         // this.$log.info('ecomp::dashboard-service::saveCommonWidgetData Succeeded');\r
86                         deferred.resolve(res.data);\r
87                     }\r
88                 })\r
89                 .catch( status => {\r
90                     deferred.reject(status);\r
91                 });\r
92             return deferred.promise;\r
93         }\r
94         \r
95         \r
96         \r
97         removeCommonWidgetData(widgetToRemove){\r
98             let deferred = this.$q.defer();\r
99             // this.$log.info('ecomp::dashboard-service::removeCommonWidgetData');\r
100             // this.$log.debug('ecomp::dashboard-service::removeCommonWidgetData with:', widgetToRemove);\r
101             this.$http({\r
102                 method: "POST",\r
103                 url: this.conf.api.deleteCommonWidget,\r
104                 data: widgetToRemove,\r
105                 cache: false,\r
106                 headers: {\r
107                     'X-ECOMP-RequestID':this.uuid.generate()\r
108                 }\r
109             }).then( res => {\r
110                     // If response comes back as a redirected HTML page which IS NOT a success\r
111                         // this.$log.info(res.data);\r
112                     if (Object.keys(res.data).length == 0) {\r
113                         deferred.reject("ecomp::dashboard-service::saveCommonWidgetData Failed");\r
114                     } else {\r
115                         // this.$log.info('ecomp::dashboard-service::saveCommonWidgetData Succeeded');\r
116                         deferred.resolve(res.data);\r
117                     }\r
118                 })\r
119                 .catch( status => {\r
120                     deferred.reject(status);\r
121                 });\r
122             return deferred.promise;\r
123         }\r
124 \r
125         getSearchAllByStringResults(searchStr) {\r
126                 // this.$log.info('ecomp::getSearchAllByStringResults::getting search by string results');\r
127             let deferred = this.$q.defer();\r
128             let url = this.conf.api.getSearchAllByStringResults;\r
129             \r
130             this.$http({\r
131                     method: "GET",\r
132                     url : url,\r
133                     params : {\r
134                         'searchString' : searchStr\r
135                     },\r
136                     cache: false,\r
137                     headers: {\r
138                         'X-ECOMP-RequestID':this.uuid.generate()\r
139                     }\r
140                 }).then( res => {\r
141                     // If response comes back as a redirected HTML page which IS NOT a success\r
142                     if (Object.keys(res.data).length == 0) {\r
143                         deferred.reject("ecomp::dashboard-service::getSearchAllByStringResults Failed");\r
144                     } else {\r
145                         //this.searchResults = res.data;\r
146                         // this.$log.info('ecomp::dashboard-service::getSearchAllByStringResults Succeeded');\r
147                         deferred.resolve(res.data.response);\r
148                     }\r
149                 }).catch( status => {\r
150                     this.$log.error('ecomp::getSearchAllByStringResults error');\r
151                     deferred.reject(status);\r
152                 });\r
153             return deferred.promise;\r
154                 \r
155         }\r
156         \r
157         getOnlineUserUpdateRate() {\r
158             let deferred = this.$q.defer();\r
159             let url = this.conf.api.onlineUserUpdateRate;\r
160             this.$http({\r
161                     method: "GET",\r
162                     url: url,\r
163                     cache: false,\r
164                     headers: {\r
165                         'X-ECOMP-RequestID':this.uuid.generate()\r
166                     }\r
167                 }).then( res => {\r
168                     // If response comes back as a redirected HTML page which IS NOT a success\r
169                         if (Object.keys(res.data).length == 0) {\r
170                         deferred.reject("ecomp::dashboard-service::getOnlineUserUpdateRate Failed");\r
171                     } else {\r
172                         // this.$log.info('ecomp::dashboard-service::getOnlineUserUpdateRate Succeeded',res);\r
173                         deferred.resolve(res.data);\r
174                     }\r
175                 }).catch( status => {\r
176                     deferred.reject(status);\r
177                 });\r
178             return deferred.promise;\r
179         }\r
180     \r
181     }\r
182     \r
183     DashboardService.$inject = ['$q', '$log', '$http', 'conf', 'uuid4'];\r
184     angular.module('ecompApp').service('dashboardService', DashboardService)\r
185 })();\r