| .. | .. |
|---|
| 24 | 24 | |
|---|
| 25 | 25 | import net.curisit.integrity.commons.Utils; |
|---|
| 26 | 26 | import net.curisit.securis.DefaultExceptionHandler; |
|---|
| 27 | +import net.curisit.securis.SeCurisException; |
|---|
| 27 | 28 | import net.curisit.securis.db.Application; |
|---|
| 28 | 29 | import net.curisit.securis.db.LicenseType; |
|---|
| 29 | 30 | import net.curisit.securis.utils.TokenHelper; |
|---|
| .. | .. |
|---|
| 41 | 42 | @Path("/licensetype") |
|---|
| 42 | 43 | public class LicenseTypeResource { |
|---|
| 43 | 44 | |
|---|
| 44 | | - private static final Logger log = LogManager.getLogger(LicenseTypeResource.class); |
|---|
| 45 | + private static final Logger LOG = LogManager.getLogger(LicenseTypeResource.class); |
|---|
| 45 | 46 | |
|---|
| 46 | 47 | @Inject |
|---|
| 47 | 48 | TokenHelper tokenHelper; |
|---|
| .. | .. |
|---|
| 61 | 62 | @Produces( |
|---|
| 62 | 63 | { MediaType.APPLICATION_JSON }) |
|---|
| 63 | 64 | public Response index() { |
|---|
| 64 | | - log.info("Getting license types list "); |
|---|
| 65 | + LOG.info("Getting license types list "); |
|---|
| 65 | 66 | |
|---|
| 66 | 67 | EntityManager em = emProvider.get(); |
|---|
| 67 | 68 | TypedQuery<LicenseType> q = em.createNamedQuery("list-license_types", LicenseType.class); |
|---|
| .. | .. |
|---|
| 79 | 80 | @Produces( |
|---|
| 80 | 81 | { MediaType.APPLICATION_JSON }) |
|---|
| 81 | 82 | 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); |
|---|
| 83 | 84 | if (ltid == null || ltid.equals("")) { |
|---|
| 84 | | - log.error("LicenseType ID is mandatory"); |
|---|
| 85 | + LOG.error("LicenseType ID is mandatory"); |
|---|
| 85 | 86 | return Response.status(Status.NOT_FOUND).build(); |
|---|
| 86 | 87 | } |
|---|
| 87 | 88 | |
|---|
| 88 | 89 | EntityManager em = emProvider.get(); |
|---|
| 89 | 90 | LicenseType lt = em.find(LicenseType.class, Integer.parseInt(ltid)); |
|---|
| 90 | 91 | 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); |
|---|
| 92 | 93 | return Response.status(Status.NOT_FOUND).build(); |
|---|
| 93 | 94 | } |
|---|
| 94 | 95 | return Response.ok(lt).build(); |
|---|
| .. | .. |
|---|
| 101 | 102 | { MediaType.APPLICATION_JSON }) |
|---|
| 102 | 103 | @Transactional |
|---|
| 103 | 104 | 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"); |
|---|
| 105 | 106 | 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"); |
|---|
| 115 | 116 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application is missing for current license type data").build(); |
|---|
| 116 | 117 | } |
|---|
| 117 | 118 | |
|---|
| 118 | | - lt.setApplication(app); |
|---|
| 119 | 119 | lt.setCreationTimestamp(new Date()); |
|---|
| 120 | 120 | em.persist(lt); |
|---|
| 121 | 121 | |
|---|
| .. | .. |
|---|
| 130 | 130 | @Produces( |
|---|
| 131 | 131 | { MediaType.APPLICATION_JSON }) |
|---|
| 132 | 132 | 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); |
|---|
| 134 | 134 | EntityManager em = emProvider.get(); |
|---|
| 135 | 135 | LicenseType currentlt = em.find(LicenseType.class, Integer.parseInt(ltid)); |
|---|
| 136 | 136 | 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); |
|---|
| 138 | 138 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License type not found with ID: " + ltid).build(); |
|---|
| 139 | 139 | } |
|---|
| 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(); |
|---|
| 147 | 145 | } |
|---|
| 146 | + |
|---|
| 148 | 147 | currentlt.setCode(lt.getCode()); |
|---|
| 149 | 148 | currentlt.setName(lt.getName()); |
|---|
| 150 | 149 | currentlt.setDescription(lt.getDescription()); |
|---|
| 151 | | - currentlt.setApplication(app); |
|---|
| 152 | 150 | em.persist(currentlt); |
|---|
| 153 | 151 | |
|---|
| 154 | 152 | 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); |
|---|
| 155 | 166 | } |
|---|
| 156 | 167 | |
|---|
| 157 | 168 | @DELETE |
|---|
| .. | .. |
|---|
| 160 | 171 | @Produces( |
|---|
| 161 | 172 | { MediaType.APPLICATION_JSON }) |
|---|
| 162 | 173 | 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); |
|---|
| 164 | 175 | EntityManager em = emProvider.get(); |
|---|
| 165 | 176 | LicenseType app = em.find(LicenseType.class, Integer.parseInt(ltid)); |
|---|
| 166 | 177 | 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); |
|---|
| 168 | 179 | return Response.status(Status.NOT_FOUND).build(); |
|---|
| 169 | 180 | } |
|---|
| 170 | 181 | |
|---|