<java>
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import com.dynamic.bpm.module.purchase.entity.Purchase;
public class ProcessListenerEvent implements ExecutionListener
{
    protected final transient Logger log = Logger.getLogger(super.getClass());
    private static final long serialVersionUID = 5914272129386342550L;
    @Override
    public void notify(DelegateExecution execution) throws Exception
    {
     
        final String eventName = execution.getEventName();
        if (log.isDebugEnabled())
            log.debug(" > processlistenerevent > " + execution.getProcessBusinessKey() + " > eventName > " + eventName);
        if(execution.getEventName().equals(ProcessListenerEvent.EVENTNAME_START))
        {
            ExecutionEntity thisEntity = (ExecutionEntity) execution;
            ExecutionEntity superExecEntity = thisEntity.getSuperExecution();
            String key = "";
            // Check if this is the main process?.
            if (superExecEntity == null)
            {
                // If it is, get the business key with the main process was launched
                // with.
                //TODO use getProcessBusinessKey() from 5.15.1
             
                key = thisEntity.getBusinessKey();
                
                if(StringUtils.isEmpty(key))
                {
                 key = thisEntity.getProcessBusinessKey();
                }
                
            }
            else
            {
                // it could be caller / subprocess ; so get the BusinessKey variable set by the
                // caller.
                // This should work for N no of multi- level deep sub processes.
                key = (String) superExecEntity.getVariable("BusinessKey");
            }
            // if both the above method fails? get from the internal variable  
            final Purchase purchase = (Purchase) execution.getVariable("purchase");
            if(StringUtils.isNotEmpty(key))
            {
                // Set a process variable with the business key. Can't actually set
                // business key because business keys
                // have to be unique per process instance.
                thisEntity.setVariable("BusinessKey", key);                
            }
            else
            {
                thisEntity.setVariable("BusinessKey", purchase.getBusinessKey());
            }
            //TODO check the alternative, it's deprecated in 5.15.1 it's been removed.!!!!
            //execution.updateProcessBusinessKey(purchase.getBusinessKey());
            
            
            execution.getEngineServices().getRuntimeService().updateBusinessKey(execution.getProcessInstanceId(), key);
                        
            if (log.isDebugEnabled())
            {
             //TODO sometimes this may throw NPE, could be bcoz of this called method execution.getProcessBusinessKey()
             try
             {
              log.debug(" < update processlistenerevent > " + key + " > eventName > " + eventName +" < purchase < "+purchase.getBusinessKey() +" > processBusinessKey < "+execution.getProcessBusinessKey()); 
             }
             catch(NullPointerException npe){
              log.debug(" key > "+key);
             }
             
            }
                
        }
        
        if(log.isDebugEnabled()) log.debug(" < processlistenerevent < event = "+eventName);
    }
}
</java>