@Override
  public void notify(DelegateExecution execution) throws Exception {

    HistoryService historyService = execution.getProcessEngineServices().getHistoryService();

    HistoricDecisionInstance historicDecisionInstance =
        historyService
            .createHistoricDecisionInstanceQuery()
            .includeInputs()
            .includeOutputs()
            .decisionDefinitionKey((String) execution.getVariableLocal("tableName"))
            .processInstanceId(execution.getProcessInstanceId())
            .singleResult();

    // Fill a new object with stuff...
    FraudScoreTableObject fraudData = new FraudScoreTableObject();

    fraudData.setFraudInstanceID(historicDecisionInstance.getId());

    List<HistoricDecisionInputInstance> inputs = historicDecisionInstance.getInputs();
    for (HistoricDecisionInputInstance historicDecisionInputInstance : inputs) {
      String inputName = historicDecisionInputInstance.getTypeName();
      if (inputName.equals("paymentRejected")) {
        fraudData.setPaymentRejected((Boolean) historicDecisionInputInstance.getValue());
      } else if (inputName.equals("numberOfPayouts")) {
        fraudData.setNumberOfPayouts((Integer) historicDecisionInputInstance.getValue());
      } else if (inputName.equals("historyOfFraud")) {
        fraudData.setHistoryOfFraud((Boolean) historicDecisionInputInstance.getValue());
      } else if (inputName.equals("claimAmount")) {
        fraudData.setCalimAmount((Long) historicDecisionInputInstance.getValue());
      }
    }
    List<HistoricDecisionOutputInstance> outputs = historicDecisionInstance.getOutputs();
    for (HistoricDecisionOutputInstance historicDecisionOutputInstance : outputs) {

      String inputName = historicDecisionOutputInstance.getTypeName();
      if (inputName.equals("frausScore")) {
        fraudData.setFraudScore((Integer) historicDecisionOutputInstance.getValue());
      }
    }

    ObjectMapper mapper = new ObjectMapper();

    String serializedHistoricDecisionInstance =
        mapper.writerWithDefaultPrettyPrinter().writeValueAsString(fraudData);

    Client client =
        TransportClient.builder()
            .build()
            .addTransportAddress(
                new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

    IndexResponse response =
        client
            .prepareIndex("camunda", "fraudData", historicDecisionInstance.getId())
            .setSource(serializedHistoricDecisionInstance)
            .get();

    LOGGER.info(response.getId());
  }
  /** Remove all batches and historic batches. Usually called in {@link org.junit.After} method. */
  public void removeAllRunningAndHistoricBatches() {
    HistoryService historyService = engineRule.getHistoryService();
    ManagementService managementService = engineRule.getManagementService();

    for (Batch batch : managementService.createBatchQuery().list()) {
      managementService.deleteBatch(batch.getId(), true);
    }

    // remove history of completed batches
    for (HistoricBatch historicBatch : historyService.createHistoricBatchQuery().list()) {
      historyService.deleteHistoricBatch(historicBatch.getId());
    }
  }
  public HistoricDecisionInstanceDto getHistoricDecisionInstance() {
    HistoryService historyService = engine.getHistoryService();
    HistoricDecisionInstance instance =
        historyService
            .createHistoricDecisionInstanceQuery()
            .decisionInstanceId(decisionInstanceId)
            .singleResult();

    if (instance == null) {
      throw new InvalidRequestException(
          Status.NOT_FOUND,
          "Historic decision instance with id '" + decisionInstanceId + "' does not exist");
    }

    return HistoricDecisionInstanceDto.fromHistoricDecisionInstance(instance);
  }
  @Test
  public void testQueryAuthenticatedTenant() {
    DecisionRequirementsDefinition decisionRequirementsDefinition =
        repositoryService
            .createDecisionRequirementsDefinitionQuery()
            .tenantIdIn(TENANT_ONE)
            .singleResult();

    identityService.setAuthentication(USER_ID, null, Arrays.asList(TENANT_ONE));

    HistoricDecisionInstanceStatisticsQuery query =
        historyService.createHistoricDecisionInstanceStatisticsQuery(
            decisionRequirementsDefinition.getId());

    assertThat(query.count(), is(3L));
  }
  @Test
  public void testQueryDisabledTenantCheck() {
    DecisionRequirementsDefinition decisionRequirementsDefinition =
        repositoryService
            .createDecisionRequirementsDefinitionQuery()
            .tenantIdIn(TENANT_ONE)
            .singleResult();

    engineRule.getProcessEngineConfiguration().setTenantCheckEnabled(false);
    identityService.setAuthentication(USER_ID, null, null);

    HistoricDecisionInstanceStatisticsQuery query =
        historyService.createHistoricDecisionInstanceStatisticsQuery(
            decisionRequirementsDefinition.getId());

    assertThat(query.count(), is(3L));
  }
 protected HistoricCaseInstance getHistoricCaseInstance() {
   return historyService
       .createHistoricCaseInstanceQuery()
       .caseInstanceId(caseInstanceId)
       .singleResult();
 }
 protected HistoricCaseActivityInstance getHistoricCaseActivityInstance() {
   return historyService
       .createHistoricCaseActivityInstanceQuery()
       .caseActivityId(ACTIVITY_ID)
       .singleResult();
 }