The historical information of the process in Activiti is stored in the Act_hi_actinst table, we can query through the API or directly write SQL, but there is no process initiator information, process initiator information is sometimes very necessary, For example, to inquire about the process initiated by someone or to do relevant statistics.
Given that the process history information in Act_hi_actinst, we still want to make a fuss on this table, but this table does not have the initiator and other related fields, at this time, we found that when a process just launched, Act_type_ for startevent, then assignee _ is empty, if we can borrow this field, save the applicant, query the code is still more elegant, although the meaning of the field itself is not the applicant.
The next step is to study the Activiti code and see if we can find the appropriate extension point to solve the problem.
After research, found the extension point, developed or relatively convenient, the following detailed description, note that the development of this article based on the Activiti5.15.1 version, roughly swept the code, the updated version 5.17 should also be compatible, but not tested.
For the code that saves the Act_hi_actinst table at the time of the process initiation, in the Recordprocessinstancestart method of the Defaulthistorymanager class, we just have to find a way to inherit this class, then overwrite this method, While the creation of Defaulthistorymanager is in Defaulthistorymanagersessionfactory, and Defaulthistorymanagersessionfactory is initialized, is in the Processengineconfigurationimpl in the Initsessionfactories method, find here, the idea has been very clear, need to write code not much.
One, extended Processengineconfigurationimpl, if using spring integration, inherit the Springprocessengineconfiguration class, overwrite the Initsessionfactories method , call the Addsessionfactory method in the method to add a new custom defaulthistorymanagersessionfactory;
Second, extend the defaulthistorymanagersessionfactory, overwrite the Opensession method, and create a new Defaulthistorymanager in the method;
Third, extend the Defaulthistorymanager, overwrite the Recordprocessinstancestart method, in this method for the Assign field assignment can, as for the current process initiator acquisition method, you have to find a way, Our solution is obtained through the process context;
The specific code looks like this:
Import Org.activiti.spring.springprocessengineconfiguration;public class Processengineconfiguration extends springprocessengineconfiguration {@Override protected void initsessionfactories () {Super.initsessionfactori ES (); Super.addsessionfactory (New Processhistorymanagersessionfactory ()); }}
Import Org.activiti.engine.impl.interceptor.session;import Org.activiti.engine.impl.persistence.defaulthistorymanagersessionfactory;public class Processhistorymanagersessionfactory extends Defaulthistorymanagersessionfactory {@Override public Session Opensessi On () {return new Processhistorymanager (); }}
public class processhistorymanager extends defaulthistorymanager { @Override public void recordprocessinstancestart (executionentity ProcessInstance) { if (Ishistorylevelatleast ( historylevel.activity)) { Historicprocessinstanceentity historicprocessinstance = new historicprocessinstanceentity ( ProcessInstance); getdbsqlsession (). Insert ( Historicprocessinstance); idgenerator idgenerator = context.getprocessengineconfiguration (). GetIdGenerator (); string processdefinitionid = pRocessinstance.getprocessdefinitionid (); string processinstanceid = processinstance.getprocessinstanceid (); string executionid = processinstance.getid (); historicactivityinstanceentity historicactivityinstance = new historicactivityinstanceentity (); historicactivityinstance.setid (Idgenerator.getnextid ()); historicactivityinstance.setprocessdefinitionid (PROCESSDEFINITIONID) ; historicactivityinstance.setprocessinstanceid ( Processinstanceid); Historicactivityinstance.setexecutionid (Executionid); historicactivityinstance.setactivityid ( Processinstance.getactivityid ()); Historicactivityinstance.setactivityname (String) processinstance.getactivity (). GetProperty ("name")); historicactivityinstance.setactivitytype (String) processinstance.getactivity (). GetProperty ("type")); date now = context.getprocessengineconfiguration (). GetClock (). GetCurrentTime (); historicactivityinstance.setstarttime (now); if (Processinstance.gettenantid () != null) { historicactivityInstance.settenantid (Processinstance.gettenantid ()); } corecontext cc = corecontextcontainer.getcontext (); if (Cc != null && cc.getsysuser ()!=null) { Historicactivityinstance.setassignee (Cc.getsysuser (). GetUserId ()); } getdbsqlsession (). Insert (historicactivityinstance); } }}
Process History in Activiti save applicant's method