#394 feature - Added error messages to Admin module
| .. | .. |
|---|
| 9 | 9 | |
|---|
| 10 | 10 | public class SecurisErrorHandler extends ErrorHandler { |
|---|
| 11 | 11 | |
|---|
| 12 | | - public static String HEADER_ERROR_MESSAGE = "X-SECURIS_ERROR"; |
|---|
| 12 | + public static String HEADER_ERROR_MESSAGE = "X-SECURIS-ERROR"; |
|---|
| 13 | 13 | |
|---|
| 14 | 14 | @Override |
|---|
| 15 | 15 | protected void writeErrorPageHead(HttpServletRequest request, Writer writer, int code, String message) throws IOException { |
|---|
| .. | .. |
|---|
| 106 | 106 | public Response create(User user, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 107 | 107 | log.info("Creating new user"); |
|---|
| 108 | 108 | 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 | + |
|---|
| 109 | 115 | List<Organization> orgs = null; |
|---|
| 110 | 116 | List<Integer> orgsIds = user.getOrgsIds(); |
|---|
| 111 | 117 | if (orgsIds != null && orgsIds.size() > 0) { |
|---|
| .. | .. |
|---|
| 2 | 2 | 'use strict'; |
|---|
| 3 | 3 | |
|---|
| 4 | 4 | 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 | + |
|---|
| 6 | 12 | app.directive( |
|---|
| 7 | 13 | 'catalogField', |
|---|
| 8 | 14 | function() { |
|---|
| .. | .. |
|---|
| 25 | 31 | app.controller('CatalogsCtrl', [ |
|---|
| 26 | 32 | '$scope', |
|---|
| 27 | 33 | '$http', |
|---|
| 34 | + 'toaster', |
|---|
| 28 | 35 | 'Catalogs', |
|---|
| 29 | | - function($scope, $http, Catalogs) { |
|---|
| 36 | + function($scope, $http, toaster, Catalogs) { |
|---|
| 30 | 37 | $scope.showForm = false; |
|---|
| 31 | 38 | $scope.isNew = false; |
|---|
| 32 | 39 | $scope.formu = {}; |
|---|
| .. | .. |
|---|
| 57 | 64 | $scope.isNew = false; |
|---|
| 58 | 65 | $scope.formu = {} |
|---|
| 59 | 66 | 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] |
|---|
| 61 | 68 | } |
|---|
| 62 | 69 | |
|---|
| 63 | 70 | setTimeout(function() { |
|---|
| .. | .. |
|---|
| 71 | 78 | var promise = Catalogs.remove(data).$promise; |
|---|
| 72 | 79 | promise.then(function(data) { |
|---|
| 73 | 80 | $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); |
|---|
| 74 | 86 | }); |
|---|
| 75 | 87 | } |
|---|
| 76 | 88 | }); |
|---|
| 77 | 89 | $scope.showForm = false; |
|---|
| 78 | 90 | $scope.isNew = false; |
|---|
| 79 | | - // TODO: Load in formu values for Form |
|---|
| 80 | | - // $scope.formu = {}; |
|---|
| 81 | 91 | } |
|---|
| 82 | 92 | |
|---|
| 83 | 93 | } ]); |
|---|
| .. | .. |
|---|
| 119 | 129 | |
|---|
| 120 | 130 | $scope.saveCatalog = function() { |
|---|
| 121 | 131 | 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"); |
|---|
| 123 | 133 | } else { |
|---|
| 124 | 134 | var promise = Catalogs.save($scope.formu).$promise; |
|---|
| 125 | 135 | promise.then(function(data, otro) { |
|---|
| .. | .. |
|---|
| 130 | 140 | $scope.cancel(); |
|---|
| 131 | 141 | } |
|---|
| 132 | 142 | $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"); |
|---|
| 133 | 146 | }, function(error) { |
|---|
| 134 | 147 | console.log(error); |
|---|
| 148 | + toaster.pop('error', Catalogs.getName(), "Error saving element, reason: " + HTTP_ERRORS[error.status] + ". Details: " + error.headers('X-SECURIS-ERROR'), 12000); |
|---|
| 135 | 149 | }); |
|---|
| 136 | 150 | |
|---|
| 137 | 151 | } |
|---|
| .. | .. |
|---|
| 146 | 160 | var type = Catalogs.getField(name).type; |
|---|
| 147 | 161 | var printedValue = type === 'date' ? $filter('date')(value, 'yyyy-MM-dd') : value; |
|---|
| 148 | 162 | 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; |
|---|
| 150 | 164 | return printedValue; |
|---|
| 151 | 165 | } |
|---|
| 152 | 166 | |
|---|
| .. | .. |
|---|
| 134 | 134 | this.query = function() { |
|---|
| 135 | 135 | return this.getResource().query({}, _success, _fail); |
|---|
| 136 | 136 | } |
|---|
| 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 | + } |
|---|
| 137 | 164 | this.loadRefs = function(refs) { |
|---|
| 138 | 165 | if (!_current) throw new Error('There is no current catalog selected'); |
|---|
| 139 | 166 | var refsFields = []; |
|---|
| .. | .. |
|---|
| 169 | 169 | "type" : "date", |
|---|
| 170 | 170 | "readOnly" : true |
|---|
| 171 | 171 | }] |
|---|
| 172 | | -}, { |
|---|
| 173 | | - "name" : "System params", |
|---|
| 174 | | - "resource" : "systemparams", |
|---|
| 175 | | - "fields" : [] |
|---|
| 176 | | -} |
|---|
| 172 | + } |
|---|
| 177 | 173 | |
|---|
| 178 | 174 | ] |
|---|