Wednesday, June 19, 2013

I was driving my son to the DVD shop one night, waiting to do a right turn onto the main road. There was a car approaching, I noted to the boy "Look, there is nobody driving that car". As my son realised there was an African man behind the wheel of that car, he turned to me and said "You're racist Dad". The fact that it was not the first time he heard a racist joke makes me wonder if in my son's eye, I would be the last person on earth who is capable of making such jokes.
Must have been a while since I last posted to Blogger. Three years is a long time to gather your thought and put it down in writing. So what has changed? Mom passed away one and a half year ago, 18 December 2011. We made the trip back to Vietnam last December, the four of us, and with the relatives on both sides we gathered on the day marking the first year of her passing. It had been 12 years since I last saw them and for some reasons I was always almost in tears talking to some of them, there was this daunting thought that I might not see them again in my life. Back in April, my oldest aunt went away after two months of suffering, proceded in death by my oldest uncle the year before. She had never been married and survived by her only adopted daughter. Too many deaths in the family, no wonder why I have been feeling a bit down. Life goes on though, I am back with my family now after three years working away from home. Job security is still an issue but i just have to do my best. Last contract with Defence was cut shot due to personality clash with work colleagues, might be I am just too old for this line of work. Fuck it

Wednesday, May 26, 2010

Form level validation with JSF and RichFaces

Validating input data is a common task for any web application. Java Server Faces (JSF) comes with a standard type conversion and validation framework that can be used for implementing validation logic at view layer. In other words, JSF validation is not focused on form level validation (which is good for validating relationships between fields like From and To date). To overcome this limitation, developers often bundle validation logic directly into the domain model, where business logic is executed, and cluttering domain classes with validation code which in most cases requires a lot more coding than the actual business logic processing.

Hibernate Validator (JSR 303) helps to remove this cluttering by defining a metadata model and API for domain model validation. This works well for implementing business validation but Hibernate Validator suffers from the fact that input data validation is purely a view concern where condition for executing validation logic is embedded in the view. Moreover, the validation model employed by Hibernate Validator framework can easily be misused and completely diminish separation between business model and view (MVC pattern violation) and also code becomes more difficult to maintain and minimises reuse.

I added this Form Validation framework to overcome some of the pitfalls mentioned above by adhering to the following objectives:

• Simplify the wiring of validation rules through configuration rather than coding.
• Bring back the focus of validation rules on performing validation logic to achieve greater reuse and ease unit testing.
• Support for validation levels to modularise validation logic.

Getting started



This following example will show you how to start with the Form Validation framework by configuring and implementing a simple validation rule to check for email address syntax.

Applying rule



Validation rule is configured in the view (.xhtml) by applying the rule to the appropriate component. The input text field email has a field-level validation rule specified against it (more about validation levels in the next chapter). The name attribute is used to specify the validation rule instance EmailAddressRule on the server side.

<h:form id="form">
<h:outputLabel for=”email” value=”Email Address” />
<h:inputtext id="email" value="#{bean.email}">
<xyz:validateField name="EmailAddressRule" />
</h:inputtext>
<h:message for=”email” />
</h:form>


Validating rule



To perform the email address validation we use a FieldRule instance called EmailAddressRule which is the same as the name attribute of the rule applied in the view. This class overrides the validate method of FieldRule to perform validation logic on the email address syntax. If an invalid email address is entered on the form, it will add an error with an appropriate message to be displayed on the view.

public class EmailAddressRule extends FieldRule {

private static final String VALID_EMAIL =
"^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)"
+ "|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";

@Override
public void validate(final List<ValidationMessage> messages,
final UIComponent component,
final Object value) {

if (value != null
&& (value instanceof String && !StringUtils.isEmpty((String) value))) {
final String emailAddress = (String) value;
if (!emailAddress.matches(VALID_EMAIL)) {
messages.add(new ValidationMessage(FacesMessage.SEVERITY_ERROR,
"Invalid {0}.");
}
}
}
}


Running the view and enter an invalid email address in the input text field will result in the following error message to be displayed after a form submit

Invalid Email Address.


Validation step by step



In this section we will see in more detail how to use the Form Validation framework to validate a view. We begin with a detailed explanation on how to configure validation rules followed by some notes detailing how it is best to implement your validation logics.

Defining rules



As seen in the previous section, the framework offers two levels of validation, namely field-level and page-level validation. One might have guessed that field-level validation is used to validate the input data of a single field and page-level for multiple field input data. On the contrary, validation level has nothing to do with whether you want to validate one or more input data values. The level is used to determine the order which validation rules will be executed on the server and how validation messages to be displayed on the view. Having said that, field-level validation will mostly consists of single input field validation since it will be performed prior to page-level validation.

As the example shows, EmailAddressRule is a FieldRule instance used to validate single input field value. In the next section we will see how to define single input field and multiple input field validation through the implementation of validation rules.

Validator tag



Both field-level and page-level validation can be specified using tag. This component is used purely for passing validation rule configuration to the server and thus has no renderer associated with it.

Example

<xyz:validate type=”fieldLevel” name=”EmailAddressRule” />


There are also two Facelets short-cuts for specifying field-level and page-level validation, namely and .

Rule parameters



Apart from the above attributes, validation rule also accepts parameters for multiple field values and validation message arguments. For the above example we can override the default message argument using tag

<xyz:validateField name=”EmailAddressRule”>
<f:param value=”email address syntax” />
</xyz:validateField>

To convert the error message to

Invalid email address syntax.


The following example shows how to specify a rule to validate multiple input values, where ValidateExtensionDatesRule is a FormRule instance which accepts two input dates and perform validation logic on those values.

<h:inputText id=”fromDate” value=”#{bean.fromDate}” />
<h:inputText id=”toDate” value=”#{bean.toDate}”>
<xyz:validateField name=”ValidateExtensionDatesRule”>
<f:param value=”#{rich:clientId(‘fromDate’)” />
<f:param value=”#{rich:clientId(‘toDate’)” />
</xyz:validateField>
</h:inputText>


Also notes that even ValidateExtensioinDatesRule performs validation across multiple input fields, it will be executed at field-level since it is configured with tag.

Conditional validation



When using AJAX request it would be a good idea to limit server-side processing to only those components whose values have been modified on the view. RichFaces provides the tag to mark areas on the JSF component tree to be processed on the server during the Apply Request Values, Process Validations, and Update Model Values phases. While we cannot change how the form is submitted (all input fields on the form will be submitted), we can control which components will be processed on the server. Components that are not processed on the server will not be validated and will not be pushed on to the model (during the Update Model phase).

Let’s look at the following example where we don’t want to validate fields for which the user hasn’t had a chance to enter anything. Placing each input and message tag inside a region will restrict processing to the region from which the request was sent.

<h:panelGroup>
<a4j:region id=”region1”>
<h:inputText id="name" value="#{bean.name">
<a4j:support event="onblur" />
<xyz:validateField name="MandatoryRule" />
</h:inputText>
<h:message for="name" />
</a4j:region>
</h:panelGroup>

<h:panelGroup id=”region2”>
<a4j:region>
<h:inputText id="age" value="#{bean.age}">
<a4j:support event="onblur" />
<xyz:validateField name="MandatoryRule" />
</h:inputText>
<h:message for="age" />
</a4j:region>
</h:panelGroup>


When the user places the mouse cursor into the name field then without entering anything, leave the field and place the mouse in the age field, an error message will be displayed next to the name field but no error message on the age field as expected.
A closely related feature to the region is ajaxSingle attribute. The attribute can be applied to just one component while can contain any number of components.. For single input field validation, ajaxSingle should be used to limit server-side processing to the designated component as shown in the following example

<a4j:outputPanel id=”idInputFieldPanel”>
<h:outputLabel for=”idInputField” value=”Field label” />
<h:inputText id=”idInputField” value=”#{bean.input}”>
<xyz:validateField name=”MandatoryRule” />
<a4j:support event="onchange"
reRender="idInputFieldPanel"
ajaxSingle="true"
bypassUpdates="true" />
</h:inputText>
</h:message for=”idInputField” />
</a4j:outputPanel>


Another important attribute is process. It’s available on all RichFaces components that fire an AJAX request and is used in conjunction with ajaxSingle. When ajaxSingle is set to true, we know that only this component will be processed. A situation might arise where you still need to process some specific other components on the page in addition. The process attribute points to those components. process can also point to an EL expression or container component id in which case all components inside the container will be processed.

<h:inputText>
<a4j:support event="onblur" ajaxSingle="true" process="infoPanel" />
</h:inputText>
<h:panelGrid id="infoPanel">
<h:inputText />
<h:dataTable>
...
</h:dataTable>
</h:panelGrid>


The framework also provides some additional means to restrict validation rules being triggered via attributes of tag and UICommand components. The disabled attribute of can be used to disable validation when certain conditions are met.

The for attribute of tag can also be used to restrict validation to those events generated by the specified list of id’s. The syntax is similar to the process attribute of tag.

You can also be able to skip all validations on a page when certain actions are applied to a UICommand component by setting the skipValidation attribute for the UICommand component. As shown in the following example, all validations will be skipped when user clicks the ‘Save As Draft’ button on the screen.

<a4j:commandButton id="save" value="Save As Draft" …>
<f:attribute name="skipValidation" value="true" />
…
</a4j:commandButton>


Validating rules



Rules are validated by corresponding java objects on the server. At the time of writing there are three classes of validation rules, namely FieldRule, FormRule, and ListRule but other sets can be added to the framework to provide specialised versions of rule classes. Each validation rule class has to provide its own implementation for the validate method which adds the appropriate ValidationMessage instances to the List when validation fails.

Validation rule class can be implemented by calling Validation Service to perform required business validation prior to INVOKE_APPLICATION phase. This will ensure that when it comes to execute your action method, user input data has passed validation and your action only needs to perform the required business logic.

Field rule



FieldRule is used for single input field validation and thus should always be attached to the designated input component. When configuring a FieldRule on the view, no parameters are required except for those to be used for error/warning message interpolation. Where no message arguments are specified, the label for the input field (if any) will be passed as the default argument.

The signature for the validate method is as follows

void validate(List messages, UIComponent component, Object value)


Form rule



FormRule is used for multiple (cross-page) field validation and would expect one or more input parameters. Even when attached to an input component, its parameter still needs to be explicitly specified. FormRule is usually configured with two sets of parameters, one to provide data input to perform validation logic, the other for error/warning message interpolation.

The signature for the validate method is as follows

int validate(List messages, Object[] values)


List rule



ListRule is a specialised version of FormRule where the only expected parameter is the AbstractListBean model instance. For that reason, when configuring a ListRule on the view, immediate flag should be set to true to trigger validation after the model has been updated.

The signature for the validate method is as follows

boolean validate(List messages, AbstractListBean listBean)


Framework design



The key to this is the following marker interface and the abstract Validator class

// Validation rules - FieldRule, FormRule, ...
public interface ValidationRule {

List accept(AbstractValidator validator);
}


// Validation levels - FieldLevelValidator, PageLevelValidator, ...
public abstract class AbstractValidator {

public List apply(final FieldRule fieldRule) {
return null;
}

public List apply(final FormRule formRule) {
return null;
}

...
}


The concrete validators are used by a phase listener which parses the component tree to build a map of configured validation rules. If a component has a validation rule applied against it, the phase listener looks up the corresponding rule object and validates the input values for that component.

Looking at the class diagram below, you can read the association between the main components of the Validation Framework. At the top of the diagram sits ValidationPhaseListener, once registered with the JSF implementation, will get notified to trigger all validation logic for the current view. The listener only listens for the following phases:

• After PROCESS_VALIDATION phase – Trigger all input validation rules with submitted values from the request.
• After UPDATE_MODEL_VALUES phase – Trigger all input validation rules where immediate flag is set to true.
• Before RENDER_RESPONSE phase – Trigger all output validation rules where validationOnRender flag is true.



The following sequence diagram shows what actually happens behind the scene in each phase the listener is listened in. First a RuleMap will be created to prepare the list of all validation rules configured for the current view. Each rule then got allocated a validator whose job is to execute validation logic and return error messages when validation fails.

Sunday, March 1, 2009

Lights, Camera, Action!

Just back to work after a week staying at home. As we're selling the house there is a lot to organise once the deal goes through. It's good that we're very low on work for the time being. I might need to take another week off early next month. A relaxing week off with lot to catch up on TV, the Slumdog Musical Oscar, Champion League knock-out first leg matches. Arsenal is leading 1-0 at home to Roma, their first win after a frustrating run of results in the Premier League. Arsenal's chances of finishing in the top four looks bleak following a fourth consecutive 0-0 draw at home to Fullham. My disappointment was rescued by a couple of better than average movies, Taken starring Liam Nelson and 21 whose supporting cast includes Lawrence Fishburn and Kevin Spacey. The Oscar ceremony is boring to watch, thank God it only lasted a bit over two hours. The winners were so predictable it took a no-brainer to figure out who would be delivering the thank-you note. Well done Slumdog Millionaire...

Wednesday, February 11, 2009

Vietnam to “hit the switch” for Earth Hour on March 28

There's no need for the call from environmentalists. Blackouts usually last more than one hour in Vietnam. Environmental awareness is part of the fact of life for all Vietnamese people. Wonder why this piece of news is not available in the Vietnamese language section... I have no doubt that everyone over there is in need of a laugh right now.

Tuesday, December 16, 2008

Perl programmers

More good reasons to go back to Perl programming amid the economic downturn and rising unemployment.

Thursday, October 30, 2008

Everyone is fit to drive a bike now

HANOI, Vietnam — Faced with mounting public criticism, Vietnam's Health Ministry suspended a widely ridiculed plan to ban short, thin and small-chested drivers.

The ministry had recommended that people whose chests measure less than 28 inches be prohibited from driving motorbikes _ as well as those who are too short (less than 4 -foot-8) or too thin (less than 88 pounds).

When the media revealed the plan this week, it prompted disbelief and scorn among members of the public, who envisioned the police pulling over female drivers to measure their breasts.

Thursday's state-run Tuoi Tre newspaper quoted ministry official Nguyen Huy Quang as saying the proposal would be suspended.

Copyright 2008 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.

Monday, October 27, 2008

Sunday, September 28, 2008

Feeling Homesick

Reading this travel article this morning really makes me feel homesick

"Vietnam offers some of the most charming tropical scenery in the world. The mist rising from the land against the morning sky is resplendent. The lush mountains and gorges and verdant valleys and endless rivers create a colorful landscape. A coast line that stretches for hundreds of miles along the South China Sea makes it a marvel of geography."

Wonder if this is a case of 'opportunities missed are opportunities gain'.

Sunday, September 7, 2008

Oracle8 and INNER JOIN

The 'new' ANSI syntax (INNER JOIN, NATURAL JOIN, etc.) is only supported when dealing with Oracle9i and higher.

All the more reason to move to Oracle9i. Especially since Oracle did announce End of Life for 8i.

Monday, September 1, 2008

XStream and Dynamic Proxies

So I've been bragging a bit (yeah, just a bit) about XStream because I'm currently using it in my unit testing framework. I started out with the original DynamicProxyConverter and there is already a very good post on how to use it from Straun's blog. But then I soon ran into problems trying to set a super class for the resulting proxy object for the following class hierarchy:

public interface MyObjectInterface { void doSomething(); }

public class MyObjectBase { public void intialize() {} }

public class MyObjectImpl extends MyObjectBase implements MyObjectInterface {
public void doSomething() {}
}

The solution is to clone DynamicProxyConverter.java and replace the last line of code in the unmarshall() method:

return Proxy.newProxyInstance(classLoader, interfacesAsArray, handler);

With

return newProxyInstance(superClass, interfacesAsArray, handler);

Where newProxyInstance is a method which utilized javaassist package to create the proxy instance in the cloned DynamicProxyConverter:

private Object newProxyInstance(Class superClass, Class[] interfaces, InvocationHandler handler)
{
ProxyFactory factory = new ProxyFactory();

if (superClass != null) {
factory.setSuperclass(superClass);
}

factory.setInterfaces(interfaces);

Class clazz = factory.createClass();

Object proxy = clazz.getConstructor().newInstance();
((ProxyObject) proxy).setHandler((MethodHanlder) handler);

return proxy;
}

For this to work, your InvocationHandler class should also need to implement MethodHandler interface, ie.

public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args)
throws Throwable
{
if (proceed == null) {
return invoke(self, thisMethod, args);
} else {
return proceed.invoke(self, args);
}
}

Sunday, August 31, 2008

XStream Deserialization

When calling XStream.fromXML() to deserialize an object, XStream by default will use ReflectionConverter to populate the fields of the given root object instead of instantiating a new one. To force instantiation of a new object, use

XStream xstream = XStream(new PureJavaReflectionProvider());

Cheap journalism

VietNamNet (VNN) today has an article about Vietnam's National Day. At the end of the article the following was added

"The Governor of the US’s California State, Arnold Schawarzeneger, on August 28 sent a message of greetings to the Vietnamese Consul General in San Francisco, expressing his thanks for the Vietnamese community’s contributions to the state’s development."

Is Arnold losing it? What the fuck does the Vietnamese Consul General have anything to do with the Viet's contributions to the state of CA and how it's related to Vietnam's National Day celebration is out of grasp. Cheap, cheap journalism at its worst.

Remains in Vietnam could be Australian soldier

If this turns out to be true, the effort to locate the last Australian soldier missing in the Vietnam war would be completed. Well done