"How can I write validation that is conditional based on some external criteria?"
In trying to solve this problem, some developers think to write validation logic in their ViewRowImpl class, because it seems like the entity object is not the right place to do it. However, the validate() method on the ViewRow is never explicitly called as part of the framework's validation cycle, so it's not the right place to accomplish the job. The validate() method on ViewRow exists so that the client can force validation to happen explicitly on that row. Only the entity object is a failsafe place to put validation logic, and this is very much by design.
If you need to write business rules that behave differently based on some external factor -- an example that came up today was a question about how validation rules could behave differently if a user was known to be a manager -- then you should take advantage of BC4J's user-data feature on the session object. To get hold of the user data Hashtable, you can call getSession().getUserData() from inside your application module implementation class (perhaps in a custom method that you write there), or in an overridden prepareSession(Session) method after calling the superclass' implementation. You can put values into this hashtable that will remain for the duration of the current user's session with the application module.
Then, within your entity object validation method, you can write code like getDBTransaction().getSession().getUserData() to get access to the current user's session user data, and check the values of entries there to make your validation conditional based on them.
11:53:30 AM
|