private void setProcessState(
     final SProcessInstance processInstance, final ProcessInstanceState state)
     throws SProcessInstanceModificationException {
   final EntityUpdateDescriptor descriptor = new EntityUpdateDescriptor();
   descriptor.addField(processInstanceKeyProvider.getStateIdKey(), state.getId());
   final long now = System.currentTimeMillis();
   switch (state) {
     case COMPLETED:
       descriptor.addField(processInstanceKeyProvider.getEndDateKey(), now);
       break;
     case ABORTED:
       descriptor.addField(processInstanceKeyProvider.getEndDateKey(), now);
       break;
     case CANCELLED:
       descriptor.addField(processInstanceKeyProvider.getEndDateKey(), now);
       break;
     case STARTED:
       descriptor.addField(processInstanceKeyProvider.getStartDateKey(), now);
       break;
     default:
       break;
   }
   descriptor.addField(processInstanceKeyProvider.getLastUpdateKey(), now);
   updateProcessInstance(
       processInstance, "updating process instance state", descriptor, PROCESSINSTANCE_STATE);
 }
 @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()));
   }
 }
 @Override
 public List<SProcessInstance> getProcessInstancesInState(
     final QueryOptions queryOptions, final ProcessInstanceState state)
     throws SProcessInstanceReadException {
   final Map<String, Object> inputParameters = new HashMap<String, Object>(1);
   inputParameters.put("state", state.getId());
   final SelectListDescriptor<SProcessInstance> selectListDescriptor =
       new SelectListDescriptor<SProcessInstance>(
           "getProcessInstancesInState", inputParameters, SProcessInstance.class, queryOptions);
   try {
     return persistenceRead.selectList(selectListDescriptor);
   } catch (final SBonitaReadException e) {
     throw new SProcessInstanceReadException(e);
   }
 }