Roberto Sánchez
2014-01-13 225dc136c1c6fba26b2e4f8b763cbd3fc4d596e3
#394 feature - Added error messages to Admin module
5 files modified
changed files
securis/src/main/java/net/curisit/securis/SecurisErrorHandler.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/UserResource.java patch | view | blame | history
securis/src/main/resources/static/js/admin.js patch | view | blame | history
securis/src/main/resources/static/js/catalogs.js patch | view | blame | history
securis/src/main/resources/static/js/catalogs.json patch | view | blame | history
securis/src/main/java/net/curisit/securis/SecurisErrorHandler.java
....@@ -9,7 +9,7 @@
99
1010 public class SecurisErrorHandler extends ErrorHandler {
1111
12
- public static String HEADER_ERROR_MESSAGE = "X-SECURIS_ERROR";
12
+ public static String HEADER_ERROR_MESSAGE = "X-SECURIS-ERROR";
1313
1414 @Override
1515 protected void writeErrorPageHead(HttpServletRequest request, Writer writer, int code, String message) throws IOException {
securis/src/main/java/net/curisit/securis/services/UserResource.java
....@@ -106,6 +106,12 @@
106106 public Response create(User user, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
107107 log.info("Creating new user");
108108 EntityManager em = emProvider.get();
109
+ User currentUser = em.find(User.class, user.getUsername());
110
+ if (currentUser != null) {
111
+ log.info("User with id {} was found in DB, we'll try to modify it", user.getUsername());
112
+ return modify(user, user.getUsername(), token);
113
+ }
114
+
109115 List<Organization> orgs = null;
110116 List<Integer> orgsIds = user.getOrgsIds();
111117 if (orgsIds != null && orgsIds.size() > 0) {
securis/src/main/resources/static/js/admin.js
....@@ -2,7 +2,13 @@
22 'use strict';
33
44 var app = angular.module('app', [ 'ngRoute', 'ngResource', 'toaster', 'localytics.directives', 'catalogs' ]);
5
-
5
+
6
+ var HTTP_ERRORS = {
7
+ 403: "Forbidden action",
8
+ 500: "Server error",
9
+ 404: "Element not found"
10
+ }
11
+
612 app.directive(
713 'catalogField',
814 function() {
....@@ -25,8 +31,9 @@
2531 app.controller('CatalogsCtrl', [
2632 '$scope',
2733 '$http',
34
+ 'toaster',
2835 'Catalogs',
29
- function($scope, $http, Catalogs) {
36
+ function($scope, $http, toaster, Catalogs) {
3037 $scope.showForm = false;
3138 $scope.isNew = false;
3239 $scope.formu = {};
....@@ -57,7 +64,7 @@
5764 $scope.isNew = false;
5865 $scope.formu = {}
5966 for (var k in data) {
60
- if (k.indexOf('$') !== 0 && !Catalogs.getField(k).listingOnly) $scope.formu[k] = data[k]
67
+ if (k.indexOf('$') !== 0 && k.indexOf('_') !== 0 && !Catalogs.getField(k).listingOnly) $scope.formu[k] = data[k]
6168 }
6269
6370 setTimeout(function() {
....@@ -71,13 +78,16 @@
7178 var promise = Catalogs.remove(data).$promise;
7279 promise.then(function(data) {
7380 $scope.list = Catalogs.query();
81
+ Catalogs.refreshRef($scope.refs, Catalogs.getMetadata().resource, $scope.list);
82
+ toaster.pop('success', Catalogs.getName(), "Element deleted successfully");
83
+ },function(error) {
84
+ console.log(error);
85
+ toaster.pop('error', Catalogs.getName(), "Error deleting element, reason: " + HTTP_ERRORS[error.status] + ". Details: " + error.headers('X-SECURIS-ERROR'), 12000);
7486 });
7587 }
7688 });
7789 $scope.showForm = false;
7890 $scope.isNew = false;
79
- // TODO: Load in formu values for Form
80
- // $scope.formu = {};
8191 }
8292
8393 } ]);
....@@ -119,7 +129,7 @@
119129
120130 $scope.saveCatalog = function() {
121131 if ($scope.catalogForm.$invalid) {
122
- alert(JSON.stringify($scope.catalogForm))
132
+ toaster.pop('error', Catalogs.getName(), "There are wrong data in current form, please fix it before to save");
123133 } else {
124134 var promise = Catalogs.save($scope.formu).$promise;
125135 promise.then(function(data, otro) {
....@@ -130,8 +140,12 @@
130140 $scope.cancel();
131141 }
132142 $scope.$parent.list = Catalogs.query();
143
+ Catalogs.refreshRef($scope.refs, Catalogs.getMetadata().resource, $scope.$parent.list);
144
+
145
+ toaster.pop('success', Catalogs.getName(), "Element saved successfully");
133146 }, function(error) {
134147 console.log(error);
148
+ toaster.pop('error', Catalogs.getName(), "Error saving element, reason: " + HTTP_ERRORS[error.status] + ". Details: " + error.headers('X-SECURIS-ERROR'), 12000);
135149 });
136150
137151 }
....@@ -146,7 +160,7 @@
146160 var type = Catalogs.getField(name).type;
147161 var printedValue = type === 'date' ? $filter('date')(value, 'yyyy-MM-dd') : value;
148162 if (printedValue !== value) // this line is a work around to allow search in formatted fields
149
- row['$display_'+name] = printedValue;
163
+ row['_display_'+name] = printedValue;
150164 return printedValue;
151165 }
152166
securis/src/main/resources/static/js/catalogs.js
....@@ -134,6 +134,33 @@
134134 this.query = function() {
135135 return this.getResource().query({}, _success, _fail);
136136 }
137
+ this.refreshRef = function(refs, res, preloadedData) {
138
+ // We check if there is some field for the resource passed as parameter
139
+ var field = (function() {
140
+ for (var i = _current.fields.length - 1; i >= 0; i--) {
141
+ if (_current.fields[i].resource === res)
142
+ return _current.fields[i];
143
+ }
144
+ return null;
145
+ })();
146
+
147
+ // If field for that resource is not found there is nothing to refresh
148
+ if (!field) return;
149
+ var resource = this.getResource(res);
150
+ var data = preloadedData || resource.query({}, _success, _fail);
151
+ var that = this;
152
+ data.$promise.then(function(responseData) {
153
+ var pk = that.getPk(that.getMetadata(field.resource))
154
+ var comboData = []
155
+ responseData.forEach(function(row) {
156
+ comboData.push({
157
+ id: row[pk],
158
+ label: row.label || row.name || row.code || row.first_name + ' ' + row.last_name
159
+ });
160
+ })
161
+ refs[field.name] = comboData;
162
+ })
163
+ }
137164 this.loadRefs = function(refs) {
138165 if (!_current) throw new Error('There is no current catalog selected');
139166 var refsFields = [];
securis/src/main/resources/static/js/catalogs.json
....@@ -169,10 +169,6 @@
169169 "type" : "date",
170170 "readOnly" : true
171171 }]
172
-}, {
173
- "name" : "System params",
174
- "resource" : "systemparams",
175
- "fields" : []
176
-}
172
+ }
177173
178174 ]