cancel
Showing results for 
Search instead for 
Did you mean: 

Remove a comment from a task

chris_joelly
Champ in-the-making
Champ in-the-making
Hello,

i add comments to a task with taskService.addComment(), but the taskService does not offer a method for removing comments. I found the CommentManager which has the method commentManager.delete(PersistentObject).

Is this the API for removing a task or is there another way to go? Somehow i have worries to use CommentManager and CommentEntity directly …

Regards
Chris
9 REPLIES 9

ronald_van_kuij
Champ on-the-rise
Champ on-the-rise
I don't think it is dangerous if you look at the datamodel…

jbarrez
Star Contributor
Star Contributor
The philosophy back then was that comments could not be removed.
Otherwise you get funny situations once people start commenting on stuff that is removed later … However, nowadays I'm not sure about that functionality …

chris_joelly
Champ in-the-making
Champ in-the-making
well, the worries i have is that CommentManager and CommentEntity looks like Activiti internals. And i expect such things to change over time.
TaskService on the other side seem to be a documented API which is not likely to change over time. At least it makes me believe that it is not likely to change a lot…
Thus i would prefer something like TaskService.deleteComment(Comment) (but Comment has no unique identifier…)

When i use CommandManager, what do i need to do so that i can use it? Just instantiating it and using it like the following snippet does not work:

public void removeTaskComment(Comment comment) {
  if (comment instanceof CommentEntity) {
   CommentManager commentManager = new CommentManager();
   CommentEntity commentEntity = (CommentEntity) comment;
   commentManager.delete(commentEntity);
  }
}

Caused by: java.lang.NullPointerException
at org.activiti.engine.impl.persistence.AbstractHistoricManager.<init>(AbstractHistoricManager.java:26)
at org.activiti.engine.impl.persistence.entity.CommentManager.<init>(CommentManager.java:27)
at erp.web.faces.beans.activiti.TaskManager.removeTaskComment(TaskManager.java:168)
is there something special which needs to be done to satisfy protected int historyLevel = Context.getProcessEngineConfiguration().getHistoryLevel();
History is enabled with "full"

jbarrez
Star Contributor
Star Contributor
Yes, you are correct: the service API's are stable and won't change much over time. Internal stuff is indeed more dangerous there.

You can't instantiate the comment manager directly. But if you want to hack your way around it: cast the process engine to 'ProcessEngineConfigurationImpl' and get the comment manager through there.

chris_joelly
Champ in-the-making
Champ in-the-making
thank u for the hint.

unfortunately the cast to ProcessEngineImpl results in the same problem:


Caused by: org.activiti.engine.ActivitiException: couldn't instantiate org.activiti.engine.impl.persistence.entity.CommentManager: null
at org.activiti.engine.impl.persistence.GenericManagerFactory.openSession(GenericManagerFactory.java:40) [activiti-engine-5.10-SNAPSHOT.jar:]
at erp.web.faces.beans.activiti.TaskManager.removeTaskComment(TaskManager.java:185) [classes:]
at erp.web.faces.beans.activiti.TaskManager$Proxy$_$$_WeldClientProxy.removeTaskComment(TaskManager$Proxy$_$$_WeldClientProxy.java) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_26]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [rt.jar:1.6.0_26]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [rt.jar:1.6.0_26]
at java.lang.reflect.Method.invoke(Method.java:597) [rt.jar:1.6.0_26]
at org.apache.el.parser.AstValue.invoke(AstValue.java:262) [jbossweb-7.0.13.Final.jar:]
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) [jbossweb-7.0.13.Final.jar:]
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:39) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) [jsf-impl-2.1.7-jbossorg-2.jar:]
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
… 37 more
Caused by: java.lang.NullPointerException
at org.activiti.engine.impl.persistence.AbstractHistoricManager.<init>(AbstractHistoricManager.java:26) [activiti-engine-5.10-SNAPSHOT.jar:]
at org.activiti.engine.impl.persistence.entity.CommentManager.<init>(CommentManager.java:27) [activiti-engine-5.10-SNAPSHOT.jar:]
at sun.reflect.GeneratedConstructorAccessor215.newInstance(Unknown Source) [:1.6.0_26]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) [rt.jar:1.6.0_26]
at java.lang.reflect.Constructor.newInstance(Constructor.java:513) [rt.jar:1.6.0_26]
at java.lang.Class.newInstance0(Class.java:355) [rt.jar:1.6.0_26]
at java.lang.Class.newInstance(Class.java:308) [rt.jar:1.6.0_26]
at org.activiti.engine.impl.persistence.GenericManagerFactory.openSession(GenericManagerFactory.java:38) [activiti-engine-5.10-SNAPSHOT.jar:]

with this code:


public void removeTaskComment(Comment comment) {
  if (comment instanceof CommentEntity) {
   ProcessEngineImpl pe = (ProcessEngineImpl) ProcessEngines.getDefaultProcessEngine();
   SessionFactory sessionFactory = pe.getProcessEngineConfiguration()
     .getSessionFactories().get(CommentManager.class);
   Session session = sessionFactory.openSession();
   if (session != null && session instanceof CommentManager) {
    CommentManager commentManager = (CommentManager) session;
    CommentEntity commentEntity = (CommentEntity) comment;
    commentManager.delete(commentEntity);
   }
  }
}

At first i tried with the injected ProcessEngine, but i could not cast that Weld proxy to ProcessEngineImpl, thats why i want to get it via ProcessEngines.getDefaultProcessEngine().

jbarrez
Star Contributor
Star Contributor
And what of you got the processEngineConfiguration first and cast that to processEngineConfigurationImpl (not processEngineImpl!), as I said in my previous comment? Didn't test it tough.

chris_joelly
Champ in-the-making
Champ in-the-making
sorry, i understood wrong when u wrote "cast the process engine to 'ProcessEngineConfigurationImpl'".

and ProcessEngines.getDefaultProcessEngine().getProcessEngineConfiguration() already returns an ProcessEngineConfigurationImpl…

jbarrez
Star Contributor
Star Contributor
Okay … then the only thing which will probably work, is to create your own Command class, and add it to the service. It's also possible to create a command, and hand it over to the Activiti command executor.

Check the service implementation how to do that. That should give you access to the full blown context in which the comment manager should operate.

jbarrez
Star Contributor
Star Contributor
Okay, since I was going to create a blog on custom command anyway, I created a simple example how to do this: https://github.com/jbarrez/activiti-custom-command