/**
  * <code>pb</code> is part of the result
  *
  * @todo move method static as utility method
  */
 @MethodContract(
     pre = {@Expression("pb != null"), @Expression("pd.id == null")},
     post = {})
 private List<PersistentBean<?>> relatedFreshPersistentBeans(PersistentBean<?> pb) {
   assert pb != null;
   assert pb.getPersistenceId() == null;
   List<PersistentBean<?>> result = new LinkedList<PersistentBean<?>>();
   result.add(pb);
   int current = 0;
   while (current < result.size()) {
     PersistentBean<?> currentPb = result.get(current);
     current++;
     PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(currentPb);
     for (int i = 0; i < pds.length; i++) {
       PersistentBean<?> related = relatedPeristentBean(currentPb, pds[i]);
       if ((related != null)
           && (related.getPersistenceId() == null)
           && (!result.contains(related))) {
         /* if it is a fresh bean and it is the first time that we encounter it,
          * it is to be part of the result;
          * we also need to process it further: remember it on the agenda */
         result.add(related); // adds at the end of the list; size++
       }
     }
   }
   return Collections.unmodifiableList(result);
 }
 /**
  * Reset the id of the {@link PersistentBean PersistentBeans} in <code>persistentBeans</code> to
  * <code>null</code>.
  */
 @MethodContract(
     pre = @Expression("persistentBeans != null"),
     post =
         @Expression(
             "for (PersistentBean<?> persistentBean : persistentBeans) {persistentBean.id == null}"))
 private void resetId(Set<PersistentBean<?>> persistentBeans) {
   assert persistentBeans != null;
   for (PersistentBean<?> persistentBean : persistentBeans) {
     persistentBean.setPersistenceId(null);
   }
 }
 @MethodContract(
     pre = @Expression("session != null"),
     post = {})
 public final void updatePersistentBean(final PersistentBean<?> pb)
     throws PropertyException, ApplicationException {
   if (LOG.isDebugEnabled()) {
     LOG.debug("Updating bean \"" + pb + "\" ...");
   }
   dependency(getSession(), "session");
   preArgumentNotNull(pb, "pb");
   pre(pb.getPersistenceId() != null, NO_ID_IN_PERSISTENT_OBJECT);
   pre(isInTransaction(), NO_PENDING_TRANSACTION);
   try {
     if (LOG.isTraceEnabled()) {
       LOG.trace("Normalizing  \"" + pb + "\" ...");
     }
     pb.normalize();
     pb.checkCivility(); // PropertyException
     // MUDO (jand) normalize and checkCivility off all reachable PB's (cascade)
     if (LOG.isTraceEnabled()) {
       LOG.trace("Normalization of \"" + pb + "\" done.");
     }
     getSession().update(pb);
     /*
      * If there is a persistent instance with the same identifier, different
      * from this pb, an exception is thrown. This cannot happen since pb is
      * fresh from the DB: we got it with retrieve or created it ourself.
      */
     LOG.debug("Update succeeded.");
   } catch (HibernateException hExc) {
     LOG.debug("Update failed.");
     handleHibernateException(hExc, "updating");
     // throws ApplicationException, PersistenceExternalError
     // MUDO need code to throw IdNotFoundException
   }
 }
 @MethodContract(
     pre = @Expression("session != null"),
     post = {})
 public void deletePersistentBean(final PersistentBean<?> pb) throws ApplicationException {
   LOG.debug("Deleting persistent bean \"" + pb + "\" ...");
   dependency(getSession(), "session");
   preArgumentNotNull(pb, "pb");
   pre(pb.getPersistenceId() != null, NO_ID_IN_PERSISTENT_OBJECT);
   pre(isInTransaction(), NO_PENDING_TRANSACTION);
   try {
     getSession().delete(pb);
     $deleted.add(pb);
     // MUDO (jand) take into account cascade delete
   } catch (HibernateException hExc) {
     LOG.debug("Deletion failed.");
     handleHibernateException(hExc, "Deleting");
     // throws ApplicationException, PersistenceExternalError
     // MUDO need code to throw IdNotFoundException
   }
   LOG.debug("Deletion succeeded.");
 }
 @MethodContract(
     pre = @Expression("session != null"),
     post = {})
 public final void createPersistentBean(final PersistentBean<?> pb)
     throws PropertyException, ApplicationException {
   LOG.debug("Creating new record for bean \"" + pb + "\" ...");
   dependency(getSession(), "session");
   preArgumentNotNull(pb, "pb");
   pre(pb.getPersistenceId() == null, SHOULD_HAVE_NO_ID_IN_PERSISTENT_OBJECT);
   pre(isInTransaction(), NO_PENDING_TRANSACTION);
   try {
     LOG.trace("Gather all beans to be created, taking into account cascade");
     List<PersistentBean<?>> allToBeCreated = relatedFreshPersistentBeans(pb);
     // we need to normalize and check all these beans
     Iterator<PersistentBean<?>> iter = allToBeCreated.iterator();
     while (iter.hasNext()) {
       PersistentBean<?> current = iter.next();
       LOG.trace("Normalizing  \"" + current + "\" and checking civility ...");
       current.normalize();
       current.checkCivility(); // PropertyException
       // MUDO (jand) package all PropertyExceptions for all beans together; don't stop after
       // one!!!
       LOG.trace("\"" + current + "\" checks out ok");
     }
     getSession().save(pb);
     // cascade done by Hibernate; all elements of allToBeCreated are created
     // IDEA (jand) by doing the cascade ourselfs, we might be able to get better exceptions
     $created.addAll(allToBeCreated);
     if (LOG.isDebugEnabled()) {
       LOG.debug("Creating succesfull.");
       iter = allToBeCreated.iterator();
       while (iter.hasNext()) {
         PersistentBean<?> current = iter.next();
         LOG.debug("    generated " + current.getPersistenceId() + " as id for " + current);
       }
     }
   } catch (HibernateException hExc) {
     LOG.debug("Creation of new record failed.");
     handleHibernateException(hExc, "Creating");
     // throws ApplicationException, ExternalError
   }
   assert pb.getPersistenceId() != null;
 }