Work with your Documents


In a previous post I have shown how to store and retrieve BaseDocuments. Now it is time to define your own Documents and store and retrieve them.

  1. First, define the document. In this example, I define a Java class called SiteDocument for storing websites URL’s (inherit it from BaseDocument in order to have the methods already implemented on it). Finally define, using @org.svenson.JSONProperty annotation the properties of your CouchDB document (those controlled by SiteDocument since you can keep usingsetProperties / getProperties). Here we define one called site that we are going to use for storing the URL of my sites.
import org.jcouchdb.document.BaseDocument;
public class SiteDocument extends BaseDocument { 
    private String site; 
    @org.svenson.JSONProperty(value = "site", ignoreIfNull = true) 
    public String getSite() { 
        return site; 
    }

    public void setSite(String name) { 
        site = name; 
    }
}
  1. Next, in your program, you create a SiteDocument object in memory (using new SiteDocument()) and assign the value to site using setSite. Storing it in CouchDB is done exactly as with BaseDocument (using createDocument or createOrUpdateDocument).
// Create a document of type Site
SiteDocument site1 = new SiteDocument();
// and assign value to 'site' property
site1.setSite("www.onabai.com");
// store document on CouchDB
db.createDocument(site1);
System.out.printf("Id: %s Revision: %s\n", site1.getId(), site1.getRevision())
  1. Retrieving the document SiteDocument given its identifier is also done using getDocument (see retrieving).
SiteDocument site2 = db.getDocument(SiteDocument.class, id);
  1. You might also set properties that are not those defined via @org.svenson.JSONProperty. In the following example I set a description property using generic setProperty interface.
site2.setProperty("description", "OnaBai");

Important to note that if you mix setSize(“XYZ”) and setProperty(“size”, “XYZ”) and then save theDocument in CouchDB you will save the value set with setProperty (no matter the other of invokingsetSize and setProperty) and when you do a getDocument you can retrieve site property usinggetSize but not using getProperty.

The following code:

// Create a SiteDocument
SiteDocument site2 = new SiteDocument();
site2.setProperty("site", "http://www.onabai.com/index.html");
// Set value using setSite
site2.setSite("www.onabai.com");
// and using setProperty
site2.setProperty("description", "OnaBai");
db.createOrUpdateDocument(site2);
System.out.printf("Id: %s Revision: %s\n", site2.getId(), site2.getRevision());
System.out.printf("Property site: %s (%s) description: %s\n",
                  site2.getSite(),
                  site2.getProperty("site"),
                  site2.getProperty("description"));

String id = site2.getId();
System.out.printf("Id: %s\n", id);

// Retrieve the document as SiteDocument
SiteDocument site3 = db.getDocument(SiteDocument.class, id);
System.out.printf("Id: %s Revision: %s\n", site3.getId(), site3.getRevision());
// Display both getSite and getProperty
System.out.printf("SiteDocument  -> site: %s (%s) description: %s\n",
                  site3.getSite(),
                  site3.getProperty("site"),
                  site3.getProperty("description"));

// Retrieve the document as BaseDocument
BaseDocument site4 = db.getDocument(BaseDocument.class, id);
System.out.printf("Id: %s Revision: %s\n", site4.getId(), site4.getRevision());

// Display value using getProperty 
// (getSite is not available since it's a BaseDocument)
System.out.printf("BaseDocument  -> Property site: %s description: %s\n",
                  site4.getProperty("site"),
                  site4.getProperty("description"));

displays the following values:

Id: f64056c1eb806125206b769f60002a98 Revision: 1-f85a34aa266f2209962e281cd05ebe5e
Property site: www.onabai.com (http://www.onabai.com/index.html) description: OnaBai
Id: f64056c1eb806125206b769f60002a98 Revision: 1-f85a34aa266f2209962e281cd05ebe5e
SiteDocument  -> site: http://www.onabai.com/index.html (null) description: OnaBai
Id: f64056c1eb806125206b769f60002a98 Revision: 1-f85a34aa266f2209962e281cd05ebe5e
BaseDocument  -> Property site: http://www.onabai.com/index.html description: OnaBai

we can see that when document is of type SiteDocument then getProperty(“size”) is null (line starting by SiteDocument). But if the  Document is retrieved using BaseDocument (line starting by BaseDocument) then getProperty(“size”) returns expected value.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s