Exemple #1
0
 @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();
   }
 }
Exemple #2
0
  @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();
  }
 @Test
 public void addProduct() {
   Product product = new Product();
   product.setCompany("company1");
   product.setCategory("category1");
   product.setLabel("product1");
   product.setPrice(50.2);
   GenericDAO<Product> genericDAO = new GenericDAO<Product>(Product.class);
   genericDAO.persist(product);
   List<Product> products = genericDAO.findAll();
   Assert.assertNotNull(products);
   Product product1 = products.get(0);
   Assert.assertNotNull(genericDAO.findById(product1.getId()));
   Assert.assertEquals(product1.getLabel(), product.getLabel());
   genericDAO.delete(product1);
   Assert.assertEquals(0, genericDAO.findAll().size());
 }