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"
1_1_1_2003_1_1.pdf
1_1_1_2003_1_2.pdf
1_1_1_2003_2_3.pdf
1_1_1_2003_2_4.pdf
1_1_1_2004_3_5.pdf
1_1_2_2005_4_6.pdf
1_1_3_2006_5_7.pdf
1_1_3_2006_6_8.pdf
1_2_4_2007_7_9.pdf
2_3_5_2008_8_10.pdf
2_3_5_2009_9_11.pdf
2_3_6_2010_10_12.pdf
2_3_6_2010_11_13.pdf
2_3_7_2011_12_14.pdf

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 "http://127.0.0.1:5984/dms4/_design/Document/_view/tree?group=true&group_level=1" -G --data-urlencode startkey='[1]' --data-urlencode endkey='[{}]'
{"rows":[
{"key":[1],"value":9},
{"key":[2],"value":5}
]}
The categories for a client (1)
nestor-nu:~ nestor$ curl -X GET "http://127.0.0.1:5984/dms4/_design/Document/_view/tree?group=true&group_level=2" -G --data-urlencode startkey='[1]' --data-urlencode endkey='[1, {}]'
{"rows":[
{"key":[1,1],"value":8},
{"key":[1,2],"value":1}
]}
The subcategories for a client category (1,1)
nestor-nu:~ nestor$ curl -X GET "http://127.0.0.1:5984/dms4/_design/Document/_view/tree?group=true&group_level=3" -G --data-urlencode startkey='[1,1]' --data-urlencode endkey='[1, 1, {}]'
{"rows":[
{"key":[1,1,1],"value":5},
{"key":[1,1,2],"value":1},
{"key":[1,1,3],"value":2}
]} 
The effective years for the client category subcategory (1,1,1)
nestor-nu:~ nestor$ curl -X GET "http://127.0.0.1:5984/dms4/_design/Document/_view/tree?group=true&group_level=4" -G --data-urlencode startkey='[1,1,1]' --data-urlencode endkey='[1,1,1,{}]'
{"rows":[
{"key":[1,1,1,"2003"],"value":4},
{"key":[1,1,1,"2004"],"value":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 "http://127.0.0.1:5984/dms4/_design/Document/_view/tree?group=true&group_level=5" -G --data-urlencode startkey='[1,1,1,"2003"]' --data-urlencode endkey='[1,1,1,"2003",{}]'
{"rows":[
{"key":[1,1,1,"2003","01"],"value":2},
{"key":[1,1,1,"2003","02"],"value":2}
]}
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 "http://127.0.0.1:5984/dms4/_design/Document/_view/tree?group=true&group_level=6" -G --data-urlencode startkey='[1,1,1,"2003","01"]' --data-urlencode endkey='[1,1,1,"2003","01",{}]'
{"rows":[
{"key":[1,1,1,"2003","01","1_1_1_2003_1_1.pdf"],"value":1},
{"key":[1,1,1,"2003","01","1_1_1_2003_1_2.pdf"],"value":1}
]}
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 "http://127.0.0.1:5984/dms4/_design/Document/_view/tree?group=true&group_level=6" -G --data-urlencode startkey='[2,3,5,"2009","09"]' --data-urlencode endkey='[2,3,5,"2009","09",{}]'
{"rows":[
{"key":[2,3,5,"2009","09","2_3_5_2009_9_11.pdf"],"value":1}
]}

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:
http://localhost:8080/nu-app/dms/document/tree?root=2,3,5,"2009","09"&ert=json
{"rows":[
{"key":[2,3,5,"2009","09","2_3_5_2009_9_11.pdf"],"value":1}
]}

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

No comments:

Followers