com.leanengine
Class LeanEntity

java.lang.Object
  extended by com.leanengine.LeanEntity

public class LeanEntity
extends java.lang.Object

LeanEntity is a basic data unit that can be stored on the server. It can be saved, retrieved, deleted and queried. Entities can contain properties of various types: long, double, boolean, String and Date.

Future enhancements: new properties will be Entity reference, Blob, GeoPoint, Image..

Basic usage is to create a named entity via init(String) method and then use save() to store it to server. Other methods are get(String), getAll(String) and delete().

All method have their asynchronous counterparts, designated by added 'InBackground' to method name, which allow performing the method in the background thread.


Field Summary
protected  java.lang.Long accountID
           
protected  java.lang.Long id
           
protected  java.lang.String kind
           
protected  java.util.Map<java.lang.String,java.lang.Object> properties
           
 
Method Summary
 void delete()
           
static void delete(java.lang.String kind, long entityId)
          Deletes an entity of the given kind an ID fro the server datastore..
 java.lang.Object get(java.lang.String key)
          Gets the property with the specified key.
static LeanEntity get(java.lang.String kind, long id)
          Retrieves from server an entity of given kind and ID.
 java.lang.Long getAccountID()
          Returns the user account ID that this entity belongs to.
static LeanEntity[] getAll(java.lang.String kind)
          Retrieves from server all entities of certain kind.
static void getAllInBackground(java.lang.String kind, NetworkCallback<LeanEntity> callback)
          Retrieves from server all entities of certain kind.
 java.lang.Boolean getBoolean(java.lang.String key)
          Gets the property with the specified key.
 java.util.Date getDate(java.lang.String key)
          Gets the property with the specified key.
 java.lang.Double getDouble(java.lang.String key)
          Gets the property with the specified key.
 java.lang.Long getId()
           
static void getInBackground(java.lang.String kind, long id, NetworkCallback<LeanEntity> callback)
          Retrieves from server an entities of certain kind and ID.
 java.util.Set<java.lang.String> getKeySet()
          Returns a Set of properties' keys .
 java.lang.String getKind()
           
 java.lang.Long getLong(java.lang.String key)
          Gets the property with the specified key.
 java.lang.String getString(java.lang.String key)
          Gets the property with the specified key.
 java.lang.String getText(java.lang.String key)
          Gets the property with the specified key.
 boolean hasProperty(java.lang.String key)
          Checks if property with given key exists.
static LeanEntity init(java.lang.String kind)
          Static method to create an entity of given kind.
 boolean isEmpty()
          Checks if entity contains any properties.
 void put(java.lang.String key, boolean value)
          Sets the property with given key to value.
 void put(java.lang.String key, java.util.Date value)
          Sets the property with given key to value.
 void put(java.lang.String key, double value)
          Sets the property with given key to value.
 void put(java.lang.String key, long value)
          Sets the property with given key to value.
 void put(java.lang.String key, java.lang.String value)
          Sets the property with given key to value.
 void putText(java.lang.String key, java.lang.String value)
          Sets the property with given key to value.
 long save()
          Saves this entity to the server.
 void saveInBackground(NetworkCallback<java.lang.Long> callback)
          Saves this entity to the server.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

kind

protected final java.lang.String kind

id

protected java.lang.Long id

accountID

protected java.lang.Long accountID

properties

protected java.util.Map<java.lang.String,java.lang.Object> properties
Method Detail

init

public static LeanEntity init(java.lang.String kind)
Static method to create an entity of given kind. Entities created via init() method do not have their id and accountID fields set, even when they are saved to server.

Parameters:
kind - Tke kind of the entity.
Returns:

saveInBackground

public void saveInBackground(NetworkCallback<java.lang.Long> callback)
Saves this entity to the server. Saving is performed on the background thread.

This method does not set the ID and accountID fields of this entity.

Parameters:
callback - NetworkCallback that on success returns ID of the saved Entity.

save

public long save()
          throws LeanException
Saves this entity to the server. This method call blocks until result is available.

This method does not set the ID and accountID fields of this entity.

Returns:
ID of the saved Entity.
Throws:
LeanException - In case of authentication, network and data parsing errors.

get

public static LeanEntity get(java.lang.String kind,
                             long id)
                      throws LeanException,
                             java.lang.IllegalArgumentException
Retrieves from server an entity of given kind and ID. Only returns entity if it belongs to the current user account. This is a blocking operation - it block the execution of current thread until result is returned.

Parameters:
kind - The kind of the Entity to be retrieved.
id - ID of the Entity
Returns:
Returns LeanEntity saved under the given id and of given kind.
Throws:
java.lang.IllegalArgumentException - If parameter id or kind is null.
LeanException - If entity could not be found or in case of authentication, network and data parsing errors.

getInBackground

public static void getInBackground(java.lang.String kind,
                                   long id,
                                   NetworkCallback<LeanEntity> callback)
Retrieves from server an entities of certain kind and ID. Only returns entity if it belongs to the current user account. Operation is performed in the background thread.

Parameters:
kind - The kind of the Entity to be retrieved.
id - ID of the Entity
callback - Callback to be invoked in case of result or error.

getAllInBackground

public static void getAllInBackground(java.lang.String kind,
                                      NetworkCallback<LeanEntity> callback)
Retrieves from server all entities of certain kind. Returns only entities belonging to current user account. Operation is performed in the background thread.

Parameters:
kind - The kind of the Entities to be retrieved.
callback - Callback to be invoked in case of result or error.

getAll

public static LeanEntity[] getAll(java.lang.String kind)
                           throws LeanException
Retrieves from server all entities of certain kind. Returns only entities belonging to current user account (private). This is a blocking operation - it block the execution of current thread until result is returned.

Parameters:
kind - The kind of the entities to be retrieved.
Returns:
An array of LeanEntity
Throws:
LeanException - In case of authentication, network and data parsing errors.

delete

public static void delete(java.lang.String kind,
                          long entityId)
                   throws LeanException
Deletes an entity of the given kind an ID fro the server datastore..

Parameters:
kind - The kind of the entity to delete.
entityId - The ID of the entity to delete.
Throws:
LeanException - In case of authentication, network and data parsing errors.

delete

public void delete()
            throws LeanException
Throws:
LeanException

getKeySet

public java.util.Set<java.lang.String> getKeySet()
Returns a Set of properties' keys .

Returns:
Set of properties' keys.

getKind

public java.lang.String getKind()
Returns:
Returns the kind of the entity.

getId

public java.lang.Long getId()
Returns:
Returns the ID of the entity.

getAccountID

public java.lang.Long getAccountID()
Returns the user account ID that this entity belongs to. Account ID is only available for entities retrieved from server.

Returns:
ID of the account this entity belongs to. Null if entity was not retrieved from server.

get

public java.lang.Object get(java.lang.String key)
Gets the property with the specified key.

The value returned may not be the same type as originally set via putXYZ(String, Object). Object types are internally converted to types supported by LeanEngine server: //todo insert LE Docs link here

Parameters:
key - Key (name) of the property.
Returns:
Value of the property or null if property with given key does not exist.

getString

public java.lang.String getString(java.lang.String key)
Gets the property with the specified key. Null is returned if key does not exist or if property is not of type String.

The value returned may not be the same type as originally set via put(String, String). Object types are internally converted to types supported by LeanEngine server: //todo insert LE Docs link here

Parameters:
key - Key under which property was stored.
Returns:
Value of property or null if key does not exist or if property is not of type String.

getText

public java.lang.String getText(java.lang.String key)
Gets the property with the specified key. Null is returned if key does not exist or if property is not of type String.

This method is to be used to retrieve properties that were saved via putText(String, String).

Parameters:
key - Key under which property was stored.
Returns:
Value of property or null if key does not exist or if property is not of type String.

getLong

public java.lang.Long getLong(java.lang.String key)
Gets the property with the specified key. Null is returned if key does not exist or if property is not of type Long.

The value returned may not be the same type as originally set via put(String, long). Object types are internally converted to types supported by LeanEngine server: //todo insert LE Docs link here

Parameters:
key - Key under which property was stored.
Returns:
Value of property or null if key does not exist or if property is not of type Long.

getDouble

public java.lang.Double getDouble(java.lang.String key)
Gets the property with the specified key. Null is returned if key does not exist or if property is not of type Double.

The value returned may not be the same type as originally set via put(String, double). Object types are internally converted to types supported by LeanEngine server: //todo insert LE Docs link here

Parameters:
key - Key under which property was stored.
Returns:
Value of property or null if key does not exist or if property is not of type Double.

getDate

public java.util.Date getDate(java.lang.String key)
Gets the property with the specified key. Null is returned if key does not exist or if property is not of type Date.

The value returned may not be the same type as originally set via put(String, java.util.Date). Object types are internally converted to types supported by LeanEngine server: //todo insert LE Docs link here

Parameters:
key - Key under which property was stored.
Returns:
Value of property or null if key does not exist or if property is not of type Date.

getBoolean

public java.lang.Boolean getBoolean(java.lang.String key)
Gets the property with the specified key. Null is returned if key does not exist or if property is not of type Boolean.

The value returned may not be the same type as originally set via put(String, boolean). Vales are internally converted to types supported by LeanEngine server: //todo insert LE Docs link here

Parameters:
key - Key under which property was stored.
Returns:
Value of property or null if key does not exist or if property is not of type Boolean.

put

public void put(java.lang.String key,
                long value)
Sets the property with given key to value.

Vales are internally converted to types supported by LeanEngine server: //todo insert LE Docs link here

Parameters:
key - Key under which property will be stored.
value - long value to be stored.

put

public void put(java.lang.String key,
                double value)
Sets the property with given key to value.

Vales are internally converted to types supported by LeanEngine server: //todo insert LE Docs link here

Parameters:
key - Key under which property will be stored.
value - double value to be stored.

put

public void put(java.lang.String key,
                java.lang.String value)
         throws java.lang.IllegalArgumentException
Sets the property with given key to value.

Value is stored on server as short string value, which can be indexed (via server configuration) and can be used in queries.

Value must be shorter that 500 Unicode characters. Use putText(String, String) if longer string values need to be stored.

Parameters:
key - Key under which property will be stored.
value - String value to be stored.
Throws:
java.lang.IllegalArgumentException - If value is longer that 500 Unicode characters.

putText

public void putText(java.lang.String key,
                    java.lang.String value)
Sets the property with given key to value.

This method supports saving String values that are longer that 500 Unicode characters. Property will be saved on the server as an unindexed text value, meaning that it can not be used in queries.

Parameters:
key - Key under which property will be stored.
value - String value to be stored.

put

public void put(java.lang.String key,
                java.util.Date value)
Sets the property with given key to value.

Vales are internally converted to types supported by LeanEngine server: //todo insert LE Docs link here

Parameters:
key - Key under which property will be stored.
value - Date value to be stored.

put

public void put(java.lang.String key,
                boolean value)
Sets the property with given key to value.

Vales are internally converted to types supported by LeanEngine server: //todo insert LE Docs link here

Parameters:
key - Key under which property will be stored.
value - boolean value to be stored.

hasProperty

public boolean hasProperty(java.lang.String key)
Checks if property with given key exists.

Parameters:
key - Key of the property.
Returns:
True if property exists, false otherwise.

isEmpty

public boolean isEmpty()
Checks if entity contains any properties.

Returns:
True if entity is empty, i.e. contains no properties, false otherwise.