/** * Update person * * @param p * @return */ public Person updatePerson(Person p) { // Call setters if (p.birthdate != null) { this.setBirthdate(p.birthdate); } if (p.email != null) { this.setEmail(p.email); } if (p.firstname != null) { this.setFirstname(p.getFirstname()); } if (p.lastname != null) { this.setLastname(p.getLastname()); } EntityManager em = LifeStyleDao.instance.createEntityManager(); EntityTransaction tx = em.getTransaction(); tx.begin(); p = em.merge(this); tx.commit(); LifeStyleDao.instance.closeConnections(em); return p; }
/** * Save new person * * @param p * @return */ public static Person savePerson(Person p) { if (p.healthProfile != null) { // Cascade health profile and add to history Person.cascadeHealthProfile(p); Person.copyHealthProfileToHistory(p); } EntityManager em = LifeStyleDao.instance.createEntityManager(); EntityTransaction tx = em.getTransaction(); tx.begin(); em.persist(p); tx.commit(); LifeStyleDao.instance.closeConnections(em); return p; }
private static Person copyHealthProfileToHistory(Person p) { // Create new history list List<HealthProfileHistory> history = new ArrayList<HealthProfileHistory>(); for (HealthProfile healthProfile : p.healthProfile) { // Create new history object HealthProfileHistory h = new HealthProfileHistory(); h.setMeasureType(healthProfile.getMeasureType()); h.setMeasureValue(healthProfile.getMeasureValue()); h.setPerson(p); h.setTimestamp(healthProfile.getTimestamp()); // Add to history list history.add(h); } p.setHealthProfileHistory(history); return p; }