Yangman history and collections tab
[dlux.git] / modules / yangman-resources / src / main / resources / yangman / services / yangman.services.js
1 define([], function () {
2     'use strict';
3
4     angular.module('app.yangman').service('YangmanService', YangmanService);
5
6     YangmanService.$inject = [
7         'RequestBuilderService',
8         'YangUtilsService',
9         'YangUtilsRestangularService',
10         'ENV',
11     ];
12
13     function YangmanService(
14         RequestBuilderService,
15         YangUtilsService,
16         YangUtilsRestangularService,
17         ENV
18     ){
19         var service = {
20             executeRequestOperation: executeRequestOperation,
21             fillNodeFromResponse: fillNodeFromResponse,
22             getDataStoreIndex: getDataStoreIndex,
23         };
24
25         return service;
26
27         /**
28          * Return index of selected datastore in list
29          * @param list
30          * @param dataStore
31          * @returns {*}
32          */
33         function getDataStoreIndex(list, dataStore){
34             var rIndex = null,
35                 result = list.some(function (item, index) {
36                     rIndex = index;
37                     return item.label === dataStore;
38                 });
39
40             return result ? rIndex : null;
41         }
42
43         function executeRequestOperation(
44             selectedApi,
45             selectedSubApi,
46             operation,
47             node, dataType,
48             requestUrl,
49             successCbk,
50             errorCbk
51         ){
52             var reqString = selectedSubApi ? selectedSubApi.buildApiRequestString() : '',
53                 requestData = {},
54                 preparedRequestData = {},
55                 headers,
56                 time = {
57                     started: 0,
58                     finished: 0,
59                 },
60                 customRestangular = null;
61
62             // set full response detail
63             YangUtilsRestangularService.setFullResponse(true);
64
65             // set correct host into restangular based on shown data type
66             if ( dataType === 'json' ){
67                 var parser = locationHelper(requestUrl, ['pathname', 'origin']),
68                     raParam = '';
69
70                 YangUtilsRestangularService.setBaseUrl(parser.origin);
71                 reqString = parser.pathname.slice(1).split('/');
72                 raParam = reqString.shift();
73                 reqString = reqString.join('/');
74
75                 customRestangular = YangUtilsRestangularService.one(raParam);
76             } else {
77                 YangUtilsRestangularService.setBaseUrl(ENV.getBaseURL('MD_SAL'));
78                 customRestangular  = YangUtilsRestangularService.one('restconf');
79             }
80
81             //reqString = reqPath ? reqPath.slice(selectedApi.basePath.length, reqPath.length) : reqString;
82             //var requestPath = selectedApi.basePath + reqString;
83
84             // if node build sent request
85             if ( node ) {
86
87                 node.buildRequest(RequestBuilderService, requestData, node.module);
88                 angular.copy(requestData, preparedRequestData);
89
90                 preparedRequestData = YangUtilsService.prepareRequestData(
91                     preparedRequestData,
92                     operation,
93                     reqString, selectedSubApi
94                 );
95
96                 headers = YangUtilsService.prepareHeaders(preparedRequestData);
97             }
98
99             operation = YangUtilsService.prepareOperation(operation);
100
101             // start track time response
102             time.started = new Date().getMilliseconds();
103
104             // executing operation
105             customRestangular
106                 .customOperation(operation.toLowerCase(), reqString, null, headers, preparedRequestData)
107                 .then(function (response) {
108                     // finish track time response
109                     time.finished = new Date().getMilliseconds();
110
111                     var reqObj = {
112                         status: response.status,
113                         statusText: response.statusText,
114                         time: (time.finished - time.started),
115                     };
116
117                     (successCbk || angular.noop)(reqObj, response);
118
119                 }, function (resp) {
120                     console.log('resp', resp);
121                     // finish track time response
122                     time.finished = new Date().getMilliseconds();
123
124                     var reqObj = {
125                         status: resp.status,
126                         statusText: resp.statusText,
127                         time: (time.finished - time.started),
128                     };
129
130                     (errorCbk || angular.noop)(reqObj);
131
132                     var errorMsg = '';
133
134                     if (resp.data && resp.data.errors && resp.data.errors.error && resp.data.errors.error.length) {
135                         errorMsg = ': ' + resp.data.errors.error.map(function (e) {
136                             return e['error-message'];
137                         }).join(', ');
138                     }
139
140                     /**
141                      * TODO after first GET we have set $scope.node with data
142                      * so build from the top of this function return requestData
143                      */
144                     if (operation === 'GET'){
145                         requestData = {};
146                     }
147
148                     console.info(
149                         'error sending request to', selectedSubApi ? selectedSubApi.buildApiRequestString() : '',
150                         'reqString', reqString,
151                         'got', resp.status,
152                         'data', resp.data
153                     );
154                 }
155             );
156         }
157
158         /**
159          * Method for parsing path to additional properties based on JS LOCATION
160          * @param path
161          * @param properties
162          * @returns {{}}
163          */
164         function locationHelper(path, properties){
165             var parser = document.createElement('a'),
166                 obj = {};
167
168             parser.href = path;
169
170             properties.forEach(function (prop) {
171                 obj[prop] = parser[prop];
172             });
173
174             return obj;
175         }
176
177         /**
178          * Fill node values from response
179          * @param node
180          * @param data
181          */
182         function fillNodeFromResponse(node, data){
183             var props = Object.getOwnPropertyNames(data);
184
185             // fill each property - needed for root mountpoint node,
186             // in other cases there should be only one property anyway
187             props.forEach(function (p) {
188                 node.fill(p, data[p]);
189             });
190         }
191     }
192
193 });