Roberto Sánchez
2014-09-18 52ce72b22ef8d92a1f35b4993bcddaaa66d67350
securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java
....@@ -24,6 +24,7 @@
2424
2525 import net.curisit.integrity.commons.Utils;
2626 import net.curisit.securis.DefaultExceptionHandler;
27
+import net.curisit.securis.SeCurisException;
2728 import net.curisit.securis.db.Application;
2829 import net.curisit.securis.db.LicenseType;
2930 import net.curisit.securis.utils.TokenHelper;
....@@ -41,7 +42,7 @@
4142 @Path("/licensetype")
4243 public class LicenseTypeResource {
4344
44
- private static final Logger log = LogManager.getLogger(LicenseTypeResource.class);
45
+ private static final Logger LOG = LogManager.getLogger(LicenseTypeResource.class);
4546
4647 @Inject
4748 TokenHelper tokenHelper;
....@@ -61,7 +62,7 @@
6162 @Produces(
6263 { MediaType.APPLICATION_JSON })
6364 public Response index() {
64
- log.info("Getting license types list ");
65
+ LOG.info("Getting license types list ");
6566
6667 EntityManager em = emProvider.get();
6768 TypedQuery<LicenseType> q = em.createNamedQuery("list-license_types", LicenseType.class);
....@@ -79,16 +80,16 @@
7980 @Produces(
8081 { MediaType.APPLICATION_JSON })
8182 public Response get(@PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
82
- log.info("Getting license type data for id: {}: ", ltid);
83
+ LOG.info("Getting license type data for id: {}: ", ltid);
8384 if (ltid == null || ltid.equals("")) {
84
- log.error("LicenseType ID is mandatory");
85
+ LOG.error("LicenseType ID is mandatory");
8586 return Response.status(Status.NOT_FOUND).build();
8687 }
8788
8889 EntityManager em = emProvider.get();
8990 LicenseType lt = em.find(LicenseType.class, Integer.parseInt(ltid));
9091 if (lt == null) {
91
- log.error("LicenseType with id {} not found in DB", ltid);
92
+ LOG.error("LicenseType with id {} not found in DB", ltid);
9293 return Response.status(Status.NOT_FOUND).build();
9394 }
9495 return Response.ok(lt).build();
....@@ -101,21 +102,20 @@
101102 { MediaType.APPLICATION_JSON })
102103 @Transactional
103104 public Response create(LicenseType lt, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
104
- log.info("Creating new license type");
105
+ LOG.info("Creating new license type");
105106 EntityManager em = emProvider.get();
106
- Application app = null;
107
- if (lt.getApplicationId() != null) {
108
- app = em.find(Application.class, lt.getApplicationId());
109
- if (app == null) {
110
- log.error("LicenseType application with id {} not found in DB", lt.getApplicationId());
111
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License type's app not found with ID: " + lt.getApplicationId()).build();
112
- }
113
- } else {
114
- log.error("Application is missing for current license type data");
107
+
108
+ try {
109
+ setApplication(lt, lt.getApplicationId(), em);
110
+ } catch (SeCurisException e) {
111
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
112
+ }
113
+
114
+ if (lt.getApplicationId() == null) {
115
+ LOG.error("Application is missing for current license type data");
115116 return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application is missing for current license type data").build();
116117 }
117118
118
- lt.setApplication(app);
119119 lt.setCreationTimestamp(new Date());
120120 em.persist(lt);
121121
....@@ -130,28 +130,39 @@
130130 @Produces(
131131 { MediaType.APPLICATION_JSON })
132132 public Response modify(LicenseType lt, @PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
133
- log.info("Modifying license type with id: {}", ltid);
133
+ LOG.info("Modifying license type with id: {}", ltid);
134134 EntityManager em = emProvider.get();
135135 LicenseType currentlt = em.find(LicenseType.class, Integer.parseInt(ltid));
136136 if (currentlt == null) {
137
- log.error("LicenseType with id {} not found in DB", ltid);
137
+ LOG.error("LicenseType with id {} not found in DB", ltid);
138138 return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License type not found with ID: " + ltid).build();
139139 }
140
- Application app = null;
141
- if (lt.getApplicationId() != null) {
142
- app = em.find(Application.class, lt.getApplicationId());
143
- if (app == null) {
144
- log.error("LicenseType application with id {} not found in DB", lt.getApplicationId());
145
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License type's app not found with ID: " + lt.getApplicationId()).build();
146
- }
140
+
141
+ try {
142
+ setApplication(currentlt, lt.getApplicationId(), em);
143
+ } catch (SeCurisException e) {
144
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
147145 }
146
+
148147 currentlt.setCode(lt.getCode());
149148 currentlt.setName(lt.getName());
150149 currentlt.setDescription(lt.getDescription());
151
- currentlt.setApplication(app);
152150 em.persist(currentlt);
153151
154152 return Response.ok(currentlt).build();
153
+ }
154
+
155
+ private void setApplication(LicenseType licType, Integer applicationId, EntityManager em) throws SeCurisException {
156
+ Application app = null;
157
+ if (applicationId != null) {
158
+ app = em.find(Application.class, applicationId);
159
+ if (app == null) {
160
+ LOG.error("LicenseType application with id {} not found in DB", applicationId);
161
+
162
+ throw new SecurityException("License type's app not found with ID: " + applicationId);
163
+ }
164
+ }
165
+ licType.setApplication(app);
155166 }
156167
157168 @DELETE
....@@ -160,11 +171,11 @@
160171 @Produces(
161172 { MediaType.APPLICATION_JSON })
162173 public Response delete(@PathParam("ltid") String ltid, @Context HttpServletRequest request) {
163
- log.info("Deleting app with id: {}", ltid);
174
+ LOG.info("Deleting app with id: {}", ltid);
164175 EntityManager em = emProvider.get();
165176 LicenseType app = em.find(LicenseType.class, Integer.parseInt(ltid));
166177 if (app == null) {
167
- log.error("LicenseType with id {} can not be deleted, It was not found in DB", ltid);
178
+ LOG.error("LicenseType with id {} can not be deleted, It was not found in DB", ltid);
168179 return Response.status(Status.NOT_FOUND).build();
169180 }
170181