/**
   * 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;
  }
  @Test
  public void test_sql_custom_crud() {

    Person _person =
        doInJPA(
            entityManager -> {
              Person person = new Person();
              person.setName("John Doe");
              entityManager.persist(person);
              return person;
            });

    doInJPA(
        entityManager -> {
          Long postId = _person.getId();
          Person person = entityManager.find(Person.class, postId);
          assertNotNull(person);
          entityManager.remove(person);
        });

    doInJPA(
        entityManager -> {
          Long postId = _person.getId();
          Person person = entityManager.find(Person.class, postId);
          assertNull(person);
        });
  }
Ejemplo n.º 3
0
  /**
   * Will copy into this Person, the details from the input Person.
   *
   * @param rs - The Person from which the details to be copied from.
   */
  public void sync(final ResearchStaff rs) {
    super.sync(rs);
    setNciIdentifier(rs.getNciIdentifier());
    if (getAddress() != null) {
      getAddress().sync(rs.getAddress());
    } else {
      setAddress(rs.getAddress());
    }

    // sync the site researchstaffs
    CollectionUtils.forAllDo(
        getSiteResearchStaffs(),
        new Closure<SiteResearchStaff>() {
          public void execute(SiteResearchStaff srs) {
            SiteResearchStaff otherSRS = rs.findSiteResearchStaff(srs);
            srs.sync(otherSRS);
          }
        });

    // add new site researchstaff if needed
    for (SiteResearchStaff srs : rs.getSiteResearchStaffs()) {
      SiteResearchStaff availableSRS = findSiteResearchStaff(srs);
      if (availableSRS == null) addSiteResearchStaff(srs);
    }
  }
  /**
   * 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;
  }
Ejemplo n.º 5
0
 @Override
 public int hashCode() {
   int result = person != null ? person.hashCode() : 0;
   result = 31 * result + (username != null ? username.hashCode() : 0);
   result = 31 * result + (encryptedPassword != null ? encryptedPassword.hashCode() : 0);
   result = 31 * result + (roles != null ? roles.hashCode() : 0);
   return result;
 }
Ejemplo n.º 6
0
 public static void saveLog(String detail) {
   Log log = new Log();
   Person person = MailManagementSystemUI.person;
   log.setUsername(person.getUsername());
   log.setRole(person.getRole().getName());
   log.setActionDate(new Date());
   log.setDetail(detail);
   EntityManager entityManager = initialDataLoader.em;
   if (!entityManager.getTransaction().isActive()) entityManager.getTransaction().begin();
   try {
     entityManager.persist(log);
     entityManager.getTransaction().commit();
   } catch (PersistenceException e) {
     System.out.println(e.getMessage());
     entityManager.getTransaction().rollback();
   }
 }
Ejemplo n.º 7
0
  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof User)) return false;

    User user = (User) o;

    if (person != null ? !person.equals(user.person) : user.person != null) return false;
    if (username != null ? !username.equals(user.username) : user.username != null) return false;
    if (encryptedPassword != null
        ? !encryptedPassword.equals(user.encryptedPassword)
        : user.encryptedPassword != null) return false;
    return roles == user.roles;
  }
  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;
  }
  /**
   * 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;
  }
Ejemplo n.º 10
0
 @PreRemove
 private void removeFamiliesFromPersons() {
   for (Person u : familyMembers) {
     u.getFamilies().remove(this);
   }
 }