@Override
  public void deleteArchivedProcessInstancesOfProcessInstance(final long processInstanceId)
      throws SBonitaException {
    final int fromIndex = 0;
    final int maxResults = 100;
    final SAProcessInstanceBuilderFactory archProcInstKeyProvider =
        BuilderFactory.get(SAProcessInstanceBuilderFactory.class);

    List<SAProcessInstance> archProcInstances = null;
    do {
      // fromIndex variable is not updated because the elements will be deleted, so we always need
      // to start from zero;
      final SAProcessInstanceBuilderFactory processInstanceBuilderFact =
          BuilderFactory.get(SAProcessInstanceBuilderFactory.class);
      final FilterOption filterOption =
          new FilterOption(
              SAProcessInstance.class,
              processInstanceBuilderFact.getSourceObjectIdKey(),
              processInstanceId);
      final OrderByOption orderBy =
          new OrderByOption(
              SAProcessInstance.class, archProcInstKeyProvider.getIdKey(), OrderByType.ASC);
      final QueryOptions queryOptions =
          new QueryOptions(
              fromIndex,
              maxResults,
              Collections.singletonList(orderBy),
              Collections.singletonList(filterOption),
              null);
      archProcInstances = searchArchivedProcessInstances(queryOptions);
      for (final SAProcessInstance archProcInstance : archProcInstances) {
        deleteArchivedProcessInstance(archProcInstance);
      }
    } while (!archProcInstances
        .isEmpty()); // never will be null as the persistence service sends an empty list if there
    // are no results
  }
  public SearchArchivedProcessInstancesDescriptor() {
    final SAProcessInstanceBuilderFactory instanceBuilder =
        BuilderFactory.get(SAProcessInstanceBuilderFactory.class);
    final SUserTaskInstanceBuilderFactory sUserTaskInstanceBuilder =
        BuilderFactory.get(SUserTaskInstanceBuilderFactory.class);

    searchEntityKeys = new HashMap<String, FieldDescriptor>(14);
    searchEntityKeys.put(
        ArchivedProcessInstancesSearchDescriptor.NAME,
        new FieldDescriptor(SAProcessInstance.class, instanceBuilder.getNameKey()));
    searchEntityKeys.put(
        ArchivedProcessInstancesSearchDescriptor.PROCESS_DEFINITION_ID,
        new FieldDescriptor(SAProcessInstance.class, instanceBuilder.getProcessDefinitionIdKey()));
    searchEntityKeys.put(
        ArchivedProcessInstancesSearchDescriptor.ID,
        new FieldDescriptor(SAProcessInstance.class, instanceBuilder.getIdKey()));
    searchEntityKeys.put(
        ArchivedProcessInstancesSearchDescriptor.STARTED_BY,
        new FieldDescriptor(SAProcessInstance.class, instanceBuilder.getStartedByKey()));
    searchEntityKeys.put(
        ArchivedProcessInstancesSearchDescriptor.STARTED_BY_DELEGATE,
        new FieldDescriptor(SAProcessInstance.class, instanceBuilder.getStartedByDelegateKey()));
    searchEntityKeys.put(
        ArchivedProcessInstancesSearchDescriptor.START_DATE,
        new FieldDescriptor(SAProcessInstance.class, instanceBuilder.getStartDateKey()));
    searchEntityKeys.put(
        ArchivedProcessInstancesSearchDescriptor.END_DATE,
        new FieldDescriptor(SAProcessInstance.class, instanceBuilder.getEndDateKey()));
    searchEntityKeys.put(
        ArchivedProcessInstancesSearchDescriptor.STATE_ID,
        new FieldDescriptor(SAProcessInstance.class, instanceBuilder.getStateIdKey()));
    searchEntityKeys.put(
        ArchivedProcessInstancesSearchDescriptor.SOURCE_OBJECT_ID,
        new FieldDescriptor(SAProcessInstance.class, instanceBuilder.getSourceObjectIdKey()));
    searchEntityKeys.put(
        ArchivedProcessInstancesSearchDescriptor.LAST_UPDATE,
        new FieldDescriptor(SAProcessInstance.class, instanceBuilder.getLastUpdateKey()));
    searchEntityKeys.put(
        ArchivedProcessInstancesSearchDescriptor.ARCHIVE_DATE,
        new FieldDescriptor(SAProcessInstance.class, instanceBuilder.getArchiveDateKey()));
    searchEntityKeys.put(
        ArchivedProcessInstancesSearchDescriptor.CALLER_ID,
        new FieldDescriptor(SAProcessInstance.class, instanceBuilder.getCallerIdKey()));
    searchEntityKeys.put(
        ArchivedProcessInstancesSearchDescriptor.USER_ID,
        new FieldDescriptor(
            SProcessSupervisor.class,
            BuilderFactory.get(SProcessSupervisorBuilderFactory.class).getUserIdKey()));
    searchEntityKeys.put(
        ArchivedProcessInstancesSearchDescriptor.GROUP_ID,
        new FieldDescriptor(
            SProcessSupervisor.class,
            BuilderFactory.get(SProcessSupervisorBuilderFactory.class).getGroupIdKey()));
    searchEntityKeys.put(
        ArchivedProcessInstancesSearchDescriptor.ROLE_ID,
        new FieldDescriptor(
            SProcessSupervisor.class,
            BuilderFactory.get(SProcessSupervisorBuilderFactory.class).getRoleIdKey()));
    searchEntityKeys.put(
        ArchivedProcessInstancesSearchDescriptor.ASSIGNEE_ID,
        new FieldDescriptor(SUserTaskInstance.class, sUserTaskInstanceBuilder.getAssigneeIdKey()));

    archivedProcessInstanceAllFields =
        new HashMap<Class<? extends PersistentObject>, Set<String>>(1);
    processInstanceFields = new HashSet<String>(1);
    // processInstanceFields.add(instanceBuilder.getStartedByKey());
    processInstanceFields.add(instanceBuilder.getNameKey());
    archivedProcessInstanceAllFields.put(SAProcessInstance.class, processInstanceFields);
  }