Best practice to extend controller
I was wondering what the best approach to extend a Roo-generated controller is. For example, after an entity is persisted, I would like to fire off an event to a service layer which will then process the new entity.
The only way I can think of doing it thus far is to copy the Roo-generated method from the .aj file and paste it into the .java file, so it becomes manually managed.
I was hoping I could keep the Roo-generated methods in the .aj file so Roo can manage them, and hook in my own post-processing step. Is such a thing possible?
Don't forget the ACID, man
Even if you could do that, it might not (depending on your application's business rules) be a good idea, because your post-processing step would run in a separate transaction to the one in which the controller called persist() or merge(). In other words, the persist/merge might commit, but your post-processing step might roll back, leaving your system in an inconsistent state.
There are two solutions:
- Old skool:
- write a service layer "save" method that encapsulates both the JPA persist/merge call and the post-processing step,
- push the create and update methods into the controller's Java class, and
- modify those methods to call the new save method instead of calling persist/merge, OR
- New skool:
- write an aspect that runs before/after/around your entity's persist and merge methods to perform the necessary additional processing (which I assume automatically takes part in the same transaction)