When you create a form handler that subclasses RepositoryFormHandler or TransactionalRepositoryFormHandler, make sure that its methods can correspond with the Transaction Manager. There are two ways to do this:
Base New Handler Methods on handleUpdate Source Code so that you can reuse the transaction code in it, then modify the rest accordingly.
Modify Existing Handler Methods by inserting code before or after their execution in the
preX
orpostX
methods, respectively.
Base New Handler Methods on handleUpdate Source Code
The code provided here implements the handleUpdate
method. Create your own handler methods by making changes to this code sample and inserting it into your subclassed form handler:
public boolean handleUpdate(DynamoHttpServletRequest pRequest, DynamoHttpServletResponse pResponse) throws ServletException, IOException { TransactionDemarcation td = getTransactionDemarcation(); TransactionManager tm = getTransactionManager(); try { if (tm != null) td.begin(tm, td.REQUIRED); int status = checkFormError(getUpdateErrorURL(), pRequest, pResponse); if (status != STATUS_SUCCESS) return status == STATUS_ERROR_STAY; // update the repository item preUpdateItem(pRequest, pResponse); if (!getFormError()) updateItem(pRequest, pResponse); postUpdateItem(pRequest, pResponse); // try to redirect on errors if ((status = checkFormError(getUpdateErrorURL(), pRequest, pResponse)) != STATUS_SUCCESS) return status == STATUS_ERROR_STAY; // try to redirect on success return checkFormSuccess(getUpdateSuccessURL(), pRequest, pResponse); } catch (TransactionDemarcationException e) { throw new ServletException(e); } finally { try { if (tm != null) td.end(); } catch (TransactionDemarcationException e) { } } }
Modify Existing Handler Methods
The three existing submit handler methods (handleCreate
, handleUpdate
, and handleDelete
) each provide a pair of empty pre
and post
methods where you can add custom code.
It is likely that you use either the preX
or the postX
method for a given existing handler method although you can customize both. For example, consider a subclass of TransactionalRepositoryFormHandler
where preUpdate
and postUpdate
are used:
The form is rendered, which causes
getX
method to display current values for properties used in the form.The user fills in the form and submits it.
The form handler’s
beforeSet
method is invoked. If a transaction is not currently in progress, theTransactionalRepositoryFormHandler
component creates a one.If tag converters are used, they are applied to the specified content. Any form exceptions that occur now or at any point during the form handler execution are saved to the form handler’s
formException
property.The
setX
method is called, followed by the form handler’shandleUpdate
method. Severe form exceptions might cause form processing to stop and the transaction to rollback, before redirecting users to a different page.The
preUpdateItem
method is invoked. ThepreX
method for thehandleUpdate
method ispreUpdateItem
. Serious errors generated from this operation might also prompt the transaction to rollback.The
updateItem
method, which is thehandleUpdate
method responsible for processing the content and updating the database, is invoked. Again, this is another operation that can cause a transaction to rollback when serious errors are detected. At this point, the changes made by the actions associated with the transaction are kept private, meaning that they are only visible within the transaction itself.The
postUpdateItem
method is invoked. Again, the transaction is rolled back if serious errors are detected.The
afterSet
method is invoked. If the transaction was started by thebeforeSet
method, the transaction concludes and the content it saved to the database is publicly visible.
Use the preX
method when you want to expand the constraints for data validation. For example, you might want to check if the user-entered zip code and country correspond to each other. The countries that use zip codes require them to be a certain length. The preX
method can verify that a zip code uses the appropriate format for the country, before saving the zip code and country values to the database. Any discrepancies produce a form exception and roll back the transaction.
The postX
method is useful for verifying user entered-data after that data has been converted by tag converters. For example, a form handler that saves credit card information might use a postX
method to handle authorization. After that credit card number has been formatted correctly and all related information is updated in the database, the postX
method executes. If the authorization fails, the transaction is rolled back, the original data refreshed, a form error exception is thrown, and the user is redirected to a page where the use can re-enter credit card information.