|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.stackmob.sdk.model.StackMobModel
public abstract class StackMobModel
The base class for StackMob data objects. Extend this class with the fields you want, and you have an object that knows how to synchronize itself with the cloud
public class Task extends StackMobModel {
private String name;
private Date dueDate;
private int priority;
private boolean done;
public Task(String name) {
super(Task.class);
this.name = name;
}
//Add whatever setters/getters/other functionality you want here
}
You can then create objects, manipulate them, and save/load them whenever you want
Task myTask = new Task("write javadocs");
myTask.save();
// ... do other stuff
myTask.fetch(new StackMobModelCallback() {
public void success() {
// The blogPostTask object is now filled in with data.
}
public void failure(StackMobException e) {
// handle failure case
}
});
You can also do complex queries and get model classes back as a result:
// Add constraints
StackMobModelQuery<Task> highPriorityQuery = new StackMobModelQuery<Task>(Task.class).field(new StackMobField("priority").isGreaterThanOrEqualTo( 3).isLessThan(6)).fieldIsEqualTo("done", false);
// Do an actual query
Task.query(highPriorityQuery, new StackMobQueryCallback<Task>() {
public void success(List<Task> result) {
// handle success
}
public void failure(StackMobException e) {
// handle failure
}
});
If you use model classes as fields, you end up with a tree-like structure that you can save/load to any depth
public class TaskList extends StackMobModel {
private String name;
private List<Task> tasks = new ArrayList<Task>();
public TaskList(String name, List<Task> tasks) {
super(TaskList.class);
this.name = name;
this.tasks = tasks;
}
}
Task javadocsTask = new Task("Write javadocs");
Task proofreadTask = new Task("Proofread");
TaskList blogTasks = new TaskList("Blog Tasks", Arrays.asList(blogPostTask, proofreadTask));
blogTasks.save(StackMobOptions.depthOf(1));
Fields in a model can be any of the following:
StackMobCounter
StackMobFile and StackMobGeoPoint (these must be set up in the schema first)
public static String overrideSchemaName() {
return "thenameyouwant";
}
| Field Summary | |
|---|---|
protected String |
id
|
| Constructor Summary | |
|---|---|
StackMobModel(Class<? extends StackMobModel> actualClass)
create a new model of the specified class. |
|
StackMobModel(String id,
Class<? extends StackMobModel> actualClass)
create a new model of the specified class with an id overriding the default, automatically generated one. |
|
| Method Summary | ||
|---|---|---|
|
append(String field,
List<T> objs,
StackMobCallback callback)
append model objects to a collection field in this object. |
|
|
appendAndSave(String field,
List<T> objs,
StackMobCallback callback)
append model objects to a collection field in this object. |
|
static
|
count(Class<T> theClass,
StackMobQuery q,
StackMobCountCallback callback)
run a count query on the server to count all the instances of your model within certain constraints |
|
void |
destroy()
delete the object from the server |
|
void |
destroy(StackMobCallback callback)
delete the object from the server |
|
void |
fetch(StackMobCallback callback)
Reload the object from the server. |
|
void |
fetch(StackMobOptions options,
StackMobCallback callback)
Reload the object from the server. |
|
void |
fillFromJson(String jsonString)
fill the objects fields in from a json string. |
|
String |
getID()
get the object's id. |
|
String |
getIDFieldName()
Determines the field name for the primary key on the server. |
|
protected String |
getSchemaName()
The name of the schema this object corresponds to on the server. |
|
boolean |
hasData()
Check if the object has been loaded with data or if it's just a stub with an id. |
|
protected void |
init(Class<? extends StackMobModel> actualClass)
|
|
static
|
newFromJson(Class<T> classOfT,
String json)
create a new instance of the specified model class from a json string. |
|
static
|
query(Class<T> theClass,
StackMobQuery q,
StackMobOptions options,
StackMobQueryCallback<T> callback)
run a query on the server to get all the instances of your model within certain constraints |
|
static
|
query(Class<T> theClass,
StackMobQuery q,
StackMobQueryCallback<T> callback)
run a query on the server to get all the instances of your model within certain constraints |
|
|
remove(String field,
List<T> objs,
StackMobCallback callback)
remove values from a collection on the client and server. |
|
|
removeAndDelete(String field,
List<T> objs,
StackMobCallback callback)
remove objects from a collection and delete them on the client and server. |
|
void |
save()
Save the object to the server |
|
void |
save(StackMobCallback callback)
Save the object to the server |
|
void |
save(StackMobOptions options)
Save the object to the server with options. |
|
void |
save(StackMobOptions options,
StackMobCallback callback)
Save the object to the server with options. |
|
static
|
saveMultiple(List<T> models,
StackMobCallback callback)
save multiple objects in one batch. |
|
void |
setID(String id)
set the object's id. |
|
String |
toJson()
Converts the model into its Json representation. |
|
String |
toJson(StackMobOptions options)
Converts the model into its Json representation, expanding any sub-objects to the given depth. |
|
protected com.google.gson.JsonElement |
toJsonElement(int depth,
com.stackmob.sdk.model.StackMobModel.Selection selection,
com.stackmob.sdk.util.RelationMapping mapping)
|
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected transient String id
| Constructor Detail |
|---|
public StackMobModel(String id,
Class<? extends StackMobModel> actualClass)
actualClass - The subclass, specified because of type erasurepublic StackMobModel(Class<? extends StackMobModel> actualClass)
actualClass - The subclass, specified because of type erasure| Method Detail |
|---|
public static <T extends StackMobModel> void query(Class<T> theClass,
StackMobQuery q,
StackMobQueryCallback<T> callback)
theClass - The class of your modelq - The query to runcallback - The callback to be invoked upon returning
public static <T extends StackMobModel> void query(Class<T> theClass,
StackMobQuery q,
StackMobOptions options,
StackMobQueryCallback<T> callback)
theClass - The class of your modelq - The query to runoptions - options, such as select and expand, to apply to the requestcallback - The callback to be invoked upon returning
public static <T extends StackMobModel> void count(Class<T> theClass,
StackMobQuery q,
StackMobCountCallback callback)
theClass - The class of your modelq - The query to runcallback - The callback to be invoked upon returning
public static <T extends StackMobModel> T newFromJson(Class<T> classOfT,
String json)
throws StackMobException
classOfT - The class to instantiatejson - The string to deserialize
StackMobException
public static <T extends StackMobModel> void saveMultiple(List<T> models,
StackMobCallback callback)
T - models - callback - protected void init(Class<? extends StackMobModel> actualClass)
public void setID(String id)
id - the primary key of the objectpublic String getID()
protected String getSchemaName()
public static String overrideSchemaName() {
return "thenameyouwant";
}
The static method is necessary for the name to be accessible from
static method By default it's the name of the class in lower case.
Must be less than 25 alphanumeric characters.
public String getIDFieldName()
public boolean hasData()
public void fillFromJson(String jsonString)
throws StackMobException
jsonString - a json string as produced by toJson()
StackMobException
protected com.google.gson.JsonElement toJsonElement(int depth,
com.stackmob.sdk.model.StackMobModel.Selection selection,
com.stackmob.sdk.util.RelationMapping mapping)
public String toJson()
public String toJson(StackMobOptions options)
options - options, such and select and expand, to apply to the request
public void fetch(StackMobCallback callback)
callback - invoked when the load is complete
public void fetch(StackMobOptions options,
StackMobCallback callback)
StackMobOptions#depthOf(int) to also save its children to the given depth.
This is not thread safe, make sure the object isn't disturbed during the load.
options - options, such and select and expand, to apply to the requestcallback - invoked when the load is completepublic void save()
public void save(StackMobOptions options)
StackMobOptions#depthOf(int) to also save its children to the given depth.
options - options, such and select and expand, to apply to the requestpublic void save(StackMobCallback callback)
callback - invoked when the save is complete
public void save(StackMobOptions options,
StackMobCallback callback)
StackMobOptions#depthOf(int) to also save its children to the given depth.
options - options, such and select and expand, to apply to the requestcallback - invoked when the save is completepublic void destroy()
public void destroy(StackMobCallback callback)
callback - invoked when the delete is complete
public <T extends StackMobModel> void append(String field,
List<T> objs,
StackMobCallback callback)
appendAndSave(String, java.util.List, com.stackmob.sdk.callback.StackMobCallback).
The items will be added to the list both locally and on the server.
T - the type of objects being appendedfield - the name of the field to append to. The field must be a java Collectionobjs - the objects to append tocallback - invoked when the append is complete
IllegalArgumentException - if the type of the field doesn't match the input objects
public <T extends StackMobModel> void appendAndSave(String field,
List<T> objs,
StackMobCallback callback)
T - the type of objects being appendedfield - the name of the field to append to. The field must be a java Collectionobjs - the objects to append tocallback - invoked when the append is complete
IllegalArgumentException - if the type of the field doesn't match the input objects
public <T extends StackMobModel> void remove(String field,
List<T> objs,
StackMobCallback callback)
T - the type of objects being removedfield - the name of the field to remove from. The field must be a java Collectionobjs - the objects to remove fromcallback - invoked when the remove is complete
IllegalArgumentException - if the type of the field doesn't match the input objects
public <T extends StackMobModel> void removeAndDelete(String field,
List<T> objs,
StackMobCallback callback)
T - the type of objects being removedfield - the name of the field to remove from. The field must be a java Collectionobjs - the objects to remove fromcallback - invoked when the remove is complete
IllegalArgumentException - if the type of the field doesn't match the input objects
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||