@Override
 public void deleteProcessInstance(final SProcessInstance sProcessInstance)
     throws SProcessInstanceModificationException {
   final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
   try {
     final long processDefinitionId = sProcessInstance.getProcessDefinitionId();
     final ClassLoader localClassLoader =
         classLoaderService.getLocalClassLoader("process", processDefinitionId);
     Thread.currentThread().setContextClassLoader(localClassLoader);
     deleteProcessInstanceElements(sProcessInstance);
     final DeleteRecord deleteRecord = new DeleteRecord(sProcessInstance);
     SDeleteEvent deleteEvent = null;
     if (eventService.hasHandlers(PROCESSINSTANCE, EventActionType.DELETED)) {
       deleteEvent =
           (SDeleteEvent)
               BuilderFactory.get(SEventBuilderFactory.class)
                   .createDeleteEvent(PROCESSINSTANCE)
                   .setObject(sProcessInstance)
                   .done();
     }
     recorder.recordDelete(deleteRecord, deleteEvent);
   } catch (final SBonitaException e) {
     throw new SProcessInstanceModificationException(e);
   } finally {
     Thread.currentThread().setContextClassLoader(contextClassLoader);
   }
 }
 private void deleteConnectorInstancesIfNecessary(
     final SProcessInstance processInstance, final SProcessDefinition processDefinition)
     throws SConnectorInstanceReadException, SConnectorInstanceDeletionException {
   if (processDefinition != null && processDefinition.hasConnectors()) {
     connectorInstanceService.deleteConnectors(
         processInstance.getId(), SConnectorInstance.PROCESS_TYPE);
   }
 }
 private void deleteDataInstancesIfNecessary(
     final SProcessInstance processInstance, final SProcessDefinition processDefinition)
     throws SDataInstanceException {
   boolean dataPresent = true;
   if (processDefinition != null) {
     dataPresent = processDefinition.getProcessContainer().getDataDefinitions().size() > 0;
   }
   dataInstanceService.deleteLocalDataInstances(
       processInstance.getId(), DataInstanceContainer.PROCESS_INSTANCE.toString(), dataPresent);
 }
  protected void deleteParentProcessInstanceAndElements(final SProcessInstance sProcessInstance)
      throws SFlowNodeReadException, SProcessInstanceHierarchicalDeletionException,
          SProcessInstanceModificationException {

    checkIfCallerIsNotActive(sProcessInstance.getCallerId());

    try {
      deleteProcessInstance(sProcessInstance);
    } catch (final SProcessInstanceModificationException e) {
      try {
        getProcessInstance(sProcessInstance.getId());
        // process is still here, that's not normal. The problem must be raised:
        throw e;
      } catch (SProcessInstanceReadException e1) {
        logProcessInstanceNotFound(e);
      } catch (SProcessInstanceNotFoundException e1) {
        logProcessInstanceNotFound(e);
      }
    }
  }
 @Override
 public void setState(final SProcessInstance processInstance, final ProcessInstanceState state)
     throws SProcessInstanceModificationException {
   // Let's archive the process instance before changing the state (to keep a track of state
   // change):
   archiveProcessInstance(processInstance);
   int previousStateId = processInstance.getStateId();
   setProcessState(processInstance, state);
   if (logger.isLoggable(getClass(), TechnicalLogSeverity.DEBUG)) {
     logger.log(
         getClass(),
         TechnicalLogSeverity.DEBUG,
         MessageFormat.format(
             "[{0} with id {1}]{2}->{3}(new={4})",
             processInstance.getClass().getSimpleName(),
             processInstance.getId(),
             previousStateId,
             state.getId(),
             state.name()));
   }
 }
 private void deleteProcessInstanceElements(final SProcessInstance processInstance)
     throws SBonitaException {
   SProcessDefinition processDefinition = null;
   try {
     processDefinition =
         processDefinitionService.getProcessDefinition(processInstance.getProcessDefinitionId());
   } catch (final SProcessDefinitionNotFoundException e) {
     // delete anyway
   }
   try {
     tokenService.deleteTokens(processInstance.getId());
   } catch (final SObjectReadException e) {
     throw new SProcessInstanceModificationException(e);
   } catch (final SObjectModificationException e) {
     throw new SProcessInstanceModificationException(e);
   }
   deleteFlowNodeInstances(processInstance.getId(), processDefinition);
   deleteDataInstancesIfNecessary(processInstance, processDefinition);
   processDocumentService.deleteDocumentsFromProcessInstance(processInstance.getId());
   deleteConnectorInstancesIfNecessary(processInstance, processDefinition);
   commentService.deleteComments(processInstance.getId());
 }
 private void archiveProcessInstance(final SProcessInstance processInstance)
     throws SProcessInstanceModificationException {
   final SAProcessInstance saProcessInstance =
       BuilderFactory.get(SAProcessInstanceBuilderFactory.class)
           .createNewInstance(processInstance)
           .done();
   final ArchiveInsertRecord insertRecord = new ArchiveInsertRecord(saProcessInstance);
   try {
     archiveService.recordInsert(System.currentTimeMillis(), insertRecord);
   } catch (final SRecorderException e) {
     throw new SProcessInstanceModificationException(e);
   } catch (final SDefinitiveArchiveNotFound e) {
     if (logger.isLoggable(this.getClass(), TechnicalLogSeverity.ERROR)) {
       logger.log(
           this.getClass(),
           TechnicalLogSeverity.ERROR,
           "The process instance was not archived. Id:" + processInstance.getId(),
           e);
     }
   }
 }