@Override public Set<Person> getSiblingsAll(Person thisPerson) { // return full and half siblings of thisPerson // exclude thisPerson from the set Set<Person> siblings = new HashSet<Person>(); List<Person> parents = new ArrayList<Person>(); em.getTransaction().begin(); Query q = em.createQuery( "select b.parents from Birth b, IN(b.person) p WHERE p.id = :personId" + " or p.id = :parent2Id"); q.setParameter("personId", thisPerson.getId()); parents.addAll((Collection<? extends Person>) q.getResultList()); if (!parents.isEmpty()) { if (parents.size() == 2) { siblings = getChildrenAll(parents.get(0), parents.get(1)); } else if (parents.size() == 1) { siblings = getChildren(parents.get(0)); } } em.getTransaction().commit(); siblings.remove(thisPerson); return siblings; }
@Override public List<Event> findAllEvents(Person person) { List<Event> allEvents = new ArrayList<Event>(); // Polymorphic queries don't work for // @MappedSuperclass entities ! :-( Event e = findBirthRecord(person); if (e != null) { allEvents.add(e); } e = findDeathRecord(person); if (e != null) { allEvents.add(e); } e = findAdoptionRecord(person); if (e != null) { allEvents.add(e); } allEvents.addAll(findMarriages(person)); allEvents.addAll(findDivorces(person)); allEvents.addAll(getChildrenBirths(person)); allEvents.addAll(getChildAdoptionRecords(person)); System.out.println("findAllEvents: size of allEvents = " + allEvents.size()); return allEvents; }
@Override public Adoption findAdoptionRecord(Person person) { // Could be null em.getTransaction().begin(); Query q = em.createQuery("select a from Adoption a WHERE a.person.id = :idParam"); q.setParameter("idParam", person.getId()); List<Adoption> results = (List<Adoption>) q.getResultList(); em.getTransaction().commit(); return (results != null && results.size() > 0) ? results.get(0) : null; }
@Override public Death findDeathRecord(Person person) { // Could be null List<Death> myresults = new ArrayList<Death>(); em.getTransaction().begin(); TypedQuery<Death> q = em.createQuery("select b from Death b WHERE b.person.id = :idParam", Death.class); q.setParameter("idParam", person.getId()); myresults.addAll(q.getResultList()); em.getTransaction().commit(); return myresults.size() >= 1 ? myresults.get(0) : null; }
@Override public void addAdoptedParent(Person child, Person parent) { em.getTransaction().begin(); TypedQuery<Adoption> q = em.createQuery("select b from Adoption b where b.person.id = :idParam", Adoption.class); q.setParameter("idParam", child.getId()); List<Adoption> results = (List<Adoption>) q.getResultList(); Adoption a = (results != null && results.size() > 0) ? results.get(0) : null; if (a != null) { a.addParent(parent); em.merge(a); } em.getTransaction().commit(); }
@Override public void addParent(Person child, Person parent) { em.getTransaction().begin(); TypedQuery<Birth> q = em.createQuery("select b from Birth b where b.person.id = :idParam", Birth.class); q.setParameter("idParam", child.getId()); List<Birth> results = (List<Birth>) q.getResultList(); Birth b = (results != null && results.size() > 0) ? results.get(0) : null; if (b != null) { b.addParent(parent); em.merge(b); } em.getTransaction().commit(); }