From 225dc136c1c6fba26b2e4f8b763cbd3fc4d596e3 Mon Sep 17 00:00:00 2001
From: Roberto Sánchez <roberto.sanchez@curisit.net>
Date: Mon, 13 Jan 2014 12:37:25 +0000
Subject: [PATCH] #394 feature - Added error messages to Admin module

---
 securis/src/main/java/net/curisit/securis/SecurisErrorHandler.java   |    2 
 securis/src/main/resources/static/js/catalogs.json                   |    6 --
 securis/src/main/java/net/curisit/securis/services/UserResource.java |    6 +++
 securis/src/main/resources/static/js/catalogs.js                     |   27 +++++++++++++
 securis/src/main/resources/static/js/admin.js                        |   28 ++++++++++---
 5 files changed, 56 insertions(+), 13 deletions(-)

diff --git a/securis/src/main/java/net/curisit/securis/SecurisErrorHandler.java b/securis/src/main/java/net/curisit/securis/SecurisErrorHandler.java
index 7eb1393..baa1a0f 100644
--- a/securis/src/main/java/net/curisit/securis/SecurisErrorHandler.java
+++ b/securis/src/main/java/net/curisit/securis/SecurisErrorHandler.java
@@ -9,7 +9,7 @@
 
 public class SecurisErrorHandler extends ErrorHandler {
 
-	public static String HEADER_ERROR_MESSAGE = "X-SECURIS_ERROR";
+	public static String HEADER_ERROR_MESSAGE = "X-SECURIS-ERROR";
 
 	@Override
 	protected void writeErrorPageHead(HttpServletRequest request, Writer writer, int code, String message) throws IOException {
diff --git a/securis/src/main/java/net/curisit/securis/services/UserResource.java b/securis/src/main/java/net/curisit/securis/services/UserResource.java
index 4e0302a..25d2445 100644
--- a/securis/src/main/java/net/curisit/securis/services/UserResource.java
+++ b/securis/src/main/java/net/curisit/securis/services/UserResource.java
@@ -106,6 +106,12 @@
 	public Response create(User user, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
 		log.info("Creating new user");
 		EntityManager em = emProvider.get();
+		User currentUser = em.find(User.class, user.getUsername());
+		if (currentUser != null) {
+			log.info("User with id {} was found in DB, we'll try to modify it", user.getUsername());
+			return modify(user, user.getUsername(), token);
+		}
+
 		List<Organization> orgs = null;
 		List<Integer> orgsIds = user.getOrgsIds();
 		if (orgsIds != null && orgsIds.size() > 0) {
diff --git a/securis/src/main/resources/static/js/admin.js b/securis/src/main/resources/static/js/admin.js
index 4cde879..53b2125 100644
--- a/securis/src/main/resources/static/js/admin.js
+++ b/securis/src/main/resources/static/js/admin.js
@@ -2,7 +2,13 @@
 	'use strict';
 
 	var app = angular.module('app', [ 'ngRoute', 'ngResource', 'toaster', 'localytics.directives', 'catalogs' ]);
-
+	
+	var HTTP_ERRORS = {
+			403: "Forbidden action",
+			500: "Server error",
+			404: "Element not found"
+	}
+	
 	app.directive(
 					'catalogField',
 					function() {
@@ -25,8 +31,9 @@
 	app.controller('CatalogsCtrl', [
 			'$scope',
 			'$http',
+			'toaster',
 			'Catalogs',
-			function($scope, $http, Catalogs) {
+			function($scope, $http, toaster, Catalogs) {
 				$scope.showForm = false;
 				$scope.isNew = false;
 				$scope.formu = {};
@@ -57,7 +64,7 @@
 					$scope.isNew = false;
 					$scope.formu = {}
 					for (var k in data) {
-						if (k.indexOf('$') !== 0 && !Catalogs.getField(k).listingOnly) $scope.formu[k] = data[k]
+						if (k.indexOf('$') !== 0 && k.indexOf('_') !== 0 && !Catalogs.getField(k).listingOnly) $scope.formu[k] = data[k]
 					}
 
 					setTimeout(function() {
@@ -71,13 +78,16 @@
 							var promise = Catalogs.remove(data).$promise;
 							promise.then(function(data) {
 								$scope.list = Catalogs.query();
+								Catalogs.refreshRef($scope.refs, Catalogs.getMetadata().resource, $scope.list);
+								toaster.pop('success', Catalogs.getName(), "Element deleted successfully");
+							},function(error) {
+								console.log(error);
+								toaster.pop('error', Catalogs.getName(), "Error deleting element, reason: " + HTTP_ERRORS[error.status] + ". Details: " + error.headers('X-SECURIS-ERROR'), 12000);
 							});
 			            }
 			        });
 					$scope.showForm = false;
 					$scope.isNew = false;
-					// TODO: Load in formu values for Form
-					// $scope.formu = {};
 				}
 
 			} ]);
@@ -119,7 +129,7 @@
 
 				$scope.saveCatalog = function() {
 					if ($scope.catalogForm.$invalid) {
-						alert(JSON.stringify($scope.catalogForm))
+						toaster.pop('error', Catalogs.getName(), "There are wrong data in current form, please fix it before to save");
 					} else {
 						var promise = Catalogs.save($scope.formu).$promise;
 						promise.then(function(data, otro) {
@@ -130,8 +140,12 @@
 								$scope.cancel();
 							}
 							$scope.$parent.list = Catalogs.query();
+							Catalogs.refreshRef($scope.refs, Catalogs.getMetadata().resource, $scope.$parent.list);
+
+							toaster.pop('success', Catalogs.getName(), "Element saved successfully");
 						}, function(error) {
 							console.log(error);
+							toaster.pop('error', Catalogs.getName(), "Error saving element, reason: " + HTTP_ERRORS[error.status] + ". Details: " + error.headers('X-SECURIS-ERROR'), 12000);
 						});
 						
 					}
@@ -146,7 +160,7 @@
 					var type = Catalogs.getField(name).type;
 					var printedValue = type === 'date' ? $filter('date')(value, 'yyyy-MM-dd') : value;
 					if (printedValue !== value) // this line is a work around to allow search in formatted fields
-						row['$display_'+name] = printedValue;
+						row['_display_'+name] = printedValue;
 					return printedValue;
 				}
 				
diff --git a/securis/src/main/resources/static/js/catalogs.js b/securis/src/main/resources/static/js/catalogs.js
index 10dc1dd..518fd65 100644
--- a/securis/src/main/resources/static/js/catalogs.js
+++ b/securis/src/main/resources/static/js/catalogs.js
@@ -134,6 +134,33 @@
 	this.query = function() {
 		return this.getResource().query({}, _success, _fail);
 	}
+	this.refreshRef = function(refs, res, preloadedData) {
+		// We check if there is some field for the resource passed as parameter
+		var field = (function() {
+			for (var i = _current.fields.length - 1; i >= 0; i--) {
+				if (_current.fields[i].resource === res)
+					return _current.fields[i];				
+			}
+			return null;
+		})();
+		
+		// If field for that resource is not found there is nothing to refresh
+		if (!field) return;
+		var resource = this.getResource(res);
+		var data = preloadedData || resource.query({}, _success, _fail);
+		var that = this;
+		data.$promise.then(function(responseData) {
+			var pk = that.getPk(that.getMetadata(field.resource))
+			var comboData = []
+			responseData.forEach(function(row) {
+				comboData.push({
+					id: row[pk],
+					label: row.label || row.name || row.code || row.first_name + ' ' + row.last_name
+				});
+			})
+			refs[field.name] = comboData;
+		})
+	}
 	this.loadRefs = function(refs) {
 		if (!_current) throw new Error('There is no current catalog selected');
 		var refsFields = [];
diff --git a/securis/src/main/resources/static/js/catalogs.json b/securis/src/main/resources/static/js/catalogs.json
index 96d817a..c2efe6b 100644
--- a/securis/src/main/resources/static/js/catalogs.json
+++ b/securis/src/main/resources/static/js/catalogs.json
@@ -169,10 +169,6 @@
 		"type" : "date",
 		"readOnly" : true
 	}]
-}, {
-	"name" : "System params",
-	"resource" : "systemparams",
-	"fields" : []
-}
+	}
 
 ]
\ No newline at end of file

--
Gitblit v1.3.2