Page 1 of 1

Finish Model Instance Callback/Hook

Posted: Sat Mar 29, 2014 8:09 pm
by matt.price
Is it possible to have a callback to some defined expression that is called when a model instance is finished through the management area?

Re: Finish Model Instance Callback/Hook

Posted: Wed Apr 02, 2014 8:39 am
by Maros Bajtos
Matt,

in general this is not possible (using only model). However, when terminating model instance, all tasks which are active (waiting for completion) are notified - see method com.whitestein.lsps.engine.ExecutableTask.terminate(TaskContext, TerminationReason). If you create your custom task and place it in the model instance in a way that it will be always running, you are able to trigger any behaviour on such an event if you implement terminate method. If you need to trigger something from the model, you can pass a closure from the model to java implementation and invoke it in the terminate method. Can you tell us something about your use case? Why do you need such a functionality?

Re: Finish Model Instance Callback/Hook

Posted: Wed Apr 02, 2014 4:08 pm
by matt.price
Use case:

We store a relationship between the Todo and our Entity models (EntityTodo) in order to surface business data when collecting Todo(s).

One performance issue we have been considering is removing the EntityTodo records when the todo is accomplished since there is little need keep these records around.

We will sometimes (mostly in development and testing) terminate model instances. At this point we would like to remove any EntityTodo(s) associated with that model instance.

It is a nice to have for cleanup.

Right now we include a check for running models in some of our business logic.

Re: Finish Model Instance Callback/Hook

Posted: Wed Apr 02, 2014 4:51 pm
by Maros Bajtos
Usually we keep this entries in the database also after the process finishes, in order to keep the track of accomplished todos. The performance should not be affected, if you have proper indexes in place. As Todos are clearly marked as accomplished or interrupted, filtering EntityTodo instances that point to todos that are not active anymore should be very fast operation (one join and one filtering based on index column).

In any case, if you still wish to do so, you can always extend the default implementation of UserTask (com.whitestein.lsps.human.task.UserTask) - just override the terminate method, first call the super implementation and then you can perform anything you wish - e.g. delete the associated EntityTodo object (e.g. by defining new task closure parameter which you will call from your custom terminate method). Let me know if you need more details!.

Re: Finish Model Instance Callback/Hook

Posted: Wed Apr 02, 2014 7:50 pm
by matt.price
Thanks Maros,

That sounds like a great solution. We may decide to leave the relationship and make a better query to filter out the non-ACTIVE Todo(s).