Пример #1
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;
  }
Пример #2
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;
 }
Пример #3
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;
  }