Roberto Sánchez
2014-01-22 1a0d1f15efa2b4cbdc6dd30b5a85b111d0599b63
securis/src/main/java/net/curisit/securis/services/PackResource.java
....@@ -9,11 +9,9 @@
99 import javax.inject.Provider;
1010 import javax.persistence.EntityManager;
1111 import javax.persistence.TypedQuery;
12
-import javax.servlet.http.HttpServletRequest;
1312 import javax.ws.rs.Consumes;
1413 import javax.ws.rs.DELETE;
1514 import javax.ws.rs.GET;
16
-import javax.ws.rs.HeaderParam;
1715 import javax.ws.rs.POST;
1816 import javax.ws.rs.PUT;
1917 import javax.ws.rs.Path;
....@@ -26,7 +24,10 @@
2624
2725 import net.curisit.integrity.commons.Utils;
2826 import net.curisit.securis.DefaultExceptionHandler;
27
+import net.curisit.securis.db.LicenseType;
28
+import net.curisit.securis.db.Organization;
2929 import net.curisit.securis.db.Pack;
30
+import net.curisit.securis.db.User;
3031 import net.curisit.securis.security.BasicSecurityContext;
3132 import net.curisit.securis.security.Securable;
3233 import net.curisit.securis.utils.TokenHelper;
....@@ -78,7 +79,6 @@
7879 q = em.createNamedQuery("list-packs-by-orgs", Pack.class);
7980 if (bsc.getOrganizationsIds() == null)
8081 Response.ok().build();
81
- // log.info("Getting only {} orgs for user: {}", securityContext.getOrganizationsIds(), securityContext.getUserPrincipal());
8282 q.setParameter("list_ids", bsc.getOrganizationsIds());
8383 }
8484
....@@ -130,10 +130,32 @@
130130 @Produces(
131131 { MediaType.APPLICATION_JSON })
132132 @Transactional
133
- public Response create(Pack pack, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
133
+ public Response create(Pack pack, @Context BasicSecurityContext bsc) {
134134 log.info("Creating new pack");
135135 EntityManager em = emProvider.get();
136136
137
+ Organization org = null;
138
+ if (pack.getOrgId() != null) {
139
+ org = em.find(Organization.class, pack.getOrgId());
140
+ if (org == null) {
141
+ log.error("Organization pack with id {} not found in DB", pack.getOrgId());
142
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack organization not found with ID: " + pack.getOrgId()).build();
143
+ }
144
+ }
145
+ LicenseType lt = null;
146
+ if (pack.getLicTypeId() != null) {
147
+ lt = em.find(LicenseType.class, pack.getLicTypeId());
148
+ if (lt == null) {
149
+ log.error("Pack license type with id {} not found in DB", pack.getLicTypeId());
150
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack license type not found with ID: " + pack.getLicTypeId()).build();
151
+ }
152
+ }
153
+
154
+ User user = em.find(User.class, bsc.getUserPrincipal().getName());
155
+
156
+ pack.setCreatedBy(user);
157
+ pack.setLicenseType(lt);
158
+ pack.setOrganization(org);
137159 pack.setCreationTimestamp(new Date());
138160 em.persist(pack);
139161
....@@ -149,11 +171,34 @@
149171 @Consumes(MediaType.APPLICATION_JSON)
150172 @Produces(
151173 { MediaType.APPLICATION_JSON })
152
- public Response modify(Pack pack, @PathParam("packId") String packId, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
174
+ public Response modify(Pack pack, @PathParam("packId") String packId) {
153175 log.info("Modifying pack with id: {}", packId);
154176 EntityManager em = emProvider.get();
177
+ Pack currentPack = em.find(Pack.class, Integer.parseInt(packId));
155178
156
- em.persist(pack);
179
+ Organization org = null;
180
+ if (pack.getOrgId() != null) {
181
+ org = em.find(Organization.class, pack.getOrgId());
182
+ if (org == null) {
183
+ log.error("Organization pack with id {} not found in DB", pack.getOrgId());
184
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack organization not found with ID: " + pack.getOrgId()).build();
185
+ }
186
+ }
187
+ LicenseType lt = null;
188
+ if (pack.getLicTypeId() != null) {
189
+ lt = em.find(LicenseType.class, pack.getLicTypeId());
190
+ if (lt == null) {
191
+ log.error("Pack license type with id {} not found in DB", pack.getLicTypeId());
192
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack license type not found with ID: " + pack.getLicTypeId()).build();
193
+ }
194
+ }
195
+ currentPack.setLicenseType(lt);
196
+ currentPack.setOrganization(org);
197
+ currentPack.setCode(pack.getCode());
198
+ currentPack.setComments(pack.getComments());
199
+ currentPack.setNumLicenses(pack.getNumLicenses());
200
+
201
+ em.persist(currentPack);
157202
158203 return Response.ok(pack).build();
159204 }
....@@ -165,7 +210,7 @@
165210 @Transactional
166211 @Produces(
167212 { MediaType.APPLICATION_JSON })
168
- public Response delete(@PathParam("packId") String packId, @Context HttpServletRequest request) {
213
+ public Response delete(@PathParam("packId") String packId) {
169214 log.info("Deleting pack with id: {}", packId);
170215 EntityManager em = emProvider.get();
171216 Pack org = em.find(Pack.class, Integer.parseInt(packId));