com.fourspaces.couchdb
Class Document

java.lang.Object
  extended by com.fourspaces.couchdb.Document
All Implemented Interfaces:
Map
Direct Known Subclasses:
ViewResults

public class Document
extends Object
implements Map

Everything in CouchDB is a Document. In this case, the document is an object backed by a JSONObject. The Document is also aware of the database that it is connected to. This allows the Document to reload it's properties when needed. The only special fields are "_id", "_rev", "_revisions", and "_view_*".

All document have an _id and a _rev. If this is a new document those fields are populated when they are saved to the CouchDB server.

_revisions is only populated if the document has been retrieved via database.getDocumentWithRevisions(); So, if this document wasn't, then when you call document.getRevisions(), it will go back to the server to reload itself via database.getDocumentWithRevisions().

The Document can be treated like a JSONObject, eventhough it doesn't extend JSONObject (it's final).

You can also get/set values by calling document.get(key), document.put(key,value), just like a Map.

You can get a handle on the backing JSONObject by calling document.getJSONObject(); If this hasn't been loaded yet, it will load the data itself using the given database connection.

If you got this Document from a view, you are likely missing elements. To load them you can call document.load().

Author:
mbreese

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
protected  Database database
           
protected  net.sf.json.JSONObject object
           
 
Constructor Summary
Document()
          Create a new Document
Document(net.sf.json.JSONObject obj)
          Create a new Document from a JSONObject
 
Method Summary
 net.sf.json.JSONObject accumulate(String arg0, boolean arg1)
           
 net.sf.json.JSONObject accumulate(String arg0, double arg1)
           
 net.sf.json.JSONObject accumulate(String arg0, int arg1)
           
 net.sf.json.JSONObject accumulate(String arg0, long arg1)
           
 net.sf.json.JSONObject accumulate(String arg0, Object arg1)
           
 void accumulateAll(Map arg0)
           
 View addView(String designDoc, String viewName, String function)
          Add a view to this document.
 void clear()
           
 boolean containsKey(Object arg0)
           
 boolean containsValue(Object arg0)
           
 void deleteView(String viewName)
          Removes a view from this document.
 net.sf.json.JSONObject element(String arg0, boolean arg1)
           
 net.sf.json.JSONObject element(String arg0, Collection arg1)
           
 net.sf.json.JSONObject element(String arg0, double arg1)
           
 net.sf.json.JSONObject element(String arg0, int arg1)
           
 net.sf.json.JSONObject element(String arg0, long arg1)
           
 net.sf.json.JSONObject element(String arg0, Map arg1)
           
 net.sf.json.JSONObject element(String arg0, Object arg1)
           
 net.sf.json.JSONObject elementOpt(String arg0, Object arg1)
           
 Set entrySet()
           
 Object get(Object arg0)
           
 Object get(String arg0)
           
 boolean getBoolean(String arg0)
           
 double getDouble(String arg0)
           
 String getId()
          This document's id (if saved)
 int getInt(String arg0)
           
 net.sf.json.JSONArray getJSONArray(String arg0)
           
 net.sf.json.JSONObject getJSONObject()
          Retrieves the backing JSONObject
 net.sf.json.JSONObject getJSONObject(String arg0)
           
 long getLong(String arg0)
           
 String getRev()
          This document's Revision (if saved)
 String[] getRevisions()
          A list of the revision numbers that this document has.
 String getString(String arg0)
           
 View getView(String name)
          Get a named view that is stored in the document.
 String getViewDocumentId()
          This strips _design from the document id
 boolean has(String arg0)
           
 boolean isEmpty()
           
 Iterator keys()
           
 Set keySet()
           
protected  void load(net.sf.json.JSONObject object2)
          Load data into this document from a differing JSONObject
 net.sf.json.JSONArray names()
           
 Object opt(String arg0)
           
 boolean optBoolean(String arg0)
           
 boolean optBoolean(String arg0, boolean arg1)
           
 double optDouble(String arg0)
           
 double optDouble(String arg0, double arg1)
           
 int optInt(String arg0)
           
 int optInt(String arg0, int arg1)
           
 net.sf.json.JSONArray optJSONArray(String arg0)
           
 net.sf.json.JSONObject optJSONObject(String arg0)
           
 long optLong(String arg0)
           
 long optLong(String arg0, long arg1)
           
 String optString(String arg0)
           
 String optString(String arg0, String arg1)
           
protected  void populateRevisions()
           
 Object put(Object arg0, Object arg1)
           
 void putAll(Map arg0)
           
 void refresh()
          Loads data from the server for this document.
 Object remove(Object arg0)
           
 Object remove(String arg0)
           
 void setId(String id)
           
 void setRev(String rev)
           
 int size()
           
 String toString()
           
 Collection values()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

database

protected Database database

object

protected net.sf.json.JSONObject object
Constructor Detail

Document

public Document()
Create a new Document


Document

public Document(net.sf.json.JSONObject obj)
Create a new Document from a JSONObject

Parameters:
obj -
Method Detail

load

protected void load(net.sf.json.JSONObject object2)
Load data into this document from a differing JSONObject

This is mainly for reloading data for an object that was retrieved from a view. This version doesn't overwrite any unsaved data that is currently present in this object.

Parameters:
object2 -

getId

public String getId()
This document's id (if saved)

Returns:

setId

public void setId(String id)

getViewDocumentId

public String getViewDocumentId()
This strips _design from the document id


getRev

public String getRev()
This document's Revision (if saved)

Returns:

setRev

public void setRev(String rev)

getRevisions

public String[] getRevisions()
                      throws IOException
A list of the revision numbers that this document has. If this hasn't been populated with a "full=true" query, then the database will be re-queried

Returns:
Throws:
IOException

getView

public View getView(String name)
Get a named view that is stored in the document.

Parameters:
name -
Returns:

addView

public View addView(String designDoc,
                    String viewName,
                    String function)
Add a view to this document. If a view function already exists with the given viewName it is overwritten.

This isn't persisted until the document is saved.

Parameters:
design - document name
viewName -
function -
Returns:

deleteView

public void deleteView(String viewName)
Removes a view from this document.

This isn't persisted until the document is saved.

Parameters:
viewName -

refresh

public void refresh()
             throws IOException
Loads data from the server for this document. Actually requests a new copy of data from the server and uses that to populate this document. This doesn't overwrite any unsaved data.

Throws:
IOException

populateRevisions

protected void populateRevisions()
                          throws IOException
Throws:
IOException

getJSONObject

public net.sf.json.JSONObject getJSONObject()
Retrieves the backing JSONObject

Returns:

toString

public String toString()
Overrides:
toString in class Object

accumulate

public net.sf.json.JSONObject accumulate(String arg0,
                                         boolean arg1)

accumulate

public net.sf.json.JSONObject accumulate(String arg0,
                                         double arg1)

accumulate

public net.sf.json.JSONObject accumulate(String arg0,
                                         int arg1)

accumulate

public net.sf.json.JSONObject accumulate(String arg0,
                                         long arg1)

accumulate

public net.sf.json.JSONObject accumulate(String arg0,
                                         Object arg1)

accumulateAll

public void accumulateAll(Map arg0)

clear

public void clear()
Specified by:
clear in interface Map

containsKey

public boolean containsKey(Object arg0)
Specified by:
containsKey in interface Map

containsValue

public boolean containsValue(Object arg0)
Specified by:
containsValue in interface Map

element

public net.sf.json.JSONObject element(String arg0,
                                      boolean arg1)

element

public net.sf.json.JSONObject element(String arg0,
                                      Collection arg1)

element

public net.sf.json.JSONObject element(String arg0,
                                      double arg1)

element

public net.sf.json.JSONObject element(String arg0,
                                      int arg1)

element

public net.sf.json.JSONObject element(String arg0,
                                      long arg1)

element

public net.sf.json.JSONObject element(String arg0,
                                      Map arg1)

element

public net.sf.json.JSONObject element(String arg0,
                                      Object arg1)

elementOpt

public net.sf.json.JSONObject elementOpt(String arg0,
                                         Object arg1)

entrySet

public Set entrySet()
Specified by:
entrySet in interface Map

get

public Object get(Object arg0)
Specified by:
get in interface Map

get

public Object get(String arg0)

getBoolean

public boolean getBoolean(String arg0)

getDouble

public double getDouble(String arg0)

getInt

public int getInt(String arg0)

getJSONArray

public net.sf.json.JSONArray getJSONArray(String arg0)

getJSONObject

public net.sf.json.JSONObject getJSONObject(String arg0)

getLong

public long getLong(String arg0)

getString

public String getString(String arg0)

has

public boolean has(String arg0)

keys

public Iterator keys()

keySet

public Set keySet()
Specified by:
keySet in interface Map

names

public net.sf.json.JSONArray names()

opt

public Object opt(String arg0)

optBoolean

public boolean optBoolean(String arg0,
                          boolean arg1)

optBoolean

public boolean optBoolean(String arg0)

optDouble

public double optDouble(String arg0,
                        double arg1)

optDouble

public double optDouble(String arg0)

optInt

public int optInt(String arg0,
                  int arg1)

optInt

public int optInt(String arg0)

optJSONArray

public net.sf.json.JSONArray optJSONArray(String arg0)

optJSONObject

public net.sf.json.JSONObject optJSONObject(String arg0)

optLong

public long optLong(String arg0,
                    long arg1)

optLong

public long optLong(String arg0)

optString

public String optString(String arg0,
                        String arg1)

optString

public String optString(String arg0)

put

public Object put(Object arg0,
                  Object arg1)
Specified by:
put in interface Map

putAll

public void putAll(Map arg0)
Specified by:
putAll in interface Map

remove

public Object remove(Object arg0)
Specified by:
remove in interface Map

remove

public Object remove(String arg0)

size

public int size()
Specified by:
size in interface Map

values

public Collection values()
Specified by:
values in interface Map

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map


Copyright © 2012. All Rights Reserved.