public void init() {
    System.out.println("Getting child list");
    childList = new ArrayList<>();
    // return ejbFacade.getEntityManager().createNamedQuery("Child.findAllSorted").getResultList();
    String queryString =
        "SELECT e FROM Child e " + buildWhereClause() + " ORDER BY e.surname ASC, e.firstname ASC";

    List<Child> lc = ejbFacade.getEntityManager().createQuery(queryString).getResultList();
    for (Child c : lc) {
      boolean add = false;
      if (searchKS1 != true || searchKS2 != true || searchKS3 != true || searchKS4 != true) {
        // Check year.
        int childYear = settings.getSchoolYearFor(c, 0);
        if ((searchKS1 && childYear >= 0 && childYear <= 2)
            || (searchKS2 && childYear >= 3 && childYear <= 6)
            || (searchKS3 && childYear >= 7 && childYear <= 9)
            || (searchKS4 && childYear >= 10 && childYear <= 11)) {
          add = true;
        }
      } else {
        add = true;
      }
      if (add && c.getExtraItems() != null && c.getExtraItems().size() > 0) {
        // Create a child container for each one.
        childList.add(new ChildContainer(c));
      }
    }
  }
 @Override
 public int compareTo(ChildContainer o) {
   // Compare by surname.
   int ret = child.getSurname().compareTo(o.getChild().getSurname());
   if (ret == 0) {
     // Surname is the same, just by firstname.
     ret = child.getFirstname().compareTo(o.getChild().getFirstname());
     // If still 0, use the id.
     if (ret == 0) {
       ret = child.getChildid().compareTo(o.getChild().getChildid());
     }
   }
   return ret;
 }
 public ChildContainer(Child child) {
   this.child = child;
   tickets = new ArrayList<>();
   List<ExtraItemEntry> extras = child.getExtraItems();
   int size = extras.size();
   List<String> rowEntries = new ArrayList<>();
   for (int i = 0; i < size; ++i) {
     int itemCount = extras.get(i).getItemCount();
     for (int j = 0; j < itemCount; ++j) {
       rowEntries.add(extras.get(i).getItem().getName());
     }
   }
   size = rowEntries.size();
   for (int i = 0; i < size; i += 3) {
     List<String> row = new ArrayList<>();
     for (int j = 0; j < 3; ++j) {
       if (i + j < size) {
         row.add(rowEntries.get(i + j));
       } else {
         row.add(null);
       }
     }
     tickets.add(new TicketRow(row));
   }
 }
 @Override
 public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
   if (object == null) {
     return null;
   }
   if (object instanceof Child) {
     Child o = (Child) object;
     return getStringKey(o.getChildid());
   } else {
     Logger.getLogger(this.getClass().getName())
         .log(
             Level.SEVERE,
             "object {0} is of type {1}; expected type: {2}",
             new Object[] {object, object.getClass().getName(), Activity.class.getName()});
     return null;
   }
 }
 //  public EventType prepareCreate() {
 //    created = new EventType();
 //    initializeEmbeddableKey();
 //    return created;
 //  }
 public Child create(Child newChild) {
   System.out.println("CREATE");
   Child ret =
       persist(
           PersistAction.CREATE,
           newChild,
           MessageFormat.format(
               ResourceBundle.getBundle("/Bundle").getString("ChildCreated"),
               newChild.getFirstname()));
   if (!JsfUtil.isValidationFailed()) {
     items = null; // Invalidate list of items to trigger re-query.
   }
   return ret;
 }
 public void update(Child child) {
   Child ret =
       persist(
           PersistAction.UPDATE,
           child,
           MessageFormat.format(
               ResourceBundle.getBundle("/Bundle").getString("ChildUpdated"),
               child.getFirstname()));
   if (ret != null) {
     int index = items.indexOf(child);
     if (index > -1) {
       items.set(index, ret);
     }
   }
 }