rsanchez
2017-03-24 4f5711b8ec555ab8307516ce178b454445d3833f
securis/src/main/java/net/curisit/securis/db/Pack.java
....@@ -20,8 +20,6 @@
2020 import javax.persistence.OneToMany;
2121 import javax.persistence.Table;
2222
23
-import net.curisit.integrity.commons.Utils;
24
-
2523 import org.hibernate.annotations.Type;
2624
2725 import com.fasterxml.jackson.annotation.JsonAutoDetect;
....@@ -30,6 +28,8 @@
3028 import com.fasterxml.jackson.annotation.JsonInclude;
3129 import com.fasterxml.jackson.annotation.JsonInclude.Include;
3230 import com.fasterxml.jackson.annotation.JsonProperty;
31
+
32
+import net.curisit.integrity.commons.Utils;
3333
3434 /**
3535 * Entity implementation class for Entity: pack
....@@ -40,358 +40,360 @@
4040 @Entity
4141 @Table(name = "pack")
4242 @JsonIgnoreProperties(ignoreUnknown = true)
43
-@NamedQueries({
44
- @NamedQuery(name = "list-packs", query = "SELECT pa FROM Pack pa"),//
45
- @NamedQuery(name = "pack-by-code", query = "SELECT pa FROM Pack pa where pa.code = :code"),//
46
- @NamedQuery(name = "list-packs-by-orgs", query = "SELECT pa FROM Pack pa where pa.organization.id in :list_ids")
47
-})
43
+@NamedQueries({ @NamedQuery(name = "list-packs", query = "SELECT pa FROM Pack pa"), //
44
+ @NamedQuery(name = "pack-by-code", query = "SELECT pa FROM Pack pa where pa.code = :code"), //
45
+ @NamedQuery(name = "list-packs-by-lic-type", query = "SELECT pa FROM Pack pa where pa.licenseType.id = :lt_id"), //
46
+ @NamedQuery(name = "list-packs-by-orgs", query = "SELECT pa FROM Pack pa where pa.organization.id in :list_ids") })
4847 public class Pack implements Serializable {
4948
50
- private static final long serialVersionUID = 1L;
49
+ private static final long serialVersionUID = 1L;
5150
52
- @Id
53
- @GeneratedValue
54
- private Integer id;
51
+ @Id
52
+ @GeneratedValue
53
+ private Integer id;
5554
56
- private String code;
55
+ private String code;
5756
58
- private String comments;
57
+ private String comments;
5958
60
- @Column(name = "creation_timestamp")
61
- @JsonProperty("creation_timestamp")
62
- private Date creationTimestamp;
59
+ @Column(name = "creation_timestamp")
60
+ @JsonProperty("creation_timestamp")
61
+ private Date creationTimestamp;
6362
64
- @JsonIgnore
65
- @ManyToOne
66
- @JoinColumn(name = "organization_id")
67
- private Organization organization;
63
+ @JsonIgnore
64
+ @ManyToOne
65
+ @JoinColumn(name = "organization_id")
66
+ private Organization organization;
6867
69
- @JsonIgnore
70
- @ManyToOne
71
- @JoinColumn(name = "license_type_id")
72
- private LicenseType licenseType;
68
+ @JsonIgnore
69
+ @ManyToOne
70
+ @JoinColumn(name = "license_type_id")
71
+ private LicenseType licenseType;
7372
74
- @JsonIgnore
75
- @ManyToOne
76
- @JoinColumn(name = "created_by")
77
- private User createdBy;
73
+ @JsonIgnore
74
+ @ManyToOne
75
+ @JoinColumn(name = "created_by")
76
+ private User createdBy;
7877
79
- @JsonIgnore
80
- @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "pack")
81
- private Set<License> licenses;
78
+ @JsonIgnore
79
+ @OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.REFRESH }, mappedBy = "pack")
80
+ private Set<License> licenses;
8281
83
- @Column(name = "num_licenses")
84
- @JsonProperty("num_licenses")
85
- private int numLicenses;
82
+ @Column(name = "num_licenses")
83
+ @JsonProperty("num_licenses")
84
+ private int numLicenses;
8685
87
- @Column(name = "init_valid_date")
88
- @JsonProperty("init_valid_date")
89
- private Date initValidDate;
86
+ @Column(name = "init_valid_date")
87
+ @JsonProperty("init_valid_date")
88
+ private Date initValidDate;
9089
91
- @Column(name = "end_valid_date")
92
- @JsonProperty("end_valid_date")
93
- private Date endValidDate;
90
+ @Column(name = "end_valid_date")
91
+ @JsonProperty("end_valid_date")
92
+ private Date endValidDate;
9493
95
- @Type(type = "net.curisit.securis.db.common.PackStatusType")
96
- private PackStatus status;
94
+ @Type(type = "net.curisit.securis.db.common.PackStatusType")
95
+ private PackStatus status;
9796
98
- @Column(name = "license_preactivation")
99
- @JsonProperty("license_preactivation")
100
- private boolean licensePreactivation;
97
+ @Column(name = "license_preactivation")
98
+ @JsonProperty("license_preactivation")
99
+ private boolean licensePreactivation;
101100
102
- @Column(name = "preactivation_valid_period")
103
- @JsonProperty("preactivation_valid_period")
104
- private Integer preactivationValidPeriod;
101
+ @Column(name = "preactivation_valid_period")
102
+ @JsonProperty("preactivation_valid_period")
103
+ private Integer preactivationValidPeriod;
105104
106
- @Column(name = "renew_valid_period")
107
- @JsonProperty("renew_valid_period")
108
- private Integer renewValidPeriod;
105
+ @Column(name = "renew_valid_period")
106
+ @JsonProperty("renew_valid_period")
107
+ private Integer renewValidPeriod;
109108
110
- @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "pack")
111
- private Set<PackMetadata> metadata;
109
+ @OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.REFRESH }, mappedBy = "pack")
110
+ private Set<PackMetadata> metadata;
112111
113
- public Integer getId() {
114
- return id;
115
- }
112
+ public Integer getId() {
113
+ return id;
114
+ }
116115
117
- public void setId(Integer id) {
118
- this.id = id;
119
- }
116
+ public void setId(Integer id) {
117
+ this.id = id;
118
+ }
120119
121
- public String getCode() {
122
- return code;
123
- }
120
+ public String getCode() {
121
+ return code;
122
+ }
124123
125
- public void setCode(String code) {
126
- this.code = code;
127
- }
124
+ public void setCode(String code) {
125
+ this.code = code;
126
+ }
128127
129
- public Date getCreationTimestamp() {
130
- return creationTimestamp;
131
- }
128
+ public Date getCreationTimestamp() {
129
+ return creationTimestamp;
130
+ }
132131
133
- public void setCreationTimestamp(Date creationTimestamp) {
134
- this.creationTimestamp = creationTimestamp;
135
- }
132
+ public void setCreationTimestamp(Date creationTimestamp) {
133
+ this.creationTimestamp = creationTimestamp;
134
+ }
136135
137
- public Organization getOrganization() {
138
- return organization;
139
- }
136
+ public Organization getOrganization() {
137
+ return organization;
138
+ }
140139
141
- public void setOrganization(Organization organization) {
142
- this.organization = organization;
143
- }
140
+ public void setOrganization(Organization organization) {
141
+ this.organization = organization;
142
+ }
144143
145
- public LicenseType getLicenseType() {
146
- return licenseType;
147
- }
144
+ public LicenseType getLicenseType() {
145
+ return licenseType;
146
+ }
148147
149
- public void setLicenseType(LicenseType licenseType) {
150
- this.licenseType = licenseType;
151
- }
148
+ public void setLicenseType(LicenseType licenseType) {
149
+ this.licenseType = licenseType;
150
+ }
152151
153
- public User getCreatedBy() {
154
- return createdBy;
155
- }
152
+ public User getCreatedBy() {
153
+ return createdBy;
154
+ }
156155
157
- public void setCreatedBy(User createdBy) {
158
- this.createdBy = createdBy;
159
- }
156
+ public void setCreatedBy(User createdBy) {
157
+ this.createdBy = createdBy;
158
+ }
160159
161
- public int getNumLicenses() {
162
- return numLicenses;
163
- }
160
+ public int getNumLicenses() {
161
+ return numLicenses;
162
+ }
164163
165
- public void setNumLicenses(int numLicenses) {
166
- this.numLicenses = numLicenses;
167
- }
164
+ public void setNumLicenses(int numLicenses) {
165
+ this.numLicenses = numLicenses;
166
+ }
168167
169
- @JsonProperty("num_activations")
170
- public int getNumActivations() {
171
- if (licenses == null) {
172
- return 0;
173
- }
174
- int num = 0;
175
- for (License lic : licenses) {
176
- if (lic.getStatus() == LicenseStatus.ACTIVE || lic.getStatus() == LicenseStatus.PRE_ACTIVE) {
177
- num++;
178
- }
179
- }
180
- return num;
181
- }
168
+ @JsonProperty("num_activations")
169
+ public int getNumActivations() {
170
+ if (licenses == null) {
171
+ return 0;
172
+ }
173
+ int num = 0;
174
+ for (License lic : licenses) {
175
+ if (lic.getStatus() == LicenseStatus.ACTIVE || lic.getStatus() == LicenseStatus.PRE_ACTIVE) {
176
+ num++;
177
+ }
178
+ }
179
+ return num;
180
+ }
182181
183
- /**
184
- * Counts all created licenses, It counts active licenses and licenses
185
- * waiting for activation This number will be used to control the max number
186
- * of licenses created. Ignore canceled licenses.
187
- *
188
- * @return
189
- */
190
- @JsonProperty("num_creations")
191
- public int getNumCreations() {
192
- if (licenses == null) {
193
- return 0;
194
- }
195
- int num = 0;
196
- for (License lic : licenses) {
197
- if (lic.getStatus() != LicenseStatus.CANCELLED) {
198
- num++;
199
- }
200
- }
201
- return num;
202
- }
182
+ /**
183
+ * Counts all created licenses, It counts active licenses and licenses
184
+ * waiting for activation This number will be used to control the max number
185
+ * of licenses created. Ignore canceled licenses.
186
+ *
187
+ * @return
188
+ */
189
+ @JsonProperty("num_creations")
190
+ public int getNumCreations() {
191
+ if (licenses == null) {
192
+ return 0;
193
+ }
194
+ int num = 0;
195
+ for (License lic : licenses) {
196
+ if (lic.getStatus() != LicenseStatus.CANCELLED) {
197
+ num++;
198
+ }
199
+ }
200
+ return num;
201
+ }
203202
204
- /**
205
- * Number of available licenses in this pack
206
- *
207
- * @return
208
- */
209
- @JsonProperty("num_available")
210
- public int getNumAvailables() {
211
- return numLicenses - getNumActivations();
212
- }
203
+ /**
204
+ * Number of available licenses in this pack
205
+ *
206
+ * @return
207
+ */
208
+ @JsonProperty("num_available")
209
+ public int getNumAvailables() {
210
+ return numLicenses - getNumActivations();
211
+ }
213212
214
- @JsonProperty("organization_name")
215
- public String getOrgName() {
216
- return organization == null ? null : organization.getName();
217
- }
213
+ @JsonProperty("organization_name")
214
+ public String getOrgName() {
215
+ return organization == null ? null : organization.getName();
216
+ }
218217
219
- @JsonProperty("application_name")
220
- public String getAppName() {
221
- if (licenseType == null) {
222
- return null;
223
- }
224
- Application app = licenseType.getApplication();
225
- return app == null ? null : app.getName();
226
- }
218
+ @JsonProperty("application_name")
219
+ public String getAppName() {
220
+ if (licenseType == null) {
221
+ return null;
222
+ }
223
+ Application app = licenseType.getApplication();
224
+ return app == null ? null : app.getName();
225
+ }
227226
228
- @JsonProperty("organization_id")
229
- public Integer getOrgId() {
230
- return organization == null ? null : organization.getId();
231
- }
227
+ @JsonProperty("organization_id")
228
+ public Integer getOrgId() {
229
+ return organization == null ? null : organization.getId();
230
+ }
232231
233
- @JsonProperty("organization_id")
234
- public void setOrgId(Integer idOrg) {
235
- if (idOrg == null) {
236
- organization = null;
237
- } else {
238
- organization = new Organization();
239
- organization.setId(idOrg);
240
- }
241
- }
232
+ @JsonProperty("organization_id")
233
+ public void setOrgId(Integer idOrg) {
234
+ if (idOrg == null) {
235
+ organization = null;
236
+ } else {
237
+ organization = new Organization();
238
+ organization.setId(idOrg);
239
+ }
240
+ }
242241
243
- @JsonProperty("license_type_id")
244
- public void setLicTypeId(Integer idLT) {
245
- if (idLT == null) {
246
- licenseType = null;
247
- } else {
248
- licenseType = new LicenseType();
249
- licenseType.setId(idLT);
250
- }
251
- }
242
+ @JsonProperty("license_type_id")
243
+ public void setLicTypeId(Integer idLT) {
244
+ if (idLT == null) {
245
+ licenseType = null;
246
+ } else {
247
+ licenseType = new LicenseType();
248
+ licenseType.setId(idLT);
249
+ }
250
+ }
252251
253
- @JsonProperty("license_type_id")
254
- public Integer getLicTypeId() {
255
- return licenseType == null ? null : licenseType.getId();
256
- }
252
+ @JsonProperty("license_type_id")
253
+ public Integer getLicTypeId() {
254
+ return licenseType == null ? null : licenseType.getId();
255
+ }
257256
258
- @JsonProperty("created_by_id")
259
- public String getCreatedById() {
260
- return createdBy == null ? null : createdBy.getUsername();
261
- }
257
+ @JsonProperty("created_by_id")
258
+ public String getCreatedById() {
259
+ return createdBy == null ? null : createdBy.getUsername();
260
+ }
262261
263
- @JsonProperty("created_by_id")
264
- public void setCreatedById(String username) {
265
- createdBy = new User();
266
- createdBy.setUsername(username);
267
- }
262
+ @JsonProperty("created_by_id")
263
+ public void setCreatedById(String username) {
264
+ createdBy = new User();
265
+ createdBy.setUsername(username);
266
+ }
268267
269
- @JsonProperty("created_by_name")
270
- public String getCreatedByname() {
271
- return createdBy == null ? null : String.format("%s %s (%s)", createdBy.getFirstName(),
272
- createdBy.getLastName() != null ? createdBy.getLastName() : "", createdBy.getUsername());
273
- }
268
+ @JsonProperty("created_by_name")
269
+ public String getCreatedByname() {
270
+ return createdBy == null ? null
271
+ : String.format("%s %s (%s)", createdBy.getFirstName(), createdBy.getLastName() != null ? createdBy.getLastName() : "", createdBy.getUsername());
272
+ }
274273
275
- @JsonProperty("licensetype_code")
276
- public String getLicenseTypeCode() {
277
- return licenseType == null ? null : licenseType.getCode();
278
- }
274
+ @JsonProperty("licensetype_code")
275
+ public String getLicenseTypeCode() {
276
+ return licenseType == null ? null : licenseType.getCode();
277
+ }
279278
280
- public String getComments() {
281
- return comments;
282
- }
279
+ public String getComments() {
280
+ return comments;
281
+ }
283282
284
- public void setComments(String comments) {
285
- this.comments = comments;
286
- }
283
+ public void setComments(String comments) {
284
+ this.comments = comments;
285
+ }
287286
288
- public boolean isLicensePreactivation() {
289
- return licensePreactivation;
290
- }
287
+ public boolean isLicensePreactivation() {
288
+ return licensePreactivation;
289
+ }
291290
292
- public void setLicensePreactivation(boolean licensePreactivation) {
293
- this.licensePreactivation = licensePreactivation;
294
- }
291
+ public void setLicensePreactivation(boolean licensePreactivation) {
292
+ this.licensePreactivation = licensePreactivation;
293
+ }
295294
296
- public Set<PackMetadata> getMetadata() {
297
- return metadata;
298
- }
295
+ public Set<PackMetadata> getMetadata() {
296
+ return metadata;
297
+ }
299298
300
- public void setMetadata(Set<PackMetadata> metadata) {
301
- this.metadata = metadata;
302
- }
299
+ public void setMetadata(Set<PackMetadata> metadata) {
300
+ this.metadata = metadata;
301
+ }
303302
304
- public PackStatus getStatus() {
305
- return status;
306
- }
303
+ public PackStatus getStatus() {
304
+ return status;
305
+ }
307306
308
- public void setStatus(PackStatus status) {
309
- this.status = status;
310
- }
307
+ public void setStatus(PackStatus status) {
308
+ this.status = status;
309
+ }
311310
312
- public Date getInitValidDate() {
313
- return initValidDate;
314
- }
311
+ public Date getInitValidDate() {
312
+ return initValidDate;
313
+ }
315314
316
- public void setInitValidDate(Date initValidDate) {
317
- this.initValidDate = initValidDate;
318
- }
315
+ public void setInitValidDate(Date initValidDate) {
316
+ this.initValidDate = initValidDate;
317
+ }
319318
320
- public Date getEndValidDate() {
321
- return endValidDate;
322
- }
319
+ public Date getEndValidDate() {
320
+ return endValidDate;
321
+ }
323322
324
- public void setEndValidDate(Date endValidDate) {
325
- this.endValidDate = endValidDate;
326
- }
323
+ public void setEndValidDate(Date endValidDate) {
324
+ this.endValidDate = endValidDate;
325
+ }
327326
328
- public Set<License> getLicenses() {
329
- return licenses;
330
- }
327
+ public Set<License> getLicenses() {
328
+ return licenses;
329
+ }
331330
332
- public void setLicenses(Set<License> licenses) {
333
- this.licenses = licenses;
334
- }
331
+ public void setLicenses(Set<License> licenses) {
332
+ this.licenses = licenses;
333
+ }
335334
336
- @Override
337
- public boolean equals(Object obj) {
338
- if (!(obj instanceof Pack))
339
- return false;
340
- Pack other = (Pack) obj;
341
- return id.equals(other.id);
342
- }
335
+ public Integer getPreactivationValidPeriod() {
336
+ return preactivationValidPeriod;
337
+ }
343338
344
- @Override
345
- public int hashCode() {
339
+ public void setPreactivationValidPeriod(Integer preactivationValidPeriod) {
340
+ this.preactivationValidPeriod = preactivationValidPeriod;
341
+ }
346342
347
- return (id == null ? 0 : id.hashCode());
348
- }
343
+ public Integer getRenewValidPeriod() {
344
+ return renewValidPeriod;
345
+ }
349346
350
- public Integer getPreactivationValidPeriod() {
351
- return preactivationValidPeriod;
352
- }
347
+ public void setRenewValidPeriod(Integer renewValidPeriod) {
348
+ this.renewValidPeriod = renewValidPeriod;
349
+ }
353350
354
- public void setPreactivationValidPeriod(Integer preactivationValidPeriod) {
355
- this.preactivationValidPeriod = preactivationValidPeriod;
356
- }
351
+ @Override
352
+ public boolean equals(Object obj) {
353
+ if (!(obj instanceof Application))
354
+ return false;
355
+ return id.equals(Pack.class.cast(obj).id);
356
+ }
357357
358
- public Integer getRenewValidPeriod() {
359
- return renewValidPeriod;
360
- }
358
+ @Override
359
+ public int hashCode() {
360
+ return (id == null ? 0 : id.hashCode());
361
+ }
361362
362
- public void setRenewValidPeriod(Integer renewValidPeriod) {
363
- this.renewValidPeriod = renewValidPeriod;
364
- }
363
+ @Override
364
+ public String toString() {
365
+ return String.format("Pack: ID: %d, code: %s", id, code);
366
+ }
365367
366
- public static class Action {
367
- public static final int CREATE = 1;
368
- public static final int ACTIVATION = 2;
369
- public static final int PUT_ONHOLD = 3;
370
- public static final int CANCEL = 4;
371
- public static final int DELETE = 5;
372
- }
368
+ public static class Action {
369
+ public static final int CREATE = 1;
370
+ public static final int ACTIVATION = 2;
371
+ public static final int PUT_ONHOLD = 3;
372
+ public static final int CANCEL = 4;
373
+ public static final int DELETE = 5;
374
+ }
373375
374
- public static class Status {
376
+ public static class Status {
375377
376
- private static final Map<Integer, List<PackStatus>> transitions = Utils.createMap( //
377
- Action.ACTIVATION, Arrays.asList(PackStatus.CREATED, PackStatus.ON_HOLD, PackStatus.EXPIRED), //
378
- Action.PUT_ONHOLD, Arrays.asList(PackStatus.ACTIVE), //
379
- Action.CANCEL, Arrays.asList(PackStatus.ACTIVE, PackStatus.ON_HOLD, PackStatus.EXPIRED), //
380
- Action.DELETE, Arrays.asList(PackStatus.CANCELLED, PackStatus.CREATED) //
381
- );
378
+ private static final Map<Integer, List<PackStatus>> transitions = Utils.createMap( //
379
+ Action.ACTIVATION, Arrays.asList(PackStatus.CREATED, PackStatus.ON_HOLD, PackStatus.EXPIRED), //
380
+ Action.PUT_ONHOLD, Arrays.asList(PackStatus.ACTIVE), //
381
+ Action.CANCEL, Arrays.asList(PackStatus.ACTIVE, PackStatus.ON_HOLD, PackStatus.EXPIRED), //
382
+ Action.DELETE, Arrays.asList(PackStatus.CANCELLED, PackStatus.CREATED) //
383
+ );
382384
383
- /**
384
- * It checks if a given action is valid for the License, passing the
385
- * action and the current license status
386
- *
387
- * @param oldStatus
388
- * @param newStatus
389
- * @return
390
- */
391
- public static boolean isActionValid(Integer action, PackStatus currentStatus) {
392
- List<PackStatus> validStatuses = transitions.get(action);
385
+ /**
386
+ * It checks if a given action is valid for the License, passing the
387
+ * action and the current license status
388
+ *
389
+ * @param oldStatus
390
+ * @param newStatus
391
+ * @return
392
+ */
393
+ public static boolean isActionValid(Integer action, PackStatus currentStatus) {
394
+ List<PackStatus> validStatuses = transitions.get(action);
393395
394
- return validStatuses != null && validStatuses.contains(currentStatus);
395
- }
396
- }
396
+ return validStatuses != null && validStatuses.contains(currentStatus);
397
+ }
398
+ }
397399 }