jp.terasoluna.fw.web.codelist
Class DBCodeListLoader

java.lang.Object
  extended by jp.terasoluna.fw.web.codelist.DBCodeListLoader
All Implemented Interfaces:
CodeListLoader, ReloadableCodeListLoader

public class DBCodeListLoader
extends java.lang.Object
implements ReloadableCodeListLoader

Implementation class of ReloadableCodeListLoader initializes the code list information by using the database.

To generate the code list by using this class, it is necessary to first execute load() method after specifying the data source in "dataSource" attribute and "load" in "init-method" attribute.
Since this class is not thread safe, the blockage design should be created by using business blockage function or server blockage function. This will enable to execute reload() method in a scenario where code list can not be referred.
Please note that if this class is made thread safe, exclusive access control is applied to refer the code list. This could result into the performance degradation.

SQL that fetches the code list from the database, is set from the Bean definition file.

Configuration example of Bean definition file.
When data source is defined as TerasolunaDataSource.

 <bean id="loader1"
       class="jp.terasoluna.fw.web.codelist.DBCodeListLoader"
       init-method="load">
   <property name="dataSource">
     <ref bean="TerasolunaDataSource"/>
   </property>
   <property name="sql">
     <value>SELECT KEY, VALUE FROM CODE_LISTS</value>
   </property>

 </bean>
 

CodeBean class which stores the codelist, possesses the attribute id and name. The codelist is stored in the CodeBean as per the sequence of columns specified in SELECT statement.
In the above-example, KEY is stored as per id sequence and VALUE is stored as per name sequence.
If the fetched columns are less than two and if only KEY is fetched, null is stored in the name. When the columns which are fetched by SELECT statement are 3 or more, columns after 3rd column are ignored.

The result which is fetched by SQL is stored in servlet context. In JSP, the "name" attribute of <logic:iterate> tag and "collection" attribute of <html:options> tag in Struts, are referred as bean name.
In the following example, bean name is specified as "loader1" in the "collection" attribute of <html:options>.

Usage example of code list in JSP.

  <ts:defineCodeList id="loader1" />
  ...
  <html:select property="selectOptions">
    <html:options collection="loader1"
                  labelProperty="name"
                  property="id"/>
  </html:select>
  

To reload the code list, refer to ReloadCodeListAction For usage method of code list in JSP, refer to DefineCodeListTag and WriteCodeCountTag.

See Also:
CodeBean, ReloadCodeListAction, DefineCodeListTag, WriteCodeCountTag

Field Summary
private  java.util.List<CodeBean> codeLists
          Code list.
private  javax.sql.DataSource dataSource
          Data source used for connecting to DB.
private  java.lang.Object lockObject
          Lock object used in the code list.
private  org.apache.commons.logging.Log log
          Log class.
private  java.lang.String sql
          SQL to fetch the code list.
 
Constructor Summary
DBCodeListLoader()
           
 
Method Summary
 CodeBean[] getCodeBeans()
          Fetches the code list. Code list can be fetched as an array of CodeBean.
*Code list is the unique information in the application. In case of overriding this method, implementation should be done in such a way that there should be no impact even if the contents of code list are edited by using business logic.
 javax.sql.DataSource getDataSource()
          Fetches the dataSource.
 java.lang.String getSql()
          Fetches the SQL which retrieves the code list.
 void load()
          Initializes the code list.
protected  void loadCodeList()
          Loads the code list. Generates the code list based on the dataSource and the specified SQL statement.
 void reload()
          Reloads the code list.
 void setDataSource(javax.sql.DataSource dataSource)
          Sets the dataSource.
 void setSql(java.lang.String sql)
          Sets the SQL to fetch the code list.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private org.apache.commons.logging.Log log
Log class.


codeLists

private java.util.List<CodeBean> codeLists
Code list.

See Also:
CodeBean

sql

private java.lang.String sql
SQL to fetch the code list.


dataSource

private javax.sql.DataSource dataSource
Data source used to connect to the DB.


lockObject

private final java.lang.Object lockObject
Lock object used in the code list.

Constructor Detail

DBCodeListLoader

public DBCodeListLoader()
Method Detail

load

public void load()
Initializes the code list.

Uses the loadCodeList() and generates the code list based on the dataSource and the specified SQL statement. No process is performed if the code list already exists.

Specified by:
load in interface CodeListLoader

reload

public void reload()
Reloads the code list.

Calls loadCodeList() method after syncronizing the codeLists. It is recommended to use this method in the administration business which is currently under blocked state.

Specified by:
reload in interface ReloadableCodeListLoader

loadCodeList

protected void loadCodeList()
Loads the code list. Generates the code list based on dataSource and the specified SQL statement.


getCodeBeans

public CodeBean[] getCodeBeans()
Fetches the code list. Code list can be fetched as an array of CodeBean.
*Code list is the unique information in the application. In case of overriding this method, implementation should be done in such a way that there should be no impact even if the contents of code list are edited by using business logic.

Specified by:
getCodeBeans in interface CodeListLoader
Returns:
Code list

getDataSource

public javax.sql.DataSource getDataSource()
Fetches dataSource.

Returns:
Field value indicating the dataSource.

setDataSource

public void setDataSource(javax.sql.DataSource dataSource)
Sets dataSource.

Parameters:
dataSource - Field value indicating dataSource.

getSql

public java.lang.String getSql()
Fetches SQL to retreive the code list.

Returns:
SQL statement

setSql

public void setSql(java.lang.String sql)
Sets the SQL to fetch the code list.

Parameters:
sql - SQL statement