@RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "text/html")
  public String delete(
      @PathVariable("id") Integer id,
      @RequestParam(value = "page", required = false) Integer page,
      @RequestParam(value = "size", required = false) Integer size,
      Model uiModel) {
    PurchaseRequest purchaseRequest = Utils.checkRights(PurchaseRequest.findPurchaseRequest(id));
    purchaseRequest.remove();
    uiModel.asMap().clear();
    uiModel.addAttribute("page", (page == null) ? "1" : page.toString());
    uiModel.addAttribute("size", (size == null) ? "10" : size.toString());

    checkProposalMinReached(purchaseRequest.getProposal());

    return "redirect:/user/purchaserequest";
  }
  @RequestMapping(value = "/{id}", params = "form", produces = "text/html")
  public String updateForm(@PathVariable("id") Integer id, Model uiModel) {

    PurchaseRequest pr = Utils.checkRights(PurchaseRequest.findPurchaseRequest(id));
    uiModel.addAttribute("purchaseRequest", pr);

    ArrayList<Proposal> mockProposal = new ArrayList<Proposal>();
    mockProposal.add(pr.getProposal());
    uiModel.addAttribute("proposals", mockProposal);

    uiModel.addAttribute("purchaserequestparts", pr.getPurchaseRequestParts());

    ArrayList<User> mockUser = new ArrayList<User>();
    mockUser.add(pr.getAcquirer());
    uiModel.addAttribute("users", mockUser);

    uiModel.addAttribute("completed", pr.getCompleted());

    return "user/purchaserequest/update";
  }
  @RequestMapping(method = RequestMethod.PUT, produces = "text/html")
  public String update(
      @Valid PurchaseRequest purchaseRequest,
      BindingResult bindingResult,
      Model uiModel,
      HttpServletRequest httpServletRequest) {

    Utils.checkRights(purchaseRequest);

    if (bindingResult.hasErrors()) {
      populateEditForm(uiModel, purchaseRequest);
      return "user/purchaserequest/update";
    }

    if (purchaseRequest.getToMin() == 0) // e' completa. Molt Ben!
    {
      purchaseRequest.setCompleted(true);
      purchaseRequest.merge();

    } else // non e' completa
    {
      purchaseRequest.setCompleted(false);
      purchaseRequest.merge();

      PurchaseRequestPart part = new PurchaseRequestPart();
      part.setPurchaseRequest(purchaseRequest);
      part.setAcquirer(purchaseRequest.getAcquirer());
      part.setQuantity(purchaseRequest.getQuantity());

      part.persist();
    }

    checkProposalMinReached(purchaseRequest.getProposal());

    uiModel.asMap().clear();
    purchaseRequest.merge();
    return "redirect:/user/purchaserequest/"
        + encodeUrlPathSegment(purchaseRequest.getId().toString(), httpServletRequest);
  }
  @RequestMapping(value = "/{id}", produces = "text/html")
  public String show(
      @PathVariable("id") Integer id,
      @RequestParam(value = "incomplete", required = false) String incomplete,
      Model uiModel) {
    uiModel.addAttribute(
        "purchaserequest", Utils.checkRights(PurchaseRequest.findPurchaseRequest(id)));
    uiModel.addAttribute("itemId", id);

    if (incomplete != null) {
      uiModel.addAttribute("incomplete", true);
      uiModel.addAttribute("toMin", PurchaseRequest.findPurchaseRequest(id).getToMin());

      uiModel.addAttribute("addTo", id);

      uiModel.addAttribute("newpurchaserequest", new PurchaseRequest());
      ArrayList<Proposal> mock = new ArrayList<Proposal>();
      mock.add(PurchaseRequest.findPurchaseRequest(id).getProposal());
      uiModel.addAttribute("proposals", mock);
    }

    return "user/purchaserequest/show";
  }