|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.planetj.taste.impl.model.jdbc.AbstractJDBCDataModel
public abstract class AbstractJDBCDataModel
An abstract superclass for JDBC-related DataModel implementations, providing most of the common
functionality that any such implementation would need.
Performance will be a concern with any JDBC-based DataModel. There are going to be lots of
simultaneous reads and some writes to one table. Make sure the table is set up optimally -- for example,
you'll want to establish indexes.
You'll also want to use connection pooling of some kind. Most J2EE containers like Tomcat
provide connection pooling, so make sure the DataSource it exposes is using pooling. Outside a
J2EE container, you can use packages like Jakarta's
DBCP to create a DataSource on top of your
database whose Connections are pooled.
Also note: this default implementation assumes that the user and item ID keys are Strings, for
maximum flexibility. You can override this behavior by subclassing an implementation and overriding
buildItem(String) and buildUser(String, List). If you don't, just make sure you use
Strings as IDs throughout your code. If your IDs are really numeric, and you use, say, Long
for IDs in the rest of your code, you will run into subtle problems because the Long values won't
be equal to or compare correctly to the underlying String key values.
| Field Summary | |
|---|---|
static java.lang.String |
DEFAULT_DATASOURCE_NAME
|
static java.lang.String |
DEFAULT_ITEM_ID_COLUMN
|
static java.lang.String |
DEFAULT_PREFERENCE_COLUMN
|
static java.lang.String |
DEFAULT_PREFERENCE_TABLE
|
static java.lang.String |
DEFAULT_USER_ID_COLUMN
|
| Constructor Summary | |
|---|---|
protected |
AbstractJDBCDataModel(javax.sql.DataSource dataSource,
java.lang.String getUserSQL,
java.lang.String getNumItemsSQL,
java.lang.String getNumUsersSQL,
java.lang.String setPreferenceSQL,
java.lang.String removePreferenceSQL,
java.lang.String getUsersSQL,
java.lang.String getItemsSQL,
java.lang.String getItemSQL,
java.lang.String getPrefsForItemSQL,
java.lang.String getUsersPreferringItemSQL)
|
| Method Summary | |
|---|---|
protected Item |
buildItem(java.lang.String id)
Default implementation which returns a new GenericItem with String IDs. |
protected Preference |
buildPreference(User user,
Item item,
double value)
Subclasses may override to return a different Preference implementation. |
protected User |
buildUser(java.lang.String id,
java.util.List<Preference> prefs)
Default implementation which returns a new GenericUser with String IDs. |
javax.sql.DataSource |
getDataSource()
|
Item |
getItem(java.lang.Object id)
|
Item |
getItem(java.lang.Object id,
boolean assumeExists)
|
java.lang.Iterable<? extends Item> |
getItems()
|
int |
getNumItems()
|
int |
getNumUsers()
|
java.lang.Iterable<? extends Preference> |
getPreferencesForItem(java.lang.Object itemID)
|
Preference[] |
getPreferencesForItemAsArray(java.lang.Object itemID)
|
User |
getUser(java.lang.Object id)
|
java.lang.Iterable<? extends User> |
getUsers()
|
static javax.sql.DataSource |
lookupDataSource(java.lang.String dataSourceName)
Looks up a DataSource by name from JNDI. |
void |
refresh()
Triggers "refresh" -- whatever that means -- of the implementation. |
void |
removePreference(java.lang.Object userID,
java.lang.Object itemID)
Removes a particular preference for a user. |
void |
setPreference(java.lang.Object userID,
java.lang.Object itemID,
double value)
Sets a particular preference (item plus rating) for a user. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final java.lang.String DEFAULT_DATASOURCE_NAME
public static final java.lang.String DEFAULT_PREFERENCE_TABLE
public static final java.lang.String DEFAULT_USER_ID_COLUMN
public static final java.lang.String DEFAULT_ITEM_ID_COLUMN
public static final java.lang.String DEFAULT_PREFERENCE_COLUMN
| Constructor Detail |
|---|
protected AbstractJDBCDataModel(javax.sql.DataSource dataSource,
java.lang.String getUserSQL,
java.lang.String getNumItemsSQL,
java.lang.String getNumUsersSQL,
java.lang.String setPreferenceSQL,
java.lang.String removePreferenceSQL,
java.lang.String getUsersSQL,
java.lang.String getItemsSQL,
java.lang.String getItemSQL,
java.lang.String getPrefsForItemSQL,
java.lang.String getUsersPreferringItemSQL)
| Method Detail |
|---|
@NotNull
public static javax.sql.DataSource lookupDataSource(java.lang.String dataSourceName)
throws TasteException
Looks up a DataSource by name from JNDI. "java:comp/env/" is prepended to the argument
before looking up the name in JNDI.
dataSourceName - JNDI name where a DataSource is bound (e.g. "jdbc/taste")
DataSource under that JNDI name
TasteException - if a JNDI error occurs@NotNull public javax.sql.DataSource getDataSource()
getDataSource in interface JDBCDataModelDataSource that this instance is using
@NotNull
public final java.lang.Iterable<? extends User> getUsers()
throws TasteException
getUsers in interface DataModelList of all Users in the model, ordered by User
TasteException - if an error occurs while accessing the data
@NotNull
public final User getUser(java.lang.Object id)
throws TasteException
getUser in interface DataModelid - user ID
User who has that ID
java.util.NoSuchElementException - if there is no such user
TasteException - if an error occurs while accessing the data
@NotNull
public final java.lang.Iterable<? extends Item> getItems()
throws TasteException
getItems in interface DataModelList of all Items in the model, order by Item
TasteException - if an error occurs while accessing the data
@NotNull
public final Item getItem(java.lang.Object id)
throws TasteException
getItem in interface DataModelid - item ID
Item that has that ID
TasteException - if an error occurs while accessing the data
@NotNull
public final Item getItem(java.lang.Object id,
boolean assumeExists)
throws TasteException
getItem in interface JDBCDataModelassumeExists - assume the item exists; don't consult the underlying database. This is a necessary
performance enhancement shortcut needed by slope one recommenders
TasteExceptionDataModel.getItem(Object)
@NotNull
public final java.lang.Iterable<? extends Preference> getPreferencesForItem(java.lang.Object itemID)
throws TasteException
getPreferencesForItem in interface DataModelitemID - item ID
Preferences expressed for that item, ordered by User
TasteException - if an error occurs while accessing the data
@NotNull
public final Preference[] getPreferencesForItemAsArray(java.lang.Object itemID)
throws TasteException
getPreferencesForItemAsArray in interface DataModelitemID - item ID
Preferences expressed for that item, ordered by User,
as an array
TasteException - if an error occurs while accessing the data
public final int getNumItems()
throws TasteException
getNumItems in interface DataModelItems known to the model. This is generally the union
of all Items preferred by at least one User but could include more.
TasteException - if an error occurs while accessing the data
public final int getNumUsers()
throws TasteException
getNumUsers in interface DataModelUsers known to the model.
TasteException - if an error occurs while accessing the data
public final void setPreference(java.lang.Object userID,
java.lang.Object itemID,
double value)
throws TasteException
Sets a particular preference (item plus rating) for a user.
setPreference in interface DataModeluserID - user to set preference foritemID - item to set preference forvalue - preference value
TasteException - if an error occurs while accessing the data
public final void removePreference(java.lang.Object userID,
java.lang.Object itemID)
throws TasteException
Removes a particular preference for a user.
removePreference in interface DataModeluserID - user from which to remove preferenceitemID - item to remove preference for
TasteException - if an error occurs while accessing the datapublic final void refresh()
Triggers "refresh" -- whatever that means -- of the implementation. The general contract is that
any Refreshable should always leave itself in a consistent, operational state, and that
the refresh atomically updates internal state from old to new.
refresh in interface Refreshable
@NotNull
protected User buildUser(java.lang.String id,
java.util.List<Preference> prefs)
Default implementation which returns a new GenericUser with String IDs.
Subclasses may override to return a different User implementation.
id - user IDprefs - user preferences
GenericUser by default@NotNull protected Item buildItem(java.lang.String id)
Default implementation which returns a new GenericItem with String IDs.
Subclasses may override to return a different Item implementation.
id - item ID
GenericItem by default
@NotNull
protected Preference buildPreference(User user,
Item item,
double value)
Preference implementation.
user - Useritem - Item
GenericPreference by default
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||