The following lines of code did not work as I expected:
Date monthStartDate = null;
Date monthEndDate = null;
dateTimeUtil.fillStartAndEndDate(date, monthStartDate, monthEndDate);
getLog().info("START: " + monthStartDate);
getLog().info("END: " + monthEndDate);
And the fillStartAndEndDate method:
public void fillStartAndEndDate(Date date, Date start, Date end) {
DateTime dateTime = new DateTime(date.getTime());
int startDay = dateTime.dayOfMonth().getMinimumValue();
DateTime startDate = new DateTime(dateTime.getYear(), dateTime.getMonthOfYear(), startDay, 0 ,0 ,0 ,0);
int endDay = dateTime.dayOfMonth().getMaximumValue();
DateTime endDate = new DateTime(dateTime.getYear(), dateTime.getMonthOfYear(), endDay, 0 ,0 ,0 ,0);
start = jodaDateTimeToJavaDate(startDate);
end = jodaDateTimeToJavaDate(endDate);
}
The results were the following:
START: null
END: null
This is one of those very basic things I sometimes overlook. At first, I thought that object reference in java works the same as the object reference in c++. In java, after creating a new instance of the object, the object (start param variable on the example) is not anymore pointing to the same reference which makes the method's local variable have a different value from the one that was originally passed.
I've written a pom.xml that will emanage the dependencies using maven. The following is my pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:artifact="antlib:org.apache.maven.artifact.ant"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.reyjexter</groupId>
<artifactId>exacto</artifactId>
<name>exacto</name>
<version>1.0</version>
<packaging>war</packaging>
<repositories>
<repository>
<id>jboss</id>
<name>The JBoss maven repo</name>
<url>http://repository.jboss.com/maven2/</url>
</repository>
</repositories>
<parent>
<groupId>org.jboss.seam</groupId>
<artifactId>root</artifactId>
<version>2.0.1.GA</version>
</parent>
<build>
<finalName>exacto</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<tasks>
<echo>***Deleting WEB-INF/lib dir</echo>
<delete>
<fileset dir="web/WEB-INF/lib" includes="**/*.jar"/>
</delete>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0</version>
<configuration>
<webappDirectory>/web</webappDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jboss.seam</groupId>
<artifactId>jboss-seam</artifactId>
<exclusions>
<exclusion>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
</exclusion>
<exclusion>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.el</groupId>
<artifactId>jboss-el</artifactId>
</exclusion>
<exclusion>
<groupId>jboss</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.seam</groupId>
<artifactId>jboss-seam-ui</artifactId>
</dependency>
<dependency>
<groupId>com.sun.facelets</groupId>
<artifactId>jsf-facelets</artifactId>
</dependency>
<dependency>
<groupId>commons-digester</groupId>
<artifactId>commons-digester</artifactId>
<version>1.8</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.seam</groupId>
<artifactId>jboss-seam-debug</artifactId>
<version>2.0.1.GA</version>
</dependency>
<dependency>
<groupId>org.richfaces.framework</groupId>
<artifactId>richfaces-api</artifactId>
<version>3.1.3.GA</version>
<exclusions>
<exclusion>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.richfaces.framework</groupId>
<artifactId>richfaces-impl</artifactId>
<version>3.1.3.GA</version>
<exclusions>
<exclusion>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>javax.faces</groupId>
<artifactId>jsf-impl</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</exclusion>
<exclusion>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.richfaces.ui</groupId>
<artifactId>richfaces-ui</artifactId>
<version>3.1.3.GA</version>
<exclusions>
<exclusion>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>javax.faces</groupId>
<artifactId>jsf-impl</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</exclusion>
<exclusion>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
The code simply removes the jar libraries from web/WEB-INF/lib and copies the maven dependencies to that folder.
I'll be posting a different configuration for ear and ejb jar in a couple of days
Knowledge in EJB is very important when studying seam framework because the framework itself was designed to be tightly coupled with ejb (although pojo's can also be used when developing applications, i think seam works better with ejb).
A week ago, the book I ordered from amazon arrived. The book entitled "EJB 3 In Action" from manning publishing covers the basics and important parts of ejb framework that is commonly used. Having read the book "Hibernate In Action" book from the same publisher, I think this book will be easily understood by people new to ejb 3.
Here are fresh from the box screenshots :D
![]() |
![]() |
To use joda time libraries in seam project, simply copy joda-time-1.5.jar to PROJECTNAME
<java>joda-time-1.5.jar</java>
</module>
When restarting jboss app server, the library for some unknown reason is not copied to deploy directory and Exception saying that joda time library cannot be found. Just copy the jar for to
I was trying to look for an easier way to clone objects (easier than using java.lang.Cloneable class) and I found out that using the jboss serialization library is easier and faster. The following code shows how to clone an object:
try {
JBossObjectOutputStream jbossSerializer = new JBossObjectOutputStream(null);
User newUser = (User) jbossSerializer.smartClone(user);
System.out.println(newUser);
}
catch(Exception e) {
System.out.println("EXCEPTION CAUGHT: " + e.getMessage());
}
using jboss serialization, you can also clone "Non serializable" objects.
you will need jboss-serialization.jar
Reference:
http://blogs.jboss.com/blog/clebert/
Labels: cloning, jboss, serialization
Custom hibernate validator that retrieves information from database in seam
6 comments Posted by Rey Jexter Bumalay at 4:37 AMUsing hibernate validation, we can decouple the validation rules and at the same time make it reusable for future projects. For example we want to validated if the username given already exist on the database and usually we just put our validation logics on the session bean or action (in struts). At the start of the project this looks ok but as the project progress and some new specification changes, new functionalities are added and some parts of your code gets refactored things get a bit messier.
Instead of simply dumping all the validation logic on the session bean we can create a custom hibernate validator which later can be used by annotating the getter of the entity property. Below is my code to validate if the username if it already exist on my database.
Username.java - The validator annotation definition
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ValidatorClass(UsernameValidator.class)
public @interface Username {
String message() default "Username already exists";
}
UserValidator.java - The validator class implementation
public class UsernameValidator implements ValidatorUser.java - and finally, the entity class, Serializable{
private static final long serialVersionUID = -1458203631809206211L;
public void initialize(Username parameters) {
}
public boolean isValid(Object value) {
UserHome userHome = (UserHome) Component.getInstance("userHome");
User user = userHome.findUser((String)value);
// return true if no user with the given value was found
if(user == null) {
return true;
}
// return true if the user retrieved is equals to the current instance in home
if(user.getId() == userHome.getInstance().getId()) {
return true;
}
// return false if the username exist on our database
return false;
}
}
public class User extends BaseEntity {The validation code above simply tries to retrieve the user from database. If it returned null it means that no user with the given user name exist to it returns true (meaning the validation is successful). If a user with the given username is found, it checks if the user instance from userHome component is the same so that the validation will still be successful if we try to save it without modifying anything. Returning false will cause the validation process to return error message which can be later display by using
private static final long serialVersionUID = 6148649482941380604L;
private String username;
@Length(min=3, max=32, message="Username should be 3-32 characters")
@NotNull
@Username
public String getUsername() {
return username;
}
}
References:
hibernate docs
hibernate validator source codes
Labels: hibernate, seam, validation
Extending EntityQuery in seam 2.0.1 GA is not working properly. For some reason, the 'firstResult' is not injected by default and isNextExists() method always return false. The solution i found was to override both setFirstResult() and isNextExists() method. I manually injected the firstResult request context parameter, created an implementation of isNextExists() and everything seems to be working. Here is my EntityQuery subclass:
@Name("userList")
public class UserList extends EntityQuery{
private static final long serialVersionUID = 4676553441988735518L;
public UserList() {
setMaxResults(2);
}
@Override
public String getEjbql() {
return "select user from User user";
}
@RequestParameter("firstResult")
@Override
public void setFirstResult(Integer firstResult) {
if(firstResult==null) {
firstResult = 0;
}
super.setFirstResult(firstResult);
}
@Override
public boolean isNextExists() {
if(getFirstResult() + getMaxResults() < getResultCount()) {
return true;
}
return false;
}
}
And my xhtml code:
<s:link view="/userList.xhtml" value="Previous" rendered="#{userList.previousExists}">
<f:param value="#{userList.previousFirstResult}" name="firstResult" />
</s:link>
<s:link view="/userList.xhtml" value="Next" rendered="#{userList.nextExists}">
<f:param value="#{userList.nextFirstResult}" name="firstResult" />
</s:link>
Labels: seam

