securis/src/main/java/net/curisit/securis/db/License.java
.. .. @@ -16,6 +16,7 @@ 16 16 import javax.persistence.ManyToOne; 17 17 import javax.persistence.NamedQueries; 18 18 import javax.persistence.NamedQuery; 19 +import javax.persistence.NoResultException;19 20 import javax.persistence.NonUniqueResultException; 20 21 import javax.persistence.OneToMany; 21 22 import javax.persistence.Table; .. .. @@ -349,6 +350,9 @@ 349 350 LOG.error("There are more than 1 active license for request data: {}\nHash: {}", requestData, BlockedRequest.generateHash(requestData)); 350 351 throw new SeCurisServiceException(ErrorCodes.DUPLICATED_REQUEST_DATA, "There are more than 1 active license for request data hash: " 351 352 + BlockedRequest.generateHash(requestData)); 353 + } catch (NoResultException e) {354 + // There is no license for request data355 + return null;352 356 } 353 357 } 354 358 securis/src/main/java/net/curisit/securis/db/LicenseHistory.java
.. .. @@ -3,6 +3,7 @@ 3 3 import java.io.Serializable; 4 4 import java.util.Date; 5 5 6 +import javax.persistence.Column;6 7 import javax.persistence.Entity; 7 8 import javax.persistence.GeneratedValue; 8 9 import javax.persistence.Id; .. .. @@ -52,7 +53,9 @@ 52 53 private String action; 53 54 private String comments; 54 55 55 - private Date timestamp;56 + @Column(name = "creation_timestamp")57 + @JsonProperty("creation_timestamp")58 + private Date creationTimestamp;56 59 57 60 public int getId() { 58 61 return id; .. .. @@ -95,18 +98,18 @@ 95 98 this.comments = comments; 96 99 } 97 100 98 - public Date getTimestamp() {99 - return timestamp;100 - }101 -102 - public void setTimestamp(Date timestamp) {103 - this.timestamp = timestamp;104 - }105 -106 101 public void setId(int id) { 107 102 this.id = id; 108 103 } 109 104 105 + public Date getCreationTimestamp() {106 + return creationTimestamp;107 + }108 +109 + public void setCreationTimestamp(Date creationTimestamp) {110 + this.creationTimestamp = creationTimestamp;111 + }112 +110 113 public static class Actions { 111 114 public static final String CREATE = "creation"; 112 115 public static final String ADD_REQUEST = "request"; securis/src/main/java/net/curisit/securis/services/ApplicationResource.java
.. .. @@ -154,22 +154,27 @@ 154 154 } 155 155 currentapp.setName(app.getName()); 156 156 currentapp.setDescription(app.getDescription()); 157 - em.persist(currentapp);157 + em.merge(currentapp);158 158 159 159 Set<ApplicationMetadata> newMD = app.getApplicationMetadata(); 160 160 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 + }164 164 } 165 165 166 166 if (newMD != null) { 167 + Set<ApplicationMetadata> oldMD = currentapp.getApplicationMetadata();167 168 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);171 177 } 172 - em.persist(md);173 178 } 174 179 } 175 180 currentapp.setApplicationMetadata(app.getApplicationMetadata()); securis/src/main/java/net/curisit/securis/services/LicenseResource.java
.. .. @@ -358,11 +358,11 @@ 358 358 try { 359 359 lic.setRequestData(JsonUtils.toJSON((RequestBean) signedLicense)); 360 360 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");362 362 } 363 363 lic.setLicenseData(JsonUtils.toJSON(signedLicense)); 364 364 } catch (SeCurisException e) { 365 - LOG.error("Error generaing license JSON", e);365 + LOG.error("Error generating license JSON", e);366 366 throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Error generating license JSON"); 367 367 } 368 368 } else { .. .. @@ -371,11 +371,15 @@ 371 371 lic.setCreatedBy(createdBy); 372 372 lic.setCreationTimestamp(new Date()); 373 373 lic.setModificationTimestamp(lic.getCreationTimestamp()); 374 + LOG.info("LICENSE: {}", lic);374 375 em.persist(lic); 376 + LOG.info("LICENSE on HISTORY create");375 377 em.persist(licenseHelper.createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE)); 376 378 if (lic.getStatus() == LicenseStatus.ACTIVE) { 379 + LOG.info("LICENSE ACTIVATION on HISTORY create");377 380 em.persist(licenseHelper.createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE, "Activated on creation")); 378 381 } 382 + LOG.info("LICENSE created oK ??");379 383 380 384 return Response.ok(lic).build(); 381 385 } securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java
.. .. @@ -132,8 +132,8 @@ 132 132 133 133 lt.setCreationTimestamp(new Date()); 134 134 em.persist(lt); 135 - Set<LicenseTypeMetadata> newMD = lt.getMetadata();136 -135 + Set<LicenseTypeMetadata> newMD = lt.getMetadata();136 +137 137 if (newMD != null) { 138 138 for (LicenseTypeMetadata md : newMD) { 139 139 md.setLicenseType(lt); .. .. @@ -174,21 +174,29 @@ 174 174 currentlt.setCode(lt.getCode()); 175 175 currentlt.setName(lt.getName()); 176 176 currentlt.setDescription(lt.getDescription()); 177 - em.persist(currentlt);178 -179 - Set<LicenseTypeMetadata> newMD = lt.getMetadata();177 +178 + Set<LicenseTypeMetadata> newMD = lt.getMetadata();180 179 for (LicenseTypeMetadata currentMd : currentlt.getMetadata()) { 181 - if (newMD == null || !newMD.contains(currentMd));180 + if (newMD == null || !newMD.contains(currentMd)) {182 181 em.remove(currentMd); 182 + LOG.info("Removing MD: {}", currentMd);183 + }183 184 } 184 -185 +185 186 if (newMD != null) { 187 + Set<LicenseTypeMetadata> oldMD = currentlt.getMetadata();188 +186 189 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 + }189 196 } 190 197 } 191 198 currentlt.setMetadata(newMD); 199 + em.merge(currentlt);192 200 193 201 return Response.ok(currentlt).build(); 194 202 } securis/src/main/java/net/curisit/securis/services/PackResource.java
.. .. @@ -225,23 +225,28 @@ 225 225 currentPack.setComments(pack.getComments()); 226 226 currentPack.setNumLicenses(pack.getNumLicenses()); 227 227 228 - em.persist(currentPack);229 -230 228 Set<PackMetadata> newMD = pack.getMetadata(); 231 229 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 + }235 233 } 236 234 237 235 if (newMD != null) { 236 + Set<PackMetadata> oldMD = currentPack.getMetadata();238 237 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 + }241 244 } 242 245 } 243 246 currentPack.setMetadata(newMD); 244 - return Response.ok(pack).build();247 + em.merge(currentPack);248 +249 + return Response.ok(currentPack).build();245 250 } 246 251 247 252 @POST securis/src/main/java/net/curisit/securis/services/helpers/LicenseHelper.java
.. .. @@ -45,7 +45,7 @@ 45 45 LicenseHistory lh = new LicenseHistory(); 46 46 lh.setLicense(lic); 47 47 lh.setUser(user); 48 - lh.setTimestamp(new Date());48 + lh.setCreationTimestamp(new Date());49 49 lh.setAction(action); 50 50 lh.setComments(comments); 51 51 return lh; securis/src/main/resources/META-INF/persistence.xml
.. .. @@ -14,8 +14,9 @@ 14 14 <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" /> 15 15 <property name="hibernate.connection.datasource" value="java:comp/env/jdbc/SeCurisDS" /> 16 16 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"/>19 20 --> 20 21 </properties> 21 22 securis/src/main/resources/log4j2.xml
.. .. @@ -23,7 +23,6 @@ 23 23 <Console name="console" target="SYSTEM_OUT"> 24 24 <PatternLayout pattern="%m%n" /> 25 25 </Console> 26 -27 26 </Appenders> 28 27 <Loggers> 29 28