diff --git a/ToDoLite/src/main/java/com/couchbase/todolite/MainActivity.java b/ToDoLite/src/main/java/com/couchbase/todolite/MainActivity.java index 31fbc09..3c0b95e 100644 --- a/ToDoLite/src/main/java/com/couchbase/todolite/MainActivity.java +++ b/ToDoLite/src/main/java/com/couchbase/todolite/MainActivity.java @@ -35,6 +35,7 @@ import com.couchbase.lite.QueryEnumerator; import com.couchbase.lite.replicator.Replication; import com.couchbase.lite.util.Log; +import com.couchbase.todolite.helper.ModelHelper; import com.couchbase.todolite.document.List; import com.couchbase.todolite.document.Profile; import com.facebook.Session; @@ -42,7 +43,10 @@ import java.io.IOException; import java.lang.reflect.Array; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Calendar; +import java.util.GregorianCalendar; import java.util.HashMap; import java.util.Map; import java.util.Observable; @@ -397,14 +401,24 @@ public void onClick(DialogInterface dialog, int whichButton) { // TODO: Show an error message. return; } - try { - String currentUserId = ((Application)getApplication()).getCurrentUserId(); - Document document = List.createNewList(getDatabase(), title, currentUserId); - displayListContent(document.getId()); - invalidateOptionsMenu(); - } catch (CouchbaseLiteException e) { - Log.e(Application.TAG, "Cannot create a new list", e); - } + String currentUserId = ((Application)getApplication()).getCurrentUserId(); + + SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); + Calendar calendar = GregorianCalendar.getInstance(); + String currentTimeString = dateFormatter.format(calendar.getTime()); + + List list = new List(); + list.setType("list"); + list.setTitle(title); + list.setCreateAt(currentTimeString); + list.setMembers(new ArrayList()); + + if (currentUserId != null) + list.setOwner("profile" + currentUserId); + + Document document = ModelHelper.save(getDatabase(), list); + displayListContent(document.getId()); + invalidateOptionsMenu(); } }); diff --git a/ToDoLite/src/main/java/com/couchbase/todolite/document/List.java b/ToDoLite/src/main/java/com/couchbase/todolite/document/List.java index 1d4f7b2..ba8469f 100644 --- a/ToDoLite/src/main/java/com/couchbase/todolite/document/List.java +++ b/ToDoLite/src/main/java/com/couchbase/todolite/document/List.java @@ -16,6 +16,8 @@ import com.couchbase.lite.UnsavedRevision; import com.couchbase.lite.util.Log; import com.couchbase.todolite.Application; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -24,10 +26,28 @@ import java.util.HashMap; import java.util.Map; +@JsonIgnoreProperties(ignoreUnknown = true) public class List { private static final String VIEW_NAME = "lists"; private static final String DOC_TYPE = "list"; + @JsonProperty(value = "_id") + private String documentId; + + private String title; + + @JsonProperty(value = "user_id") + private int userId; + + @JsonProperty(value = "created_at") + private String createAt; + + private String type; + + private ArrayList members; + + private String owner; + public static Query getQuery(Database database) { com.couchbase.lite.View view = database.getView(VIEW_NAME); if (view.getMap() == null) { @@ -46,26 +66,6 @@ public void map(Map document, Emitter emitter) { return query; } - public static Document createNewList(Database database, String title, String userId) - throws CouchbaseLiteException { - SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); - Calendar calendar = GregorianCalendar.getInstance(); - String currentTimeString = dateFormatter.format(calendar.getTime()); - - Map properties = new HashMap(); - properties.put("type", "list"); - properties.put("title", title); - properties.put("created_at", currentTimeString); - properties.put("members", new ArrayList()); - if (userId != null) - properties.put("owner", "profile:" + userId); - - Document document = database.createDocument(); - document.putProperties(properties); - - return document; - } - public static void assignOwnerToListsIfNeeded(Database database, Document user) throws CouchbaseLiteException { QueryEnumerator enumerator = getQuery(database).run(); @@ -114,4 +114,44 @@ public static void removeMemberFromList(Document list, Document user) list.putProperties(newProperties); } + + public String getCreateAt() { + return createAt; + } + + public void setCreateAt(String createAt) { + this.createAt = createAt; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public ArrayList getMembers() { + return members; + } + + public void setMembers(ArrayList members) { + this.members = members; + } + + public String getOwner() { + return owner; + } + + public void setOwner(String owner) { + this.owner = owner; + } } diff --git a/ToDoLite/src/main/java/com/couchbase/todolite/helper/ModelHelper.java b/ToDoLite/src/main/java/com/couchbase/todolite/helper/ModelHelper.java new file mode 100644 index 0000000..e58cfa4 --- /dev/null +++ b/ToDoLite/src/main/java/com/couchbase/todolite/helper/ModelHelper.java @@ -0,0 +1,42 @@ +package com.couchbase.todolite.helper; + +import com.couchbase.lite.CouchbaseLiteException; +import com.couchbase.lite.Database; +import com.couchbase.lite.Document; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.util.Map; + +public class ModelHelper { + + public static Document save(Database database, Object object) { + ObjectMapper m = new ObjectMapper(); + Map props = m.convertValue(object, Map.class); + String id = (String) props.get("_id"); + + Document document; + if (id == null) { + document = database.createDocument(); + } else { + document = database.getExistingDocument(id); + if (document == null) { + document = database.getDocument(id); + } else { + props.put("_rev", document.getProperty("_rev")); + } + } + + try { + document.putProperties(props); + } catch (CouchbaseLiteException e) { + e.printStackTrace(); + } + return document; + } + + public static T modelForDocument(Document document, Class aClass) { + ObjectMapper m = new ObjectMapper(); + return m.convertValue(document.getProperties(), aClass); + } + +}