14-Dec-04 (Created: 14-Dec-04) | More in 'Data Access'

How to retrieve a tree structure from a database using Aspire HDS: An Example

Property file entries


#############################################
# Declare a data set called "ihdsTest"
#############################################
request.ihdsTest.className=com.ai.htmlgen.DBHashTableFormHandler1
request.ihdsTest.loopNames=root-folders


#############################################
# The first child Root folders retrieve the set of folders
# The root folders may have
#           sub folders
#           and files 
# as children.
# 
# See how these children are defined
#############################################
request.ihdsTest.root-folders.class_request.className=com.ai.htmlgen.GenericTableHandler6
request.ihdsTest.root-folders.loopNames=subfolders,files
request.ihdsTest.root-folders.query_request.className=com.ai.db.DBRequestExecutor2
request.ihdsTest.root-folders.query_request.db=reportsDB
request.ihdsTest.root-folders.query_request.stmt=\
    select * from folders \
    where parent_folder_id is null \
    and owner_user_id = 'satya'

#############################################
# sub folders uses itself as a child recursively
# The sub folders may have
#           sub folders
#           and files 
# as children.
#############################################
request.subfolders.class_request.className=com.ai.htmlgen.GenericTableHandler6
request.subfolders.loopNames=subfolders,files
request.subfolders.query_request.className=com.ai.db.DBRequestExecutor2
request.subfolders.query_request.db=reportsDB
request.subfolders.query_request.stmt=\
select * from folders \
where parent_folder_id = {folder_id} \
and owner_user_id = 'satya'


#############################################
# files are leaf nodes with out any further children
#############################################
request.files.class_request.className=com.ai.htmlgen.GenericTableHandler6
request.files.query_request.className=com.ai.db.DBRequestExecutor2
request.files.query_request.db=reportsDB
request.files.query_request.stmt=\
select * from filed_items \
where folder_id = {folder_id}

#############################################
# Declare a URL so that you can see the output
# data set on the screen using tomcat
#
# The "none" is just a place holder which
# would otherwise point to an html or xslt template
# that would paint the data in a requisite format
#############################################
HDSTestURL=none
HDSTestURL.dataRequestName=ihdsTest

How to see the data on a tomcat screen

Place the above values in a properties file. Include the properties file in aspire.properties. Restart tomcat. Then issue the following command from your web browser


http://your-host/your-webapp/display?url=HDSTestURL&aspire_output_format=text
Some of the pre-requisites are that you have defined the proper servlet mappings for "/display". The above will print the output in a textual format. Also if you have the propert xml jar files in the lib directory you can also get the output in xml using the following command

http://your-host/your-webapp/display?url=HDSTestURL&aspire_output_format=object-xml

Explanation

HDS or hierarchical data sets in Asprie allows you to retrieve tree structures from relational databases. This is accomplished by arranging select statements in a parent/child relationship, including recursive references. When asked for an hds Aspire will walk through these select statements in a lazy load fashion. Although you can construct an "ihds" using purely java objects, the default facilities will allow you to do the same declaratively for the most common cases.

Every loop structure in hds is a collection of rows. Each row is allowed to have again 1 to n loops that are obtained using the values of the row as input. In the example above for instance, the rows of the "root folders" representing a "folder" can contain as its children more folders (sub folders) and more "files". Same thing applies to the sub folders. Files being leaf node it will have no children

References

1. Obtaining an ihds from Aspire

2. Overview of Data acess using Aspire