rsanchez
2014-11-17 04f9a9d2dbd2d79bfffea9c5a6b7ba1b32c5f42b
#396 fix - Supporting MySQL, changed "persist()" by "merge()"
9 files modified
changed files
securis/src/main/java/net/curisit/securis/db/License.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/db/LicenseHistory.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/ApplicationResource.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/LicenseResource.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/java/net/curisit/securis/services/helpers/LicenseHelper.java patch | view | blame | history
securis/src/main/resources/META-INF/persistence.xml patch | view | blame | history
securis/src/main/resources/log4j2.xml patch | view | blame | history
securis/src/main/java/net/curisit/securis/db/License.java
....@@ -16,6 +16,7 @@
1616 import javax.persistence.ManyToOne;
1717 import javax.persistence.NamedQueries;
1818 import javax.persistence.NamedQuery;
19
+import javax.persistence.NoResultException;
1920 import javax.persistence.NonUniqueResultException;
2021 import javax.persistence.OneToMany;
2122 import javax.persistence.Table;
....@@ -349,6 +350,9 @@
349350 LOG.error("There are more than 1 active license for request data: {}\nHash: {}", requestData, BlockedRequest.generateHash(requestData));
350351 throw new SeCurisServiceException(ErrorCodes.DUPLICATED_REQUEST_DATA, "There are more than 1 active license for request data hash: "
351352 + BlockedRequest.generateHash(requestData));
353
+ } catch (NoResultException e) {
354
+ // There is no license for request data
355
+ return null;
352356 }
353357 }
354358
securis/src/main/java/net/curisit/securis/db/LicenseHistory.java
....@@ -3,6 +3,7 @@
33 import java.io.Serializable;
44 import java.util.Date;
55
6
+import javax.persistence.Column;
67 import javax.persistence.Entity;
78 import javax.persistence.GeneratedValue;
89 import javax.persistence.Id;
....@@ -52,7 +53,9 @@
5253 private String action;
5354 private String comments;
5455
55
- private Date timestamp;
56
+ @Column(name = "creation_timestamp")
57
+ @JsonProperty("creation_timestamp")
58
+ private Date creationTimestamp;
5659
5760 public int getId() {
5861 return id;
....@@ -95,18 +98,18 @@
9598 this.comments = comments;
9699 }
97100
98
- public Date getTimestamp() {
99
- return timestamp;
100
- }
101
-
102
- public void setTimestamp(Date timestamp) {
103
- this.timestamp = timestamp;
104
- }
105
-
106101 public void setId(int id) {
107102 this.id = id;
108103 }
109104
105
+ public Date getCreationTimestamp() {
106
+ return creationTimestamp;
107
+ }
108
+
109
+ public void setCreationTimestamp(Date creationTimestamp) {
110
+ this.creationTimestamp = creationTimestamp;
111
+ }
112
+
110113 public static class Actions {
111114 public static final String CREATE = "creation";
112115 public static final String ADD_REQUEST = "request";
securis/src/main/java/net/curisit/securis/services/ApplicationResource.java
....@@ -154,22 +154,27 @@
154154 }
155155 currentapp.setName(app.getName());
156156 currentapp.setDescription(app.getDescription());
157
- em.persist(currentapp);
157
+ em.merge(currentapp);
158158
159159 Set<ApplicationMetadata> newMD = app.getApplicationMetadata();
160160 for (ApplicationMetadata currentMd : currentapp.getApplicationMetadata()) {
161
- if (newMD == null || !newMD.contains(currentMd))
162
- ;
163
- em.remove(currentMd);
161
+ if (newMD == null || !newMD.contains(currentMd)) {
162
+ em.remove(currentMd);
163
+ }
164164 }
165165
166166 if (newMD != null) {
167
+ Set<ApplicationMetadata> oldMD = currentapp.getApplicationMetadata();
167168 for (ApplicationMetadata md : newMD) {
168
- md.setApplication(app);
169
- if (md.getCreationTimestamp() == null) {
170
- md.setCreationTimestamp(app.getCreationTimestamp());
169
+ if (oldMD.contains(md)) {
170
+ em.merge(md);
171
+ } else {
172
+ md.setApplication(app);
173
+ if (md.getCreationTimestamp() == null) {
174
+ md.setCreationTimestamp(app.getCreationTimestamp());
175
+ }
176
+ em.persist(md);
171177 }
172
- em.persist(md);
173178 }
174179 }
175180 currentapp.setApplicationMetadata(app.getApplicationMetadata());
securis/src/main/java/net/curisit/securis/services/LicenseResource.java
....@@ -358,11 +358,11 @@
358358 try {
359359 lic.setRequestData(JsonUtils.toJSON((RequestBean) signedLicense));
360360 if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) {
361
- throw new SeCurisServiceException(ErrorCodes.BLOCKED_REQUEST_DATA, "Given request data is blocked and cannot be activate");
361
+ throw new SeCurisServiceException(ErrorCodes.BLOCKED_REQUEST_DATA, "Given request data is blocked and cannot be activated");
362362 }
363363 lic.setLicenseData(JsonUtils.toJSON(signedLicense));
364364 } catch (SeCurisException e) {
365
- LOG.error("Error generaing license JSON", e);
365
+ LOG.error("Error generating license JSON", e);
366366 throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Error generating license JSON");
367367 }
368368 } else {
....@@ -371,11 +371,15 @@
371371 lic.setCreatedBy(createdBy);
372372 lic.setCreationTimestamp(new Date());
373373 lic.setModificationTimestamp(lic.getCreationTimestamp());
374
+ LOG.info("LICENSE: {}", lic);
374375 em.persist(lic);
376
+ LOG.info("LICENSE on HISTORY create");
375377 em.persist(licenseHelper.createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE));
376378 if (lic.getStatus() == LicenseStatus.ACTIVE) {
379
+ LOG.info("LICENSE ACTIVATION on HISTORY create");
377380 em.persist(licenseHelper.createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE, "Activated on creation"));
378381 }
382
+ LOG.info("LICENSE created oK ??");
379383
380384 return Response.ok(lic).build();
381385 }
securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java
....@@ -132,8 +132,8 @@
132132
133133 lt.setCreationTimestamp(new Date());
134134 em.persist(lt);
135
- Set<LicenseTypeMetadata> newMD = lt.getMetadata();
136
-
135
+ Set<LicenseTypeMetadata> newMD = lt.getMetadata();
136
+
137137 if (newMD != null) {
138138 for (LicenseTypeMetadata md : newMD) {
139139 md.setLicenseType(lt);
....@@ -174,21 +174,29 @@
174174 currentlt.setCode(lt.getCode());
175175 currentlt.setName(lt.getName());
176176 currentlt.setDescription(lt.getDescription());
177
- em.persist(currentlt);
178
-
179
- Set<LicenseTypeMetadata> newMD = lt.getMetadata();
177
+
178
+ Set<LicenseTypeMetadata> newMD = lt.getMetadata();
180179 for (LicenseTypeMetadata currentMd : currentlt.getMetadata()) {
181
- if (newMD == null || !newMD.contains(currentMd));
180
+ if (newMD == null || !newMD.contains(currentMd)) {
182181 em.remove(currentMd);
182
+ LOG.info("Removing MD: {}", currentMd);
183
+ }
183184 }
184
-
185
+
185186 if (newMD != null) {
187
+ Set<LicenseTypeMetadata> oldMD = currentlt.getMetadata();
188
+
186189 for (LicenseTypeMetadata md : newMD) {
187
- md.setLicenseType(currentlt);
188
- em.persist(md);
190
+ if (oldMD.contains(md)) {
191
+ em.merge(md);
192
+ } else {
193
+ md.setLicenseType(currentlt);
194
+ em.persist(md);
195
+ }
189196 }
190197 }
191198 currentlt.setMetadata(newMD);
199
+ em.merge(currentlt);
192200
193201 return Response.ok(currentlt).build();
194202 }
securis/src/main/java/net/curisit/securis/services/PackResource.java
....@@ -225,23 +225,28 @@
225225 currentPack.setComments(pack.getComments());
226226 currentPack.setNumLicenses(pack.getNumLicenses());
227227
228
- em.persist(currentPack);
229
-
230228 Set<PackMetadata> newMD = pack.getMetadata();
231229 for (PackMetadata currentMd : currentPack.getMetadata()) {
232
- if (newMD == null || !newMD.contains(currentMd))
233
- ;
234
- em.remove(currentMd);
230
+ if (newMD == null || !newMD.contains(currentMd)) {
231
+ em.remove(currentMd);
232
+ }
235233 }
236234
237235 if (newMD != null) {
236
+ Set<PackMetadata> oldMD = currentPack.getMetadata();
238237 for (PackMetadata md : newMD) {
239
- md.setPack(currentPack);
240
- em.persist(md);
238
+ if (oldMD.contains(md)) {
239
+ em.merge(md);
240
+ } else {
241
+ md.setPack(currentPack);
242
+ em.persist(md);
243
+ }
241244 }
242245 }
243246 currentPack.setMetadata(newMD);
244
- return Response.ok(pack).build();
247
+ em.merge(currentPack);
248
+
249
+ return Response.ok(currentPack).build();
245250 }
246251
247252 @POST
securis/src/main/java/net/curisit/securis/services/helpers/LicenseHelper.java
....@@ -45,7 +45,7 @@
4545 LicenseHistory lh = new LicenseHistory();
4646 lh.setLicense(lic);
4747 lh.setUser(user);
48
- lh.setTimestamp(new Date());
48
+ lh.setCreationTimestamp(new Date());
4949 lh.setAction(action);
5050 lh.setComments(comments);
5151 return lh;
securis/src/main/resources/META-INF/persistence.xml
....@@ -14,8 +14,9 @@
1414 <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
1515 <property name="hibernate.connection.datasource" value="java:comp/env/jdbc/SeCurisDS" />
1616
17
-<!-- <property name="hibernate.show_sql" value="true" />
18
- <property name="hibernate.format_sql" value="true"/>
17
+ <property name="hibernate.show_sql" value="true" />
18
+
19
+ <!-- <property name="hibernate.format_sql" value="true"/>
1920 -->
2021 </properties>
2122
securis/src/main/resources/log4j2.xml
....@@ -23,7 +23,6 @@
2323 <Console name="console" target="SYSTEM_OUT">
2424 <PatternLayout pattern="%m%n" />
2525 </Console>
26
-
2726 </Appenders>
2827 <Loggers>
2928