示例#1
0
  /**
   * Deletes the entity on the server.
   *
   * @return an ApiResponse object
   */
  public ApiResponse destroy() {
    ApiResponse response = new ApiResponse();
    String type = getType();
    String uuidAsString = null;
    UUID uuid = getUuid();
    if (uuid != null) {
      uuidAsString = uuid.toString();
    } else {
      String error = "Error trying to delete object: No UUID specified.";
      this.client.writeLog(error);
      response.setError(error);
      // response.setErrorCode(error);
      return response;
    }

    response = this.client.removeEntity(type, uuidAsString);

    if ((response != null) && (response.getError() != null)) {
      this.client.writeLog("Entity could not be deleted.");
    } else {
      this.properties.clear();
    }

    return response;
  }
示例#2
0
  /**
   * Deletes the provided entity on the server, then updates the Collection object by calling
   * fetch(). Executes synchronously.
   *
   * @param entity an Entity object that contains a 'type' and 'uuid' property
   */
  public ApiResponse destroyEntity(Entity entity) {
    ApiResponse response = entity.destroy();
    if (response.getError() != null) {
      this.client.writeLog("Could not destroy entity.");
    } else {
      response = this.fetch();
    }

    return response;
  }
示例#3
0
 /**
  * Adds an entity to the Collection object.
  *
  * @param entityData a Map object of entity properties to be saved in the entity
  * @return an Entity object that represents the newly added entity. Must include a 'type'
  *     property. Executes synchronously.
  */
 public Entity addEntity(Map<String, Object> entityData) {
   Entity entity = null;
   ApiResponse response = this.client.createEntity(entityData);
   if ((response != null) && (response.getError() == null) && (response.getEntityCount() > 0)) {
     entity = response.getFirstEntity();
     if (entity != null) {
       this.list.add(entity);
     }
   }
   return entity;
 }
示例#4
0
  /**
   * Fetches the current state of the entity from the server and saves it in the Entity object. Runs
   * synchronously.
   *
   * @return an ApiResponse object
   */
  public ApiResponse fetch() {
    ApiResponse response = new ApiResponse();
    String type = this.getType();
    UUID uuid = this.getUuid(); // may be NULL
    String entityId = null;
    if (uuid != null) {
      entityId = uuid.toString();
    } else {
      if (User.isSameType(type)) {
        String username = this.getStringProperty(User.PROPERTY_USERNAME);
        if ((username != null) && (username.length() > 0)) {
          entityId = username;
        } else {
          String error = "no_username_specified";
          this.client.writeLog(error);
          response.setError(error);
          // response.setErrorCode(error);
          return response;
        }
      } else {
        String name = this.getStringProperty(PROPERTY_NAME);
        if ((name != null) && (name.length() > 0)) {
          entityId = name;
        } else {
          String error = "no_name_specified";
          this.client.writeLog(error);
          response.setError(error);
          // response.setErrorCode(error);
          return response;
        }
      }
    }

    Query q =
        this.client.queryEntitiesRequest(
            "GET",
            null,
            null,
            this.client.getOrganizationId(),
            this.client.getApplicationId(),
            type,
            entityId);
    response = q.getResponse();
    if (response.getError() != null) {
      this.client.writeLog("Could not get entity.");
    } else {
      if (response.getUser() != null) {
        this.addProperties(response.getUser().getProperties());
      } else if (response.getEntityCount() > 0) {
        Entity entity = response.getFirstEntity();
        this.setProperties(entity.getProperties());
      }
    }

    return response;
  }
示例#5
0
  /**
   * Saves the Entity object as an entity on the server. Any conflicting properties on the server
   * will be overwritten. Runs synchronously.
   *
   * @return an ApiResponse object
   */
  public ApiResponse save() {
    ApiResponse response = null;
    UUID uuid = this.getUuid();
    boolean entityAlreadyExists = false;

    if (client.isUuidValid(uuid)) {
      entityAlreadyExists = true;
    }

    // copy over all properties except some specific ones
    Map<String, Object> data = new HashMap<String, Object>();
    Set<String> keySet = this.properties.keySet();
    Iterator<String> keySetIter = keySet.iterator();

    while (keySetIter.hasNext()) {
      String key = keySetIter.next();
      if (!key.equals(PROPERTY_METADATA)
          && !key.equals(PROPERTY_CREATED)
          && !key.equals(PROPERTY_MODIFIED)
          && !key.equals(PROPERTY_ACTIVATED)
          && !key.equals(PROPERTY_UUID)) {
        data.put(key, this.properties.get(key));
      }
    }

    if (entityAlreadyExists) {
      // update it
      response = this.client.updateEntity(uuid.toString(), data);
    } else {
      // create it
      response = this.client.createEntity(data);
    }

    if (response.getError() != null) {
      this.client.writeLog("Could not save entity.");
    } else {
      if (response.getEntityCount() > 0) {
        Entity entity = response.getFirstEntity();
        this.setProperties(entity.getProperties());
      }
    }

    return response;
  }
示例#6
0
  /**
   * Retrieves the current state of the collection from the server, and populates an the Collection
   * object with the returned set of entities. Executes synchronously.
   *
   * @return an ApiResponse object
   */
  public ApiResponse fetch() {
    if (this.cursor != null) {
      this.qs.put("cursor", this.cursor);
    }

    Query query =
        this.client.queryEntitiesRequest(
            "GET",
            this.qs,
            null,
            this.client.getOrganizationId(),
            this.client.getApplicationId(),
            this.type);
    ApiResponse response = query.getResponse();
    if (response.getError() != null) {
      this.client.writeLog("Error getting collection.");
    } else {
      String theCursor = response.getCursor();
      int count = response.getEntityCount();

      UUID nextUUID = response.getNext();
      if (nextUUID != null) {
        this.next = nextUUID.toString();
      } else {
        this.next = null;
      }
      this.cursor = theCursor;

      this.saveCursor(theCursor);
      if (count > 0) {
        this.resetEntityPointer();
        this.list = new ArrayList<Entity>();
        List<Entity> retrievedEntities = response.getEntities();

        for (Entity retrievedEntity : retrievedEntities) {
          if (retrievedEntity.getUuid() != null) {
            retrievedEntity.setType(this.type);
            this.list.add(retrievedEntity);
          }
        }
      }
    }

    return response;
  }