/**
  * Has the Batch Processing finished
  *
  * <p>Fetches a Batch result
  *
  * <p>{@sample.xml ../../../doc/avalara-connector.xml.sample avalara:is-batch-finished}
  *
  * @param batchId The numerical identifier of the BatchFile.
  * @return a boolean representing if the Batch finished or not
  * @throws AvalaraRuntimeException
  */
 @Processor
 public boolean isBatchFinished(String batchId) {
   final FetchRequest batchFetchRequest = new FetchRequest();
   batchFetchRequest.setFilters("BatchId=" + batchId);
   BatchFetchResult batchFetchResult =
       apiClient.sendBatchRequestToAvalara(BatchRequestType.BatchFetch, batchFetchRequest);
   if (batchFetchResult.getBatches().getBatch().size() == 0
       || (batchFetchResult.getBatches().getBatch().get(0).getRecordCount()
           > batchFetchResult.getBatches().getBatch().get(0).getCurrentRecord())) {
     return false;
   } else {
     return true;
   }
 }
  /**
   * Batch Fetch processor.
   *
   * <p>Fetches a Batch result
   *
   * <p>{@sample.xml ../../../doc/avalara-connector.xml.sample avalara:fetch-batch-file}
   *
   * @param batchId The numerical identifier of the BatchFile.
   * @return The {@link Map<String,BatchFileFetchResult>}
   * @throws AvalaraRuntimeException
   */
  @Processor
  public Map<String, BatchFileFetchResult> fetchBatchFile(String batchId) {
    // This Request is needed to retrieve the batch file ids. The actual content cannot be retrieved
    // at once.
    FetchRequest batchFetchRequest = new FetchRequest();
    batchFetchRequest.setFields("Files");
    batchFetchRequest.setFilters("BatchId=" + batchId);
    BatchFetchResult batchFetchResult =
        apiClient.sendBatchRequestToAvalara(BatchRequestType.BatchFetch, batchFetchRequest);
    Map<String, BatchFileFetchResult> resultHashMap = new HashMap<String, BatchFileFetchResult>();
    // This is in order to be able to return result and error file to the caller.
    for (BatchFile bf : batchFetchResult.getBatches().getBatch().get(0).getFiles().getBatchFile()) {
      if (bf.getName().equalsIgnoreCase("Result")) {
        FetchRequest fetchRequest = new FetchRequest();
        fetchRequest.setFields("*,Content");
        fetchRequest.setFilters("BatchFileId=" + bf.getBatchFileId());
        resultHashMap.put(
            "result",
            (BatchFileFetchResult)
                apiClient.sendBatchRequestToAvalara(BatchRequestType.BatchFileFetch, fetchRequest));
      } else if (bf.getName().equalsIgnoreCase("Error")) {
        FetchRequest fetchRequest = new FetchRequest();
        fetchRequest.setFields("*,Content");
        fetchRequest.setFilters("BatchFileId=" + bf.getBatchFileId());
        resultHashMap.put(
            "error",
            (BatchFileFetchResult)
                apiClient.sendBatchRequestToAvalara(BatchRequestType.BatchFileFetch, fetchRequest));
      }
    }

    return resultHashMap;
  }