@RolesAllowed("users") @GET @Path("productlist") @Produces("application/json") public Response getProducts(/*@Context SecurityContext sc*/ ) { JSONObject jsonGlobal = new JSONObject(); JSONArray jsonArray = new JSONArray(); try { // mockFillProducts(); List<Product> products = genericDAO.findAll(); for (Product product : products) { JSONObject json = new JSONObject(); json.put("id", product.getId()); json.put("category", product.getCategory()); json.put("company", product.getCompany()); json.put("label", product.getLabel()); json.put("price", product.getPrice()); jsonArray.put(json); } // String userId = sc.getUserPrincipal().getName(); return Response.ok(jsonArray.toString(), MediaType.APPLICATION_JSON).build(); } catch (Exception e) { return Response.status(HTTPStatus.INTERNAL_ERROR).build(); } }
@RolesAllowed("users") @POST @Path("submit") @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) public Response editProduct(Product product) { Product productBean = null; if (product.getId() != 0) { productBean = (Product) genericDAO.findById(product.getId()); if (productBean != null) { productBean.setCategory(product.getCategory()); productBean.setPrice(product.getPrice()); productBean.setLabel(product.getLabel()); productBean.setCompany(product.getCompany()); } } if (productBean == null && product != null) { genericDAO.persist(product); } else if (productBean != null) { genericDAO.merge(productBean); } return getProducts(); }