From 79121484b7e6f721f5435a102018152a164ed655 Mon Sep 17 00:00:00 2001
From: Roberto Sánchez <roberto.sanchez@curisit.net>
Date: Wed, 22 Jan 2014 18:55:29 +0000
Subject: [PATCH] #395 feature - Implemented pack section

---
 securis/src/main/resources/static/js/licenses.js |  214 +++++++++++++++++++++++++++++++++++------------------
 1 files changed, 141 insertions(+), 73 deletions(-)

diff --git a/securis/src/main/resources/static/js/licenses.js b/securis/src/main/resources/static/js/licenses.js
index 3db4b48..73648ee 100644
--- a/securis/src/main/resources/static/js/licenses.js
+++ b/securis/src/main/resources/static/js/licenses.js
@@ -1,92 +1,160 @@
 (function() {
 	'use strict';
 
+	   var HTTP_ERRORS = {
+	            401: "Unathorized action",
+	            403: "Forbidden action",
+	            500: "Server error",
+	            404: "Element not found"
+	    }
+
 	var app = angular.module('securis');
+
+	app.controller('PackAndLicensesCtrl', [
+	                    			'$scope',
+	                    			'$http',
+	                    			'toaster',
+	                    			'$store',
+	                    			'$L',
+	   			function($scope, $http, toaster, $store, $L) {
+	                    				$scope.licenses = [
+	                    					                {id: 1,
+	                    					                	"code": "BP-SA-001-AKSJMS234",
+	                    					                	"user_fullname": "Johnny Belmonte",
+	                    					                	"user_email": "jb@curisit.net",
+	                    					                	"status": 3},
+	                    						                {id: 2,
+	                    						                	"code": "BP-SA-001-KAJSDHAJS",
+	                    						                	"user_fullname": "Walter Simons",
+	                    						                	"user_email": "ws@curisit.net",
+	                    						                	"status": 1},
+	                    						                {id: 3,
+	                    						                	"code": "BP-SA-001-ASKDGHKA",
+	                    						                	"user_fullname": "Frank Belmonte",
+	                    						                	"user_email": "fb@curisit.net",
+	                    						                	"status": 2},
+	                    							                {id: 4,
+	                    							                	"code": "BP-SA-001-BBBGGGG",
+	                    							                	"user_fullname": "John Dalton",
+	                    							                	"user_email": "jd@curisit.net",
+	                    							                	"status": 3},
+	                    							                {id: 5,
+	                    							                	"code": "BP-SA-001-AKADNAJANA",
+	                    							                	"user_fullname": "Walter Martins",
+	                    							                	"user_email": "wm@curisit.net",
+	                    							                	"status": 3},
+	                    							                {id: 6,
+	                    							                	"code": "BP-SA-001-AKANDAKS",
+	                    							                	"user_fullname": "Joe Bolton",
+	                    							                	"user_email": "jbol@curisit.net",
+	                    							                	"status": 2}
+	                    					                ];
+	                    					
+        				$scope.maxLengthErrorMsg = function(displayname, fieldMaxlength) {
+        					return $L.get("{0} length is too long (max: {1}).", $L.get(displayname), fieldMaxlength);
+        				}
+        				$scope.mandatoryFieldErrorMsg = function(displayname) {
+        					return $L.get("'{0}' is required.", $L.get(displayname));
+        				}
+        				$scope.ellipsis = function(txt, len) {
+        					if (!txt || txt.length <= len) return txt;
+        					return txt.substring(0, len) + '...';
+        				}
+                        $scope.currentPackId = $store.get('currentPackId');
+
+	   			}]);
 	
-	app.controller('LicensesCtrl', [
+	app.controller('PacksCtrl', [
 			'$scope',
 			'$http',
+			'$resource',
 			'toaster',
 			'Catalogs',
 			'$store',
 			'$L',
-			function($scope, $http, toaster, Catalogs, $store, $L) {
-				$scope.currentPack = $store.get('currentPack');
-				$scope.packs = [
-				                {id: 1,
-				                	"organization_name": "BP-Spain",
-				                	"application_name": "CurisIntegrity",
-				                	"licensetype_code": "CIBS",
-				                	"code": "BP-SA-0001",
-				                	"licenses": 50,
-				                	"lic_available": 23},
-				                {id: 2,
-				                	"organization_name": "Exxon",
-				                	"application_name": "CurisData",
-				                	"licensetype_code": "CDL1",
-				                	"code": "EX-SA-0001",
-				                	"licenses": 1,
-				                	"lic_available": 0},
-				                {id: 3,
-				                	"organization_name": "Repsol S.A. empresa con nombre muy largo",
-				                	"application_name": "CurisData",
-				                	"licensetype_code": "CDL2",
-				                	"code": "RE-SA-0001",
-				                	"licenses": 5,
-				                	"lic_available": 2},
-				                {id: 4,
-				                	"organization_name": "BP-Spain",
-				                	"application_name": "CurisIntegrity v3.0",
-				                	"code": "BP-SA-0002",
-				                	"licensetype_code": "CISA",
-				                	"licenses": 150,
-				                	"lic_available": 13},
-				                ];
+			function($scope, $http, $resource, toaster, Catalogs, $store, $L) {
+				var packResource = $resource('/pack/:packId', {
+					packId : '@id'
+				});
+				$scope.mandatory = {
+						code: true,
+						num_licenses: true,
+						organization_id: true,
+						license_type_id: true
+				}
+				$scope.maxlength = {
+						code: 50,
+						comments: 1024
+				}
+				$scope.refs = {};
+                Catalogs.init().then(function() {
+                    Catalogs.loadRefs($scope.refs, [{resource: 'organization', name: 'organization_id'},{resource: 'licensetype', name: 'license_type_id'}])
+                }); 
 
-				$scope.licenses = [
-				                {id: 1,
-				                	"code": "BP-SA-001-AKSJMS234",
-				                	"user_fullname": "Johnny Belmonte",
-				                	"user_email": "jb@curisit.net",
-				                	"status": 3},
-					                {id: 2,
-					                	"code": "BP-SA-001-KAJSDHAJS",
-					                	"user_fullname": "Walter Simons",
-					                	"user_email": "ws@curisit.net",
-					                	"status": 1},
-					                {id: 3,
-					                	"code": "BP-SA-001-ASKDGHKA",
-					                	"user_fullname": "Frank Belmonte",
-					                	"user_email": "fb@curisit.net",
-					                	"status": 2},
-						                {id: 4,
-						                	"code": "BP-SA-001-BBBGGGG",
-						                	"user_fullname": "John Dalton",
-						                	"user_email": "jd@curisit.net",
-						                	"status": 3},
-						                {id: 5,
-						                	"code": "BP-SA-001-AKADNAJANA",
-						                	"user_fullname": "Walter Martins",
-						                	"user_email": "wm@curisit.net",
-						                	"status": 3},
-						                {id: 6,
-						                	"code": "BP-SA-001-AKANDAKS",
-						                	"user_fullname": "Joe Bolton",
-						                	"user_email": "jbol@curisit.net",
-						                	"status": 2}
-				                ];
-				
+				// Used to create the form with the appropriate data
+				$scope.isNew = undefined;
 
+				// Selected pack from listing
+				// pack is the edited pack, in creation contains the data for the new pack
+				$scope.pack = null;
+
+				$scope.packs = packResource.query();
 				
-				$scope.ellipsis = function(txt, len) {
-					if (!txt || txt.length <= len) return txt;
-					return txt.substring(0, len) + '...';
+				$scope.save = function() {
+					var _success = function() {
+						$scope.packs = packResource.query();
+					}
+					packResource.save($scope.pack, _success)
 				}
 				
-				$scope.selectPack = function(pack) {
-					console.log('Pack selected: ' + JSON.stringify(pack));
-					$scope.currentPack = pack;
-					$store.put('currentPack', pack);
+				$scope.newPack = function() {
+					$scope.isNew = true;
+					$scope.showForm = true;
+					$scope.pack = {
+					        num_licenses: 1,
+                            license_type_id: !$scope.refs.license_type_id || !$scope.refs.license_type_id.length ? null : $scope.refs.license_type_id[0].id,
+                            organization_id: !$scope.refs.organization_id || !$scope.refs.organization_id.length ? null : $scope.refs.organization_id[0].id
+					}
+                   setTimeout(function() {
+                        $('#code').focus();
+                    }, 0);
+				}
+
+                $scope.editPack = function(selectedPack) {
+                    $scope.isNew = false;
+                    $scope.showForm = true;
+                    $scope.pack = selectedPack;
+                   setTimeout(function() {
+                        $('#code').focus();
+                    }, 0);
+                }
+
+                $scope.deletePack = function(selectedPack) {
+                    $scope.showForm = false;
+                    BootstrapDialog.confirm($L.get("The pack '{0}' will be deleted, are you sure?", selectedPack.code), function(result){
+                        if(result) {
+                            var promise = packResource.remove({}, {id: selectedPack.id}).$promise;
+                            promise.then(function(data) {
+                                $scope.selectPack(null);
+                                $scope.packs = packResource.query();
+                                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);
+                            });
+                        }
+                    });
+                    $scope.isNew = false;
+                }
+
+
+                $scope.cancel = function() {
+                    $scope.showForm = false;
+                }
+
+				$scope.selectPack = function(packId) {
+					$scope.$parent.currentPackId = packId;
+					$store.put('currentPackId', packId);
 				}
 				
 			} ]);

--
Gitblit v1.3.2