rsanchez
2014-10-23 c4d513ca26fe80946a5d90264de5d8e13e4ea974
securis/src/main/java/net/curisit/securis/db/License.java
....@@ -16,11 +16,17 @@
1616 import javax.persistence.ManyToOne;
1717 import javax.persistence.NamedQueries;
1818 import javax.persistence.NamedQuery;
19
+import javax.persistence.NonUniqueResultException;
1920 import javax.persistence.OneToMany;
2021 import javax.persistence.Table;
2122 import javax.persistence.TypedQuery;
2223
2324 import net.curisit.integrity.commons.Utils;
25
+import net.curisit.securis.services.exception.SeCurisServiceException;
26
+import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes;
27
+
28
+import org.apache.logging.log4j.LogManager;
29
+import org.apache.logging.log4j.Logger;
2430
2531 import com.fasterxml.jackson.annotation.JsonAutoDetect;
2632 import com.fasterxml.jackson.annotation.JsonIgnore;
....@@ -39,11 +45,13 @@
3945 @JsonIgnoreProperties(ignoreUnknown = true)
4046 @NamedQueries({
4147 @NamedQuery(name = "list-licenses-by-pack", query = "SELECT l FROM License l where l.pack.id = :packId"),
48
+ @NamedQuery(name = "list-licenses-by-req-data", query = "SELECT l FROM License l where l.reqDataHash = :hash"),
4249 @NamedQuery(name = "list-active-licenses-by-req-data", query = "SELECT l FROM License l where l.reqDataHash = :hash and l.status in ('AC', 'PA')")
4350 })
4451 public class License implements Serializable {
52
+ private static final long serialVersionUID = 2700310404904877227L;
4553
46
- private static final long serialVersionUID = 1L;
54
+ private static final Logger LOG = LogManager.getLogger(License.class);
4755
4856 @Id
4957 @GeneratedValue
....@@ -63,8 +71,8 @@
6371
6472 @JsonIgnore
6573 @ManyToOne
66
- @JoinColumn(name = "canceled_by")
67
- private User canceledBy;
74
+ @JoinColumn(name = "cancelled_by")
75
+ private User cancelledBy;
6876
6977 private LicenseStatus status;
7078
....@@ -78,6 +86,10 @@
7886 @JsonProperty("request_data")
7987 private String requestData;
8088
89
+ /**
90
+ * request data hash is automatically set when we use
91
+ * {@link License#setRequestData(String)} method
92
+ */
8193 @Column(name = "request_data_hash")
8294 @JsonIgnore
8395 private String reqDataHash;
....@@ -158,18 +170,18 @@
158170 }
159171 }
160172
161
- @JsonProperty("canceled_by_id")
162
- public String getCanceledById() {
163
- return canceledBy == null ? null : canceledBy.getUsername();
173
+ @JsonProperty("cancelled_by_id")
174
+ public String getCancelledById() {
175
+ return cancelledBy == null ? null : cancelledBy.getUsername();
164176 }
165177
166
- @JsonProperty("canceled_by_id")
167
- public void setCanceledById(String username) {
178
+ @JsonProperty("cancelled_by_id")
179
+ public void setCancelledById(String username) {
168180 if (username == null) {
169
- canceledBy = null;
181
+ cancelledBy = null;
170182 } else {
171
- canceledBy = new User();
172
- canceledBy.setUsername(username);
183
+ cancelledBy = new User();
184
+ cancelledBy.setUsername(username);
173185 }
174186 }
175187
....@@ -229,12 +241,12 @@
229241 this.id = id;
230242 }
231243
232
- public User getCanceledBy() {
233
- return canceledBy;
244
+ public User getCancelledBy() {
245
+ return cancelledBy;
234246 }
235247
236
- public void setCanceledBy(User canceledBy) {
237
- this.canceledBy = canceledBy;
248
+ public void setCancelledBy(User cancelledBy) {
249
+ this.cancelledBy = cancelledBy;
238250 }
239251
240252 public Date getLastAccessTimestamp() {
....@@ -251,6 +263,7 @@
251263
252264 public void setRequestData(String requestData) {
253265 this.requestData = requestData;
266
+ this.reqDataHash = BlockedRequest.generateHash(this.requestData);
254267 }
255268
256269 public String getLicenseData() {
....@@ -283,6 +296,10 @@
283296
284297 public void setExpirationDate(Date expirationDate) {
285298 this.expirationDate = expirationDate;
299
+ }
300
+
301
+ public String getReqDataHash() {
302
+ return reqDataHash;
286303 }
287304
288305 public static class Action {
....@@ -323,17 +340,16 @@
323340 }
324341 }
325342
326
- public License findLicenseByRequestData(String requestData, EntityManager em) {
343
+ public static License findLicenseByRequestData(String requestData, EntityManager em) throws SeCurisServiceException {
327344 TypedQuery<License> query = em.createNamedQuery("list-active-licenses-by-req-data", License.class);
328345 query.setParameter("hash", BlockedRequest.generateHash(requestData));
329
- return null;
346
+ try {
347
+ return query.getSingleResult();
348
+ } catch (NonUniqueResultException e) {
349
+ LOG.error("There are more than 1 active license for request data: {}\nHash: {}", requestData, BlockedRequest.generateHash(requestData));
350
+ throw new SeCurisServiceException(ErrorCodes.DUPLICATED_REQUEST_DATA, "There are more than 1 active license for request data hash: "
351
+ + BlockedRequest.generateHash(requestData));
352
+ }
330353 }
331354
332
- public String getReqDataHash() {
333
- return reqDataHash;
334
- }
335
-
336
- public void setReqDataHash(String reqDataHash) {
337
- this.reqDataHash = reqDataHash;
338
- }
339355 }