rsanchez
2014-10-15 973ee9056995d1c9e7a533d9999329a70f0d2b2d
#2021 config - Added pack and LicenseType Metadata management
12 files modified
changed files
securis/src/main/java/net/curisit/securis/db/LicenseType.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/db/LicenseTypeMetadata.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/db/Pack.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/db/PackMetadata.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/PackResource.java patch | view | blame | history
securis/src/main/resources/db/schema.sql 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.json patch | view | blame | history
securis/src/main/resources/static/licenses.html patch | view | blame | history
securis/src/main/resources/static/main.html patch | view | blame | history
securis/src/main/java/net/curisit/securis/db/LicenseType.java
....@@ -45,7 +45,7 @@
4545
4646 @Id
4747 @GeneratedValue
48
- private int id;
48
+ private Integer id;
4949
5050 private String code;
5151 private String name;
....@@ -138,4 +138,17 @@
138138 this.creationTimestamp = creationTimestamp;
139139 }
140140
141
+ @Override
142
+ public boolean equals(Object obj) {
143
+ if (!(obj instanceof LicenseType))
144
+ return false;
145
+ LicenseType other = (LicenseType)obj;
146
+ return id.equals(other.id);
147
+ }
148
+
149
+ @Override
150
+ public int hashCode() {
151
+
152
+ return (id == null ? 0 : id.hashCode());
153
+ }
141154 }
securis/src/main/java/net/curisit/securis/db/LicenseTypeMetadata.java
....@@ -32,25 +32,15 @@
3232 private static final long serialVersionUID = 1L;
3333
3434 @Id
35
- @GeneratedValue
36
- private int id;
37
-
38
- private String key;
39
-
40
- private String value;
41
-
4235 @JsonIgnore
4336 @ManyToOne
4437 @JoinColumn(name = "license_type_id")
4538 private LicenseType licenseType;
4639
47
- public int getId() {
48
- return id;
49
- }
40
+ @Id
41
+ private String key;
5042
51
- public void setId(int id) {
52
- this.id = id;
53
- }
43
+ private String value;
5444
5545 @JsonProperty("licensetype_id")
5646 public Integer getLicenseTypeId() {
....@@ -91,4 +81,17 @@
9181 this.key = key;
9282 }
9383
84
+ @Override
85
+ public boolean equals(Object obj) {
86
+ if (!(obj instanceof LicenseTypeMetadata))
87
+ return false;
88
+ LicenseTypeMetadata other = (LicenseTypeMetadata)obj;
89
+ return key.equals(other.key) && (licenseType == null || licenseType.equals(other.licenseType));
90
+ }
91
+
92
+ @Override
93
+ public int hashCode() {
94
+
95
+ return key.hashCode() + (licenseType == null ? 0 : licenseType.hashCode());
96
+ }
9497 }
securis/src/main/java/net/curisit/securis/db/Pack.java
....@@ -42,7 +42,7 @@
4242
4343 @Id
4444 @GeneratedValue
45
- private int id;
45
+ private Integer id;
4646
4747 private String code;
4848
....@@ -263,4 +263,25 @@
263263 this.licensePreactivation = licensePreactivation;
264264 }
265265
266
+ public Set<PackMetadata> getMetadata() {
267
+ return metadata;
268
+ }
269
+
270
+ public void setMetadata(Set<PackMetadata> metadata) {
271
+ this.metadata = metadata;
272
+ }
273
+
274
+ @Override
275
+ public boolean equals(Object obj) {
276
+ if (!(obj instanceof Pack))
277
+ return false;
278
+ Pack other = (Pack)obj;
279
+ return id.equals(other.id);
280
+ }
281
+
282
+ @Override
283
+ public int hashCode() {
284
+
285
+ return (id == null ? 0 : id.hashCode());
286
+ }
266287 }
securis/src/main/java/net/curisit/securis/db/PackMetadata.java
....@@ -32,27 +32,18 @@
3232 private static final long serialVersionUID = 1L;
3333
3434 @Id
35
- @GeneratedValue
36
- private int id;
35
+ @JsonIgnore
36
+ @ManyToOne
37
+ @JoinColumn(name = "pack_id")
38
+ private Pack pack;
3739
40
+ @Id
3841 private String key;
3942
4043 private String value;
4144
4245 private boolean readonly;
4346
44
- @JsonIgnore
45
- @ManyToOne
46
- @JoinColumn(name = "pack_id")
47
- private Pack pack;
48
-
49
- public int getId() {
50
- return id;
51
- }
52
-
53
- public void setId(int id) {
54
- this.id = id;
55
- }
5647
5748 @JsonProperty("pack_id")
5849 public Integer getPackId() {
....@@ -100,5 +91,19 @@
10091 public void setReadonly(boolean readonly) {
10192 this.readonly = readonly;
10293 }
94
+
95
+ @Override
96
+ public boolean equals(Object obj) {
97
+ if (!(obj instanceof PackMetadata))
98
+ return false;
99
+ PackMetadata other = (PackMetadata)obj;
100
+ return key.equals(other.key) && (pack == null || pack.equals(other.pack));
101
+ }
102
+
103
+ @Override
104
+ public int hashCode() {
105
+
106
+ return key.hashCode() + (pack == null ? 0 : pack.hashCode());
107
+ }
103108
104109 }
securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java
....@@ -2,6 +2,7 @@
22
33 import java.util.Date;
44 import java.util.List;
5
+import java.util.Set;
56
67 import javax.inject.Inject;
78 import javax.inject.Provider;
....@@ -26,7 +27,9 @@
2627 import net.curisit.securis.DefaultExceptionHandler;
2728 import net.curisit.securis.SeCurisException;
2829 import net.curisit.securis.db.Application;
30
+import net.curisit.securis.db.ApplicationMetadata;
2931 import net.curisit.securis.db.LicenseType;
32
+import net.curisit.securis.db.LicenseTypeMetadata;
3033 import net.curisit.securis.utils.TokenHelper;
3134
3235 import org.apache.logging.log4j.LogManager;
....@@ -123,6 +126,15 @@
123126
124127 lt.setCreationTimestamp(new Date());
125128 em.persist(lt);
129
+ Set<LicenseTypeMetadata> newMD = lt.getMetadata();
130
+
131
+ if (newMD != null) {
132
+ for (LicenseTypeMetadata md : newMD) {
133
+ md.setLicenseType(lt);
134
+ em.persist(md);
135
+ }
136
+ }
137
+ lt.setMetadata(newMD);
126138
127139 return Response.ok(lt).build();
128140 }
....@@ -155,6 +167,20 @@
155167 currentlt.setName(lt.getName());
156168 currentlt.setDescription(lt.getDescription());
157169 em.persist(currentlt);
170
+
171
+ Set<LicenseTypeMetadata> newMD = lt.getMetadata();
172
+ for (LicenseTypeMetadata currentMd : currentlt.getMetadata()) {
173
+ if (newMD == null || !newMD.contains(currentMd));
174
+ em.remove(currentMd);
175
+ }
176
+
177
+ if (newMD != null) {
178
+ for (LicenseTypeMetadata md : newMD) {
179
+ md.setLicenseType(currentlt);
180
+ em.persist(md);
181
+ }
182
+ }
183
+ currentlt.setMetadata(newMD);
158184
159185 return Response.ok(currentlt).build();
160186 }
securis/src/main/java/net/curisit/securis/services/PackResource.java
....@@ -3,6 +3,7 @@
33 import java.security.Principal;
44 import java.util.Date;
55 import java.util.List;
6
+import java.util.Set;
67
78 import javax.annotation.security.RolesAllowed;
89 import javax.inject.Inject;
....@@ -26,8 +27,10 @@
2627 import net.curisit.securis.DefaultExceptionHandler;
2728 import net.curisit.securis.SeCurisException;
2829 import net.curisit.securis.db.LicenseType;
30
+import net.curisit.securis.db.PackMetadata;
2931 import net.curisit.securis.db.Organization;
3032 import net.curisit.securis.db.Pack;
33
+import net.curisit.securis.db.PackMetadata;
3134 import net.curisit.securis.db.User;
3235 import net.curisit.securis.security.BasicSecurityContext;
3336 import net.curisit.securis.security.Securable;
....@@ -155,7 +158,15 @@
155158 pack.setCreatedBy(user);
156159 pack.setCreationTimestamp(new Date());
157160 em.persist(pack);
158
-
161
+ Set<PackMetadata> newMD = pack.getMetadata();
162
+
163
+ if (newMD != null) {
164
+ for (PackMetadata md : newMD) {
165
+ md.setPack(pack);
166
+ em.persist(md);
167
+ }
168
+ }
169
+ pack.setMetadata(newMD);
159170 return Response.ok(pack).build();
160171 }
161172
....@@ -205,6 +216,19 @@
205216
206217 em.persist(currentPack);
207218
219
+ Set<PackMetadata> newMD = pack.getMetadata();
220
+ for (PackMetadata currentMd : currentPack.getMetadata()) {
221
+ if (newMD == null || !newMD.contains(currentMd));
222
+ em.remove(currentMd);
223
+ }
224
+
225
+ if (newMD != null) {
226
+ for (PackMetadata md : newMD) {
227
+ md.setPack(currentPack);
228
+ em.persist(md);
229
+ }
230
+ }
231
+ currentPack.setMetadata(newMD);
208232 return Response.ok(pack).build();
209233 }
210234
securis/src/main/resources/db/schema.sql
....@@ -48,11 +48,10 @@
4848
4949 drop table IF EXISTS licensetype_metadata;
5050 CREATE TABLE IF NOT EXISTS licensetype_metadata (
51
- id INT NOT NULL auto_increment,
5251 license_type_id INT NOT NULL ,
5352 key VARCHAR(100) NOT NULL ,
5453 value VARCHAR(200) NULL ,
55
- PRIMARY KEY (id));
54
+ PRIMARY KEY (license_type_id, key));
5655
5756 drop table IF EXISTS organization;
5857 CREATE TABLE IF NOT EXISTS organization (
....@@ -85,12 +84,11 @@
8584
8685 drop table IF EXISTS pack_metadata;
8786 CREATE TABLE IF NOT EXISTS pack_metadata (
88
- id INT NOT NULL auto_increment,
8987 pack_id INT NOT NULL ,
9088 key VARCHAR(100) NOT NULL ,
9189 value VARCHAR(200) NULL ,
9290 readonly BOOlEAN NOT NULL default false,
93
- PRIMARY KEY (id));
91
+ PRIMARY KEY (pack_id, key));
9492
9593
9694 drop table IF EXISTS license;
securis/src/main/resources/static/admin.html
....@@ -52,8 +52,8 @@
5252 <p ng-switch-when="readonly" class="form-control-static">{{formu[field.name]}}</p>
5353 <p ng-switch-when="readonly_date" class="form-control-static">{{formu[field.name] | date:'medium'}}</p>
5454 <select ng-switch-when="select" class="form-control" ng-required="field.mandatory" ng-model="formu[field.name]"
55
- ng-options="o.id as o.label for o in refs[field.name]" >
56
- <option selected="true" ng-if="!field.mandatory" value=""></option>
55
+ ng-options="o.id as o.label for o in refs[field.name]" ng-change="selectFieldChanged(field.onchange)">
56
+ <option value="-1" selected></option>
5757 </select>
5858 <select chosen multiple ng-switch-when="multiselect" class="form-control" ng-required="field.mandatory" ng-model="formu[field.name]"
5959 ng-options="o.id as o.label for o in refs[field.name]" data-placeholder="...">
securis/src/main/resources/static/js/admin.js
....@@ -66,7 +66,7 @@
6666 $scope.edit = function(data) {
6767 $scope.showForm = true;
6868 $scope.isNew = false;
69
- // Next line is a wirkaround due to some issues with values with ID == 0
69
+ // Next line is a workaround due to some issues with values with ID == 0
7070 $('select').val(null);
7171 $scope.formu = {}
7272 var fields = Catalogs.getMetadata().fields;
....@@ -130,6 +130,10 @@
130130 $scope.$parent.showForm = true;
131131 $('select').val(null);
132132 $scope.$parent.formu = {};
133
+
134
+ console.log("Refs:");
135
+ console.log($scope.refs);
136
+
133137 setTimeout(function() {
134138 $('#'+Catalogs.getFFF()).focus();
135139 }, 0);
....@@ -165,6 +169,11 @@
165169 }
166170 }
167171
172
+ $scope.selectFieldChanged = function(onchangehandler) {
173
+ if (onchangehandler) {
174
+ $scope[onchangehandler]();
175
+ }
176
+ }
168177 // Metadata management
169178
170179 $scope.createMetadataRow = function() {
....@@ -176,6 +185,14 @@
176185 $scope.removeMetadataKey = function(row_md) {
177186 $scope.formu.metadata.splice( $scope.formu.metadata.indexOf(row_md), 1 );
178187 }
188
+ $scope.updateMetadata = function() {
189
+ // Called when Application ID change in current field
190
+ var newAppId = $scope.formu['application_id'];
191
+ console.log('Ready to get metadata from application: ' + newAppId);
192
+ Catalogs.getResource('application').get({appId: newAppId}).$promise.then(function(app) {
193
+ $scope.formu.metadata = app.metadata;
194
+ });
195
+ }
179196
180197 } ]);
181198
securis/src/main/resources/static/js/catalogs.json
....@@ -67,7 +67,8 @@
6767 "display" : "Application",
6868 "resource" : "application",
6969 "mandatory" : true,
70
- "type" : "select"
70
+ "type" : "select",
71
+ "onchange": "updateMetadata"
7172 }, {
7273 "name" : "creationTimestamp",
7374 "display" : "Creation date",
securis/src/main/resources/static/licenses.html
....@@ -88,8 +88,7 @@
8888 ng-required="mandatory.license_type_id"
8989 ng-model="pack.license_type_id"
9090 ng-options="o.id as o.label for o in refs.license_type_id">
91
- <option selected="true" ng-if="!mandatory.license_type_id"
92
- value=""></option>
91
+
9392 </select>
9493 <div class="alert inline-alert alert-warning"
9594 ng-show="packForm.license_type_id.$invalid">
....@@ -106,8 +105,6 @@
106105 <select class="form-control" ng-required="field.mandatory"
107106 ng-model="pack.organization_id"
108107 ng-options="o.id as o.label for o in refs.organization_id">
109
- <option selected="true" ng-if="!mandatory.organization_id"
110
- value=""></option>
111108 </select>
112109 <div class="alert inline-alert alert-warning"
113110 ng-show="packForm.organization_id.$invalid">
securis/src/main/resources/static/main.html
....@@ -41,7 +41,7 @@
4141 <script type="text/javascript"
4242 src="/js/vendor/bootstrap-dialog.js"></script>
4343 <script type="text/javascript"
44
- src="/js/angular/angular.min.js"></script>
44
+ src="/js/angular/angular.js"></script>
4545 <script type="text/javascript"
4646 src="/js/angular/angular-route.min.js"></script>
4747 <script type="text/javascript"