I write solutions to the problems I can't find much about elsewhere on the Web, also some code/script snippets that are absolutely awesome and make my life easier. Will be glad if someone finds these posts interesting and helpful!

Monday, May 3, 2010

Global validation for a Date input field in ADF 10

Say you have a table with a date column and you have created a view accessing it. Now try to insert an abnormal date into the dateInput field on the page (something like 1/1/111111111), commit the changes, check the value in the DB and you’ll get what I’m talking about here. I had queried a question on the OTN forum regarding this and the only answer I got was from Frank Nimphius to
configure a value change listener on the date field and verify that the provided value is valid, If not call FacesContext.renderResponse to ignore the request
this is a solution, but well, it requires more or less to apply it to each dateInput field on every single page. The other way around that I finally used, was to override the EntityImpl setAttributeInternal method like the following:
protected void setAttributeInternal(int attributeNumber, Object attributeValue) {
if (attributeValue instanceof oracle.jbo.domain. Date) {
java.util.Date value = ((oracle.jbo.domain.Date)attributeValue).getValue();
GregorianCalendar gc = new GregorianCalendar();
java.util.Date inAHundreedYears = new java.util.Date(new GregorianCalendar(gc.get(Calendar.YEAR) + 100, gc.get(Calendar.MONTH), gc.get(Calendar.DAY_OF_MONTH)).getTimeInMillis());
if (value.after(inAHundreedYears)) {
System.err.println("Invalid date provided (" + value + ")");
}
else {
super.setAttributeInternal(attributeNumber, attributeValue);
System.out.println("Set date " + value);
}
}
else {
super.setAttributeInternal(attributeNumber, attributeValue);
}
}
view raw gistfile1.java hosted with ❤ by GitHub

which actually just checks that the Date attribute provided is of a logical range.


This method has to be part of a class extending oracle.jbo.server.EntityImpl and obviously the entity implementation for that table with a date column must extend the new EntityImpl class.


That's it. Hope this helps!

No comments:

Post a Comment