Saturday, October 6, 2007

Wicket And the Command Pattern - Part 2

Another benefit I see from using the command pattern in wicket is through the use of buttons.
This time I use another command abstract class
This is the button command. This class will be used by anonymous classes to encapsulate the calls that I would need.


/**
* Button Command - implements the command design pattern.
* Should replace all button instances.
* @author carloc
*
*/
public abstract class ButtonCommand implements Serializable {
public abstract void submit();

}



I then create a custom button which executes the command.
One benefit of this is that I would no longer have to catch exceptions in the other parts of my code.
All of the exceptions would be caught in one place. And a change of the type of button would only be change in one part of my code.


public class CCTIButton extends IndicatingAjaxButton {

/**
*
*/
private static final long serialVersionUID = 1L;

private ButtonCommand buttonCommand;

public CCTIButton(String id, CCTIForm form, ButtonCommand buttonCommand) {
super(id, form);
this.buttonCommand = buttonCommand;
}

@Override
protected void onSubmit(AjaxRequestTarget target, Form form) {
// TODO Auto-generated method stub
target.addComponent(getFeedback());
try {
buttonCommand.submit();
} catch(DataIntegrityViolationException e) {
error("Unique constraint violated.");
} catch(StaleObjectStateException e) {
error("Row(s) was updated or deleted by another transaction.");
} catch(VersionMismatchException e) {
error("Row(s) was updated or deleted by another transaction.");
} catch(CCTIException e) {
error(e.getMessage());
}catch(RuntimeException e) {
error("Application Error Occurred");
e.printStackTrace();
}catch(Exception e) {
error("Application Error Occurred");
e.printStackTrace();
}
}

@Override
protected void onError(AjaxRequestTarget target, Form form) {
target.addComponent(getFeedback());
super.onError(target, form);
}
}


A simple implementation of this would look like this


ButtonCommand buttonCommand = new ButtonCommand() {
@Override
public void submit() {
// TODO Auto-generated method stub
Corporation corporation = (Corporation) SaveCorporationForm.this.getModelObject();
corporation.setCreateDt(new Date());
corporation.setCreateBy(WebUtils.getUser(SaveCorporationForm.this).getId());

final CorporateUser corporateAdminOne = CorporateUser.createCorporateAdmin();
populatecorporateAdmin(corporation, corporateAdminOne);

final CorporateUser corporateAdminTwo = CorporateUser.createCorporateAdmin();
populateCorporateAdminTwo(corporation, corporateAdminTwo);
corporationService.save(corporation,corporateAdminOne,corporateAdminTwo,getFunctionsList());
setResponsePage(new AckPage(getMenu(), new Ack("Corporation has been saved with ref no. " + corporation.getRefNo() + ". Administrator password set to '"+ corporateAdminOne.getConfPswd() +"'", Transaction.CREATE_CORPORATION), new SaveCorporation()));
}
};
return buttonCommand;


As you can see I don't catch my exceptions here anymore.
I would then add the button to my form.


add(new CCTIButton("id", form, buttonCommand)


Again I'm saving a lot of code space

No comments: