Roberto Sánchez
2014-01-17 c8eb07e8dc020346aaee0d859040ccabb79349bd
#395 feature - Changes in REST API for license system
5 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/LicenseType.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/services/LicenseTypeResource.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/UserResource.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/db/License.java
....@@ -14,6 +14,8 @@
1414 import javax.persistence.Table;
1515
1616 import org.codehaus.jackson.annotate.JsonAutoDetect;
17
+import org.codehaus.jackson.annotate.JsonIgnore;
18
+import org.codehaus.jackson.annotate.JsonProperty;
1719 import org.codehaus.jackson.map.annotate.JsonSerialize;
1820
1921 /**
....@@ -36,22 +38,45 @@
3638
3739 private String code;
3840
39
- @Column(name = "creation_timestamp")
40
- private Date creationTimestamp;
41
-
42
- @ManyToOne
43
- @JoinColumn(name = "organization_id")
44
- private Organization organization;
45
-
41
+ @JsonIgnore
4642 @ManyToOne
4743 @JoinColumn(name = "pack_id")
4844 private Pack pack;
4945
46
+ @JsonIgnore
5047 @ManyToOne
5148 @JoinColumn(name = "created_by")
5249 private User createdBy;
5350
54
- private int numLicenses;
51
+ @JsonIgnore
52
+ @ManyToOne
53
+ @JoinColumn(name = "canceled_by")
54
+ private User canceledBy;
55
+
56
+ private int status;
57
+
58
+ @JoinColumn(name = "full_name")
59
+ private String fullName;
60
+
61
+ private String email;
62
+
63
+ @Column(name = "creation_timestamp")
64
+ private Date creationTimestamp;
65
+
66
+ @Column(name = "modification_timestamp")
67
+ private Date modificationTimestamp;
68
+
69
+ @Column(name = "activation_timestamp")
70
+ private Date activationTimestamp;
71
+
72
+ @Column(name = "cancelation_timestamp")
73
+ private Date cancelationTimestamp;
74
+
75
+ @Column(name = "send_timestamp")
76
+ private Date sendTimestamp;
77
+
78
+ @Column(name = "last_access_timestamp")
79
+ private Date lastAccessTimestamp;
5580
5681 public int getId() {
5782 return id;
....@@ -73,28 +98,12 @@
7398 this.creationTimestamp = creationTimestamp;
7499 }
75100
76
- public Organization getOrganization() {
77
- return organization;
78
- }
79
-
80
- public void setOrganization(Organization organization) {
81
- this.organization = organization;
82
- }
83
-
84101 public User getCreatedBy() {
85102 return createdBy;
86103 }
87104
88105 public void setCreatedBy(User createdBy) {
89106 this.createdBy = createdBy;
90
- }
91
-
92
- public int getNumLicenses() {
93
- return numLicenses;
94
- }
95
-
96
- public void setNumLicenses(int numLicenses) {
97
- this.numLicenses = numLicenses;
98107 }
99108
100109 public Pack getPack() {
....@@ -105,4 +114,136 @@
105114 this.pack = pack;
106115 }
107116
117
+ @JsonProperty("created_by_id")
118
+ public String getCreatedById() {
119
+ return createdBy == null ? null : createdBy.getUsername();
120
+ }
121
+
122
+ @JsonProperty("created_by_id")
123
+ public void setCreatedById(String username) {
124
+ if (username == null) {
125
+ createdBy = null;
126
+ } else {
127
+ createdBy = new User();
128
+ createdBy.setUsername(username);
129
+ }
130
+ }
131
+
132
+ @JsonProperty("canceled_by_id")
133
+ public String getCanceledById() {
134
+ return canceledBy == null ? null : canceledBy.getUsername();
135
+ }
136
+
137
+ @JsonProperty("canceled_by_id")
138
+ public void setCanceledById(String username) {
139
+ if (username == null) {
140
+ canceledBy = null;
141
+ } else {
142
+ canceledBy = new User();
143
+ canceledBy.setUsername(username);
144
+ }
145
+ }
146
+
147
+ @JsonProperty("pack_code")
148
+ public String getLicenseTypcode() {
149
+ return pack == null ? null : pack.getCode();
150
+ }
151
+
152
+ @JsonProperty("pack_id")
153
+ public Integer getOrgId() {
154
+ return pack == null ? null : pack.getId();
155
+ }
156
+
157
+ @JsonProperty("pack_id")
158
+ public void setOrgId(Integer idPack) {
159
+ if (idPack == null) {
160
+ pack = null;
161
+ } else {
162
+ pack = new Pack();
163
+ pack.setId(idPack);
164
+ }
165
+ }
166
+
167
+ public int getStatus() {
168
+ return status;
169
+ }
170
+
171
+ public void setStatus(int status) {
172
+ this.status = status;
173
+ }
174
+
175
+ public Date getModificationTimestamp() {
176
+ return modificationTimestamp;
177
+ }
178
+
179
+ public void setModificationTimestamp(Date modificationTimestamp) {
180
+ this.modificationTimestamp = modificationTimestamp;
181
+ }
182
+
183
+ public String getFullName() {
184
+ return fullName;
185
+ }
186
+
187
+ public void setFullName(String fullName) {
188
+ this.fullName = fullName;
189
+ }
190
+
191
+ public String getEmail() {
192
+ return email;
193
+ }
194
+
195
+ public void setEmail(String email) {
196
+ this.email = email;
197
+ }
198
+
199
+ public Date getActivationTimestamp() {
200
+ return activationTimestamp;
201
+ }
202
+
203
+ public void setActivationTimestamp(Date activationTimestamp) {
204
+ this.activationTimestamp = activationTimestamp;
205
+ }
206
+
207
+ public Date getSendTimestamp() {
208
+ return sendTimestamp;
209
+ }
210
+
211
+ public void setSendTimestamp(Date sendTimestamp) {
212
+ this.sendTimestamp = sendTimestamp;
213
+ }
214
+
215
+ public void setId(int id) {
216
+ this.id = id;
217
+ }
218
+
219
+ public User getCanceledBy() {
220
+ return canceledBy;
221
+ }
222
+
223
+ public void setCanceledBy(User canceledBy) {
224
+ this.canceledBy = canceledBy;
225
+ }
226
+
227
+ public Date getCancelationTimestamp() {
228
+ return cancelationTimestamp;
229
+ }
230
+
231
+ public void setCancelationTimestamp(Date cancelationTimestamp) {
232
+ this.cancelationTimestamp = cancelationTimestamp;
233
+ }
234
+
235
+ public Date getLastAccessTimestamp() {
236
+ return lastAccessTimestamp;
237
+ }
238
+
239
+ public void setLastAccessTimestamp(Date lastAccessTimestamp) {
240
+ this.lastAccessTimestamp = lastAccessTimestamp;
241
+ }
242
+
243
+ public static class Status {
244
+ public static final int CREATED = 0;
245
+ public static final int SENT = 1;
246
+ public static final int ACTIVE = 2;
247
+ public static final int CANCELED = 3;
248
+ }
108249 }
securis/src/main/java/net/curisit/securis/db/LicenseType.java
....@@ -14,6 +14,7 @@
1414 import javax.persistence.Table;
1515
1616 import org.codehaus.jackson.annotate.JsonAutoDetect;
17
+import org.codehaus.jackson.annotate.JsonIgnore;
1718 import org.codehaus.jackson.annotate.JsonIgnoreProperties;
1819 import org.codehaus.jackson.annotate.JsonProperty;
1920 import org.codehaus.jackson.map.annotate.JsonSerialize;
....@@ -30,7 +31,7 @@
3031 @Entity
3132 @Table(name = "license_type")
3233 @NamedQueries(
33
- { @NamedQuery(name = "list-license_types", query = "SELECT new map(lt.id as id, lt.code as code, lt.name as name, lt.description as description, lt.creationTimestamp as creationTimestamp, ap.id as application_id, ap.name as application_name) FROM LicenseType lt inner join lt.application ap") })
34
+ { @NamedQuery(name = "list-license_types", query = "SELECT lt FROM LicenseType lt") })
3435 public class LicenseType implements Serializable {
3536
3637 private static final Logger log = LoggerFactory.getLogger(LicenseType.class);
....@@ -47,6 +48,7 @@
4748 @Column(name = "creation_timestamp")
4849 private Date creationTimestamp;
4950
51
+ @JsonIgnore
5052 @ManyToOne
5153 @JoinColumn(name = "application_id")
5254 private Application application;
....@@ -83,6 +85,11 @@
8385 return application;
8486 }
8587
88
+ @JsonProperty("application_name")
89
+ public String getParentOrgName() {
90
+ return application == null ? null : application.getName();
91
+ }
92
+
8693 @JsonProperty("application_id")
8794 public Integer getApplicationId() {
8895 log.info("application " + application);
securis/src/main/java/net/curisit/securis/db/Pack.java
....@@ -17,6 +17,8 @@
1717 import javax.persistence.Table;
1818
1919 import org.codehaus.jackson.annotate.JsonAutoDetect;
20
+import org.codehaus.jackson.annotate.JsonIgnore;
21
+import org.codehaus.jackson.annotate.JsonProperty;
2022 import org.codehaus.jackson.map.annotate.JsonSerialize;
2123
2224 /**
....@@ -42,25 +44,34 @@
4244 @Column(name = "creation_timestamp")
4345 private Date creationTimestamp;
4446
47
+ @JsonIgnore
4548 @ManyToOne
4649 @JoinColumn(name = "organization_id")
4750 private Organization organization;
4851
52
+ @JsonIgnore
4953 @ManyToOne
5054 @JoinColumn(name = "license_type_id")
5155 private LicenseType licenseType;
5256
57
+ @JsonIgnore
5358 @ManyToOne
5459 @JoinColumn(name = "created_by")
5560 private User createdBy;
5661
62
+ @JsonIgnore
5763 @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "pack")
5864 private Set<License> licenses;
5965
66
+ @JoinColumn(name = "num_licenses")
6067 private int numLicenses;
6168
6269 public int getId() {
6370 return id;
71
+ }
72
+
73
+ public void setId(int id) {
74
+ this.id = id;
6475 }
6576
6677 public String getCode() {
....@@ -111,4 +122,92 @@
111122 this.numLicenses = numLicenses;
112123 }
113124
125
+ @JsonProperty("num_activations")
126
+ public int getNumActivations() {
127
+ if (licenses == null)
128
+ return 0;
129
+ int num = 0;
130
+ for (License lic : licenses) {
131
+ if (lic.getStatus() == License.Status.ACTIVE)
132
+ num++;
133
+ }
134
+ return num;
135
+ }
136
+
137
+ /**
138
+ * Counts all created licenses, It counts active licenses and licenses waiting for activation This number will be used to control the max number of licenses created. Ignore canceled licenses.
139
+ *
140
+ * @return
141
+ */
142
+ @JsonProperty("num_creations")
143
+ public int getNumCreations() {
144
+ if (licenses == null)
145
+ return 0;
146
+ int num = 0;
147
+ for (License lic : licenses) {
148
+ if (lic.getStatus() != License.Status.CANCELED)
149
+ num++;
150
+ }
151
+ return num;
152
+ }
153
+
154
+ /**
155
+ * Number of available licenses in this pack
156
+ *
157
+ * @return
158
+ */
159
+ @JsonProperty("num_available")
160
+ public int getNumAvailables() {
161
+ return numLicenses - getNumCreations();
162
+ }
163
+
164
+ @JsonProperty("organization_name")
165
+ public String getOrgName() {
166
+ return organization == null ? null : organization.getName();
167
+ }
168
+
169
+ @JsonProperty("application_name")
170
+ public String getAppName() {
171
+ if (licenseType == null)
172
+ return null;
173
+ Application app = licenseType.getApplication();
174
+ return app == null ? null : app.getName();
175
+ }
176
+
177
+ @JsonProperty("organization_id")
178
+ public Integer getOrgId() {
179
+ return organization == null ? null : organization.getId();
180
+ }
181
+
182
+ @JsonProperty("organization_id")
183
+ public void setOrgId(Integer idOrg) {
184
+ if (idOrg == null) {
185
+ organization = null;
186
+ } else {
187
+ organization = new Organization();
188
+ organization.setId(idOrg);
189
+ }
190
+ }
191
+
192
+ @JsonProperty("license_type_id")
193
+ public Integer getLicTypeId() {
194
+ return licenseType == null ? null : licenseType.getId();
195
+ }
196
+
197
+ @JsonProperty("created_by_id")
198
+ public String getCreatedById() {
199
+ return createdBy == null ? null : createdBy.getUsername();
200
+ }
201
+
202
+ @JsonProperty("created_by_id")
203
+ public void setCreatedById(String username) {
204
+ createdBy = new User();
205
+ createdBy.setUsername(username);
206
+ }
207
+
208
+ @JsonProperty("licensetype_code")
209
+ public String getLicenseTypcode() {
210
+ return licenseType == null ? null : licenseType.getCode();
211
+ }
212
+
114213 }
securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java
....@@ -6,7 +6,7 @@
66 import javax.inject.Inject;
77 import javax.inject.Provider;
88 import javax.persistence.EntityManager;
9
-import javax.persistence.Query;
9
+import javax.persistence.TypedQuery;
1010 import javax.servlet.http.HttpServletRequest;
1111 import javax.ws.rs.Consumes;
1212 import javax.ws.rs.DELETE;
....@@ -64,9 +64,8 @@
6464 log.info("Getting license types list ");
6565
6666 EntityManager em = emProvider.get();
67
- Query q = em.createNamedQuery("list-license_types");
68
- @SuppressWarnings("unchecked")
69
- List<Object> list = q.getResultList();
67
+ TypedQuery<LicenseType> q = em.createNamedQuery("list-license_types", LicenseType.class);
68
+ List<LicenseType> list = q.getResultList();
7069
7170 return Response.ok(list).build();
7271 }
securis/src/main/java/net/curisit/securis/services/UserResource.java
....@@ -208,7 +208,7 @@
208208
209209 request.getSession().setAttribute("username", user);
210210 if ("no".equals(password))
211
- return Response.status(Status.FORBIDDEN).build();
211
+ return Response.status(Status.UNAUTHORIZED).build();
212212 String tokenAuth = tokenHelper.generateToken(user);
213213 return Response.ok(Utils.createMap("success", true, "token", tokenAuth)).build();
214214 }