org.mentawai.list
Class DBListData

java.lang.Object
  extended by org.mentawai.list.DBListData
All Implemented Interfaces:
Runnable, ListData

public class DBListData
extends Object
implements ListData, Runnable

The ListData that will load its ListItems from a database. All you have to do is provide: - the name of the key column - the name of the value column - the name of the locale column - the name of the table - the column to sort Or if you database table is not localized: - the name of the key column - the name of the value column - the name of the table - the single locale to use for that list

Since:
1.9
Author:
Sergio Oliveira Jr.

Constructor Summary
DBListData(String name)
           
DBListData(String name, String orderBy)
           
DBListData(String name, String keyColumn, String valueColumn, String tableName, String orderByColumn)
          If your database table is not localized, in other words, if it does not care about locales and have only one list in one locale, use this constructor.
DBListData(String name, String keyColumn, String valueColumn, String localeColumn, String tableName, String orderByColumn)
          If your database table is localized, in other words, if it has a locale column to define the locale, use this constructor.
 
Method Summary
protected  String buildSQL(boolean isLocalized)
           
protected  Locale getLocaleFromString(String loc)
          Override this method to map whatever you have in your database to a Java locale.
 String getName()
          Returns the name of this list.
 String getValue(int key)
          Returns the string value of the list data item with the given id with the default locale.
 String getValue(int key, Locale loc)
          Returns the string value of the list data item with the given id in the given locale.
 String getValue(String key)
          Returns the string value for the list data item with the default locale.
 String getValue(String key, Locale loc)
          Returns the string value of the list data item with the given id in the given locale.
 List<ListItem> getValues()
          Returns a list of ListItem for the default locale.
 List<ListItem> getValues(Locale loc)
          Returns a list of ListItem in the given locale.
 boolean isLocalized()
           
 void load(Connection conn)
          Call this method passing a db connection to load the lists from database! The SQL statement that will be constructed: "select " + keyColumn + "," + valueColumn + "," + localeColumn + " from " + tableName + " order by " + orderByColumn; or if localeColumn is null and a single locale (unique) is provided "select " + keyColumn + "," + valueColumn + " from " + tableName + " order by " + orderByColumn;
 void load(ConnectionHandler connHandler)
           
 void loadUsingJPA()
          load the lists from database from JPA Entity Manager warning: you NEED a defined persistence unit !
 void refresh(Connection conn)
          The only difference of this method to the load(conn) method is that it will clear everything before loading...
 void refresh(ConnectionHandler connHandler)
           
 void run()
           
 void setWhere(String where)
           
 int size()
          Returns the size of this list.
 void startAutoRefresh(ConnectionHandler connHandler, long time)
           
 void stopAutoRefresh()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DBListData

public DBListData(String name,
                  String keyColumn,
                  String valueColumn,
                  String localeColumn,
                  String tableName,
                  String orderByColumn)
If your database table is localized, in other words, if it has a locale column to define the locale, use this constructor.

Parameters:
name -
keyColumn -
valueColumn -
localeColumn -
tableName -
orderByColumn -

DBListData

public DBListData(String name,
                  String keyColumn,
                  String valueColumn,
                  String tableName,
                  String orderByColumn)
If your database table is not localized, in other words, if it does not care about locales and have only one list in one locale, use this constructor.

Parameters:
name -
keyColumn -
valueColumn -
tableName -
orderByColumn -

DBListData

public DBListData(String name)

DBListData

public DBListData(String name,
                  String orderBy)
Method Detail

isLocalized

public boolean isLocalized()

setWhere

public void setWhere(String where)

startAutoRefresh

public void startAutoRefresh(ConnectionHandler connHandler,
                             long time)

stopAutoRefresh

public void stopAutoRefresh()

run

public void run()
Specified by:
run in interface Runnable

size

public int size()
Description copied from interface: ListData
Returns the size of this list. Note: the size does not depend on the number of locales.

Specified by:
size in interface ListData
Returns:
The size of the list.

getValue

public String getValue(String key,
                       Locale loc)
Description copied from interface: ListData
Returns the string value of the list data item with the given id in the given locale.

Specified by:
getValue in interface ListData
Parameters:
key - The id of the list data item.
loc - The locale of the list data item.
Returns:
The string value for the list data item.

getValue

public String getValue(String key)
Description copied from interface: ListData
Returns the string value for the list data item with the default locale. If there is no value for the default locale, use any locale available to return the value.

Specified by:
getValue in interface ListData
Returns:
The string value of the list data item.

getValue

public String getValue(int key)
Description copied from interface: ListData
Returns the string value of the list data item with the given id with the default locale.

Specified by:
getValue in interface ListData
Parameters:
key - The id of the list data item
Returns:
The string value of the list data item

getValue

public String getValue(int key,
                       Locale loc)
Description copied from interface: ListData
Returns the string value of the list data item with the given id in the given locale.

Specified by:
getValue in interface ListData
Parameters:
key - The id of the list data item.
loc - The locale of the list data item.
Returns:
The string value for the list data item.

getValues

public List<ListItem> getValues(Locale loc)
Description copied from interface: ListData
Returns a list of ListItem in the given locale.

Specified by:
getValues in interface ListData
Parameters:
loc - The locale of the ListItems.
Returns:
A list of ListItems.

getValues

public List<ListItem> getValues()
Description copied from interface: ListData
Returns a list of ListItem for the default locale. If there is no list for the default locale, then try any locale in order to return a list.

Specified by:
getValues in interface ListData
Returns:
A list of ListItems.

getName

public String getName()
Description copied from interface: ListData
Returns the name of this list.

Specified by:
getName in interface ListData
Returns:
The name of the list.

getLocaleFromString

protected Locale getLocaleFromString(String loc)
Override this method to map whatever you have in your database to a Java locale. Ex: pt_BR can return new Locale("pt_BR") (default implementation) "1" = new Locale("pt_BR"); "2" = new Locale("en_US"); etc...

Parameters:
loc -
Returns:
The locale corresping to the String loc in your database

load

public void load(ConnectionHandler connHandler)

buildSQL

protected String buildSQL(boolean isLocalized)

load

public void load(Connection conn)
Call this method passing a db connection to load the lists from database! The SQL statement that will be constructed: "select " + keyColumn + "," + valueColumn + "," + localeColumn + " from " + tableName + " order by " + orderByColumn; or if localeColumn is null and a single locale (unique) is provided "select " + keyColumn + "," + valueColumn + " from " + tableName + " order by " + orderByColumn;

Parameters:
conn -
Throws:
SQLException

loadUsingJPA

public void loadUsingJPA()
load the lists from database from JPA Entity Manager warning: you NEED a defined persistence unit !


refresh

public void refresh(ConnectionHandler connHandler)
             throws SQLException
Throws:
SQLException

refresh

public void refresh(Connection conn)
             throws SQLException
The only difference of this method to the load(conn) method is that it will clear everything before loading...

Parameters:
conn -
Throws:
SQLException


Copyright © 2013. All Rights Reserved.