Yangman history and collections tab
[dlux.git] / modules / yangman-resources / src / main / resources / yangman / models / history-request.model.js
1 define([], function (){
2     'use strict';
3
4     /**
5      * Base history request object
6      * @constructor
7      * @param PathUtilsService
8      * @param YangUtilsService
9      * @param ParsingJsonService
10      */
11     function HistoryRequestModel(PathUtilsService, YangUtilsService, ParsingJsonService){
12         var self = this;
13
14         // properties
15         self.name = '';
16         self.sentData = null;
17         self.path = '';
18         self.parametrizedPath = null;
19         self.method = '';
20         self.status = '';
21         self.receivedData = null;
22         self.api = null;
23         self.availability = false;
24         self.collection = '';
25         self.timestamp = '';
26         self.selected = false;
27
28         // functions
29         self.getIdentifiers = getIdentifiers;
30         self.refresh = refresh;
31         self.clone = clone;
32         self.toJSON = toJSON;
33         self.clonePathArray = clonePathArray;
34         self.setParametrizedPath = setParametrizedPath;
35         self.getLastPathDataElemName = getLastPathDataElemName;
36         self.setDataForView = setDataForView;
37         self.clearParametrizedData = clearParametrizedData;
38         self.copyWithParametrizationAsNatural = copyWithParametrizationAsNatural;
39         self.setData = setData;
40
41         /**
42          * Grouped setter
43          *
44          * @param sentData
45          * @param receivedData
46          * @param status
47          * @param path
48          * @param parametrizedPath
49          * @param operation
50          * @param api
51          * @param name
52          * @param collection
53          * @param timestamp
54          */
55         function setData(sentData, receivedData, status, path, parametrizedPath, operation, api, name, collection,
56                          timestamp) {
57
58             self.sentData = sentData === null || sentData === undefined || $.isEmptyObject(sentData) ? null : sentData;
59             self.name = name;
60             self.path = path;
61             self.parametrizedPath = parametrizedPath;
62             self.method = operation;
63             self.status = status;
64             self.receivedData = receivedData === null || receivedData === undefined || $.isEmptyObject(receivedData) ?
65                 null :
66                 receivedData;
67             self.api = api;
68             self.availability = (api !== null);
69             self.collection = collection;
70             self.timestamp = timestamp;
71         }
72
73         /**
74          *
75          * @returns {Array}
76          */
77         function getIdentifiers() {
78             var identifiers = [];
79
80             self.api.pathArray.forEach(function (elem) {
81                 elem.identifiers.forEach(function (i) {
82                     identifiers.push(i);
83                 });
84             });
85
86             return identifiers;
87         }
88
89         /**
90          * Refresh each element using getApiFunction
91          * @param getApiFunction
92          */
93         function refresh(getApiFunction) {
94             var refreshedApi = getApiFunction(self.path);
95
96             self.api = refreshedApi;
97             self.availability = (refreshedApi !== null);
98         }
99
100         /**
101          *
102          * @returns {{
103              *  sentData: *,
104              *  receivedData: *,
105              *  path: *,
106              *  collection: *,
107              *  parametrizedPath: *,
108              *  method: *,
109              *  status: *,
110              *  name: *,
111              *  timestamp: *
112              * }}
113          */
114         function toJSON() {
115             var obj = {
116                 sentData: self.sentData,
117                 receivedData: self.receivedData,
118                 path: self.path,
119                 collection: self.collection,
120                 parametrizedPath: self.parametrizedPath,
121                 method: self.method,
122                 status: self.status,
123                 name: self.name,
124                 timestamp: self.timestamp,
125             };
126
127             return obj;
128         }
129
130         /**
131          *
132          */
133         function clonePathArray() {
134             if ( self.api && self.api.pathArray ) {
135                 self.api.clonedPathArray = self.api.pathArray.map(function (pe) {
136                     return pe.clone();
137                 });
138             } else {
139                 self.api.clonedPathArray = [];
140             }
141         }
142
143         function setParametrizedPath(){
144             self.clonePathArray();
145             PathUtilsService.fillPath(self.api.clonedPathArray, self.parametrizedPath);
146         }
147
148         /**
149          *
150          * @returns {*}
151          */
152         function getLastPathDataElemName() {
153             var pathArray = self.path.split(':');
154             return pathArray[pathArray.length - 1];
155         }
156
157         /**
158          *
159          * @param sent
160          * @param data
161          * @returns {string}
162          */
163         function setDataForView(sent, data){
164             var newData = {},
165                 parsedData = '';
166
167             angular.copy(data, newData);
168             parsedData = JSON.stringify(
169                 YangUtilsService.stripAngularGarbage(newData, self.getLastPathDataElemName()), null, 4);
170
171             if ( sent && self.api ) {
172                 if ( self.parametrizedPath ) {
173                     self.setParametrizedPath();
174                 } else {
175                     self.clonePathArray();
176                 }
177             }
178
179             return parsedData;
180         }
181
182         function clearParametrizedData() {
183             self.parametrizedPath = null;
184             self.clonePathArray();
185         }
186
187         /**
188          *
189          * @returns {HistoryRequest}
190          */
191         function clone() {
192             var result = new HistoryRequestModel(PathUtilsService, YangUtilsService, ParsingJsonService);
193             result.setData(self.sentData, self.receivedData, self.status, self.path,
194                 self.parametrizedPath, self.method, self.api, self.name, self.collection, self.timestamp);
195             return result;
196         }
197
198         /**
199          *
200          * @param parametrizedPath
201          * @param getApiFunction
202          * @param dataForView
203          * @param JSONparsingErrorClbk
204          * @returns {*}
205          */
206         function copyWithParametrizationAsNatural(parametrizedPath, getApiFunction, dataForView,
207                                                   JSONparsingErrorClbk){
208
209             var parsedJsonObj = null,
210                 result = null;
211
212             parsedJsonObj = ParsingJsonService.parseJson(dataForView, JSONparsingErrorClbk);
213
214             if (parsedJsonObj){
215                 result = new HistoryRequestModel(PathUtilsService, YangUtilsService, ParsingJsonService);
216                 result.setData(parsedJsonObj, self.receivedData, self.status, parametrizedPath, '', self.method,
217                     (getApiFunction || angular.noop)(result.path), self.name, self.collection, self.timestamp);
218             }
219
220             return result;
221         }
222
223     }
224
225     return HistoryRequestModel;
226 });