rsanchez
2014-10-24 e5bed7ea0d65114d00b583841066b4883771cae3
securis/src/main/resources/static/js/licenses.js
....@@ -1,22 +1,7 @@
11 (function() {
22 'use strict';
33
4
- var LIC_STATUS = {
5
- CREATED: 1,
6
- REQUESTED: 2,
7
- PREACTIVE: 3,
8
- ACTIVE: 4,
9
- CANCELED: 5
10
- }
11
-
12
- var ACTIONS_BY_STATUS = {
13
- activate: [LIC_STATUS.REQUESTED, LIC_STATUS.PREACTIVE],
14
- send: [LIC_STATUS.ACTIVE, LIC_STATUS.PREACTIVE],
15
- download: [LIC_STATUS.ACTIVE, LIC_STATUS.PREACTIVE],
16
- request: [LIC_STATUS.CREATED, LIC_STATUS.REQUESTED],
17
- cancel: [LIC_STATUS.REQUESTED, LIC_STATUS.PREACTIVE, LIC_STATUS.ACTIVE],
18
- 'delete': [LIC_STATUS.CREATED, LIC_STATUS.CANCELED],
19
- }
4
+
205
216 var HTTP_ERRORS = {
227 401: "Unathorized action",
....@@ -27,6 +12,154 @@
2712 }
2813
2914 var app = angular.module('securis');
15
+ app.service('Packs', ['$L','$resource', 'toaster', function($L, $resource, toaster) {
16
+ var PACK_STATUS = {
17
+ CREATED: 'CR',
18
+ ACTIVE: 'AC',
19
+ ONHOLD: 'OH',
20
+ EXPIRED: 'EX',
21
+ CANCELLED: 'CA'
22
+ }
23
+ var PACK_STATUSES = {
24
+ 'CR': $L.get('Created'),
25
+ 'AC': $L.get('Active'),
26
+ 'OH': $L.get('On Hold'),
27
+ 'EX': $L.get('Expired'),
28
+ 'CA': $L.get('Cancelled')
29
+ };
30
+ /**
31
+ * These transitions could be get from server, class Pack.Status, but we copy them for simplicity, this info won't change easily
32
+ */
33
+ var PACK_ACTIONS_BY_STATUS = {
34
+ activate: [PACK_STATUS.CREATED, PACK_STATUS.EXPIRED, PACK_STATUS.ONHOLD],
35
+ putonhold: [PACK_STATUS.ACTIVE],
36
+ cancel: [PACK_STATUS.EXPIRED, PACK_STATUS.ONHOLD, PACK_STATUS.ACTIVE],
37
+ 'delete': [PACK_STATUS.CREATED, PACK_STATUS.CANCELLED]
38
+ }
39
+
40
+ var packResource = $resource('/pack/:packId/:action',
41
+ {
42
+ packId : '@id',
43
+ action : '@action'
44
+ },
45
+ {
46
+ activate: {
47
+ method: "POST",
48
+ params: {action: "activate"}
49
+ },
50
+ putonhold: {
51
+ method: "POST",
52
+ params: {action: "putonhold"}
53
+ },
54
+ cancel: {
55
+ method: "POST",
56
+ params: {action: "cancel"}
57
+ }
58
+ }
59
+ );
60
+ this.savePackData = function(pack, isNew, _onsuccess) {
61
+ var _success = function() {
62
+ _onsuccess();
63
+ toaster.pop('success', 'Packs', $L.get("Pack '{0}' {1} successfully", pack.code, isNew ? $L.get("created") : $L.get("updated")));
64
+ }
65
+ var _error = function(error) {
66
+ console.log(error);
67
+ toaster.pop('error', 'Packs', $L.get("Error {0} pack '{1}'. Reason: {2}", isNew ? $L.get("creating") : $L.get("updating"), pack.code, $L.get(error.headers('X-SECURIS-ERROR'))));
68
+ }
69
+ packResource.save(pack, _success, _error);
70
+ }
71
+
72
+ this.isActionAvailable = function(action, pack) {
73
+ var validStatuses = PACK_ACTIONS_BY_STATUS[action];
74
+ return pack && validStatuses && validStatuses.indexOf(pack.status) !== -1;
75
+ }
76
+ this.getStatusName = function(status) {
77
+ return PACK_STATUSES[status];
78
+ }
79
+
80
+ var _createSuccessCallback = function(actionName, message, _innerCallback) {
81
+ return function() {
82
+ _innerCallback && _innerCallback();
83
+ toaster.pop('success', actionName, message);
84
+ }
85
+ }
86
+ var _createErrorCallback = function(pack, actionName, _innerCallback) {
87
+ return function(error) {
88
+ console.log(error);
89
+ _innerCallback && _innerCallback();
90
+ toaster.pop('error', actionName, $L.get("Error on action '{0}', pack '{1}'. Reason: {2}", actionName, pack.code, $L.get(error.headers('X-SECURIS-ERROR'))));
91
+ }
92
+ }
93
+ this.getPacksList = function(pack, _onsuccess, _onerror) {
94
+ return packResource.query(_onsuccess, _onerror);
95
+ }
96
+ this.activate = function(pack, _onsuccess, _onerror) {
97
+ console.log('Activation on pack: ' + pack.id);
98
+ var _success = _createSuccessCallback($L.get('Activation'), $L.get("Pack '{0}' {1} successfully", pack.code, $L.get("activated")), _onsuccess);
99
+ var _error = _createErrorCallback(pack, $L.get('Activation'), _onerror);
100
+ packResource.activate({id: pack.id}, _success, _error);
101
+ }
102
+ this.putonhold = function(pack, _onsuccess, _onerror) {
103
+ console.log('Put on hold on pack: ' + pack.id);
104
+ var _success = _createSuccessCallback($L.get('Put on hold'), $L.get("Pack '{0}' {1} successfully", pack.code, $L.get("put on hold")), _onsuccess);
105
+ var _error = _createErrorCallback(pack, $L.get('Put on hold'), _onerror);
106
+ packResource.putonhold({id: pack.id}, _success, _error);
107
+ }
108
+ this.cancel = function(pack, _onsuccess, _onerror) {
109
+ console.log('Cancellation on pack: ' + pack.id);
110
+ var _success = _createSuccessCallback($L.get('Cancellation'), $L.get("Pack '{0}' {1} successfully", pack.code, $L.get("cancelled")), _onsuccess);
111
+ var _error = _createErrorCallback(pack, $L.get('Cancellation'), _onerror);
112
+ packResource.cancel({id: pack.id}, _success, _error);
113
+ }
114
+ this.delete = function(pack, _onsuccess, _onerror) {
115
+ console.log('Delete on pack: ' + pack.id);
116
+ var _success = _createSuccessCallback($L.get('Deletion'), $L.get("Pack '{0}' {1} successfully", pack.code, $L.get("deleted")), _onsuccess);
117
+ var _error = _createErrorCallback(pack, $L.get('Deletion'), _onerror);
118
+ packResource.delete({packId: pack.id}, _success, _error);
119
+ }
120
+
121
+ }]);
122
+
123
+ app.service('Licenses', ['$L', '$resource', 'toaster', function($L, $resource, toaster) {
124
+ var LIC_STATUS = {
125
+ CREATED: 'CR',
126
+ ACTIVE: 'AC',
127
+ REQUESTED: 'RE',
128
+ PREACTIVE: 'PA',
129
+ EXPIRED: 'EX',
130
+ CANCELLED: 'CA'
131
+ }
132
+
133
+ var LIC_STATUSES = {
134
+ 'CR': $L.get('Created'),
135
+ 'AC': $L.get('Active'),
136
+ 'PA': $L.get('Pre-active'),
137
+ 'RE': $L.get('Requested'),
138
+ 'EX': $L.get('Expired'),
139
+ 'CA': $L.get('Cancelled')
140
+ };
141
+
142
+ /**
143
+ * These transitions could be get from server, class License.Status, but we copy them for simplicity, this info won't change easily
144
+ */
145
+ var LIC_ACTIONS_BY_STATUS = {
146
+ activate: [LIC_STATUS.CREATED, LIC_STATUS.REQUESTED, LIC_STATUS.PREACTIVE],
147
+ send: [LIC_STATUS.ACTIVE, LIC_STATUS.PREACTIVE],
148
+ download: [LIC_STATUS.ACTIVE, LIC_STATUS.PREACTIVE],
149
+ cancel: [LIC_STATUS.REQUESTED, LIC_STATUS.EXPIRED, LIC_STATUS.PREACTIVE, LIC_STATUS.ACTIVE],
150
+ 'delete': [LIC_STATUS.CREATED, LIC_STATUS.CANCELLED]
151
+ }
152
+
153
+ this.isActionAvailable = function(action, lic) {
154
+ var validStatuses = LIC_ACTIONS_BY_STATUS[action];
155
+ return lic && validStatuses && validStatuses.indexOf(lic.status) !== -1;
156
+ }
157
+
158
+ this.getStatusName = function(status) {
159
+ return LIC_STATUSES[status];
160
+ }
161
+
162
+ }]);
30163
31164 app.directive('fileLoader',
32165 function($timeout, $parse) {
....@@ -98,28 +231,13 @@
98231 '$resource',
99232 'toaster',
100233 'Catalogs',
234
+ 'Packs',
101235 '$store',
102236 '$L',
103
- function($scope, $http, $resource, toaster, Catalogs, $store, $L) {
104
- var packResource = $resource('/pack/:packId/:action',
105
- {
106
- packId : '@id',
107
- action : '@action'
108
- },
109
- {
110
- activate: {
111
- method: "POST",
112
- params: {action: "activate"}
113
- }
114
- }
115
- );
116
- var PACK_STATUS = [
117
- {id: 'CR', label: $L.get('Created')},
118
- {id: 'AC', label: $L.get('Active')},
119
- {id: 'OH', label: $L.get('On Hold')},
120
- {id: 'EX', label: $L.get('Expired')},
121
- {id: 'CA', label: $L.get('Cancelled')}
122
- ];
237
+ function($scope, $http, $resource, toaster, Catalogs, Packs, $store, $L) {
238
+ $scope.Packs = Packs;
239
+
240
+
123241 $scope.mandatory = {
124242 code: true,
125243 num_licenses: true,
....@@ -138,7 +256,6 @@
138256 var refFields = [{resource: 'organization', name: 'organization_id'},{resource: 'licensetype', name: 'license_type_id'}];
139257 Catalogs.loadRefs(function(refs) {
140258 $scope.refs = refs;
141
- $scope.refs['pack_status'] = PACK_STATUS;
142259 }, refFields);
143260 });
144261
....@@ -149,47 +266,39 @@
149266 // pack is the edited pack, in creation contains the data for the new pack
150267 $scope.pack = null;
151268
152
- $scope.packs = packResource.query();
269
+ $scope.packs = Packs.getPacksList();
153270
154
- var _savePackData = function() {
155
- var _success = function() {
156
- if (!$scope.isNew) $scope.showForm = false;
157
- $scope.packs = packResource.query();
158
- toaster.pop('success', Catalogs.getName(), $L.get("Pack '{0}' {1} successfully", $scope.pack.code, $scope.isNew ? $L.get("created") : $L.get("updated")));
159
- }
160
- var _error = function(error) {
161
- console.log(error);
162
- toaster.pop('error', Catalogs.getName(), $L.get("Error {0} pack '{1}'. Reason: {2}", $scope.isNew ? $L.get("creating") : $L.get("updating"), $scope.pack.code, $L.get(error.headers('X-SECURIS-ERROR'))));
163
- }
164
- packResource.save($scope.pack, _success, _error);
165
- }
166271 $scope.save = function() {
167272 if ($scope.pack.num_activations > 0) {
168273 BootstrapDialog.confirm($L.get("The pack '{0}' has active licenses, Do you want to modify it ?", $scope.pack.code), function(answer){
169274 if (answer) {
170
- _savePackData();
275
+ Packs.savePackData($scope.pack, $scope.isNew, function() {
276
+ if (!$scope.isNew) $scope.showForm = false;
277
+ $scope.packs = Packs.getPacksList();
278
+ });
171279 }
172280 });
173281 } else {
174
- _savePackData();
282
+ Packs.savePackData($scope.pack, $scope.isNew, function() {
283
+ if (!$scope.isNew) {
284
+ $scope.showForm = false;
285
+ } else {
286
+ $scope.newPack();
287
+ }
288
+ $scope.packs = Packs.getPacksList();
289
+ });
175290 }
176291 }
177292
178293 /**
179294 * Execute an action over the pack, activation, onhold, cancellation
180295 */
181
- $scope.execute = function(action) {
182
- console.log('Action: '+ action +' on pack: ' + $scope.pack.id);
183
- var _success = function() {
296
+ $scope.execute = function(action, pack) {
297
+
298
+ Packs[action](pack || $scope.pack, function() {
184299 if (!$scope.isNew) $scope.showForm = false;
185
- $scope.packs = packResource.query();
186
- toaster.pop('success', Catalogs.getName(), $L.get("Pack '{0}' {1} successfully", $scope.pack.code, $L.get("activated")));
187
- }
188
- var _error = function(error) {
189
- console.log(error);
190
- toaster.pop('error', Catalogs.getName(), $L.get("Error {0} pack '{1}'. Reason: {2}", $L.get("activating"), $scope.pack.code, $L.get(error.headers('X-SECURIS-ERROR'))));
191
- }
192
- packResource.activate({id: $scope.pack.id}, _success, _error);
300
+ $scope.packs = Packs.getPacksList();
301
+ });
193302 }
194303
195304
....@@ -222,7 +331,7 @@
222331
223332 //$scope.pack.organization_name = $scope.getLabelFromId('organization_id', $scope.pack.organization_id);
224333 $scope.pack.license_type_name = $scope.getLabelFromId('license_type_id', $scope.pack.license_type_id);
225
- $scope.pack.status_name = $scope.getLabelFromId('pack_status', $scope.pack.status);
334
+ $scope.pack.status_name = Packs.getStatusName($scope.pack.status);
226335
227336 setTimeout(function() {
228337 $('#code').focus();
....@@ -302,9 +411,11 @@
302411 '$http',
303412 '$resource',
304413 'toaster',
414
+ 'Licenses',
305415 '$store',
306416 '$L',
307
- function($scope, $http, $resource, toaster, $store, $L) {
417
+ function($scope, $http, $resource, toaster, Licenses, $store, $L) {
418
+ $scope.Licenses = Licenses;
308419 $scope.$on('pack_changed', function(evt, message) {
309420 $scope.licenses = licenseResource.query({packId: $scope.currentPack.id});
310421 $scope.creationAvailable = $scope.currentPack.status == 'AC';
....@@ -430,6 +541,8 @@
430541 $scope.isNew = false;
431542 $scope.showForm = true;
432543 $scope.license = selectedlicense;
544
+ $scope.license.status_name = Licenses.getStatusName($scope.license.status);
545
+
433546 setTimeout(function() {
434547 $('#licenseForm * #code').focus();
435548 }, 0);
....@@ -463,10 +576,6 @@
463576 }
464577 $scope.showStatusLong = function(license) {
465578
466
- }
467
- $scope.isActionVisible = function(action, lic) {
468
- var validStatuses = ACTIONS_BY_STATUS[action];
469
- return lic && validStatuses && validStatuses.indexOf(lic.status) !== -1;
470579 }
471580
472581 } ]);