Merge "Yangman - Deselect history requests"
[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.collection = '';
16         self.method = '';
17         self.name = '';
18         self.path = '';
19         self.receivedData = null;
20         self.selected = false;
21         self.sentData = null;
22         self.status = '';
23         self.timestamp = '';
24
25         // functions
26         self.clone = clone;
27         self.toJSON = toJSON;
28         self.getLastPathDataElemName = getLastPathDataElemName;
29         self.setDataForView = setDataForView;
30         self.setData = setData;
31         self.setExecutionData = setExecutionData;
32
33         /**
34          * Grouped setter
35          *
36          * @param sentData
37          * @param receivedData
38          * @param status
39          * @param path
40          * @param operation
41          * @param name
42          * @param collection
43          * @param timestamp
44          */
45         function setData(sentData, receivedData, status, path, operation, name, collection, timestamp) {
46
47             self.sentData = sentData === null || sentData === undefined || $.isEmptyObject(sentData) ? null : sentData;
48             self.name = name;
49             self.path = path;
50             self.method = operation;
51             self.status = status;
52             self.receivedData = receivedData === null || receivedData === undefined || $.isEmptyObject(receivedData) ?
53                 null : receivedData;
54             self.collection = collection;
55             self.timestamp = timestamp;
56         }
57
58         /**
59          * Set data which might be available after executing request
60          * @param sentData
61          * @param receivedData
62          * @param status - http status from response header
63          */
64         function setExecutionData(sentData, receivedData, status) {
65             self.sentData = sentData;
66             self.receivedData = receivedData;
67             self.status = status ? (status > 199 && status < 205 ? 'success' : 'erorr') : '';
68         }
69
70
71         /**
72          *
73          * @returns {{sentData: (null|*), receivedData: (null|*), path: (string|*), collection: (string|*),
74          * method: (string|*), status: (string|*), name: (string|*), timestamp: (string|*)}}
75          */
76         function toJSON() {
77             var obj = {
78                 sentData: self.sentData,
79                 receivedData: self.receivedData,
80                 path: self.path,
81                 collection: self.collection,
82                 method: self.method,
83                 status: self.status,
84                 name: self.name,
85                 timestamp: self.timestamp,
86             };
87
88             return obj;
89         }
90
91
92
93         /**
94          *
95          * @returns {*}
96          */
97         function getLastPathDataElemName() {
98             var pathArray = self.path.split(':');
99             return pathArray[pathArray.length - 1];
100         }
101
102         /**
103          *
104          * @param sent
105          * @param data
106          * @returns {string}
107          */
108         function setDataForView(data){
109             var newData = {},
110                 parsedData = '';
111
112             angular.copy(data, newData);
113             parsedData = JSON.stringify(
114                 YangUtilsService.stripAngularGarbage(newData, self.getLastPathDataElemName()), null, 4);
115
116             return parsedData;
117         }
118
119
120         /**
121          *
122          * @returns {HistoryRequest}
123          */
124         function clone() {
125             var result = new HistoryRequestModel(PathUtilsService, YangUtilsService, ParsingJsonService);
126             result.setData(self.sentData, self.receivedData, self.status, self.path, self.method, self.name,
127                 self.collection, self.timestamp);
128             return result;
129         }
130
131     }
132
133     return HistoryRequestModel;
134 });