/**
   * This method is used by the updatePerson method to sync a person before updating it on the
   * database. When updating only specific fields, other fields will be set to null, this method
   * makes sure that the old values of the attributes that will not be updated remain in the
   * database.
   *
   * @param oldPerson The person that was retrieved from the database. It contains the old
   *     information of a person.
   * @param updatedPerson The person containing only the attributes that will be updated.
   * @return A person with updated information but also keeping its old attributes
   */
  public static Person syncPerson(Person oldPerson, Person updatedPerson) {
    updatedPerson.setPersonId(oldPerson.getPersonId());
    updatedPerson.setMeasures(
        oldPerson.getMeasures()); // Prevent Measures to be lost when updating a person

    if (updatedPerson.getFirstname() == null) updatedPerson.setFirstname(oldPerson.getFirstname());

    if (updatedPerson.getLastname() == null) updatedPerson.setLastname(oldPerson.getLastname());

    if (updatedPerson.getBirthdate() == null) updatedPerson.setBirthdate(oldPerson.getBirthdate());

    return updatedPerson;
  }
  /**
   * 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;
  }