All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mule.runtime.api.bulk.BulkOperationResult Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
/*
 * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */

package org.mule.runtime.api.bulk;

import org.mule.runtime.api.bulk.BulkItem.BulkItemBuilder;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * This class is used to provide item level information about a bulk operation. This master entity represents the bulk operation
 * as a whole, while the detail entity {@link BulkItem} represents the operation status for each individual data piece. The
 * {@link #items} list defines a contract in which the ordering of those items needs to match the ordering of the original
 * objects. For example, if the bulk operation consisted of 10 person objects in which number X corresponded to the person 'John
 * Doe', then the Xth item in the {@link #items} list must reference to the result of procesing the same 'John Doe'
 *
 * @since 1.0
 */
public final class BulkOperationResult implements Serializable {

  private static final long serialVersionUID = 8039267004891928585L;

  private final Serializable id;
  private final boolean successful;
  private final List> items;
  private final Map customProperties;

  private BulkOperationResult(Serializable id,
                              boolean successful,
                              List> items,
                              Map customProperties) {
    this.id = id;
    this.successful = successful;
    this.items = items;
    if (customProperties != null) {
      this.customProperties = new HashMap<>(customProperties);
    } else {
      this.customProperties = null;
    }
  }

  /**
   * @return The operation id
   */
  public Serializable getId() {
    return id;
  }

  /**
   * @return Whether or not the operation was successful. Should be {@code true} if and only if all the child {@link BulkItem}
   * entities were also successful
   */
  public boolean isSuccessful() {
    return successful;
  }

  /**
   * @return An ordered list of {@link BulkItem}, one per each item in the original operation, no matter if the record was successful or
   * not
   */
  public List> getItems() {
    return items;
  }

  /**
   * A custom property stored under the given key
   *
   * @param key the key of the custom property
   * @return a {@link Serializable} value
   */
  public Serializable getCustomProperty(String key) {
    return this.customProperties != null ? this.customProperties.get(key) : null;
  }

  public static  BulkOperationResultBuilder builder() {
    return new BulkOperationResultBuilder();
  }

  public static class BulkOperationResultBuilder extends AbstractBulkBuilder {

    private Serializable id;
    private boolean successful = true;
    private List> items = new ArrayList<>();

    private BulkOperationResultBuilder() {}

    public BulkOperationResultBuilder setSuccessful(boolean successful) {
      this.successful = successful;
      return this;
    }

    public BulkOperationResultBuilder setId(Serializable id) {
      this.id = id;
      return this;
    }

    public BulkOperationResultBuilder addItem(BulkItemBuilder recordResultBuilder) {
      this.items.add(recordResultBuilder);
      return this;
    }

    public BulkOperationResultBuilder addCustomProperty(String key, Serializable value) {
      this.customProperty(key, value);
      return this;
    }

    public BulkOperationResult build() {
      if (this.items.isEmpty()) {
        throw new IllegalStateException("A BulkOperationResult must have at least one BulkItem. Please add a result an try again");
      }

      List> items = new ArrayList<>(this.items.size());
      for (BulkItemBuilder recordBuilder : this.items) {
        BulkItem record = recordBuilder.build();
        items.add(record);
        if (!record.isSuccessful()) {
          this.successful = false;
        }
      }

      return new BulkOperationResult(this.id, this.successful, items, this.getCustomProperties());
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy