Permalink
Browse files

Initial import of code for document update handlers.

  • Loading branch information...
1 parent 002d5b1 commit fce8e931444376cf80844a35b6b377881f8813cb @bytefoundry bytefoundry committed Aug 5, 2012
View
@@ -1,6 +1,7 @@
.classpath
.project
.svn
+.settings
build/
dist/
test.log
@@ -17,7 +17,6 @@
package com.fourspaces.couchdb;
import java.io.IOException;
-
import com.fourspaces.couchdb.util.JSONUtils;
import static com.fourspaces.couchdb.util.JSONUtils.urlEncodePath;
import net.sf.json.*;
@@ -45,6 +44,7 @@
private static final String VIEW = "/_view/";
private static final String DESIGN = "_design/";
+ private static final String UPDATE = "/_update/";
/**
@@ -414,10 +414,82 @@ public String getAttachment(String id, String attachment) throws IOException {
* @param fname attachment name
* @param ctype content type
* @param attachment attachment body
- * @return was the delete successful?
+ * @return was the PUT successful?
*/
public String putAttachment(String id, String fname, String ctype, String attachment) throws IOException {
CouchResponse resp = session.put(name + "/" + urlEncodePath(id) + "/" + fname, ctype, attachment);
return resp.getBody();
}
+
+ /**
+ * Update an existing document using a document update handler. Returns false if there is a failure
+ * making the PUT/POST or there is a problem with the CouchResponse.
+ * @author rwilson
+ * @param update
+ * @return
+ */
+ public boolean updateDocument(Update update) {
+ if ((update == null) || (update.getDocId() == null) || (update.getDocId().equals(""))) {
+ return false;
+ }
+
+ String url = null;
+
+ String[] elements = update.getName().split("/");
+ url = this.name + "/" + ((elements.length < 2) ? elements[0] : DESIGN + elements[0] + UPDATE + elements[1]) + "/" + update.getDocId();
+
+ if (update.getMethodPOST()) {
+ try {
+ // Invoke the POST method passing the parameters in the body
+ CouchResponse resp = session.post(url, "application/x-www-form-urlencoded", update.getURLFormEncodedString(), null);
+ return resp.isOk();
+ } catch (Exception e) {
+ return false;
+ }
+ } else {
+ try {
+ // Invoke the PUT method passing the parameters as a query string
+ CouchResponse resp = session.put(url, null, null, update.getQueryString());
+ return resp.isOk();
+ } catch (Exception e) {
+ return false;
+ }
+ }
+ }
+
+ /**
+ * Update an existing document using a document update handler and return the message body.
+ * Returns null if the is problem with the PUT/POST or CouchResponse.
+ * @author rwilson
+ * @param update
+ * @return
+ */
+ public String updateDocumentWithResponse(Update update) {
+ if ((update == null) || (update.getDocId() == null) || (update.getDocId().equals(""))) {
+ return "";
+ }
+
+ String url = null;
+
+ String[] elements = update.getName().split("/");
+ url = this.name + "/" + ((elements.length < 2) ? elements[0] : DESIGN + elements[0] + UPDATE + elements[1]) + "/" + update.getDocId();
+
+ if (update.getMethodPOST()) {
+ try {
+ // Invoke the POST method passing the parameters in the body
+ CouchResponse resp = session.post(url, "application/x-www-form-urlencoded", update.getURLFormEncodedString(), null);
+ return resp.getBody();
+ } catch (Exception e) {
+ return null;
+ }
+ } else {
+ try {
+ // Invoke the PUT method passing the parameters as a query string
+ CouchResponse resp = session.put(url, null, null, update.getQueryString());
+ return resp.getBody();
+ } catch (Exception e) {
+ return null;
+ }
+ }
+ }
}
@@ -209,6 +209,48 @@ public View addView(String designDoc, String viewName, String function) {
}
/**
+ * Adds an update handler to the document and sets the document ID to that of a design
+ * document. If the function name key already exists within the "updates" element it will
+ * be overwritten. The document must be saved for the change to persist.
+ * @author rwilson
+ * @param designDoc
+ * @param functionName
+ * @param function
+ */
+ public void addUpdateHandler(String designDoc, String functionName, String function) {
+ object.put("_id", "_design/"+ designDoc);
+
+ if (object.has("updates")) {
+ JSONObject updates = object.getJSONObject("updates");
+ updates.put(functionName, JSONUtils.stringSerializedFunction(function));
+ } else {
+ JSONObject func = new JSONObject();
+ func.put(functionName, JSONUtils.stringSerializedFunction(function));
+ object.put("updates", func);
+ }
+ }
+
+ /**
+ * Adds an update handler to the document. The ID of the document is not modified. If the function
+ * name key already exists within the "updates" element it will be overwritten. The document
+ * must be saved for the change to persist.
+ * @author rwilson
+ * @param functionName
+ * @param function
+ * @return
+ */
+ public void addUpdateHandler(String functionName, String function) {
+ if (object.has("updates")) {
+ JSONObject updates = object.getJSONObject("updates");
+ updates.put(functionName, JSONUtils.stringSerializedFunction(function));
+ } else {
+ JSONObject func = new JSONObject();
+ func.put(functionName, JSONUtils.stringSerializedFunction(function));
+ object.put("updates", func);
+ }
+ }
+
+ /**
* Removes a view from this document.
* <p>
* This isn't persisted until the document is saved.
@@ -324,6 +324,33 @@ CouchResponse post(String url, String content, String queryString) {
}
/**
+ * Send a POST with a body, query string and specified content type
+ * @author rwilson
+ * @param url
+ * @param ctype
+ * @param content
+ * @param queryString
+ * @return
+ */
+ CouchResponse post(String url, String ctype, String content, String queryString) {
+ HttpPost post = new HttpPost(buildUrl(url, queryString));
+ if (content!=null) {
+ HttpEntity entity;
+ try {
+ entity = new StringEntity(content, DEFAULT_CHARSET);
+ post.setEntity(entity);
+ if (ctype != null) {
+ post.setHeader(new BasicHeader("Content-Type", ctype));
+ }
+ } catch (UnsupportedEncodingException e) {
+ log.error(ExceptionUtils.getStackTrace(e));
+ }
+ }
+
+ return http(post);
+ }
+
+ /**
* Send a PUT (for creating databases)
* @param url
* @return
@@ -369,6 +396,33 @@ CouchResponse put(String url, String ctype, String content) {
}
return http(put);
}
+
+ /**
+ * Overloaded Put using by attachments and query string
+ * @author rwilson
+ * @param url
+ * @param ctype
+ * @param content
+ * @param queryString
+ * @return
+ */
+ CouchResponse put(String url, String ctype, String content, String queryString) {
+ HttpPut put = new HttpPut(buildUrl(url, queryString));
+ if (content!=null) {
+ HttpEntity entity;
+ try {
+ entity = new StringEntity(content, DEFAULT_CHARSET);
+ put.setEntity(entity);
+ if (ctype!=null) {
+ put.setHeader(new BasicHeader("Content-Type", ctype));
+ }
+ } catch (UnsupportedEncodingException e) {
+ log.error(ExceptionUtils.getStackTrace(e));
+ }
+ }
+ return http(put);
+ }
+
/**
* Send a GET request
* @param url

0 comments on commit fce8e93

Please sign in to comment.