/** * Retrieve the persistent bean with id <code>form.getIdFromForm()</code> from the persistent * storage, and set it in the form. This method does not deal with transactions. A transaction * needs to be started and closed around a call to this method. * * @pre form != null; * @pre asyncCRUD != null; */ private void retrieveWithId(final CrudDynaActionForm form, final AsynchronousCRUD asyncCRUD) throws IdException, TechnicalException { assert form != null; assert asyncCRUD != null; Long id = form.getIdFromForm(); // IdException Class type = form.getPersistentBeanType(); if (id == null) { throw new IdException("ID_NULL", null, type); // $NON-NLS-1$ } if (LOG.isDebugEnabled()) { LOG.debug( "retrieving persistent bean with id " //$NON-NLS-1$ + id.toString() + " and type " //$NON-NLS-1$ + type.getName() + "..."); //$NON-NLS-1$ } form.setPersistentBean(asyncCRUD.retrievePersistentBean(id, type)); // pre id != null; IdNotFoundException if (LOG.isDebugEnabled()) { // if makes that there really is lazy loading if not in debug LOG.debug( "retrieved persistent bean is " //$NON-NLS-1$ + form.getPersistentBean()); } assert form.getPersistentBean() != null; assert form.getPersistentBean() .getId() .toString() .equals(form.get(CrudDynaActionForm.ID_PROPERTY_NAME)); }
/** * 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); }
/** * Delete request mode means that the user requests the application to delete an existing bean. In * delete request mode, existing data is deleted from persistent storage. The request should * provide data for the {@link PersistentBean#getId()}. If the delete operation succeeds, the data * is displayed in a non-editable way, with a notice that signifies successful deletion. If the * delete operation fails, the data is displayed in a non-editable way, with an error notice. 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 persistent bean will be deleted. This is done * to have an early warning of errors in the key, or access violations. * * @idea (jand) This would also make it possible to check here whether all information in the * request matches the information in persistent storage during this transaction. * @pre form != null; * @pre form.getPersistentBean() != null; * @pre request != null; */ private void templateDelete(final CrudDynaActionForm form, final HttpServletRequest request) throws CompoundPropertyException, IdException, TechnicalException { assert form != null; // assert form.getPersistentBean() != null; assert request != null; if (LOG.isDebugEnabled()) { LOG.debug("delete action ..."); // $NON-NLS-1$ } form.setViewModeEdit(false); AsynchronousCRUD asyncCRUD = createAsynchronousCRUD(request); DeletedEvent pbdEvent = null; try { asyncCRUD.startTransaction(); retrieveWithId(form, asyncCRUD); // IdException assert form.getPersistentBean() != null; pbdEvent = new DeletedEvent(form.getPersistentBean()); asyncCRUD.deletePersistentBean(form.getPersistentBean()); asyncCRUD.commitTransaction(form.getPersistentBean()); assert form.getPersistentBean().getId() == null; form.beanToId(); // must be null form.setViewModeDeleted(true); // rae.isEmpty()); if (LOG.isDebugEnabled()) { LOG.debug("delete action succeeded"); // $NON-NLS-1$ } } catch (CompoundPropertyException cpExc) { asyncCRUD.cancelTransaction(); form.setViewModeDeleted(false); throw cpExc; } catch (IdException pkvExc) { asyncCRUD.cancelTransaction(); form.setViewModeDeleted(false); throw pkvExc; } finally { form.beanToForm(asyncCRUD); releaseAsynchronousCRUD(asyncCRUD); } fireCommittedEvent(pbdEvent); assert !form.isViewModeNew(); assert !form.isViewModeEdit(); }