rsanchez
2014-10-30 e2aa9b0177cdfeb9e3f26c511f541c37a03134aa
securis/src/main/resources/static/js/licenses.js
....@@ -28,7 +28,8 @@
2828 'CA': $L.get('Cancelled')
2929 };
3030 /**
31
- * These transitions could be get from server, class Pack.Status, but we copy them for simplicity, this info won't change easily
31
+ * These transitions could be get from server, class Pack.Status, but we
32
+ * copy them for simplicity, this info won't change easily
3233 */
3334 var PACK_ACTIONS_BY_STATUS = {
3435 activate: [PACK_STATUS.CREATED, PACK_STATUS.EXPIRED, PACK_STATUS.ONHOLD],
....@@ -57,6 +58,21 @@
5758 }
5859 }
5960 );
61
+ this.getStatusColor = function(status) {
62
+ var COLORS_BY_STATUS = {
63
+ 'CR': '#808080',
64
+ 'AC': '#329e5a',
65
+ 'OH': '#9047c7',
66
+ 'EX': '#ea7824',
67
+ 'CA': '#a21717'
68
+ };
69
+
70
+ return COLORS_BY_STATUS[status];
71
+ },
72
+ this.getStatusName = function(status) {
73
+ return PACK_STATUSES[status];
74
+ }
75
+
6076 this.savePackData = function(pack, isNew, _onsuccess) {
6177 var _success = function() {
6278 _onsuccess();
....@@ -64,7 +80,7 @@
6480 }
6581 var _error = function(error) {
6682 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'))));
83
+ 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-MSG'))));
6884 }
6985 packResource.save(pack, _success, _error);
7086 }
....@@ -73,10 +89,6 @@
7389 var validStatuses = PACK_ACTIONS_BY_STATUS[action];
7490 return pack && validStatuses && validStatuses.indexOf(pack.status) !== -1;
7591 }
76
- this.getStatusName = function(status) {
77
- return PACK_STATUSES[status];
78
- }
79
-
8092 var _createSuccessCallback = function(actionName, message, _innerCallback) {
8193 return function() {
8294 _innerCallback && _innerCallback();
....@@ -87,10 +99,10 @@
8799 return function(error) {
88100 console.log(error);
89101 _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'))));
102
+ toaster.pop('error', actionName, $L.get("Error on action '{0}', pack '{1}'. Reason: {2}", actionName, pack.code, $L.get(error.headers('X-SECURIS-ERROR-MSG'))));
91103 }
92104 }
93
- this.getPacksList = function(pack, _onsuccess, _onerror) {
105
+ this.getPacksList = function(_onsuccess, _onerror) {
94106 return packResource.query(_onsuccess, _onerror);
95107 }
96108 this.activate = function(pack, _onsuccess, _onerror) {
....@@ -141,24 +153,81 @@
141153 };
142154
143155 /**
144
- * These transitions could be get from server, class License.Status, but we copy them for simplicity, this info won't change easily
156
+ * These transitions could be get from server, class License.Status, but
157
+ * we copy them for simplicity, this info won't change easily
145158 */
146159 var LIC_ACTIONS_BY_STATUS = {
147160 activate: [LIC_STATUS.CREATED, LIC_STATUS.REQUESTED, LIC_STATUS.PREACTIVE],
148161 send: [LIC_STATUS.ACTIVE, LIC_STATUS.PREACTIVE],
149162 download: [LIC_STATUS.ACTIVE, LIC_STATUS.PREACTIVE],
163
+ block: [LIC_STATUS.CANCELLED],
164
+ unblock: [LIC_STATUS.CANCELLED],
150165 cancel: [LIC_STATUS.REQUESTED, LIC_STATUS.EXPIRED, LIC_STATUS.PREACTIVE, LIC_STATUS.ACTIVE],
151166 'delete': [LIC_STATUS.CREATED, LIC_STATUS.CANCELLED]
152167 }
168
+
169
+ var licenseResource = $resource('/license/:licenseId/:action', {
170
+ licenseId : '@id',
171
+ action : '@action'
172
+ },
173
+ {
174
+ activate: {
175
+ method: "POST",
176
+ params: {action: "activate"}
177
+ },
178
+ cancel: {
179
+ method: "POST",
180
+ params: {action: "cancel"}
181
+ },
182
+ download: {
183
+ method: "GET",
184
+ params: {action: "download"}
185
+ },
186
+ block: {
187
+ method: "POST",
188
+ params: {action: "block"}
189
+ },
190
+ send: {
191
+ method: "POST",
192
+ params: {action: "send"}
193
+ },
194
+ unblock: {
195
+ method: "POST",
196
+ params: {action: "unblock"}
197
+ }
198
+ });
199
+
153200
154201 this.isActionAvailable = function(action, lic) {
155202 var validStatuses = LIC_ACTIONS_BY_STATUS[action];
156203 return lic && validStatuses && validStatuses.indexOf(lic.status) !== -1;
157204 }
158
-
205
+ this.getStatusColor = function(status) {
206
+ var COLORS_BY_STATUS = {
207
+ 'CR': '#808080',
208
+ 'AC': '#329e5a',
209
+ 'RE': '#2981d4',
210
+ 'EX': '#ea7824',
211
+ 'CA': '#a21717'
212
+ };
213
+
214
+ return COLORS_BY_STATUS[status];
215
+ },
159216 this.getStatusName = function(status) {
160217 return LIC_STATUSES[status];
161218 }
219
+
220
+ this.saveLicenseData = function(license, isNew, _onsuccess) {
221
+ var _success = function() {
222
+ _onsuccess();
223
+ toaster.pop('success', 'Licenses', $L.get("License '{0}' {1} successfully", license.code, isNew ? $L.get("created") : $L.get("updated")));
224
+ }
225
+ var _error = function(error) {
226
+ console.log(error);
227
+ toaster.pop('error', 'Licenses', $L.get("Error {0} license '{1}'. Reason: {2}", isNew ? $L.get("creating") : $L.get("updating"), license.code, $L.get(error.headers('X-SECURIS-ERROR-MSG'))));
228
+ }
229
+ licenseResource.save(license, _success, _error);
230
+ }
162231
163232 var _createSuccessCallback = function(actionName, message, _innerCallback) {
164233 return function() {
....@@ -166,15 +235,16 @@
166235 toaster.pop('success', actionName, message);
167236 }
168237 }
169
- var _createErrorCallback = function(pack, actionName, _innerCallback) {
238
+ var _createErrorCallback = function(license, actionName, _innerCallback) {
170239 return function(error) {
171240 console.log(error);
172241 _innerCallback && _innerCallback();
173
- toaster.pop('error', actionName, $L.get("Error on action '{0}', pack '{1}'. Reason: {2}", actionName, pack.code, $L.get(error.headers('X-SECURIS-ERROR'))));
242
+ toaster.pop('error', actionName, $L.get("Error on action '{0}', license '{1}'. Reason: {2}", actionName, license.code, $L.get(error.headers('X-SECURIS-ERROR-MSG'))));
174243 }
175244 }
176
- this.getPacksList = function(pack, _onsuccess, _onerror) {
177
- return packResource.query(_onsuccess, _onerror);
245
+
246
+ this.getLicensesList = function(pack, _onsuccess, _onerror) {
247
+ return licenseResource.query({packId: pack.id}, _onsuccess, _onerror);
178248 }
179249 this.activate = function(license, _onsuccess, _onerror) {
180250 console.log('Activation on license: ' + license.id);
....@@ -218,14 +288,16 @@
218288 console.log('scope.license: ' + scope.$parent.license);
219289 var setter = $parse(attrs.fileLoader).assign;
220290 element.bind('change', function(evt) {
221
- if (!window.FileReader) { // Browser is not compatible
291
+ if (!window.FileReader) { // Browser is not
292
+ // compatible
222293 BootstrapDialog.alert($L.get("Open your .req file with a text editor and copy&paste the content in the form text field?"));
223294 return;
224295 }
225296 console.log('File selected');
226
- //console.log('scope.license: ' + scope.$parent.license);
297
+ // console.log('scope.license: ' +
298
+ // scope.$parent.license);
227299 var field = $parse(attrs.fileLoader);
228
- //console.log('field: ' + field);
300
+ // console.log('field: ' + field);
229301 var fileList = evt.target.files;
230302 if (fileList != null && fileList[0]) {
231303 var reader = new FileReader();
....@@ -314,7 +386,8 @@
314386 $scope.isNew = undefined;
315387
316388 // Selected pack from listing
317
- // pack is the edited pack, in creation contains the data for the new pack
389
+ // pack is the edited pack, in creation contains the data for
390
+ // the new pack
318391 $scope.pack = null;
319392
320393 $scope.packs = Packs.getPacksList();
....@@ -331,7 +404,8 @@
331404 }
332405
333406 /**
334
- * Execute an action over the pack, activation, onhold, cancellation
407
+ * Execute an action over the pack, activation, onhold,
408
+ * cancellation
335409 */
336410 $scope.execute = function(action, pack) {
337411 var _execute = function(extra_data) {
....@@ -348,7 +422,7 @@
348422 }
349423 }
350424 if (action === 'delete') {
351
- BootstrapDialog.confirm($L.get("The pack '{0}' will be deleted, are you sure ?", $scope.pack.code), function(answer) {
425
+ BootstrapDialog.confirm($L.get("The pack '{0}' will be deleted, are you sure ?", pack.code), function(answer) {
352426 if (answer) {
353427 _execute();
354428 }
....@@ -361,14 +435,13 @@
361435 message: function(dialog) {
362436 var $content = $('<div></div>');
363437 var $message = $('<div></div>');
364
- var pageToLoad = dialog.getData('pageToLoad');
365
- $message.append($('<label/>').text($L.get("The pack '{0}' and all its licenses will be cancelled, this action cannot be undone", $scope.pack.code)));
438
+ $message.append($('<label/>').text($L.get("The pack '{0}' and all its licenses will be cancelled, this action cannot be undone", pack.code)));
366439 $content.append($message);
367440
368441 var $message = $('<div style="margin-top:10pt;"/>');
369442 var pageToLoad = dialog.getData('pageToLoad');
370443 $message.append($('<label style="margin-right:5pt;"/>').text($L.get("Cancellation reason:") + " "));
371
- $message.append($('<input type="text" style="width:100%;" maxlength="512" id="_cancellation_reason"/>'));
444
+ $message.append($('<input type="text" style="width:100%;" maxlength="512" id="_pack_cancellation_reason"/>'));
372445 $content.append($message);
373446 return $content;
374447 },
....@@ -385,10 +458,10 @@
385458 label: $L.get('Cancel pack'),
386459 cssClass: 'btn-primary',
387460 action: function(dialogRef){
388
- var reason = $('#_cancellation_reason').val();
461
+ var reason = $('#_pack_cancellation_reason').val();
389462 console.log('Ready to cancel pack, by reason: ' + reason);
390463 if (!reason) {
391
- $('#_cancellation_reason').focus();
464
+ $('#_pack_cancellation_reason').focus();
392465 } else {
393466 _execute({reason: reason});
394467 dialogRef.close();
....@@ -413,7 +486,11 @@
413486 init_valid_date: new Date(),
414487 default_valid_period: 30,
415488 license_type_id: null,
416
- organization_id: null //!$scope.refs.organization_id || !$scope.refs.organization_id.length ? null : $scope.refs.organization_id[0].id
489
+ organization_id: null // !$scope.refs.organization_id
490
+ // ||
491
+ // !$scope.refs.organization_id.length
492
+ // ? null :
493
+ // $scope.refs.organization_id[0].id
417494 }
418495 setTimeout(function() {
419496 $('#code').focus();
....@@ -432,7 +509,9 @@
432509
433510 $scope.pack = selectedPack;
434511
435
- //$scope.pack.organization_name = $scope.getLabelFromId('organization_id', $scope.pack.organization_id);
512
+ // $scope.pack.organization_name =
513
+ // $scope.getLabelFromId('organization_id',
514
+ // $scope.pack.organization_id);
436515 $scope.pack.license_type_name = $scope.getLabelFromId('license_type_id', $scope.pack.license_type_id);
437516 $scope.pack.status_name = Packs.getStatusName($scope.pack.status);
438517
....@@ -452,7 +531,7 @@
452531 toaster.pop('success', Catalogs.getName(), $L.get("Pack '{0}' deleted successfully", selectedPack.code));
453532 },function(error) {
454533 console.log(error);
455
- toaster.pop('error', Catalogs.getName(), $L.get("Error deleting pack, reason: {0}. Details: {1}", $L.get(HTTP_ERRORS[error.status]), error.headers('X-SECURIS-ERROR')), 10000);
534
+ toaster.pop('error', Catalogs.getName(), $L.get("Error deleting pack, reason: {0}. Details: {1}", $L.get(HTTP_ERRORS[error.status]), error.headers('X-SECURIS-ERROR-MSG')), 10000);
456535 });
457536 }
458537 });
....@@ -493,7 +572,8 @@
493572 // Called when Application ID change in current field
494573 var newLTId = $scope.pack['license_type_id'];
495574 if (newLTId) {
496
- // Only if there is a "valid" value selected we should update the metadata
575
+ // Only if there is a "valid" value selected we should
576
+ // update the metadata
497577 Catalogs.getResource('licensetype').get({licenseTypeId: newLTId}).$promise.then(function(lt) {
498578 $scope.pack.metadata = [];
499579 lt.metadata.forEach(function(md) {
....@@ -520,7 +600,7 @@
520600 function($scope, $http, $resource, toaster, Licenses, $store, $L) {
521601 $scope.Licenses = Licenses;
522602 $scope.$on('pack_changed', function(evt, message) {
523
- $scope.licenses = licenseResource.query({packId: $scope.currentPack.id});
603
+ $scope.licenses = Licenses.getLicensesList($scope.currentPack);
524604 $scope.creationAvailable = $scope.currentPack.status == 'AC';
525605 if ($scope.showForm) {
526606 if ($scope.isNew) {
....@@ -531,28 +611,6 @@
531611 }
532612 })
533613
534
- var licenseResource = $resource('/license/:licenseId', {
535
- licenseId : '@id'
536
- }, {
537
- save_w_upload: {
538
- method: "POST",
539
- transformRequest: function(data, headersGetter) {
540
- // To use an object without FormData, follow: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest?redirectlocale=en-US&redirectslug=DOM%2FXMLHttpRequest%2FUsing_XMLHttpRequest#Submitting_forms_and_uploading_files
541
- var formData = new FormData();
542
- angular.forEach(data, function(value, key) {
543
- if (key !== 'request_data')
544
- formData.append(key, value)
545
- else
546
- formData.append(key, $('input#request_data_file').get(0).files[0]);
547
- })
548
-
549
- return formData;
550
- },
551
- headers: {
552
- 'Content-Type': undefined
553
- }
554
- }
555
- });
556614 $scope.mandatory = {
557615 code: true,
558616 email: true
....@@ -564,41 +622,29 @@
564622 }
565623 $scope.refs = {};
566624
567
- // Used to create the form with the appropriate data
625
+ // Used to create the form with the
626
+ // appropriate data
568627 $scope.isNew = undefined;
569628
570629 // Selected license from listing
571
- // license is the edited license, in creation contains the data for the new license
630
+ // license is the edited license, in
631
+ // creation contains the data for
632
+ // the new license
572633 $scope.license = null;
573
- if ($scope.currentPack)
574
- $scope.licenses = licenseResource.query({packId: $scope.currentPack.id});
575
-
576
-// $(document).on('change', '#request_data_file', function(newValue, oldValue) {
577
-// console.log('File changed!!!!');
578
-// var reader = new FileReader();
579
-// reader.onload = function(data) {
580
-// console.log('LOAD complete: ' + data);
581
-// console.log('LOAD reader.result: ' + reader.result);
582
-// $('input#request_data').val(reader.result)
583
-// }
584
-// console.log('file: ' + $('input#request_data_file').get(0).files[0]);
585
-// console.log('file2: ' + $scope.request_data_file);
586
-// reader.readAsText($('input#request_data_file').get(0).files[0]);
587
-// });
588
-
589
- $scope.save = function() {
590
- //$( "form#licenseForm" ).attr( "enctype", "multipart/form-data" ).attr( "encoding", "multipart/form-data" );
591
- var _success = function() {
592
- if (!$scope.isNew) $scope.showForm = false;
593
- $scope.licenses = licenseResource.query({packId: $scope.currentPack.id});
594
- toaster.pop('success', Catalogs.getName(), $L.get("License '{0}' {1} successfully", $scope.license.code, $scope.isNew ? $L.get("created") : $L.get("updated")));
595
- }
596
- var _error = function(error) {
597
- console.log(error);
598
- toaster.pop('error', Catalogs.getName(), $L.get("Error {0} license '{1}'. Reason: {2}", $scope.isNew ? $L.get("creating") : $L.get("updating"), $scope.pack.code, $L.get(error.headers('X-SECURIS-ERROR'))));
599
- }
600
- licenseResource.save($scope.license, _success, _error)
634
+ if ($scope.currentPack) {
635
+ $scope.licenses = Licenses.getLicensesList($scope.currentPack);
601636 }
637
+
638
+ $scope.save = function() {
639
+ Licenses.saveLicenseData($scope.license, $scope.isNew, function() {
640
+ if (!$scope.isNew) {
641
+ $scope.showForm = false;
642
+ } else {
643
+ $scope.newLicense();
644
+ }
645
+ $scope.licenses = Licenses.getLicensesList($scope.currentPack);
646
+ });
647
+ }
602648
603649 $scope.newLicense = function() {
604650 if (!$scope.currentPack) {
....@@ -658,17 +704,88 @@
658704 var promise = licenseResource.remove({}, {id: selectedlicense.id}).$promise;
659705 promise.then(function(data) {
660706 $scope.selectlicense(null);
661
- $scope.licenses = licenseResource.query({packId: $scope.currentPack.id});
707
+ $scope.licenses = Licenses.getLicensesList($scope.currentPack);
662708 toaster.pop('success', Catalogs.getName(), $L.get("License '{0}' deleted successfully", selectedlicense.code));
663709 },function(error) {
664710 console.log(error);
665
- toaster.pop('error', Catalogs.getName(), $L.get("Error deleting license, reason: {0}. Details: {1}", $L.get(HTTP_ERRORS[error.status]), error.headers('X-SECURIS-ERROR')), 10000);
711
+ toaster.pop('error', Catalogs.getName(), $L.get("Error deleting license, reason: {0}. Details: {1}", $L.get(HTTP_ERRORS[error.status]), error.headers('X-SECURIS-ERROR-MSG')), 10000);
666712 });
667713 }
668714 });
669715 $scope.isNew = false;
670716 }
671717
718
+ $scope.execute = function(action, license) {
719
+ if (!license) {
720
+ license = $scope.license;
721
+ }
722
+ var _execute = function(extra_data) {
723
+ if (extra_data) {
724
+ Licenses[action](license, extra_data, function() {
725
+ if (!$scope.isNew) $scope.showForm = false;
726
+ $scope.licenses = Licenses.getLicensesList($scope.currentPack);
727
+ });
728
+ } else {
729
+ Licenses[action](license, function() {
730
+ if (!$scope.isNew) $scope.showForm = false;
731
+ $scope.licenses = Licenses.getLicensesList($scope.currentPack);
732
+ });
733
+ }
734
+ }
735
+ if (action === 'delete') {
736
+ BootstrapDialog.confirm($L.get("The license '{0}' will be deleted, are you sure?", license.code), function(result){
737
+ if(result) {
738
+ _execute();
739
+ }
740
+ });
741
+ } else {
742
+ if (action === 'cancel') {
743
+ BootstrapDialog.show({
744
+ title: $L.get("License cancellation"),
745
+ type: BootstrapDialog.TYPE_DANGER,
746
+ message: function(dialog) {
747
+ var $content = $('<div></div>');
748
+ var $message = $('<div></div>');
749
+ var pageToLoad = dialog.getData('pageToLoad');
750
+ $message.append($('<label/>').text($L.get("This action cannot be undone.", $scope.pack.code)));
751
+ $content.append($message);
752
+
753
+ var $message = $('<div style="margin-top:10pt;"/>');
754
+ $message.append($('<label style="margin-right:5pt;"/>').text($L.get("Cancellation reason:") + " "));
755
+ $message.append($('<input type="text" style="width:100%;" maxlength="512" id="_lic_cancellation_reason"/>'));
756
+ $content.append($message);
757
+ return $content;
758
+ },
759
+ closable: true,
760
+ buttons: [{
761
+ id: 'btn-cancel',
762
+ label: $L.get('Close'),
763
+ cssClass: 'btn-default',
764
+ action: function(dialogRef) {
765
+ dialogRef.close();
766
+ }
767
+ }, {
768
+ id: 'btn-ok',
769
+ label: $L.get('Cancel license'),
770
+ cssClass: 'btn-primary',
771
+ action: function(dialogRef){
772
+ var reason = $('#_lic_cancellation_reason').val();
773
+ console.log('Ready to cancel license, by reason: ' + reason);
774
+ if (!reason) {
775
+ $('#_lic_cancellation_reason').focus();
776
+ } else {
777
+ _execute({reason: reason});
778
+ dialogRef.close();
779
+ }
780
+ }
781
+ }]
782
+ });
783
+ } else {
784
+ _execute();
785
+ }
786
+ }
787
+ }
788
+
672789
673790 $scope.cancel = function() {
674791 $scope.showForm = false;