/** * For test purpose only * * @param entities * @return */ @POST @Consumes({"application/json"}) @Produces({"application/json"}) public Response post(List<Product> entities, @Context UriInfo info) throws UnknownResourceException { if (entities == null) { return Response.status(Response.Status.BAD_REQUEST.getStatusCode()).build(); } int previousRows = productInventoryFacade.count(); int affectedRows = 0; // Try to persist entities try { for (Product entitie : entities) { productInventoryFacade.checkCreation(entitie); productInventoryFacade.create(entitie); entitie.setHref(info.getAbsolutePath() + "/" + Long.toString(entitie.getId())); productInventoryFacade.edit(entitie); affectedRows = affectedRows + 1; // publisher.createNotification(entitie, new Date()); } } catch (BadUsageException e) { return Response.status(Response.Status.BAD_REQUEST.getStatusCode()).build(); } Report stat = new Report(productInventoryFacade.count()); stat.setAffectedRows(affectedRows); stat.setPreviousRows(previousRows); // 201 OK return Response.created(null).entity(stat).build(); }
/** * For test purpose only * * @param id * @return * @throws UnknownResourceException */ @DELETE @Path("{id}") public Response delete(@PathParam("id") Long id) throws UnknownResourceException { int previousRows = productInventoryFacade.count(); Product entity = productInventoryFacade.find(id); // Event deletion // publisher.deletionNotification(entity, new Date()); try { // Pause for 4 seconds to finish notification Thread.sleep(4000); } catch (InterruptedException ex) { Logger.getLogger(ProductAdminResource.class.getName()).log(Level.SEVERE, null, ex); } // remove event(s) binding to the resource List<ProductEvent> events = eventFacade.findAll(); for (ProductEvent event : events) { if (event.getResource().getId().equals(id)) { eventFacade.remove(event.getId()); } } // remove resource productInventoryFacade.remove(id); int affectedRows = 1; Report stat = new Report(productInventoryFacade.count()); stat.setAffectedRows(affectedRows); stat.setPreviousRows(previousRows); // 200 Response response = Response.ok(stat).build(); return response; }
@PUT @Path("{id}") @Consumes({"application/json"}) @Produces({"application/json"}) public Response update(@PathParam("id") long id, Product entity) throws UnknownResourceException { Response response = null; Product productInventory = productInventoryFacade.find(id); if (productInventory != null) { entity.setId(id); productInventoryFacade.edit(entity); // publisher.valueChangedNotification(entity, new Date()); // 200 OK + location response = Response.status(Response.Status.OK).entity(entity).build(); } else { // 404 not found response = Response.status(Response.Status.NOT_FOUND).build(); } return response; }
/** * For test purpose only * * @return * @throws org.tmf.dsmapi.commons.exceptions.UnknownResourceException */ @DELETE public Report deleteAll() throws UnknownResourceException { eventFacade.removeAll(); int previousRows = productInventoryFacade.count(); productInventoryFacade.removeAll(); List<Product> pis = productInventoryFacade.findAll(); for (Product pi : pis) { delete(pi.getId()); } int currentRows = productInventoryFacade.count(); int affectedRows = previousRows - currentRows; Report stat = new Report(currentRows); stat.setAffectedRows(affectedRows); stat.setPreviousRows(previousRows); return stat; }
@GET @Produces({"application/json"}) public List<Product> findAll() { return productInventoryFacade.findAll(); }
/** @return */ @GET @Path("count") @Produces({"application/json"}) public Report count() { return new Report(productInventoryFacade.count()); }