Version 4.10.0

hirondelle.web4j.action
Class ActionTemplateListAndEdit

Object
  extended by hirondelle.web4j.action.ActionImpl
      extended by hirondelle.web4j.action.ActionTemplateListAndEdit
All Implemented Interfaces:
Action

public abstract class ActionTemplateListAndEdit
extends ActionImpl

Template for "all-in-one" Actions, which perform common operations on a Model Object.

Typically a single JSP is used, for displaying both a listing of Model Objects, and an accompanying form for editing these Model Objects one at a time. This style is practical when :

The SupportedOperations for this template are a subset of the members of the Operation enumeration. If other operations are desired, then this template class cannot be used.

This class interacts a bit with its JSP - the form changes from "Add" mode to "Change" mode according to the value of the Operation.

If an operation is not appropriate in a given case, then simply provide an empty implementation of its corresponding abstract method (or an implementation that throws an UnsupportedOperationException).

To communicate messages to the end user, the implementation must use the various addMessage and addError methods.


Field Summary
static RequestParameter SupportedOperation
          The operations supported by this template.
 
Fields inherited from class hirondelle.web4j.action.ActionImpl
DATA, ERRORS, ITEM_FOR_EDIT, ITEMS_FOR_LISTING, MESSAGES, USER_ID
 
Constructor Summary
protected ActionTemplateListAndEdit(ResponsePage aForward, ResponsePage aRedirect, RequestParser aRequestParser)
          Constructor.
 
Method Summary
protected  void addDynamicParameterToRedirectPage(String aParamName, String aParamValue)
          Add a dynamic query parameter to the redirect ResponsePage.
protected abstract  void attemptAdd()
          Attempt an INSERT operation on the database.
protected abstract  void attemptChange()
          Attempt an UPDATE operation on the database.
protected abstract  void attemptDelete()
          Attempt a DELETE operation on the database.
protected abstract  void attemptFetchForChange()
          Attempt to fetch a single Model Object from the database, in preparation for editing it (SELECT operation).
protected abstract  void doList()
          Retrieve a listing of Model Objects from the database (SELECT operation).
 ResponsePage execute()
          Template method.
protected abstract  void validateUserInput()
          Validate items input by the user into a form.
 
Methods inherited from class hirondelle.web4j.action.ActionImpl
addError, addError, addError, addMessage, addMessage, addToRequest, addToSession, copyFromSessionToRequest, createSessionAndCsrfToken, endSession, getErrors, getExistingSession, getFromSession, getIdParam, getIdParams, getLocale, getLoggedInUserName, getMessages, getOperation, getOrderBy, getParam, getParamUnsafe, getRequestParser, getResponsePage, getTimeZone, getUserId, hasErrors, removeFromSession, setResponsePage
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SupportedOperation

public static final RequestParameter SupportedOperation
The operations supported by this template.

This action supports :

The source of the Operation is described by ActionImpl.getOperation().

Constructor Detail

ActionTemplateListAndEdit

protected ActionTemplateListAndEdit(ResponsePage aForward,
                                    ResponsePage aRedirect,
                                    RequestParser aRequestParser)
Constructor.

Parameters:
aForward - used for Operation.List and Operation.FetchForChange operations, and also for failed Operation.Add, Operation.Change, and Operation.Delete operations. This is the default response.
aRedirect - used for successful Operation.Add, Operation.Change, and Operation.Delete operations.
aRequestParser - passed to the superclass constructor.
Method Detail

execute

public final ResponsePage execute()
                           throws AppException
Template method.

In order to clearly understand the operation of this method, here is the core of its implementation, with all abstract methods in italics :

    if (Operation.List == getOperation() ){
      doList();
    }
    else if (Operation.FetchForChange == getOperation()){
      attemptFetchForChange();
    }
    else if (Operation.Add == getOperation()) {
      validateUserInput();
      if ( ! hasErrors() ){
        attemptAdd();
        ifNoErrorsRedirectToListing();
      }
    }
    else if (Operation.Change == getOperation()) {
      validateUserInput();
      if ( ! hasErrors() ){
        attemptChange();
        ifNoErrorsRedirectToListing();
      }
    }
    else if(Operation.Delete == getOperation()) {
      attemptDelete();
      ifNoErrorsRedirectToListing();
    }
    //Fresh listing WITHOUT a redirect is required if there is an error, 
    //and for successful FetchForChange operations.
    if( hasErrors() || Operation.FetchForChange == getOperation() ){
      doList();
    }
   

Specified by:
execute in interface Action
Specified by:
execute in class ActionImpl
Throws:
AppException

validateUserInput

protected abstract void validateUserInput()
Validate items input by the user into a form.

Applied to Operation.Add and Operation.Change. If an error occurs, then addError must be called.

Example of a typical implementation :

  protected void validateUserInput() {
    try {
      ModelFromRequest builder = new ModelFromRequest(getRequestParser());
      fResto = builder.build(Resto.class, RESTO_ID, NAME, LOCATION, PRICE, COMMENT);
    }
    catch (ModelCtorException ex){
      addError(ex);
    }    
  }
   

Note that the Model Object constructed in this example (fResto) is retained as a field, for later use when applying an edit to the database. This is the recommended style.


doList

protected abstract void doList()
                        throws DAOException
Retrieve a listing of Model Objects from the database (SELECT operation).

Throws:
DAOException

attemptAdd

protected abstract void attemptAdd()
                            throws DAOException
Attempt an INSERT operation on the database. The data will first be validated using validateUserInput().

Throws:
DAOException

attemptFetchForChange

protected abstract void attemptFetchForChange()
                                       throws DAOException
Attempt to fetch a single Model Object from the database, in preparation for editing it (SELECT operation).

Throws:
DAOException

attemptChange

protected abstract void attemptChange()
                               throws DAOException
Attempt an UPDATE operation on the database. The data will first be validated using validateUserInput().

Throws:
DAOException

attemptDelete

protected abstract void attemptDelete()
                               throws DAOException
Attempt a DELETE operation on the database.

Throws:
DAOException

addDynamicParameterToRedirectPage

protected void addDynamicParameterToRedirectPage(String aParamName,
                                                 String aParamValue)
Add a dynamic query parameter to the redirect ResponsePage.

This method will URL-encode the name and value.


Version 4.10.0

Copyright Hirondelle Systems. Published October 19, 2013 - User Guide - All Docs.