LifeStatus getLifestatusByPersonIdAndMeasureType(int personID, String measurename) { List<LifeStatus> list = LifeStatus.getAll(); for (LifeStatus ls : list) { if (ls.getPerson().getIdPerson() == personID && ls.getMeasureDefinition().getMeasureName().equals(measurename)) { return ls; } } return null; }
@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(); }