Sunday, October 16, 2011

Document Management System with CouchDB - Second Part

In the first part we installed CouchDB and explain how to create databases, documents, update them, create attachments and run queries.

Now we will design a DMS and plan for the different functionality we will need with one example.

For simplicity we will say our documents will be imported in batch for which it makes sense to have a convention for the file names client_cat_subcat_year_month_investor.ext. After importing the below 14 documents we can start navigating the tree. Note that in this example the importer code will replace the month by two digits so "3" becomes "03"

We want to offer a tree view of our documents. First we show the available clients. When the user clicks one of them we show the categories available. Clicking one of the categories will render all subcategories and so on. Let us go by the example of the first document (1_1_1_2003_1_1.pdf)

Here is how to pull all clients. Note the use of {} which means "any":
nestor-nu:~ nestor$ curl -X GET "" -G --data-urlencode startkey='[1]' --data-urlencode endkey='[{}]'
The categories for a client (1)
nestor-nu:~ nestor$ curl -X GET "" -G --data-urlencode startkey='[1]' --data-urlencode endkey='[1, {}]'
The subcategories for a client category (1,1)
nestor-nu:~ nestor$ curl -X GET "" -G --data-urlencode startkey='[1,1]' --data-urlencode endkey='[1, 1, {}]'
The effective years for the client category subcategory (1,1,1)
nestor-nu:~ nestor$ curl -X GET "" -G --data-urlencode startkey='[1,1,1]' --data-urlencode endkey='[1,1,1,{}]'
The effective months for the client category subcategory year (1,1,1,"2003"). Note the quotes for 2003 as it is a String obtained from a token.
nestor-nu:~ nestor$ curl -X GET "" -G --data-urlencode startkey='[1,1,1,"2003"]' --data-urlencode endkey='[1,1,1,"2003",{}]'
The documents for the client category subcategory year month(1,1,1,"2003","01"). Note "01" instead "1" just because our importer is treating months as 2 digits values.
nestor-nu:~ nestor$ curl -X GET "" -G --data-urlencode startkey='[1,1,1,"2003","01"]' --data-urlencode endkey='[1,1,1,"2003","01",{}]'
So you have figured if we want to navigate to document 2_3_5_2009_9_11.pdf we just have to pass startkey='[2]' and endkey=[2,3,5,"2009","09",{}] and the group_level 6:
nestor-nu:~ nestor$ curl -X GET "" -G --data-urlencode startkey='[2,3,5,"2009","09"]' --data-urlencode endkey='[2,3,5,"2009","09",{}]'

If we were to build a JSON web service we just need to accept the startkey. The endkey is always an array containing startkey with a new last element: {}.

Specifically if I use BHUB (which is just a concept around Spring Framework) a typical request and response will look like:

In the final part I show how to use Java Erktop library to implement the DMS we have been covering so far.

No comments: