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

co.easimart.EasimartOperationSet Maven / Gradle / Ivy

package co.easimart;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.UUID;

/**
 * A set of field-level operations that can be performed on an object, corresponding to one command.
 * For example, all of the data for a single call to save() will be packaged here. It is assumed
 * that the EasimartObject that owns the operations handles thread-safety.
 */
/** package */ class EasimartOperationSet extends HashMap {
  private static final long serialVersionUID = 1L;

  private static final String REST_KEY_IS_SAVE_EVENTUALLY = "__isSaveEventually";
  private static final String REST_KEY_UUID = "__uuid";
  
  // A unique id for this operation set.
  private final String uuid;

  // Does this set correspond to a call to saveEventually?
  private boolean isSaveEventually = false;
  
  /**
   * Creates a new operation set with a random UUID.
   */
  public EasimartOperationSet() {
    this(UUID.randomUUID().toString());
  }

  public EasimartOperationSet(EasimartOperationSet operations) {
    super(operations);
    uuid = operations.getUUID();
    isSaveEventually = operations.isSaveEventually;
  }

  /**
   * Creates a new operation set with the given UUID.
   */
  private EasimartOperationSet(String uuid) {
    this.uuid = uuid;
  }

  public String getUUID() {
    return uuid;
  }

  public void setIsSaveEventually(boolean value) {
    isSaveEventually = value;
  }

  public boolean isSaveEventually() {
    return isSaveEventually;
  }
  
  /**
   * Merges the changes from the given operation set into this one. Most typically, this is what
   * happens when a save fails and changes need to be rolled into the next save.
   */
  public void mergeFrom(EasimartOperationSet other) {
    for (String key : other.keySet()) {
      EasimartFieldOperation operation1 = other.get(key);
      EasimartFieldOperation operation2 = get(key);
      if (operation2 != null) {
        operation2 = operation2.mergeWithPrevious(operation1);
      } else {
        operation2 = operation1;
      }
      put(key, operation2);
    }
  }

  /**
   * Converts this operation set into its REST format for serializing to LDS.
   */
  public JSONObject toRest(EasimartEncoder objectEncoder) throws JSONException {
    JSONObject operationSetJSON = new JSONObject();
    for (String key : keySet()) {
      EasimartFieldOperation op = get(key);
      operationSetJSON.put(key, op.encode(objectEncoder));
    }

    operationSetJSON.put(REST_KEY_UUID, uuid);
    if (isSaveEventually) {
      operationSetJSON.put(REST_KEY_IS_SAVE_EVENTUALLY, true);
    }
    return operationSetJSON;
  }
  
  /**
   * The inverse of toRest. Creates a new OperationSet from the given JSON.
   */
  public static EasimartOperationSet fromRest(JSONObject json, EasimartDecoder decoder)
      throws JSONException {
    // Copy the json object to avoid making changes to the old object
    Iterator keysIter = json.keys();
    String[] keys = new String[json.length()];
    int index = 0;
    while (keysIter.hasNext()) {
      String key = keysIter.next();
      keys[index++] = key;
    }

    JSONObject jsonCopy = new JSONObject(json, keys);
    String uuid = (String) jsonCopy.remove(REST_KEY_UUID);
    EasimartOperationSet operationSet =
        (uuid == null ? new EasimartOperationSet() : new EasimartOperationSet(uuid));

    boolean isSaveEventually = jsonCopy.optBoolean(REST_KEY_IS_SAVE_EVENTUALLY);
    jsonCopy.remove(REST_KEY_IS_SAVE_EVENTUALLY);
    operationSet.setIsSaveEventually(isSaveEventually);

    Iterator opKeys = jsonCopy.keys();
    while (opKeys.hasNext()) {
      String opKey = (String) opKeys.next();
      Object value = decoder.decode(jsonCopy.get(opKey));
      EasimartFieldOperation fieldOp;
      if (opKey.equals("ACL")) {
        value = EasimartACL.createACLFromJSONObject(jsonCopy.getJSONObject(opKey), decoder);
      }
      if (value instanceof EasimartFieldOperation) {
        fieldOp = (EasimartFieldOperation) value;
      } else {
        fieldOp = new EasimartSetOperation(value);
      }
      operationSet.put(opKey, fieldOp);
    }

    return operationSet;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy