Roberto Sánchez
2014-01-10 59cdd2b7ebceae94fbecdb1eeb46a969666dc88f
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.TypedQuery;
9
+import javax.persistence.Query;
1010 import javax.servlet.http.HttpServletRequest;
1111 import javax.ws.rs.Consumes;
1212 import javax.ws.rs.DELETE;
....@@ -23,6 +23,8 @@
2323 import javax.ws.rs.core.Response.Status;
2424
2525 import net.curisit.integrity.commons.Utils;
26
+import net.curisit.securis.SecurisErrorHandler;
27
+import net.curisit.securis.db.Application;
2628 import net.curisit.securis.db.LicenseType;
2729 import net.curisit.securis.utils.TokenHelper;
2830
....@@ -39,13 +41,13 @@
3941 @Path("/licensetype")
4042 public class LicenseTypeResource {
4143
44
+ private static final Logger log = LoggerFactory.getLogger(LicenseTypeResource.class);
45
+
4246 @Inject
4347 TokenHelper tokenHelper;
4448
4549 @Inject
4650 Provider<EntityManager> emProvider;
47
-
48
- private static final Logger log = LoggerFactory.getLogger(LicenseTypeResource.class);
4951
5052 public LicenseTypeResource() {
5153 }
....@@ -62,8 +64,9 @@
6264 log.info("Getting license types list ");
6365
6466 EntityManager em = emProvider.get();
65
- TypedQuery<LicenseType> q = em.createNamedQuery("list-license_types", LicenseType.class);
66
- List<LicenseType> list = q.getResultList();
67
+ Query q = em.createNamedQuery("list-license_types");
68
+ @SuppressWarnings("unchecked")
69
+ List<Object> list = q.getResultList();
6770
6871 return Response.ok(list).build();
6972 }
....@@ -98,13 +101,23 @@
98101 @Produces(
99102 { MediaType.APPLICATION_JSON })
100103 @Transactional
101
- public Response create(LicenseType app, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
104
+ public Response create(LicenseType lt, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
102105 log.info("Creating new license type");
103106 EntityManager em = emProvider.get();
104
- app.setCreationTimestamp(new Date());
105
- em.persist(app);
107
+ Application app = null;
108
+ if (lt.getApplicationId() != null) {
109
+ app = em.find(Application.class, lt.getApplicationId());
110
+ if (app == null) {
111
+ log.error("LicenseType application with id {} not found in DB", lt.getApplicationId());
112
+ return Response.status(Status.NOT_FOUND).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "License type's app not found with ID: " + lt.getApplicationId()).build();
113
+ }
114
+ }
106115
107
- return Response.ok(app).build();
116
+ lt.setApplication(app);
117
+ lt.setCreationTimestamp(new Date());
118
+ em.persist(lt);
119
+
120
+ return Response.ok(lt).build();
108121 }
109122
110123 @PUT
....@@ -114,16 +127,26 @@
114127 @Consumes(MediaType.APPLICATION_JSON)
115128 @Produces(
116129 { MediaType.APPLICATION_JSON })
117
- public Response modify(LicenseType app, @PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
130
+ public Response modify(LicenseType lt, @PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
118131 log.info("Modifying license type with id: {}", ltid);
119132 EntityManager em = emProvider.get();
120133 LicenseType currentlt = em.find(LicenseType.class, Integer.parseInt(ltid));
121134 if (currentlt == null) {
122135 log.error("LicenseType with id {} not found in DB", ltid);
123
- return Response.status(Status.NOT_FOUND).build();
136
+ return Response.status(Status.NOT_FOUND).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "License type not found with ID: " + ltid).build();
124137 }
125
- currentlt.setName(app.getName());
126
- currentlt.setDescription(app.getDescription());
138
+ Application app = null;
139
+ if (lt.getApplicationId() != null) {
140
+ app = em.find(Application.class, lt.getApplicationId());
141
+ if (app == null) {
142
+ log.error("LicenseType application with id {} not found in DB", lt.getApplicationId());
143
+ return Response.status(Status.NOT_FOUND).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "License type's app not found with ID: " + lt.getApplicationId()).build();
144
+ }
145
+ }
146
+ currentlt.setCode(lt.getCode());
147
+ currentlt.setName(lt.getName());
148
+ currentlt.setDescription(lt.getDescription());
149
+ currentlt.setApplication(app);
127150 em.persist(currentlt);
128151
129152 return Response.ok(currentlt).build();