Roberto Sánchez
2014-01-13 edd47c68c6a08bd756d96213c38e896a0a257bd1
#394 feature - Several fixes related with form management
8 files modified
changed files
securis/src/main/java/net/curisit/securis/db/Organization.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/OrganizationResource.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/UserResource.java patch | view | blame | history
securis/src/main/resources/static/admin.html 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/db/Organization.java
....@@ -136,8 +136,12 @@
136136 // information about the referenced entities.
137137 @JsonProperty("org_parent_id")
138138 public void setParentOrgId(Integer orgId) {
139
- parentOrganization = new Organization();
140
- parentOrganization.setId(orgId);
139
+ if (orgId != null) {
140
+ parentOrganization = new Organization();
141
+ parentOrganization.setId(orgId);
142
+ } else {
143
+ parentOrganization = null;
144
+ }
141145 }
142146
143147 @JsonProperty("org_parent_id")
securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java
....@@ -111,6 +111,9 @@
111111 log.error("LicenseType application with id {} not found in DB", lt.getApplicationId());
112112 return Response.status(Status.NOT_FOUND).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "License type's app not found with ID: " + lt.getApplicationId()).build();
113113 }
114
+ } else {
115
+ log.error("Application is missing for current license type data");
116
+ return Response.status(Status.NOT_FOUND).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "Application is missing for current license type data").build();
114117 }
115118
116119 lt.setApplication(app);
securis/src/main/java/net/curisit/securis/services/OrganizationResource.java
....@@ -96,6 +96,15 @@
9696 return Response.ok(lt).build();
9797 }
9898
99
+ private boolean isCyclicalRelationship(int currentId, Organization parent) {
100
+ while (parent != null) {
101
+ if (parent.getId() == currentId)
102
+ return true;
103
+ parent = parent.getParentOrganization();
104
+ }
105
+ return false;
106
+ }
107
+
99108 @POST
100109 @Path("/")
101110 @Consumes(MediaType.APPLICATION_JSON)
....@@ -157,6 +166,10 @@
157166 log.error("Organization parent with id {} not found in DB", org.getParentOrgId());
158167 return Response.status(Status.NOT_FOUND).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "Organization's parent not found with ID: " + org.getParentOrgId()).build();
159168 }
169
+ if (isCyclicalRelationship(currentOrg.getId(), parentOrg)) {
170
+ log.error("Organization parent generate a cyclical relationship, parent id {}, current id: {}", org.getParentOrgId(), currentOrg.getId());
171
+ return Response.status(Status.FORBIDDEN).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "Cyclical relationships are not allowed, please change the parent organization, current Parent: " + parentOrg.getName()).build();
172
+ }
160173 }
161174
162175 List<User> users = null;
....@@ -165,8 +178,8 @@
165178 users = new ArrayList<>();
166179 for (String username : usersIds) {
167180 User user = em.find(User.class, username);
168
- if (parentOrg == null) {
169
- log.error("Organization user with id {} not found in DB", username);
181
+ if (user == null) {
182
+ log.error("Organization user with id '{}' not found in DB", username);
170183 return Response.status(Status.NOT_FOUND).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "Organization's user not found with ID: " + username).build();
171184 }
172185 users.add(user);
securis/src/main/java/net/curisit/securis/services/UserResource.java
....@@ -89,7 +89,7 @@
8989 }
9090
9191 EntityManager em = emProvider.get();
92
- User lt = em.find(User.class, Integer.parseInt(uid));
92
+ User lt = em.find(User.class, uid);
9393 if (lt == null) {
9494 log.error("User with id {} not found in DB", uid);
9595 return Response.status(Status.NOT_FOUND).build();
....@@ -187,7 +187,7 @@
187187 public Response delete(@PathParam("uid") String uid, @Context HttpServletRequest request) {
188188 log.info("Deleting app with id: {}", uid);
189189 EntityManager em = emProvider.get();
190
- User app = em.find(User.class, Integer.parseInt(uid));
190
+ User app = em.find(User.class, uid);
191191 if (app == null) {
192192 log.error("User with id {} can not be deleted, It was not found in DB", uid);
193193 return Response.status(Status.NOT_FOUND).build();
securis/src/main/resources/static/admin.html
....@@ -154,7 +154,8 @@
154154 <p ng-switch-when="readonly" class="form-control-static">{{formu[field.name]}}</p>
155155 <p ng-switch-when="readonly_date" class="form-control-static">{{formu[field.name] | date:'medium'}}</p>
156156 <select ng-switch-when="select" class="form-control" ng-required="field.mandatory" ng-model="formu[field.name]"
157
- ng-options="o.id as o.label for o in refs[field.name]">
157
+ ng-options="o.id as o.label for o in refs[field.name]" >
158
+ <option selected="true" ng-if="!field.mandatory" value=""></option>
158159 </select>
159160 <select chosen multiple ng-switch-when="multiselect" class="form-control" ng-required="field.mandatory" ng-model="formu[field.name]"
160161 ng-options="o.id as o.label for o in refs[field.name]" data-placeholder="...">
securis/src/main/resources/static/js/admin.js
....@@ -62,10 +62,15 @@
6262 $scope.edit = function(data) {
6363 $scope.showForm = true;
6464 $scope.isNew = false;
65
+ // Next line is a wirkaround due to some issues with values with ID == 0
66
+ $('select').val(null);
6567 $scope.formu = {}
66
- for (var k in data) {
67
- if (k.indexOf('$') !== 0 && k.indexOf('_') !== 0 && !Catalogs.getField(k).listingOnly) $scope.formu[k] = data[k]
68
- }
68
+ var fields = Catalogs.getMetadata().fields;
69
+ console.log($scope);
70
+
71
+ fields.forEach(function(field) {
72
+ if (!field.listingOnly) $scope.formu[field.name] = data[field.name] || null;
73
+ })
6974
7075 setTimeout(function() {
7176 $('#'+Catalogs.getFFF()).focus();
....@@ -117,6 +122,7 @@
117122 $scope.editNew = function() {
118123 $scope.$parent.isNew = true;
119124 $scope.$parent.showForm = true;
125
+ $('select').val(null);
120126 $scope.$parent.formu = {};
121127 setTimeout(function() {
122128 $('#'+Catalogs.getFFF()).focus();
....@@ -125,6 +131,7 @@
125131 }
126132 $scope.cancel = function() {
127133 $scope.$parent.showForm = false;
134
+ $scope.catalogForm.$setPristine();
128135 }
129136
130137 $scope.saveCatalog = function() {
....@@ -134,6 +141,7 @@
134141 var promise = Catalogs.save($scope.formu).$promise;
135142 promise.then(function(data, otro) {
136143 if ($scope.isNew) {
144
+ $scope.catalogForm.$setPristine();
137145 $scope.$parent.formu = {}
138146 $('#'+ Catalogs.getFFF()).focus();
139147 } else {
securis/src/main/resources/static/js/catalogs.js
....@@ -183,13 +183,15 @@
183183 $q.all(promises).then(function() {
184184
185185 for (var k in refs) {
186
- var pk = that.getPk(that.getMetadata(that.getField(k).resource))
186
+ var field = that.getField(k);
187
+ var pk = that.getPk(that.getMetadata(field.resource))
187188 console.log('PK field for ' + k + ' is ' + pk)
188189 var comboData = []
189190 refs[k].forEach(function(row) {
191
+ console.log('field.resource !== _current.resource: ' + field.resource +' '+ _current.resource)
190192 comboData.push({
191193 id: row[pk],
192
- label: row.label || row.name || row.code || row.first_name + ' ' + row.last_name
194
+ label: row.label || row.name || row.code || row.first_name + ' ' + (row.last_name || '')
193195 });
194196 })
195197 refs[k] = comboData;
securis/src/main/resources/static/js/catalogs.json
....@@ -61,6 +61,7 @@
6161 "name" : "application_id",
6262 "display" : "Application",
6363 "resource" : "application",
64
+ "mandatory" : true,
6465 "type" : "select"
6566 }, {
6667 "name" : "creationTimestamp",