/**
  * Create request mode means that the user requests the application to create a new persistent
  * bean with supplied information.. In create request mode, new data is stored in persistent
  * storage. The request should provide data for all properties of the {@link PersistentBean},
  * except the {@link PersistentBean#getId()}. If the create operation succeeds, the data is simply
  * displayed in a non-editable way. If the create operation fails, the data is displayed in an
  * HTML form, so that it can be corrected.
  *
  * @pre form != null;
  * @pre form.getPersistentBean() != null;
  * @pre request != null;
  */
 private void templateCreate(final CrudDynaActionForm form, final HttpServletRequest request)
     throws CompoundPropertyException, TechnicalException {
   assert form != null;
   assert request != null;
   if (LOG.isDebugEnabled()) {
     LOG.debug("create action ..."); // $NON-NLS-1$
   }
   form.initBean();
   /*
    * BeanInstantiationException; this means that the app is wrongly
    * configured; this will generate a general error page.
    */
   assert form.getPersistentBean() != null;
   assert form.getPersistentBean().getId() == null;
   form.setViewModeDeleted(false);
   AsynchronousCRUD asyncCRUD = createAsynchronousCRUD(request);
   try {
     asyncCRUD.startTransaction();
     form.formToBean(asyncCRUD); // PropertyException, inside transaction
     form.getPersistentBean().setId(null);
     asyncCRUD.createPersistentBean(form.getPersistentBean());
     // PropertyException
     form.beanToId();
     form.beanToForm(asyncCRUD); // inside transaction
     asyncCRUD.commitTransaction(form.getPersistentBean());
     form.setViewModeEdit(false);
     CreatedEvent pbnEvent = new CreatedEvent(form.getPersistentBean());
     fireCommittedEvent(pbnEvent);
     assert !form.isViewModeNew();
     assert !form.isViewModeDeleted();
     if (LOG.isDebugEnabled()) {
       LOG.debug("create action succeeded"); // $NON-NLS-1$
     }
   } catch (CompoundPropertyException cpExc) {
     form.beanToForm(asyncCRUD);
     asyncCRUD.cancelTransaction();
     form.setViewModeNew(true);
     assert form.isViewModeEdit();
     assert !form.isViewModeDeleted();
     throw cpExc;
   } finally {
     releaseAsynchronousCRUD(asyncCRUD);
   }
 }
 /**
  * Update request mode means that the user requests the application to update existing information
  * with supplied information. In update request mode, data is stored in persistent storage. The
  * request should provide data for all properties of the {@link PersistentBean}. If the update
  * operation succeeds, the data is imply displayed in a non-editable way. If the update operation
  * fails, the data is displayed in an HTML form, so that it can be corrected. This method first
  * sets the id of the persistent bean from the request. Next, the persistent bean is retrieved
  * from the database. Only after that, the information from the request is filled out into the
  * bean. This is done to have an early warning of errors in the key, or access violations, but
  * mainly to make it possible to operate easily with a request that contains only partial
  * information, i.e., a request that only carries values for a subset of the properties of the
  * persistent bean. With this protocol, the given information will overwrite information that is
  * currently in persistent storage, but if there is no data in the request for a given property,
  * it's old value is retained. Once the data that is offered in the request is copied successfully
  * in the persistent bean, it is stored.
  *
  * @pre form != null;
  * @pre form.getPersistentBean() != null;
  * @pre request != null;
  */
 private void templateUpdate(final CrudDynaActionForm form, final HttpServletRequest request)
     throws IdException, CompoundPropertyException, TechnicalException {
   assert form != null;
   assert request != null;
   if (LOG.isDebugEnabled()) {
     LOG.debug("update action ..."); // $NON-NLS-1$
   }
   AsynchronousCRUD asyncCRUD = createAsynchronousCRUD(request);
   try {
     asyncCRUD.startTransaction();
     retrieveWithId(form, asyncCRUD); // IdException, TechnicalException
     assert form.getPersistentBean() != null;
     // @mudo (dvankeer): toString() needs to be replaced by something more
     //                   usefull.
     String oldBeanString = form.getPersistentBean().toString();
     form.formToBean(asyncCRUD); // PropertyException, inside transaction
     asyncCRUD.updatePersistentBean(form.getPersistentBean()); // PropertyException
     form.beanToForm(asyncCRUD); // inside transaction
     asyncCRUD.commitTransaction(form.getPersistentBean());
     releaseAsynchronousCRUD(asyncCRUD);
     // do not do this in a finally block; this can throw exceptions too
     form.setViewModeEdit(false);
     // @todo (jand): try to move this delta stuff to AsynchronousCRUD instead
     //               of here
     fireCommittedEvent(new UpdatedEvent(form.getPersistentBean(), oldBeanString));
   } catch (CompoundPropertyException cpExc) {
     form.beanToForm(asyncCRUD);
     if (LOG.isDebugEnabled()) {
       LOG.debug("update action failed; cancelling ...", cpExc); // $NON-NLS-1$
     }
     asyncCRUD.cancelTransaction();
     releaseAsynchronousCRUD(asyncCRUD);
     // do not do this in a finally block; this can throw exceptions too
     form.setViewModeEdit(true);
     LOG.debug("update action cancelled; rethrowing exeption"); // $NON-NLS-1$
     throw cpExc;
   }
   /*
    * on IdException, we go to a different page that shows a not found message.
    * For this page we do not need to set any information
    */
   form.setViewModeDeleted(false);
   form.setViewModeNew(false);
 }
 /**
  * New request mode means that the user requests the application to present a form with default
  * information, to be able to request the application to create a new object in the next step. In
  * new request mode, a new persistent bean is created with default data and displayed in an HTML
  * form, so that it can be edited. All the data that is already in this request is used to display
  * the new entity the first time.
  *
  * @throws BeanInstantiationException Could not create a new bean of the expected type.
  * @throws TechnicalException Could evaluate data for a technical reason
  * @throws CompoundPropertyException Some of the provided data is wrong.
  */
 private void templateNew(final CrudDynaActionForm form, final HttpServletRequest request)
     throws BeanInstantiationException, TechnicalException, CompoundPropertyException {
   if (LOG.isDebugEnabled()) {
     LOG.debug("new action ..."); // $NON-NLS-1$
   }
   form.initBean(); // bean contains programmatic defaults
   /*
    * BeanInstantiationException; this means that the app is wrongly
    * configured; this will generate a general error page.
    */
   assert form.getPersistentBean() != null;
   assert form.getPersistentBean().getId() == null;
   form.beanToId();
   AsynchronousCRUD asyncCRUD = createAsynchronousCRUD(request);
   CompoundPropertyException cpExcStore = null;
   try {
     // @todo (dvankeer): Why re-use the data?
     try {
       form.formToBean(asyncCRUD);
       /* get the data that is already in this request,
       overwriting programmatic defaults */
     } catch (CompoundPropertyException cpExc) {
       cpExcStore = cpExc;
       // remember this exception
     }
     // we are not doing this in a transaction, deliberately
     // @todo (jand): is this a good idea?
     form.beanToForm(asyncCRUD);
     // copy default data and data already set into the form
   } finally {
     releaseAsynchronousCRUD(asyncCRUD);
   }
   form.setViewModeNew(true);
   assert form.isViewModeEdit();
   assert !form.isViewModeDeleted();
   if (LOG.isDebugEnabled()) {
     LOG.debug("new action succeeded"); // $NON-NLS-1$
   }
   if (cpExcStore != null) {
     throw cpExcStore;
   }
 }
 /**
  * Display request mode means that the user requested the application to display an object. In
  * display request mode, data is retrieved from persistent storage and simply displayed in a
  * non-editable way. The request should provide an {@link PersistentBean#getId()}. Edit request
  * mode means that the user request the application to display an existing object for editing. In
  * edit request mode, data is retrieved from persistent storage and displayed in an HTML form, so
  * that it can be edited. The request should provide an {@link PersistentBean#getId()}.
  *
  * @pre form.getPersistentBean() != null;
  * @pre request != null;
  */
 private void templateRetrieve(
     final CrudDynaActionForm form, final boolean viewModeEdit, final HttpServletRequest request)
     throws IdException, TechnicalException {
   assert request != null;
   if (LOG.isDebugEnabled()) {
     LOG.debug(
         "retrieve for " //$NON-NLS-1$
             + (viewModeEdit
                 ? "edit" //$NON-NLS-1$
                 : "display") //$NON-NLS-1$
             + " ..."); //$NON-NLS-1$
   }
   AsynchronousCRUD asyncCRUD = createAsynchronousCRUD(request);
   try {
     // we are not doing this in a transaction, deliberately
     retrieveWithId(form, asyncCRUD); // IdException
     if (LOG.isDebugEnabled()) {
       LOG.debug("retrieve action succeeded"); // $NON-NLS-1$
     }
     form.beanToForm(asyncCRUD);
     /*
      * We are not doing this in a transaction, deliberately. This means data
      * that is shown could be out of sync; but chances are this will hardly
      * occur, and if it does, errors will be caught when something is really
      * done. @todo (jand): think more about this
      */
     /*
      * on IdException, we go to a different page that shows a not found
      * message. For this page we do not need to set any information
      */
   } finally {
     releaseAsynchronousCRUD(asyncCRUD);
   }
   form.setViewModeEdit(viewModeEdit);
   form.setViewModeDeleted(false);
   form.setViewModeNew(false);
 }