There is another way to accomplish this that will make every Date filter within your application time agnostic so that developers do not need to add this extra column every time they want to filter on the date portion of a date/time column.To do this you need to create a custom SQL builder class that extends OracleSQLBuilderImpl and override the getFormattedLHSCompareFragment method (Left Hand Side Compare Fragment) This is called whenever filtering is performed. In this method add the trunc(...) around the value. Sample code is shown below.
This class is activated by adding a JVM parameter on startup (jbo.SQLBuilder)
-Djbo.SQLBuilder=com.yourCompany.model.custom.MySQLBuilderImpl
/** * Override method in OracleSQLBuilderImpl in order to trunc (remove time component) * when filtering on Dates in tables (Query By Example) QBE * This class is activated by adding a JVM parameter * -Djbo.SQLBuilder=com.yourCompany.model.custom.MySQLBuilderImpl */ public class MySQLBuilderImpl extends OracleSQLBuilderImpl { private static final Log log = LogFactory.getLog(MySQLBuilderImpl.class); private static SQLBuilder mSQLBuilderInterface=null; public MySQLBuilderImpl() { } /** * Gets the singleton instance of this class. * @return a SQLBuilder object. */ synchronized public static SQLBuilder getInterface() { if (mSQLBuilderInterface == null) { if (Diagnostic.isOn()) { Diagnostic.println("OracleSQLBuilder reached getInterface"); } mSQLBuilderInterface = (SQLBuilder)(new MySQLBuilderImpl()); if (Diagnostic.isOn()) { Diagnostic.println(mSQLBuilderInterface.getVersion()); } } return mSQLBuilderInterface; } @Override public String getFormattedLHSCompareFragment(ViewObject vo, ViewCriteria vc, AttributeDef attrDef, int sqltype, String lhs, Object rhs) { String value = super.getFormattedLHSCompareFragment(vo, vc, attrDef, sqltype, lhs, rhs); if (sqltype==Types.DATE || sqltype==Types.TIMESTAMP) { // only apply trunc to database columns, not view alias names used for in-memory filtering // ADF does not handle a trunc for a in-memory filter and will get an error Integer attr = vo.getAttributeIndexOf(value); // if (attr == -1){ // not found, meaning it's a database column name, not an attribute name. value = "TRUNC("+value+")"; if (log.isDebugEnabled()) { log.debug("MySQLBuilderImpl truncating date value:"+value); } } } return value; } }
Very helpful. Thanks a Lot.
ReplyDeleteMany thanks to you, really it's helpful to me.
ReplyDelete