// Return the list of health measure history to the user in the browser
 @GET
 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
 public List<HealthMeasureHistory> getMeasures() {
   System.out.println("Getting list of measures...");
   List<HealthMeasureHistory> measures = HealthMeasureHistory.getAll();
   System.out.println(measures);
   return measures;
 }
  @POST
  @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  public Response newLifeStatus(LifeStatus record) throws IOException {
    // we should create a new Lifestatus and archive the old one
    System.out.println("POST /person/{id}/{measureType} aka request 8!");
    Person existingPerson = Person.getPersonById(id);
    if (existingPerson == null) {
      return Response.status(404)
          .entity("You are trying to post a measure related to a non existing Person!")
          .build();
    }
    System.out.println("personID valid");
    System.out.println("getting measuredefinition with type=" + measureType);
    MeasureDefinition md = null;
    try {
      List<MeasureDefinition> mdlist = MeasureDefinition.getAll();
      for (MeasureDefinition measureDefinition : mdlist) {
        if (measureDefinition.getMeasureName().equals(measureType)) {
          md = measureDefinition;
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println("errore nel prendere la measuredef");
    }
    if (record.getMeasureDefinition() != null) {
      if (!record.getMeasureDefinition().getMeasureName().equals(md.getMeasureName())) {
        System.out.println(
            "You set a path with measuretypeid="
                + measureType
                + " that means="
                + md.getMeasureName()
                + "\nbut you set in the body a measuredef="
                + record.getMeasureDefinition().getMeasureName());
      }
    }
    // to update lifestatus we update the lifestatus with the same
    // measuretype
    LifeStatus existingLS = this.getLifestatusByPersonIdAndMeasureType(id, md.getMeasureName());
    if (existingLS != null) {
      System.out.println("removing old lifestatus");
      LifeStatus.removeLifeStatus(existingLS);
    }
    System.out.println("preparing new lifestatus");
    LifeStatus toSave = new LifeStatus();
    toSave.setMeasureDefinition(md);
    toSave.setPerson(existingPerson);
    toSave.setValue(record.getValue());
    LifeStatus.saveLifeStatus(toSave);

    // and put the record in the history too
    HealthMeasureHistory hm = new HealthMeasureHistory();
    hm.setPerson(existingPerson);
    hm.setMeasureDefinition(md);
    hm.setValue(record.getValue());
    hm.setTimestamp(new Date(System.currentTimeMillis()));
    System.out.println("saving new lifestatus");
    HealthMeasureHistory.saveHealthMeasureHistory(hm);
    System.out.println("end");

    return Response.ok(toSave).build();
  }
  private List<HealthMeasureHistory> getHMhistoryByUserAndType(int id2, String measureType) {
    System.out.println(
        "Reading HMhistory from DB for person with id: "
            + id2
            + " and measuretype of type="
            + measureType);

    // this will work within a Java EE container, where not DAO will be
    // needed
    // Person person = entityManager.find(Person.class, personId);

    List<HealthMeasureHistory> hMhistoryList = HealthMeasureHistory.getAll();
    System.out.println("Got " + hMhistoryList.size() + " records");
    List<HealthMeasureHistory> retval = new ArrayList<>();
    for (HealthMeasureHistory hm : hMhistoryList) {
      if (hm.getPerson() != null) {
        if (hm.getPerson().getName() != null) {
          System.out.println(hm.getPerson().getName());
        }
        int idperson = hm.getPerson().getIdPerson();
        if (id2 == idperson) {
          if (hm.getMeasureDefinition() != null) {
            String measurename = hm.getMeasureDefinition().getMeasureName();
            if (measurename.equals(measureType)) {
              retval.add(hm);
              System.out.println(
                  hm.getPerson().getName()
                      + " "
                      + hm.getValue()
                      + " "
                      + hm.getMeasureDefinition().getMeasureName());
            }
          } else {
            System.out.println("no measurdef! for this record");
          }
        }
      }
    }
    return retval;
  }