| .. | .. |
|---|
| 30 | 30 | import net.curisit.securis.db.Organization; |
|---|
| 31 | 31 | import net.curisit.securis.db.Pack; |
|---|
| 32 | 32 | import net.curisit.securis.db.PackMetadata; |
|---|
| 33 | +import net.curisit.securis.db.PackStatus; |
|---|
| 33 | 34 | import net.curisit.securis.db.User; |
|---|
| 34 | 35 | import net.curisit.securis.security.BasicSecurityContext; |
|---|
| 35 | 36 | import net.curisit.securis.security.Securable; |
|---|
| 37 | +import net.curisit.securis.services.exception.SeCurisServiceException; |
|---|
| 38 | +import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes; |
|---|
| 36 | 39 | import net.curisit.securis.utils.TokenHelper; |
|---|
| 37 | 40 | |
|---|
| 38 | 41 | import org.apache.logging.log4j.LogManager; |
|---|
| .. | .. |
|---|
| 154 | 157 | |
|---|
| 155 | 158 | User user = em.find(User.class, bsc.getUserPrincipal().getName()); |
|---|
| 156 | 159 | |
|---|
| 160 | + pack.setStatus(PackStatus.CREATED); |
|---|
| 157 | 161 | pack.setCreatedBy(user); |
|---|
| 158 | 162 | pack.setCreationTimestamp(new Date()); |
|---|
| 159 | 163 | em.persist(pack); |
|---|
| 160 | | - Set<PackMetadata> newMD = pack.getMetadata(); |
|---|
| 161 | | - |
|---|
| 164 | + Set<PackMetadata> newMD = pack.getMetadata(); |
|---|
| 165 | + |
|---|
| 162 | 166 | if (newMD != null) { |
|---|
| 163 | 167 | for (PackMetadata md : newMD) { |
|---|
| 164 | 168 | md.setPack(pack); |
|---|
| .. | .. |
|---|
| 215 | 219 | |
|---|
| 216 | 220 | em.persist(currentPack); |
|---|
| 217 | 221 | |
|---|
| 218 | | - Set<PackMetadata> newMD = pack.getMetadata(); |
|---|
| 222 | + Set<PackMetadata> newMD = pack.getMetadata(); |
|---|
| 219 | 223 | for (PackMetadata currentMd : currentPack.getMetadata()) { |
|---|
| 220 | | - if (newMD == null || !newMD.contains(currentMd)); |
|---|
| 221 | | - em.remove(currentMd); |
|---|
| 224 | + if (newMD == null || !newMD.contains(currentMd)) |
|---|
| 225 | + ; |
|---|
| 226 | + em.remove(currentMd); |
|---|
| 222 | 227 | } |
|---|
| 223 | | - |
|---|
| 228 | + |
|---|
| 224 | 229 | if (newMD != null) { |
|---|
| 225 | 230 | for (PackMetadata md : newMD) { |
|---|
| 226 | 231 | md.setPack(currentPack); |
|---|
| .. | .. |
|---|
| 231 | 236 | return Response.ok(pack).build(); |
|---|
| 232 | 237 | } |
|---|
| 233 | 238 | |
|---|
| 239 | + @POST |
|---|
| 240 | + @Path("/{packId}/activate") |
|---|
| 241 | + @Transactional |
|---|
| 242 | + @Securable |
|---|
| 243 | + @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 244 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 245 | + @Produces({ |
|---|
| 246 | + MediaType.APPLICATION_JSON |
|---|
| 247 | + }) |
|---|
| 248 | + public Response activate(@PathParam("packId") Integer packId) throws SeCurisServiceException { |
|---|
| 249 | + LOG.info("Activating pack with id: {}", packId); |
|---|
| 250 | + EntityManager em = emProvider.get(); |
|---|
| 251 | + |
|---|
| 252 | + Pack currentPack = em.find(Pack.class, packId); |
|---|
| 253 | + |
|---|
| 254 | + if (!Pack.Status.isActionValid(Pack.Action.ACTIVATION, currentPack.getStatus())) { |
|---|
| 255 | + LOG.error("Pack with id {} cannot be activaed from status {}", packId, currentPack.getStatus().name()); |
|---|
| 256 | + throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "Pack cannot be activated in status: " + currentPack.getStatus().name()); |
|---|
| 257 | + } |
|---|
| 258 | + |
|---|
| 259 | + currentPack.setStatus(PackStatus.ACTIVE); |
|---|
| 260 | + em.persist(currentPack); |
|---|
| 261 | + |
|---|
| 262 | + return Response.ok(currentPack).build(); |
|---|
| 263 | + } |
|---|
| 264 | + |
|---|
| 265 | + @POST |
|---|
| 266 | + @Path("/{packId}/putonhold") |
|---|
| 267 | + @Transactional |
|---|
| 268 | + @Securable |
|---|
| 269 | + @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 270 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 271 | + @Produces({ |
|---|
| 272 | + MediaType.APPLICATION_JSON |
|---|
| 273 | + }) |
|---|
| 274 | + public Response onhold(@PathParam("packId") Integer packId) throws SeCurisServiceException { |
|---|
| 275 | + LOG.info("Putting On hold pack with id: {}", packId); |
|---|
| 276 | + EntityManager em = emProvider.get(); |
|---|
| 277 | + |
|---|
| 278 | + Pack currentPack = em.find(Pack.class, packId); |
|---|
| 279 | + |
|---|
| 280 | + if (!Pack.Status.isActionValid(Pack.Action.PUT_ONHOLD, currentPack.getStatus())) { |
|---|
| 281 | + LOG.error("Pack with id {} cannot be put on hold from status {}", packId, currentPack.getStatus().name()); |
|---|
| 282 | + throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "Pack cannot be put on hold in status: " + currentPack.getStatus().name()); |
|---|
| 283 | + } |
|---|
| 284 | + |
|---|
| 285 | + currentPack.setStatus(PackStatus.ON_HOLD); |
|---|
| 286 | + em.persist(currentPack); |
|---|
| 287 | + |
|---|
| 288 | + return Response.ok(currentPack).build(); |
|---|
| 289 | + } |
|---|
| 290 | + |
|---|
| 291 | + @POST |
|---|
| 292 | + @Path("/{packId}/cancel") |
|---|
| 293 | + @Transactional |
|---|
| 294 | + @Securable |
|---|
| 295 | + @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 296 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 297 | + @Produces({ |
|---|
| 298 | + MediaType.APPLICATION_JSON |
|---|
| 299 | + }) |
|---|
| 300 | + public Response cancel(@PathParam("packId") Integer packId) throws SeCurisServiceException { |
|---|
| 301 | + LOG.info("Putting On hold pack with id: {}", packId); |
|---|
| 302 | + EntityManager em = emProvider.get(); |
|---|
| 303 | + |
|---|
| 304 | + Pack currentPack = em.find(Pack.class, packId); |
|---|
| 305 | + |
|---|
| 306 | + if (!Pack.Status.isActionValid(Pack.Action.CANCEL, currentPack.getStatus())) { |
|---|
| 307 | + LOG.error("Pack with id {} cannot cancelled from status {}", packId, currentPack.getStatus().name()); |
|---|
| 308 | + throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "Pack cannot be cancelled in status: " + currentPack.getStatus().name()); |
|---|
| 309 | + } |
|---|
| 310 | + |
|---|
| 311 | + currentPack.setStatus(PackStatus.CANCELLED); |
|---|
| 312 | + em.persist(currentPack); |
|---|
| 313 | + |
|---|
| 314 | + return Response.ok(currentPack).build(); |
|---|
| 315 | + } |
|---|
| 316 | + |
|---|
| 234 | 317 | private void setPackOrganization(Pack currentPack, Integer orgId, EntityManager em) throws SeCurisException { |
|---|
| 235 | 318 | Organization org = null; |
|---|
| 236 | 319 | if (orgId != null) { |
|---|