I've developed a action that one can inherit from to help perform validation
of code. The action handles conditional and unconditional branching and
status messages. One can choose to visit a template if the validation was
successful and can also choose to visit an alternate template if the
validation was not successful. Alternatively, one can just always visit a
template after validation.
Lastly, one can decide not to use the branching logic support that the
action gives. Below is code that shows how the action is used in code. The
class it is in extends IntakeValidatingAction:-
public void doInsert(RunData data, Context context)
throws Exception
setTargetScreens(ShowScreen, EditScreen);
setStatusMessages(StringConstants.deptAdd,
"Correct Mistakes and Try Again");
if(!isAllValid())
return;
Department dept = new Department();
retrieve("Department", dept);
dept.setNew(true);
dept.save();
}
This action builds upon my recent patch to Intake.
Below is the actual proposed IntakeValidatingAction
package org.apache.turbine.util.intake
/* Java stuff */
import java.util.ArrayList;
import java.util.Iterator;
/* Velocity stuff */
import org.apache.velocity.context.Context;
/* Turbine stuff */
import org.apache.turbine.util.Log;
import org.apache.turbine.util.RunData;
import org.apache.turbine.modules.actions.VelocityAction;
/* Intake stuff */
import org.apache.turbine.services.intake.IntakeTool;
import org.apache.turbine.services.intake.model.*;
/**
* Inherit from this class to provide basic validation capabilities
* for data input / editing actions. Supports simple success / failure
* target screens and branches to these screens depending on results
* of the validation process. However, if you wish to implement more
* fancy control flow, you can simply ignore the functionality.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Guido Sohne</a>
*/
public abstract class IntakeValidatingAction extends VelocityAction
{
private Context context;
private RunData data;
private String failureScreen;
private String successScreen;
private String failureMessage;
private String successMessage;
boolean unconditional_branch = false;
boolean unconditional_status = false;
boolean conditional_branch = false;
boolean conditional_status = false;
protected IntakeTool intakeTool;
/**
* Set screen to proceed to if validation fails
*/
private void setFailureScreen(String failureScreen)
{
this.failureScreen = failureScreen;
}
/**
* Get screen to proceed to if validation fails
*/
private String getFailureScreen()
{
return this.failureScreen;
}
/**
* Set screen to proceed to if validation succeeds
*/
private void setSuccessScreen(String successScreen)
{
this.successScreen = successScreen;
}
/**
* Get screen to proceed to if validation succeeds
*/
private String getSuccessScreen()
{
return this.successScreen;
}
/**
* Set message to display if validation fails
*/
private void setFailureMessage(String failureMessage)
{
this.failureMessage = failureMessage;
}
/**
* Get message to display if validation fails
*/
private String getFailureMessage()
{
return this.failureMessage;
}
/**
* Set message to display if validation succeeds
*/
private void setSuccessMessage(String successMessage)
{
this.successMessage = successMessage;
}
/**
* Get message to display if validation succeeds
*/
private String getSuccessMessage()
{
return this.successMessage;
}
/**
* Sets messages that make available status of validation
* to screens that are reached after the a
ction event
* returns from processing the event that was triggered
*/
public void setStatusMessages(String successMessage,
String failureMessage)
{
conditional_status = true;
setSuccessMessage(successMessage);
setFailureMessage(failureMessage);
}
/**
* Set message that makes available the status of validation
* to the screen that is reached after the action event
* returns from processing the event that was triggered
*/
public void setStatusMessage(String statusMessage)
{
unconditional_status = true;
setSuccessMessage(statusMessage);
}
/**
* Rudimentary control flow for validation success / failure
* Branches to appropriate screen depending on validation result
*/
public void setTargetScreens(String successScreen, String failureScreen)
{
conditional_branch = true;
setSuccessScreen(successScreen);
setFailureScreen(failureScreen);
}
/**
* Use this when you don't need to branch for success / failure
*/
public void setTargetScreen(String targetScreen)
{
unconditional_branch = true;
setSuccessScreen(targetScreen);
}
/**
* Initialize internal state per action event for
* other validation methods to use, dispatch the
* action event and finally branch to success or
* failure templates if these have been defined.
*/
public void executeEvents(RunData data, Context context)
throws Exception
{
unconditional_branch = false;
unconditional_status = false;
conditional_branch = false;
conditional_status = false;
this.context = context;
this.data = data;
this.intakeTool = (IntakeTool) context.get("intake");
// dispatch action events to appropriate handler
super.executeEvents(data, context);
// branch if necessary. target depends on validation status
if(conditional_branch)
{
if(isAllValid() && successScreen != null) {
setTemplate(data, successScreen); Log.info("a " +
successScreen); }
else if (!isAllValid() && failureScreen != null) {
setTemplate(data, failureScreen); Log.info("b " +
failureScreen); }
}
else if(unconditional_branch && successScreen != null) {
setTemplate(data, successScreen); Log.info("c " +
successScreen); }
// provide status messages as necessary
if(conditional_status)
{
if(isAllValid() && successMessage != null) {
data.setMessage(successMessage); Log.info("d " +
successMessage); }
else if (!isAllValid() && failureMessage != null) {
data.setMessage(failureMessage); Log.info("e " +
failureMessage); }
}
else if(unconditional_status) {
data.setMessage(successMessage); Log.info("f " +
successMessage); }
}
/**
* Check to see if all the data presented is indeed
* valid.
*
* @returns a <code>boolean</code> value
*/
public boolean isAllValid()
throws Exception
{
boolean is_valid = false;
if (intakeTool != null && intakeTool.isAllValid())
{
is_valid = true;
}
return is_valid;
}
/**
* Sets properties of object to contents of the Intake
* group. Object must be of same type that group
* is. That is, for Department group, object
* must be a Department object!
*
* @returns the <code>Object</code> that was passed
* as an argument
*/
public Object retrieve(String groupName, Object obj)
throws Exception
{
Group group = intakeTool.get(groupName, IntakeTool.DEFAULT_KEY);
if(group != null && obj != null)
group.setProperties(obj);
return obj;
}
/**
* Convenience method to set both the next screen
* and a message after validation fails.
*/
public void redo(String screen, String message)
{
data.setMessage(message);
setTemplate(data, screen);
}
/**
* Places the contents of an object into the
* Intake group named <code>groupName</code>.
* Useful prior to returning to a template
* for further editing after validation.
*/
public void populate(String groupName, Object obj)
throws Exception
{
Group group = intakeTool.get(groupName, IntakeTool.DEFAULT_KEY);
if(group != null && obj != null)
group.getProperties(obj);
}
}
Any comments ?
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]