Thursday, August 28, 2008

Splitting a Project into Modules in Maven

The beauty of Maven and Idea is the ease with which they allow you to manage, build and test your applications. Let us suppose we have a project that we wish to build in modules. For sake of simplicity we assume that the project has two basic modules: a webapp module and a core module.

Make a folder on the system for your project. In the folder make two folders by name "core" and "webapp". Also put a pom.xml which should look like:

parent pom

Notable things are the modules node and that the packaging is pom.

In the "core" folder make a folder "src" and put a pom.xml something like:

core pom

Main things to note about the pom is that packaging is "jar". ID are same as those in the parent pom. In "src" folder make two folders "main" and "test". Put a folder named "java" in each of them.

In the "webapp" folder create same directory structure as "core". The pom.xml here would look like:

webapp pom

Make the directory structure webapp -> src -> main -> webapp -> WEB-INF
in WEB-INF put in a web.xml file

You are good to go now. Now all you need to do is mvn idea:idea or mvn eclipse:eclipse (depending on what you are using) while being in the main folder to create the project with its modules.

Wednesday, August 6, 2008

Alternative to Using Valang Custom Functions


Valang provides a rich set of validation tools and is pretty simple. However in some cases you want more. The most obvious case that we encounter is validating if a date is in future or past wrt the current time on server. I know we can parse and input date to a date object readily using initBinder function of a SimpleFormController. But to compare it with the current date using pure varlang is something I could not find.

Luckily valang has an ability to use custom function. However I found it cumbersome to make one separate class for each function I may need. It would make more sense to allow users to group functions in one class to build a custom library of functions. Not to mention I could not find a proper working example of a custom function. So till I get my hands on one and this implementation improves, I have a simple work around.

Step 1: Define valang validations in the config file


<bean id="webMessageFormValidator"
class="myPackage.validators.FormBeanValidator">
<property name="valang">
<value>
<![CDATA[
{to:? IS NOT BLANK:'Please enter email addresses
for recepients.'}
{subject:? IS NOT BLANK:'Mail can not be blank.'}
{from:(length(?)> 0):'Please specify at least one
address.'}
]]>
</value>
</property>
</bean>


Step 2: Define the validator as follows


package mypackage.validators;

import org.springframework.validation.Errors;
import org.springmodules.validation.valang.ValangValidator;
import mypackage.FormBean;


public class FormBeanValidator extends ValangValidator {

public void validate(Object object, Errors errors) {

FormBean form = (FormBean) object;

//perform all validations and raise error flag
//if you find an error
if(isFutureTime(form.myDate()){
errors.rejectValue("myDate", "error.futureDate"
,"Time cannot be in the future.");
}

//dont forget call to super so as the validations
//defined in XML take place
super.validate(object, errors);
}


private boolean isFutureTime(Date formDate,
int hours, int minutes){
//check if time is in future
return false;
}