From e2aa9b0177cdfeb9e3f26c511f541c37a03134aa Mon Sep 17 00:00:00 2001
From: rsanchez <rsanchez@curisit.net>
Date: Thu, 30 Oct 2014 11:52:50 +0000
Subject: [PATCH] #2021 feature - Added menu color based on element status in packs and licenses listings

---
 securis/src/main/resources/static/js/licenses.js |  285 ++++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 201 insertions(+), 84 deletions(-)

diff --git a/securis/src/main/resources/static/js/licenses.js b/securis/src/main/resources/static/js/licenses.js
index 2e250fa..71b5014 100644
--- a/securis/src/main/resources/static/js/licenses.js
+++ b/securis/src/main/resources/static/js/licenses.js
@@ -28,7 +28,8 @@
 	            '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
+		 * 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],
@@ -57,6 +58,21 @@
                     }
                 }
 			);
+		this.getStatusColor = function(status) {
+			var COLORS_BY_STATUS = {
+					'CR': '#808080',
+		            'AC': '#329e5a',
+		            'OH': '#9047c7',
+		            'EX': '#ea7824',
+		            'CA': '#a21717'
+		            };
+			
+			return COLORS_BY_STATUS[status];
+		},
+		this.getStatusName = function(status) {
+			return PACK_STATUSES[status];
+		}
+
 		this.savePackData = function(pack, isNew, _onsuccess) {
 			var _success = function() {
 				_onsuccess();
@@ -64,7 +80,7 @@
 			}
 			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'))));
+				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'))));
 			}
 			packResource.save(pack, _success, _error);
 		} 
@@ -73,10 +89,6 @@
             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();
@@ -87,10 +99,10 @@
 			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'))));
+				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'))));
 			}
 		}
-		this.getPacksList = function(pack, _onsuccess, _onerror) {
+		this.getPacksList = function(_onsuccess, _onerror) {
 			return packResource.query(_onsuccess, _onerror);
 		}
 		this.activate = function(pack, _onsuccess, _onerror) {
@@ -141,24 +153,81 @@
 	            };
 
 		/**
-		 * These transitions could be get from server, class License.Status, but we copy them for simplicity, this info won't change easily
+		 * 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],
+	            block: [LIC_STATUS.CANCELLED],
+	            unblock: [LIC_STATUS.CANCELLED],
 	            cancel: [LIC_STATUS.REQUESTED, LIC_STATUS.EXPIRED, LIC_STATUS.PREACTIVE, LIC_STATUS.ACTIVE],
 	            'delete': [LIC_STATUS.CREATED, LIC_STATUS.CANCELLED]
 	    }
+		
+        var licenseResource = $resource('/license/:licenseId/:action', {
+            licenseId : '@id',
+			action : '@action'
+		},
+		{
+            activate: {
+                method: "POST",
+                params: {action: "activate"}
+            },
+            cancel: {
+                method: "POST",
+                params: {action: "cancel"}
+            },
+            download: {
+                method: "GET",
+                params: {action: "download"}
+            },
+            block: {
+                method: "POST",
+                params: {action: "block"}
+            },
+            send: {
+                method: "POST",
+                params: {action: "send"}
+            },
+            unblock: {
+                method: "POST",
+                params: {action: "unblock"}
+            }
+        });
+
 
         this.isActionAvailable = function(action, lic) {
             var validStatuses = LIC_ACTIONS_BY_STATUS[action];
             return lic && validStatuses && validStatuses.indexOf(lic.status) !== -1;
         }
-
+        this.getStatusColor = function(status) {
+			var COLORS_BY_STATUS = {
+					'CR': '#808080',
+		            'AC': '#329e5a',
+		            'RE': '#2981d4',
+		            'EX': '#ea7824',
+		            'CA': '#a21717'
+		            };
+			
+			return COLORS_BY_STATUS[status];
+		},
 		this.getStatusName = function(status) {
 			return LIC_STATUSES[status];
 		}
+
+		this.saveLicenseData = function(license, isNew, _onsuccess) {
+			var _success = function() {
+				_onsuccess();
+				toaster.pop('success', 'Licenses', $L.get("License '{0}' {1} successfully", license.code, isNew ? $L.get("created") : $L.get("updated")));
+			}
+			var _error =  function(error) {
+                console.log(error);
+				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'))));
+			}
+			licenseResource.save(license, _success, _error);
+		} 
 
 		var _createSuccessCallback = function(actionName, message, _innerCallback) {
 			return function() {
@@ -166,15 +235,16 @@
 				toaster.pop('success', actionName, message);
 			}
 		}
-		var _createErrorCallback = function(pack, actionName, _innerCallback) {
+		var _createErrorCallback = function(license, 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'))));
+				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'))));
 			}
 		}
-		this.getPacksList = function(pack, _onsuccess, _onerror) {
-			return packResource.query(_onsuccess, _onerror);
+		
+		this.getLicensesList = function(pack, _onsuccess, _onerror) {
+			return licenseResource.query({packId: pack.id}, _onsuccess, _onerror);
 		}
 		this.activate = function(license, _onsuccess, _onerror) {
 			console.log('Activation on license: ' + license.id);
@@ -218,14 +288,16 @@
                             console.log('scope.license: ' + scope.$parent.license);
                             var setter = $parse(attrs.fileLoader).assign;
 	                        element.bind('change', function(evt) {
-	                            if (!window.FileReader) { // Browser is not compatible
+	                            if (!window.FileReader) { // Browser is not
+															// compatible
 	                                BootstrapDialog.alert($L.get("Open your .req file with a text editor and copy&paste the content in the form text field?"));
 	                                return;
 	                            }
 	                            console.log('File selected');
-	                            //console.log('scope.license: ' + scope.$parent.license);
+	                            // console.log('scope.license: ' +
+								// scope.$parent.license);
 	                            var field = $parse(attrs.fileLoader);
-	                            //console.log('field: ' + field);
+	                            // console.log('field: ' + field);
 	                            var fileList = evt.target.files;
 	                            if (fileList != null && fileList[0]) {
 	                                var reader = new FileReader();
@@ -314,7 +386,8 @@
 				$scope.isNew = undefined;
 
 				// Selected pack from listing
-				// pack is the edited pack, in creation contains the data for the new pack
+				// pack is the edited pack, in creation contains the data for
+				// the new pack
 				$scope.pack = null;
 
 				$scope.packs = Packs.getPacksList();
@@ -331,7 +404,8 @@
 				}
 
 				/**
-				 * Execute an action over the pack, activation, onhold, cancellation
+				 * Execute an action over the pack, activation, onhold,
+				 * cancellation
 				 */
 				$scope.execute = function(action, pack) {
 					var _execute = function(extra_data) {
@@ -348,7 +422,7 @@
 						}
 					}
 					if (action === 'delete') {
-						BootstrapDialog.confirm($L.get("The pack '{0}' will be deleted, are you sure ?", $scope.pack.code), function(answer) {
+						BootstrapDialog.confirm($L.get("The pack '{0}' will be deleted, are you sure ?", pack.code), function(answer) {
 	                        if (answer) {
 	                        	_execute();
 	                        }
@@ -361,14 +435,13 @@
 								message: function(dialog) {
 									var $content = $('<div></div>');
 									var $message = $('<div></div>');
-					                var pageToLoad = dialog.getData('pageToLoad');
-					                $message.append($('<label/>').text($L.get("The pack '{0}' and all its licenses will be cancelled, this action cannot be undone", $scope.pack.code)));
+					                $message.append($('<label/>').text($L.get("The pack '{0}' and all its licenses will be cancelled, this action cannot be undone", pack.code)));
 					                $content.append($message);
 
 									var $message = $('<div style="margin-top:10pt;"/>');
 					                var pageToLoad = dialog.getData('pageToLoad');
 					                $message.append($('<label style="margin-right:5pt;"/>').text($L.get("Cancellation reason:") + " "));
-					                $message.append($('<input type="text" style="width:100%;" maxlength="512" id="_cancellation_reason"/>'));
+					                $message.append($('<input type="text" style="width:100%;" maxlength="512" id="_pack_cancellation_reason"/>'));
 					                $content.append($message);
 					            	return $content;								 
 								},
@@ -385,10 +458,10 @@
 							        label: $L.get('Cancel pack'),
 							        cssClass: 'btn-primary', 
 							        action: function(dialogRef){
-							        	var reason = $('#_cancellation_reason').val();
+							        	var reason = $('#_pack_cancellation_reason').val();
 							        	console.log('Ready to cancel pack, by reason: ' + reason);
 							            if (!reason) {
-							            	$('#_cancellation_reason').focus();
+							            	$('#_pack_cancellation_reason').focus();
 							            } else {
 							            	_execute({reason: reason});
 								            dialogRef.close();
@@ -413,7 +486,11 @@
                             init_valid_date: new Date(),
                             default_valid_period: 30,
                             license_type_id: null,
-                            organization_id: null  //!$scope.refs.organization_id || !$scope.refs.organization_id.length ? null : $scope.refs.organization_id[0].id
+                            organization_id: null  // !$scope.refs.organization_id
+													// ||
+													// !$scope.refs.organization_id.length
+													// ? null :
+													// $scope.refs.organization_id[0].id
 					}
                    setTimeout(function() {
                         $('#code').focus();
@@ -432,7 +509,9 @@
                     
                     $scope.pack = selectedPack;
 
-                    //$scope.pack.organization_name = $scope.getLabelFromId('organization_id', $scope.pack.organization_id); 
+                    // $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 = Packs.getStatusName($scope.pack.status); 
                     
@@ -452,7 +531,7 @@
                                 toaster.pop('success', Catalogs.getName(), $L.get("Pack '{0}' deleted successfully", selectedPack.code));
                             },function(error) {
                                 console.log(error);
-                                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);
+                                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);
                             });
                         }
                     });
@@ -493,7 +572,8 @@
 					// Called when Application ID change in current field
 					var newLTId = $scope.pack['license_type_id'];
 					if (newLTId) {
-						// Only if there is a "valid" value selected we should update the metadata
+						// Only if there is a "valid" value selected we should
+						// update the metadata
 						Catalogs.getResource('licensetype').get({licenseTypeId: newLTId}).$promise.then(function(lt) {
 							$scope.pack.metadata = [];
 							lt.metadata.forEach(function(md) {
@@ -520,7 +600,7 @@
 	                                   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.licenses = Licenses.getLicensesList($scope.currentPack);
                                                $scope.creationAvailable = $scope.currentPack.status == 'AC';
 	                                           if ($scope.showForm) {
 	                                               if ($scope.isNew) {
@@ -531,28 +611,6 @@
 	                                           }
 	                                       })
 	                                           
-	                                       var licenseResource = $resource('/license/:licenseId', {
-	                                           licenseId : '@id'
-	                                       }, {
-	                                           save_w_upload: {
-	                                               method: "POST",
-	                                               transformRequest: function(data, headersGetter) {
-	                                                   // 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
-	                                                   var formData = new FormData();
-	                                                   angular.forEach(data, function(value, key) {
-	                                                       if (key !== 'request_data')
-	                                                           formData.append(key, value)
-                                                           else
-                                                               formData.append(key, $('input#request_data_file').get(0).files[0]);
-	                                                   })
-	                                                   
-	                                                   return formData;
-	                                               },
-	                                               headers: {
-	                                                   'Content-Type': undefined
-	                                               }
-	                                           }
-	                                       });
 	                                       $scope.mandatory = {
 	                                               code: true,
 	                                               email: true
@@ -564,41 +622,29 @@
 	                                       }
 	                                       $scope.refs = {};
 
-	                                       // Used to create the form with the appropriate data
+	                                       // Used to create the form with the
+											// appropriate data
 	                                       $scope.isNew = undefined;
 
 	                                       // Selected license from listing
-	                                       // license is the edited license, in creation contains the data for the new license
+	                                       // license is the edited license, in
+											// creation contains the data for
+											// the new license
 	                                       $scope.license = null;
-	                                       if ($scope.currentPack)
-	                                           $scope.licenses = licenseResource.query({packId: $scope.currentPack.id});
-	                                       
-//	                                       $(document).on('change', '#request_data_file', function(newValue, oldValue) {
-//	                                          console.log('File changed!!!!'); 
-//	                                           var reader = new FileReader();
-//	                                           reader.onload = function(data) {
-//                                                   console.log('LOAD complete: ' + data);
-//                                                   console.log('LOAD reader.result: ' + reader.result);
-//                                                   $('input#request_data').val(reader.result)
-//	                                           }
-//                                               console.log('file: ' + $('input#request_data_file').get(0).files[0]);
-//                                               console.log('file2: ' + $scope.request_data_file);
-//	                                           reader.readAsText($('input#request_data_file').get(0).files[0]);	                                            
-//	                                       });
-	                                       
-	                                       $scope.save = function() {
-	                                           //$( "form#licenseForm" ).attr( "enctype", "multipart/form-data" ).attr( "encoding", "multipart/form-data" );
-	                                           var _success = function() {
-	                                               if (!$scope.isNew) $scope.showForm = false;
-	                                               $scope.licenses = licenseResource.query({packId: $scope.currentPack.id});
-		                       						toaster.pop('success', Catalogs.getName(), $L.get("License '{0}' {1} successfully", $scope.license.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} license '{1}'. Reason: {2}", $scope.isNew ? $L.get("creating") : $L.get("updating"), $scope.pack.code, $L.get(error.headers('X-SECURIS-ERROR'))));
-		                       					}
-	                                           licenseResource.save($scope.license, _success, _error)
+	                                       if ($scope.currentPack) {
+	                                           $scope.licenses = Licenses.getLicensesList($scope.currentPack);
 	                                       }
+	                                       
+		                       				$scope.save = function() {
+		                                    	Licenses.saveLicenseData($scope.license, $scope.isNew, function() {
+		                            			    if (!$scope.isNew) {
+		                            			    	$scope.showForm = false;
+		                            			    } else {
+		                            			    	$scope.newLicense();
+		                            			    }
+			                                        $scope.licenses = Licenses.getLicensesList($scope.currentPack);
+		                                    	});
+		                    				}
 	                                       
 	                                       $scope.newLicense = function() {
 	                                           if (!$scope.currentPack) {
@@ -658,17 +704,88 @@
 	                                                   var promise = licenseResource.remove({}, {id: selectedlicense.id}).$promise;
 	                                                   promise.then(function(data) {
 	                                                       $scope.selectlicense(null);
-	                                                       $scope.licenses = licenseResource.query({packId: $scope.currentPack.id});
+	                                                       $scope.licenses = Licenses.getLicensesList($scope.currentPack);
 	                                                       toaster.pop('success', Catalogs.getName(), $L.get("License '{0}' deleted successfully", selectedlicense.code));
 	                                                   },function(error) {
 	                                                       console.log(error);
-	                                                       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);
+	                                                       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);
 	                                                   });
 	                                               }
 	                                           });
 	                                           $scope.isNew = false;
 	                                       }
 
+	                                       $scope.execute = function(action, license) {
+	                                    	   if (!license) {
+	                                    		   license = $scope.license;
+	                                    	   }
+	                       					var _execute = function(extra_data) {
+	                       						if (extra_data) {
+	                       							Licenses[action](license, extra_data, function() {
+	                       							    if (!$scope.isNew) $scope.showForm = false;
+	                       								$scope.licenses = Licenses.getLicensesList($scope.currentPack);
+	                       							});
+	                       						} else {
+	                       							Licenses[action](license, function() {
+	                       							    if (!$scope.isNew) $scope.showForm = false;
+	                       								$scope.licenses = Licenses.getLicensesList($scope.currentPack);
+	                       							});
+	                       						}
+	                       					}
+	                       					if (action === 'delete') {
+	                       						BootstrapDialog.confirm($L.get("The license '{0}' will be deleted, are you sure?", license.code), function(result){
+		                                               if(result) {
+		                                            	   _execute();
+		                                               }
+		                                           });
+	                       					} else {
+	                       						if (action === 'cancel') {
+	                       							BootstrapDialog.show({
+	                       								title: $L.get("License cancellation"),
+	                       								type: BootstrapDialog.TYPE_DANGER,
+	                       								message: function(dialog) {
+	                       									var $content = $('<div></div>');
+	                       									var $message = $('<div></div>');
+	                       					                var pageToLoad = dialog.getData('pageToLoad');
+	                       					                $message.append($('<label/>').text($L.get("This action cannot be undone.", $scope.pack.code)));
+	                       					                $content.append($message);
+
+	                       									var $message = $('<div style="margin-top:10pt;"/>');
+	                       					                $message.append($('<label style="margin-right:5pt;"/>').text($L.get("Cancellation reason:") + " "));
+	                       					                $message.append($('<input type="text" style="width:100%;" maxlength="512" id="_lic_cancellation_reason"/>'));
+	                       					                $content.append($message);
+	                       					            	return $content;								 
+	                       								},
+	                       								closable: true,
+	                       								buttons: [{
+	                       							        id: 'btn-cancel',   
+	                       							        label: $L.get('Close'),
+	                       							        cssClass: 'btn-default', 
+	                       							        action: function(dialogRef) {    
+	                       							            dialogRef.close();
+	                       							        }
+	                       							    }, {
+	                       							        id: 'btn-ok',   
+	                       							        label: $L.get('Cancel license'),
+	                       							        cssClass: 'btn-primary', 
+	                       							        action: function(dialogRef){
+	                       							        	var reason = $('#_lic_cancellation_reason').val();
+	                       							        	console.log('Ready to cancel license, by reason: ' + reason);
+	                       							            if (!reason) {
+	                       							            	$('#_lic_cancellation_reason').focus();
+	                       							            } else {
+	                       							            	_execute({reason: reason});
+	                       								            dialogRef.close();
+	                       							            }
+	                       							        }
+	                       							    }]
+	                       							});
+	                       						} else {
+	                       							_execute();
+	                       						}
+	                       					}
+	                       				}
+	                       				
 
 	                                       $scope.cancel = function() {
 	                                           $scope.showForm = false;

--
Gitblit v1.3.2