Our system uses Struts 1.2. It deals with insurance applications. Let me give you a brief description of an issue we are facing.
We have a requirement to only allow one user to be able to modify an insurance application at a given time - hence if a user A opens application 123, then user B cannot modify it. So user A locks the application for a given time. If this user does not perform any actions (i.e. leaves the application idle) for more than 20 minutes, then we must release the lock (times out) and allow other users to open and modify the application.
We have created a DB table to hold details of the locked applications and their associated users. Every time a user performs an action on these applications, we update a timestamp in this table so that the lock does not time out. So basically any action on the application will require a database call to check the validity (and if necessary) update the lock record.
So, I was wondering - could I use filters to basically intercept a set of actions - so that we look-up/update this database table and then delegate control to the associated controller/action classes?
Or am I better off having a base action class that basically does the above work and then have all the other relevant action classes extend this base class?
If any of the above is not clear, please let me know. I'm also open to any other suggestions/advice. Thanks very much!
The Struts Action classes are part of the Controller, in MVC terms. The requirements that you describe are part of the application (in MVC terms, the Model).
The requirement of checking to see if something can be done or not should go in the application (Model), especially if it deals with checking a database table. Business logic should not be coded in the Struts Action classes or any of the filters associated with the HttpRequest.
Check out the Business Delegate design pattern for more information.
Create a "lock utility" class that will monitor the users behavior and check the database table. A Business Delegate can use this class to regulate the application. [ November 09, 2008: Message edited by: James Clark ]
Joined: Nov 04, 2008
Hi James - thanks for the reply.
Yes, I am aware that we shouldn't be making database calls from the controller/filter. I will simply be looking up the relevant locking service/lock utility from within my action class - it is the service that will then check the table and make any updates, etc.
Since I will be monitoring a number of user actions, I was wondering what the best strategy would be to check what action/link they clicked on so as to then call my lock service. i.e. if filters would be appropriate or simply creating a base class whereby any of its children will then have to check the database (via the service).
Hope that makes sense.
Joined: Apr 16, 2008
I think the best bet would be to create a Base Action class and then have all the relevant Action classes of the the application extend this Base Action class.
You could then add a private method in the Base Action class which will connect to the service. And you could then call this method from whichever Action you want
The Base Action will extend the Struts Action class.