Merge "Yangman - improvements"
[dlux.git] / modules / yangman-resources / src / main / resources / yangman / services / requests.services.js
1 define([
2     'app/yangman/models/historylist.model',
3     'app/yangman/models/collectionlist.model',
4     'app/yangman/models/history-request.model',
5 ], function (HistoryListModel, CollectionListModel, HistoryRequestModel) {
6     'use strict';
7
8     angular.module('app.yangman').service('RequestsService', RequestsService);
9
10     RequestsService.$inject = ['PathUtilsService', 'ParametersService', 'ParsingJsonService', 'YangUtilsService'];
11
12     function RequestsService(PathUtilsService, ParametersService, ParsingJsonService, YangUtilsService){
13
14         var service = {};
15
16         service.applyParams = applyParams;
17         service.createEmptyCollectionList = createEmptyCollectionList;
18         service.createEmptyHistoryList = createEmptyHistoryList;
19         service.createHistoryRequestFromElement = createHistoryRequestFromElement;
20         service.createHistoryRequest = createHistoryRequest;
21         service.scanDataParams = scanDataParams;
22         service.replaceStringInText = replaceStringInText;
23
24         /**
25          * Scan used parameters in current line of codemirror
26          * @param {ParametersListModel} paramsObj - list of parameters to be searched for
27          * @param {string} lineString - line from current codemirror to be inspected
28          * @returns array of {ParameterModel}
29          */
30         function scanDataParams(paramsObj, lineString) {
31
32             var usedParamLabelArray = [];
33
34             var params = lineString ? lineString.match(/<<(?!<<)[a-zA-Z0-9]+>>/g) : null;
35
36             if ( params ) {
37                 params
38                     .filter(onlyUnique)
39                     .forEach(function (param) {
40                         usedParamLabelArray.push(removeUnwantedChars(param));
41                     });
42             }
43
44             var returnedParamsList = paramsObj.list.filter( function (param){
45                 var paramIndex = usedParamLabelArray.indexOf(param.name);
46
47                 if ( paramIndex !== -1 ) {
48                     return usedParamLabelArray.splice(paramIndex, 1);
49                 }
50                 else {
51                     return false;
52                 }
53             });
54
55             usedParamLabelArray.forEach(function (param){
56                 returnedParamsList.push(ParametersService.createParameter({ name: param }));
57             });
58
59             return returnedParamsList;
60
61             /**
62              * remove chars greater then and less then from parameter definition
63              * @param val
64              * @returns {string}
65              */
66             function removeUnwantedChars(val){
67                 var string = val.substring(2);
68                 return string.substring(0, string.indexOf('>>'));
69             }
70
71             /**
72              * Filter function
73              * @param value
74              * @param index
75              * @param self
76              * @returns {boolean}
77              */
78             function onlyUnique(value, index, self) {
79                 return self.indexOf(value) === index;
80             }
81         }
82
83         /**
84          * Replace all parameters with its values
85          * @param paramsObj
86          * @param requestData
87          * @returns {*}
88          */
89         function applyParams(paramsObj, data) {
90             var dataStr = JSON.stringify(data);
91
92             if (paramsObj && paramsObj.hasOwnProperty('list')) {
93                 paramsObj.list.forEach(function (param){
94                     dataStr = service.replaceStringInText(dataStr, '<<' + param.name + '>>', param.value);
95                 });
96             }
97
98             return ParsingJsonService.parseJson(dataStr);
99         }
100
101         /**
102          * Service for replacing string in text
103          * @param text
104          * @param strToReplace
105          * @param newStr
106          * @returns {*}
107          */
108         function replaceStringInText(text, strToReplace, newStr) {
109             var replacedText = text;
110             if (text.indexOf(strToReplace) > -1) {
111                 replacedText = text.split(strToReplace).join(newStr);
112             }
113             return replacedText;
114         }
115
116         /**
117          * Service for creating basic history object
118          * @param sentData
119          * @param receivedData
120          * @param path
121          * @param operation
122          * @param status
123          * @param name
124          * @param collection
125          * @returns {*}
126          * @param timestamp
127          */
128         function createHistoryRequest(sentData, receivedData, path, operation, status, name, collection, timestamp,
129                         responseStatus, responseStatusText, responseTime) {
130             var receivedDataProcessed = status === 'success' ? receivedData : null,
131                 result = new HistoryRequestModel(PathUtilsService, YangUtilsService, ParsingJsonService);
132
133             result.setData(sentData, receivedDataProcessed, status, path, operation, name, collection, timestamp,
134                 responseStatus, responseStatusText, responseTime);
135
136             return result;
137         }
138
139         /**
140          * Creating {HistoryRequest} from elem containing all necessary data
141          * @param {Object} elem
142          * @returns {*}
143          */
144         function createHistoryRequestFromElement(elem) {
145             if (!elem.hasOwnProperty('timestamp')){
146                 elem.timestamp = Date.now();
147             }
148
149             return service.createHistoryRequest(elem.sentData, elem.receivedData, elem.path, elem.method,
150                     elem.status, elem.name, elem.collection, elem.timestamp, elem.responseStatus,
151                     elem.responseStatusText, elem.responseTime
152                 );
153         }
154
155         /**
156          * Service for creating empty collection list
157          * @param name
158          * @param getApiFunction
159          * @returns {CollectionList}
160          */
161         function createEmptyCollectionList(name){
162             var result = new CollectionListModel(ParsingJsonService, service);
163             result.setName(name);
164             return result;
165         }
166
167         /**
168          * Service for creating empty history list
169          * @param name
170          * @returns {*}
171          */
172         function createEmptyHistoryList(name){
173             var result = new HistoryListModel(ParsingJsonService, service);
174             result.setName(name);
175             return result;
176         }
177
178         return service;
179
180     }
181
182 });