From e5bed7ea0d65114d00b583841066b4883771cae3 Mon Sep 17 00:00:00 2001
From: rsanchez <rsanchez@curisit.net>
Date: Fri, 24 Oct 2014 12:26:36 +0000
Subject: [PATCH] #2021 feature - Added pack actions frontend management

---
 securis/src/main/resources/static/js/licenses.js |  247 +++++++++++++++++++++++++++++++++++-------------
 1 files changed, 178 insertions(+), 69 deletions(-)

diff --git a/securis/src/main/resources/static/js/licenses.js b/securis/src/main/resources/static/js/licenses.js
index a7d5761..7617858 100644
--- a/securis/src/main/resources/static/js/licenses.js
+++ b/securis/src/main/resources/static/js/licenses.js
@@ -1,22 +1,7 @@
 (function() {
 	'use strict';
 	
-	var LIC_STATUS = {
-	        CREATED: 1,
-            REQUESTED: 2,
-            PREACTIVE: 3,
-            ACTIVE: 4,
-            CANCELED: 5
-	}
-	
-	var ACTIONS_BY_STATUS = {
-            activate: [LIC_STATUS.REQUESTED, LIC_STATUS.PREACTIVE],
-            send: [LIC_STATUS.ACTIVE, LIC_STATUS.PREACTIVE],
-            download: [LIC_STATUS.ACTIVE, LIC_STATUS.PREACTIVE],
-            request: [LIC_STATUS.CREATED, LIC_STATUS.REQUESTED],
-            cancel: [LIC_STATUS.REQUESTED, LIC_STATUS.PREACTIVE, LIC_STATUS.ACTIVE],
-            'delete': [LIC_STATUS.CREATED, LIC_STATUS.CANCELED],
-    }
+
 
 	   var HTTP_ERRORS = {
 	            401: "Unathorized action",
@@ -27,6 +12,154 @@
 	    }
 
 	var app = angular.module('securis');
+	app.service('Packs', ['$L','$resource', 'toaster', function($L, $resource, toaster) {
+		var PACK_STATUS = {
+		        CREATED: 'CR',
+	            ACTIVE: 'AC',
+	            ONHOLD: 'OH',
+	            EXPIRED: 'EX',
+	            CANCELLED: 'CA'
+		}
+		var PACK_STATUSES = {
+				'CR': $L.get('Created'),
+	            'AC': $L.get('Active'),
+	            'OH': $L.get('On Hold'),
+	            'EX': $L.get('Expired'),
+	            'CA': $L.get('Cancelled')
+	            };
+		/**
+		 * These transitions could be get from server, class Pack.Status, but we copy them for simplicity, this info won't change easily
+		 */
+		var PACK_ACTIONS_BY_STATUS = {
+	            activate: [PACK_STATUS.CREATED, PACK_STATUS.EXPIRED, PACK_STATUS.ONHOLD],
+	            putonhold: [PACK_STATUS.ACTIVE],
+	            cancel: [PACK_STATUS.EXPIRED, PACK_STATUS.ONHOLD, PACK_STATUS.ACTIVE],
+	            'delete': [PACK_STATUS.CREATED, PACK_STATUS.CANCELLED]
+	    }
+		
+		var packResource = $resource('/pack/:packId/:action', 
+				{
+					packId : '@id',
+					action : '@action'
+				},
+				{
+                    activate: {
+                        method: "POST",
+                        params: {action: "activate"}
+                    },
+                    putonhold: {
+                        method: "POST",
+                        params: {action: "putonhold"}
+                    },
+                    cancel: {
+                        method: "POST",
+                        params: {action: "cancel"}
+                    }
+                }
+			);
+		this.savePackData = function(pack, isNew, _onsuccess) {
+			var _success = function() {
+				_onsuccess();
+				toaster.pop('success', 'Packs', $L.get("Pack '{0}' {1} successfully", pack.code, isNew ? $L.get("created") : $L.get("updated")));
+			}
+			var _error =  function(error) {
+                console.log(error);
+				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'))));
+			}
+			packResource.save(pack, _success, _error);
+		} 
+
+		this.isActionAvailable = function(action, pack) {
+            var validStatuses = PACK_ACTIONS_BY_STATUS[action];
+            return pack && validStatuses && validStatuses.indexOf(pack.status) !== -1;
+		}
+		this.getStatusName = function(status) {
+			return PACK_STATUSES[status];
+		}
+		
+		var _createSuccessCallback = function(actionName, message, _innerCallback) {
+			return function() {
+				_innerCallback && _innerCallback();
+				toaster.pop('success', actionName, message);
+			}
+		}
+		var _createErrorCallback = function(pack, actionName, _innerCallback) {
+			return function(error) {
+	            console.log(error);
+	            _innerCallback && _innerCallback();
+				toaster.pop('error', actionName, $L.get("Error on action '{0}', pack '{1}'. Reason: {2}", actionName, pack.code, $L.get(error.headers('X-SECURIS-ERROR'))));
+			}
+		}
+		this.getPacksList = function(pack, _onsuccess, _onerror) {
+			return packResource.query(_onsuccess, _onerror);
+		}
+		this.activate = function(pack, _onsuccess, _onerror) {
+			console.log('Activation on pack: ' + pack.id);
+			var _success = _createSuccessCallback($L.get('Activation'), $L.get("Pack '{0}' {1} successfully", pack.code, $L.get("activated")), _onsuccess);
+			var _error = _createErrorCallback(pack, $L.get('Activation'), _onerror);
+			packResource.activate({id: pack.id}, _success, _error);
+		}
+		this.putonhold = function(pack, _onsuccess, _onerror) {
+			console.log('Put on hold on pack: ' + pack.id);
+			var _success = _createSuccessCallback($L.get('Put on hold'), $L.get("Pack '{0}' {1} successfully", pack.code, $L.get("put on hold")), _onsuccess);
+			var _error = _createErrorCallback(pack, $L.get('Put on hold'), _onerror);
+			packResource.putonhold({id: pack.id}, _success, _error);
+		}
+		this.cancel = function(pack, _onsuccess, _onerror) {
+			console.log('Cancellation on pack: ' + pack.id);
+			var _success = _createSuccessCallback($L.get('Cancellation'), $L.get("Pack '{0}' {1} successfully", pack.code, $L.get("cancelled")), _onsuccess);
+			var _error = _createErrorCallback(pack, $L.get('Cancellation'), _onerror);
+			packResource.cancel({id: pack.id}, _success, _error);
+		}
+		this.delete = function(pack, _onsuccess, _onerror) {
+			console.log('Delete on pack: ' + pack.id);
+			var _success = _createSuccessCallback($L.get('Deletion'), $L.get("Pack '{0}' {1} successfully", pack.code, $L.get("deleted")), _onsuccess);
+			var _error = _createErrorCallback(pack, $L.get('Deletion'), _onerror);
+			packResource.delete({packId: pack.id}, _success, _error);
+		}
+
+	}]);
+	
+	app.service('Licenses', ['$L', '$resource', 'toaster', function($L, $resource, toaster) {
+		var LIC_STATUS = {
+		        CREATED: 'CR',
+	            ACTIVE: 'AC',
+	            REQUESTED: 'RE',
+	            PREACTIVE: 'PA',
+	            EXPIRED: 'EX',
+	            CANCELLED: 'CA'
+		}
+		
+		var LIC_STATUSES = {
+				'CR': $L.get('Created'),
+	            'AC': $L.get('Active'),
+	            'PA': $L.get('Pre-active'),
+	            'RE': $L.get('Requested'),
+	            'EX': $L.get('Expired'),
+	            'CA': $L.get('Cancelled')
+	            };
+
+		/**
+		 * These transitions could be get from server, class License.Status, but we copy them for simplicity, this info won't change easily
+		 */
+		var LIC_ACTIONS_BY_STATUS = {
+	            activate: [LIC_STATUS.CREATED, LIC_STATUS.REQUESTED, LIC_STATUS.PREACTIVE],
+	            send: [LIC_STATUS.ACTIVE, LIC_STATUS.PREACTIVE],
+	            download: [LIC_STATUS.ACTIVE, LIC_STATUS.PREACTIVE],
+	            cancel: [LIC_STATUS.REQUESTED, LIC_STATUS.EXPIRED, LIC_STATUS.PREACTIVE, LIC_STATUS.ACTIVE],
+	            'delete': [LIC_STATUS.CREATED, LIC_STATUS.CANCELLED]
+	    }
+
+        this.isActionAvailable = function(action, lic) {
+            var validStatuses = LIC_ACTIONS_BY_STATUS[action];
+            return lic && validStatuses && validStatuses.indexOf(lic.status) !== -1;
+        }
+
+		this.getStatusName = function(status) {
+			return LIC_STATUSES[status];
+		}
+
+	}]);
 	
 	app.directive('fileLoader',
 	            function($timeout, $parse) {
@@ -98,28 +231,13 @@
 			'$resource',
 			'toaster',
 			'Catalogs',
+			'Packs',
 			'$store',
 			'$L',
-			function($scope, $http, $resource, toaster, Catalogs, $store, $L) {
-				var packResource = $resource('/pack/:packId/:action', 
-					{
-						packId : '@id',
-						action : '@action'
-					},
-					{
-                        activate: {
-                            method: "POST",
-                            params: {action: "activate"}
-                        }
-                    }
-				);
-				var PACK_STATUS = [
-				                   {id: 'CR', label: $L.get('Created')},
-				                   {id: 'AC', label: $L.get('Active')},
-				                   {id: 'OH', label: $L.get('On Hold')},
-				                   {id: 'EX', label: $L.get('Expired')},
-				                   {id: 'CA', label: $L.get('Cancelled')}
-				                   ];
+			function($scope, $http, $resource, toaster, Catalogs, Packs, $store, $L) {
+         	   $scope.Packs = Packs;
+				
+				
 				$scope.mandatory = {
 						code: true,
 						num_licenses: true,
@@ -138,7 +256,6 @@
                 	var refFields = [{resource: 'organization', name: 'organization_id'},{resource: 'licensetype', name: 'license_type_id'}]; 
                     Catalogs.loadRefs(function(refs) {
 	                    	$scope.refs = refs;
-	                    	$scope.refs['pack_status'] = PACK_STATUS;
 	                    }, refFields);
                 }); 
 
@@ -149,47 +266,39 @@
 				// pack is the edited pack, in creation contains the data for the new pack
 				$scope.pack = null;
 
-				$scope.packs = packResource.query();
+				$scope.packs = Packs.getPacksList();
 				
-				var _savePackData = function() {
-					var _success = function() {
-					    if (!$scope.isNew) $scope.showForm = false;
-						$scope.packs = packResource.query();
-						toaster.pop('success', Catalogs.getName(), $L.get("Pack '{0}' {1} successfully", $scope.pack.code, $scope.isNew ? $L.get("created") : $L.get("updated")));
-					}
-					var _error =  function(error) {
-                        console.log(error);
-						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'))));
-					}
-					packResource.save($scope.pack, _success, _error);
-				} 
 				$scope.save = function() {
 					if ($scope.pack.num_activations > 0) {
 						BootstrapDialog.confirm($L.get("The pack '{0}' has active licenses, Do you want to modify it ?", $scope.pack.code), function(answer){
 	                        if (answer) {
-	                        	_savePackData();
+	                        	Packs.savePackData($scope.pack, $scope.isNew, function() {
+	                			    if (!$scope.isNew) $scope.showForm = false;
+	                				$scope.packs = Packs.getPacksList();
+	                        	});
 	                        }
 	                    });
 					} else {
-				    	_savePackData();
+                    	Packs.savePackData($scope.pack, $scope.isNew, function() {
+            			    if (!$scope.isNew) {
+            			    	$scope.showForm = false;
+            			    } else {
+            			    	$scope.newPack();
+            			    }
+            				$scope.packs = Packs.getPacksList();
+                    	});
 				    }
 				}
 
 				/**
 				 * Execute an action over the pack, activation, onhold, cancellation
 				 */
-				$scope.execute = function(action) {
-					console.log('Action: '+ action +' on pack: ' + $scope.pack.id);
-					var _success = function() {
+				$scope.execute = function(action, pack) {
+					
+					Packs[action](pack || $scope.pack, function() {
 					    if (!$scope.isNew) $scope.showForm = false;
-						$scope.packs = packResource.query();
-						toaster.pop('success', Catalogs.getName(), $L.get("Pack '{0}' {1} successfully", $scope.pack.code, $L.get("activated")));
-					}
-					var _error =  function(error) {
-                        console.log(error);
-						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'))));
-					}
-					packResource.activate({id: $scope.pack.id}, _success, _error);
+						$scope.packs = Packs.getPacksList();
+					});
 				}
 				
 
@@ -222,7 +331,7 @@
 
                     //$scope.pack.organization_name = $scope.getLabelFromId('organization_id', $scope.pack.organization_id); 
                     $scope.pack.license_type_name = $scope.getLabelFromId('license_type_id', $scope.pack.license_type_id); 
-                    $scope.pack.status_name = $scope.getLabelFromId('pack_status', $scope.pack.status); 
+                    $scope.pack.status_name = Packs.getStatusName($scope.pack.status); 
                     
                     setTimeout(function() {
                         $('#code').focus();
@@ -302,9 +411,11 @@
 	                                   '$http',
 	                                   '$resource',
 	                                   'toaster',
+	                       			   'Licenses',
 	                                   '$store',
 	                                   '$L',
-	                                   function($scope, $http, $resource, toaster, $store, $L) {
+	                                   function($scope, $http, $resource, toaster, Licenses, $store, $L) {
+	                                	   $scope.Licenses = Licenses;
 	                                       $scope.$on('pack_changed', function(evt, message) {
                                                $scope.licenses = licenseResource.query({packId: $scope.currentPack.id});
                                                $scope.creationAvailable = $scope.currentPack.status == 'AC';
@@ -430,6 +541,8 @@
 	                                           $scope.isNew = false;
 	                                           $scope.showForm = true;
 	                                           $scope.license = selectedlicense;
+	                                           $scope.license.status_name = Licenses.getStatusName($scope.license.status); 
+
 	                                          setTimeout(function() {
 	                                               $('#licenseForm * #code').focus();
 	                                           }, 0);
@@ -463,10 +576,6 @@
                                            }
                                            $scope.showStatusLong = function(license) {
                                                
-                                           }
-                                           $scope.isActionVisible = function(action, lic) {
-                                               var validStatuses = ACTIONS_BY_STATUS[action];
-                                               return lic && validStatuses && validStatuses.indexOf(lic.status) !== -1;
                                            }
 	                                       
 	                                   } ]);

--
Gitblit v1.3.2