示例#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
 /** @y.exclude */
 public static <T extends Entity> List<T> toType(List<Entity> entities, Class<T> t) {
   List<T> l = new ArrayList<T>(entities != null ? entities.size() : 0);
   if (entities != null) {
     for (Entity entity : entities) {
       T newEntity = entity.toType(t);
       if (newEntity != null) {
         l.add(newEntity);
       }
     }
   }
   return l;
 }
示例#3
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;
  }
示例#4
0
 /** @y.exclude */
 public static <T extends Entity> T toType(Entity entity, Class<T> t) {
   if (entity == null) {
     return null;
   }
   T newEntity = null;
   if (entity.getClass().isAssignableFrom(t)) {
     try {
       newEntity = (t.newInstance());
       if ((newEntity.getNativeType() != null)
           && newEntity.getNativeType().equals(entity.getType())) {
         newEntity.properties = entity.properties;
       }
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
   return newEntity;
 }
示例#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
  /**
   * 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;
  }
示例#7
0
 /**
  * Retrieves an entity from the server.
  *
  * @param uuid the UUID of the entity to retrieve
  * @return an ApiResponse object
  */
 public ApiResponse getEntityByUuid(UUID uuid) {
   Entity entity = new Entity(this.client);
   entity.setType(this.type);
   entity.setUuid(uuid);
   return entity.fetch();
 }
示例#8
0
 /**
  * Constructs a Device object from an Entity object. If the Entity has a 'type' property with a
  * value other than 'device', the value will be overwritten.
  */
 public Device(Entity entity) {
   super(entity.getUGClient());
   properties = entity.properties;
   setType(ENTITY_TYPE);
 }
示例#9
0
 /**
  * Destroys a connection between two entities.
  *
  * @param connectType the type of connection
  * @param targetEntity the UUID of the entity to disconnect from
  * @return an ApiResponse object
  */
 public ApiResponse disconnect(String connectType, Entity targetEntity) {
   return this.client.disconnectEntities(
       this.getType(), this.getUuid().toString(), connectType, targetEntity.getUuid().toString());
 }