Permalink
Browse files

migrated code to use GroceryItem POJO

  • Loading branch information...
1 parent f61b444 commit 03a5828e8792ffe57f3949a6345ccad44aede1e8 @mschoch committed Sep 1, 2011
@@ -1,13 +1,5 @@
package com.couchbase.grocerysync;
-import java.util.Calendar;
-import java.util.GregorianCalendar;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.UUID;
-
-import org.codehaus.jackson.JsonNode;
-import org.codehaus.jackson.node.ObjectNode;
import org.ektorp.CouchDbConnector;
import org.ektorp.CouchDbInstance;
import org.ektorp.ReplicationCommand;
@@ -24,7 +16,6 @@
import android.os.Bundle;
import android.os.RemoteException;
import android.preference.PreferenceManager;
-import android.text.format.DateFormat;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
@@ -36,8 +27,8 @@
import android.widget.EditText;
import android.widget.ListView;
-import com.couchbase.android.ICouchbaseDelegate;
import com.couchbase.android.CouchbaseMobile;
+import com.couchbase.android.ICouchbaseDelegate;
public class AndroidGrocerySyncActivity extends Activity implements OnItemClickListener, OnItemLongClickListener, OnKeyListener {
@@ -56,6 +47,8 @@
//ektorp impl
protected CouchDbConnector couchDbConnector;
+ protected ReplicationCommand pushReplication;
+ protected ReplicationCommand pullReplication;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -75,11 +68,14 @@ public void onCreate(Bundle savedInstanceState) {
}
protected void onDestroy() {
- super.onDestroy();
+
try {
unbindService(couchServiceConnection);
} catch (IllegalArgumentException e) {
- }
+ //ignore
+ }
+
+ super.onDestroy();
}
protected ICouchbaseDelegate couchCallbackHandler = new ICouchbaseDelegate.Stub() {
@@ -117,15 +113,15 @@ public void couchbaseStarted(String host, int port) throws RemoteException {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
- ReplicationCommand pushReplication = new ReplicationCommand.Builder()
+ pushReplication = new ReplicationCommand.Builder()
.source(DATABASE_NAME)
.target(prefs.getString("sync_url", "http://couchbase.iriscouch.com/grocery-sync"))
.continuous(true)
.build();
dbInstance.replicate(pushReplication);
- ReplicationCommand pullReplication = new ReplicationCommand.Builder()
+ pullReplication = new ReplicationCommand.Builder()
.source(prefs.getString("sync_url", "http://couchbase.iriscouch.com/grocery-sync"))
.target(DATABASE_NAME)
.continuous(true)
@@ -162,24 +158,23 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
* Handle click on item in list
*/
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- JsonNode document = (JsonNode)parent.getItemAtPosition(position);
- toggleItemChecked(document);
+ GroceryItem item = (GroceryItem)parent.getItemAtPosition(position);
+ toggleItemChecked(item);
}
/**
* Handle long-click on item in list
*/
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
- final JsonNode document = (JsonNode)parent.getItemAtPosition(position);
- JsonNode textNode = document.get("text");
+ final GroceryItem item = (GroceryItem)parent.getItemAtPosition(position);
AlertDialog.Builder builder = new AlertDialog.Builder(AndroidGrocerySyncActivity.this);
AlertDialog alert = builder.setTitle("Delete Item?")
- .setMessage("Are you sure you want to delete " + textNode.getValueAsText() + "?")
+ .setMessage("Are you sure you want to delete \"" + item.getText() + "\"?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
- deleteGroceryItem(document);
+ deleteGroceryItem(item);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@@ -233,41 +228,20 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
public void createGroceryItem(String name) {
- UUID uuid = UUID.randomUUID();
- Calendar calendar = GregorianCalendar.getInstance();
- long currentTime = calendar.getTimeInMillis();
- String currentTimeString = DateFormat.format("EEEE-MM-dd'T'HH:mm:ss.SSS'Z'", calendar).toString();
-
- String id = currentTime + "-" + uuid.toString();
-
- Map<String, String> newItem = new HashMap<String, String>();
- newItem.put("_id", id);
- newItem.put("text", name);
- newItem.put("check", Boolean.FALSE.toString());
- newItem.put("created_at", currentTimeString);
-
+ GroceryItem item = GroceryItem.createWithText(name);
CouchDocumentAsyncTask createTask = new CouchDocumentAsyncTask(couchDbConnector, CouchDocumentAsyncTask.OPERATION_CREATE);
- createTask.execute(newItem);
+ createTask.execute(item);
}
- public void toggleItemChecked(JsonNode document) {
- JsonNode check = document.get("check");
- if(check.getBooleanValue()) {
- ObjectNode documentObject = (ObjectNode)document;
- documentObject.put("check", false);
- }
- else {
- ObjectNode documentObject = (ObjectNode)document;
- documentObject.put("check", true);
- }
-
+ public void toggleItemChecked(GroceryItem item) {
+ item.toggleCheck();
CouchDocumentAsyncTask updateTask = new CouchDocumentAsyncTask(couchDbConnector, CouchDocumentAsyncTask.OPERATION_UPDATE);
- updateTask.execute(document);
+ updateTask.execute(item);
}
- public void deleteGroceryItem(JsonNode document) {
+ public void deleteGroceryItem(GroceryItem item) {
CouchDocumentAsyncTask deleteTask = new CouchDocumentAsyncTask(couchDbConnector, CouchDocumentAsyncTask.OPERATION_DELETE);
- deleteTask.execute(document);
+ deleteTask.execute(item);
}
}
@@ -56,7 +56,8 @@ protected void onProgressUpdate(DocumentChange... values) {
parent.rowMap.remove(id);
}
else {
- parent.rowMap.put(id, documentChange.getDocAsNode());
+ GroceryItem item = new GroceryItem(documentChange.getDocAsNode());
+ parent.rowMap.put(id, item);
}
}
parent.notifyDataSetChanged();
@@ -2,13 +2,11 @@
import java.util.HashMap;
import java.util.Iterator;
+import java.util.List;
-import org.codehaus.jackson.JsonNode;
import org.ektorp.CouchDbConnector;
import org.ektorp.DbInfo;
import org.ektorp.ViewQuery;
-import org.ektorp.ViewResult;
-import org.ektorp.ViewResult.Row;
import android.content.Context;
import android.view.LayoutInflater;
@@ -21,21 +19,21 @@
public class CouchListAdapter extends BaseAdapter {
protected Context context;
- public HashMap<String, JsonNode> rowMap;
+ public HashMap<String, GroceryItem> rowMap;
public CouchListAdapter(Context context, CouchDbConnector couchDbConnector) {
this.context = context;
- rowMap = new HashMap<String, JsonNode>();
+ rowMap = new HashMap<String, GroceryItem>();
DbInfo dbInfo = couchDbConnector.getDbInfo();
long lastUpdateSeq = dbInfo.getUpdateSeq();
- ViewResult vr = couchDbConnector.queryView(new ViewQuery().allDocs().includeDocs(true));
- Iterator<Row> rowIterator = vr.iterator();
- while(rowIterator.hasNext()) {
- Row row = rowIterator.next();
- rowMap.put(row.getId(), row.getDocAsNode());
+ List<GroceryItem> items = couchDbConnector.queryView(new ViewQuery().allDocs().includeDocs(true), GroceryItem.class);
+ Iterator<GroceryItem> itemIterator = items.iterator();
+ while(itemIterator.hasNext()) {
+ GroceryItem item = itemIterator.next();
+ rowMap.put(item.getId(), item);
}
//create an ansyc task to get updates
@@ -68,19 +66,17 @@ public View getView(int position, View itemView, ViewGroup parent) {
}
TextView label = (TextView) v.findViewById(R.id.label);
- JsonNode document = (JsonNode)getItem(position);
- JsonNode textNode = document.get("text");
- if(textNode != null) {
- label.setText(textNode.getTextValue());
+ GroceryItem item = (GroceryItem)getItem(position);
+ if(item.getText() != null) {
+ label.setText(item.getText());
}
else {
label.setText("");
}
- JsonNode checkNode = document.get("check");
- if(checkNode != null) {
+ if(item.getCheck() != null) {
ImageView icon = (ImageView) v.findViewById(R.id.icon);
- if(checkNode.getBooleanValue()) {
+ if(item.getCheck().booleanValue()) {
icon.setImageResource(R.drawable.list_area___checkbox___checked);
}
else {
@@ -0,0 +1,115 @@
+package com.couchbase.grocerysync;
+
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.UUID;
+
+import org.codehaus.jackson.JsonNode;
+import org.codehaus.jackson.annotate.JsonIgnoreProperties;
+import org.codehaus.jackson.annotate.JsonProperty;
+import org.ektorp.support.CouchDbDocument;
+
+import android.text.format.DateFormat;
+
+@SuppressWarnings("serial")
+@JsonIgnoreProperties(ignoreUnknown=true)
+public class GroceryItem extends CouchDbDocument {
+
+ private String text;
+ private Boolean check;
+ private String createdAt;
+
+ public GroceryItem() {
+
+ }
+
+ //eventually this constructor should go away
+ //need to patch changes feed to return GroceryItem
+ public GroceryItem(JsonNode document) {
+ //internals
+ JsonNode idNode = document.get("_id");
+ if(idNode != null) {
+ setId(idNode.getTextValue());
+ }
+
+ JsonNode revNode = document.get("_rev");
+ if(revNode != null) {
+ setRevision(revNode.getTextValue());
+ }
+
+
+ //text
+ JsonNode textNode = document.get("text");
+ if(textNode != null) {
+ setText(textNode.getTextValue());
+ }
+ else {
+ setText("");
+ }
+
+ //check
+ JsonNode checkNode = document.get("check");
+ if(checkNode != null) {
+ if(checkNode.getBooleanValue()) {
+ setCheck(Boolean.TRUE);
+ }
+ else {
+ setCheck(Boolean.FALSE);
+ }
+ }
+
+ JsonNode createdNode = document.get("created_at");
+ if(createdNode != null) {
+ setCreatedAt(createdNode.getTextValue());
+ }
+ }
+
+ public String getText() {
+ return text;
+ }
+
+ public void setText(String text) {
+ this.text = text;
+ }
+
+ public Boolean getCheck() {
+ return check;
+ }
+
+ public void setCheck(Boolean check) {
+ this.check = check;
+ }
+
+ @JsonProperty("created_at")
+ public String getCreatedAt() {
+ return createdAt;
+ }
+
+ @JsonProperty("created_at")
+ public void setCreatedAt(String createdAt) {
+ this.createdAt = createdAt;
+ }
+
+ public void toggleCheck() {
+ check = !check;
+ }
+
+ public static GroceryItem createWithText(String text) {
+ UUID uuid = UUID.randomUUID();
+ Calendar calendar = GregorianCalendar.getInstance();
+ long currentTime = calendar.getTimeInMillis();
+ String currentTimeString = DateFormat.format("EEEE-MM-dd'T'HH:mm:ss.SSS'Z'", calendar).toString();
+
+ String id = currentTime + "-" + uuid.toString();
+
+ GroceryItem result = new GroceryItem();
+
+ result.setId(id);
+ result.setText(text);
+ result.setCheck(Boolean.FALSE);
+ result.setCreatedAt(currentTimeString);
+
+ return result;
+ }
+
+}

0 comments on commit 03a5828

Please sign in to comment.