Class JdbcPlainUserPreferencesStorage
- java.lang.Object
-
- org.apache.avalon.framework.logger.AbstractLogEnabled
-
- org.ametys.plugins.core.impl.userpref.JdbcPlainUserPreferencesStorage
-
- All Implemented Interfaces:
UserPreferencesStorage
,Configurable
,LogEnabled
,ThreadSafe
public class JdbcPlainUserPreferencesStorage extends AbstractLogEnabled implements UserPreferencesStorage, ThreadSafe, Configurable
This class is a JDBC implementation ofUserPreferencesStorage
, which stores preferences in database, one preference by column.
Currently, it only supports storing in string-typed columns (VARCHAR, TEXT, ...), but allows all preference types by casting them from/to strings when retrieving/getting them.
This component does not impose a table structure. This is a configuration example:
<component role="com.mydomain.test.MyDatabaseUserPreferencesStorage"
class="org.ametys.plugins.core.userpref.JdbcPlainUserPreferencesStorage">
<pool>com.mydomain.test.MyPool</pool>
<table>MyUserPreferences</table>
<loginColumn>user</loginColumn>
<contextColumn>context</contextColumn>
<columnPattern>^(mypref_\w+)$</columnPattern>
<mappings>
<mapping prefId="lastname" column="mypref_lastname"/>
<mapping prefId="firstname" column="mypref_firstname"/>
<mapping prefId="email" column="mypref_email_address"/>
</mappings>
</component>
Column names must be configured lowercase, both when setting login and context columns and when setting mapping columns.
This class differs fromJdbcXmlUserPreferencesStorage
as it does not implementDefaultUserPreferencesStorage
, and because the latter imposes the DB table structure and stores the preferences as an XML binary.
-
-
Field Summary
Fields Modifier and Type Field Description protected Pattern
_columnPattern
A pattern to filter the columns which correspond to preferences.protected Map<String,String>
_columnToPrefId
Mapping from column name to preference id.protected String
_contextColumn
The context column, can be null if the database is not context-dependent.protected String
_databaseTable
The database table in which the preferences are stored.protected String
_dataSourceId
The id of the data source used.protected String
_loginColumn
The login column, cannot be null.protected String
_populationColumn
The population id column, cannot be null.protected Map<String,String>
_prefIdToColumn
Mapping from preference id to column name.
-
Constructor Summary
Constructors Constructor Description JdbcPlainUserPreferencesStorage()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
configure(Configuration configuration)
void
configureMappings(Configuration configuration)
Configure the mappings from preference ID to column name.protected boolean
dataExists(Connection connection, UserIdentity user, String storageContext)
Test if a record exists for this user and context.protected String
getPreferenceValue(ResultSet rs, int columnIndex, int jdbcType)
Get a preference value as a String.Map<String,String>
getUnTypedUserPrefs(UserIdentity user, String storageContext, Map<String,String> contextVars)
Get a user's preference values (as String) for a given context.Boolean
getUserPreferenceAsBoolean(UserIdentity user, String storageContext, Map<String,String> contextVars, String id)
Get a single boolean user preference value for a given context.Date
getUserPreferenceAsDate(UserIdentity user, String storageContext, Map<String,String> contextVars, String id)
Get a single date user preference value for a given context.Double
getUserPreferenceAsDouble(UserIdentity user, String storageContext, Map<String,String> contextVars, String id)
Get a single double user preference value for a given context.Long
getUserPreferenceAsLong(UserIdentity user, String storageContext, Map<String,String> contextVars, String id)
Get a single long user preference value for a given context.String
getUserPreferenceAsString(UserIdentity user, String storageContext, Map<String,String> contextVars, String id)
Get a single string user preference value for a given context.protected void
insertPreferences(Connection connection, Map<String,String> preferences, UserIdentity user, String storageContext)
Insert preferences into the database.protected boolean
isColumnValid(String name)
Test if the given column corresponds to a preference value.void
removeUserPreferences(UserIdentity user, String storageContext, Map<String,String> contextVars)
Remove the stored user preferences for a login in a given context.void
setUserPreferences(UserIdentity user, String storageContext, Map<String,String> contextVars, Map<String,String> preferences)
Set a user's preferences for a given context.protected void
updatePreferences(Connection connection, Map<String,String> preferences, UserIdentity user, String storageContext)
Update existing preferences.-
Methods inherited from class org.apache.avalon.framework.logger.AbstractLogEnabled
enableLogging, getLogger, setupLogger, setupLogger, setupLogger
-
-
-
-
Field Detail
-
_dataSourceId
protected String _dataSourceId
The id of the data source used.
-
_databaseTable
protected String _databaseTable
The database table in which the preferences are stored.
-
_loginColumn
protected String _loginColumn
The login column, cannot be null.
-
_populationColumn
protected String _populationColumn
The population id column, cannot be null.
-
_contextColumn
protected String _contextColumn
The context column, can be null if the database is not context-dependent.
-
_columnPattern
protected Pattern _columnPattern
A pattern to filter the columns which correspond to preferences.
-
_prefIdToColumn
protected Map<String,String> _prefIdToColumn
Mapping from preference id to column name.
-
_columnToPrefId
protected Map<String,String> _columnToPrefId
Mapping from column name to preference id.
-
-
Constructor Detail
-
JdbcPlainUserPreferencesStorage
public JdbcPlainUserPreferencesStorage()
-
-
Method Detail
-
configure
public void configure(Configuration configuration) throws ConfigurationException
- Specified by:
configure
in interfaceConfigurable
- Throws:
ConfigurationException
-
configureMappings
public void configureMappings(Configuration configuration) throws ConfigurationException
Configure the mappings from preference ID to column name.- Parameters:
configuration
- the mapping configuration root.- Throws:
ConfigurationException
- if an error occurs.
-
getUnTypedUserPrefs
public Map<String,String> getUnTypedUserPrefs(UserIdentity user, String storageContext, Map<String,String> contextVars) throws UserPreferencesException
Description copied from interface:UserPreferencesStorage
Get a user's preference values (as String) for a given context.- Specified by:
getUnTypedUserPrefs
in interfaceUserPreferencesStorage
- Parameters:
user
- the user.storageContext
- the preferences storage context.contextVars
- The context variables including environment elements- Returns:
- the user preference values as a Map of String indexed by preference ID.
- Throws:
UserPreferencesException
- if an error occurs getting the preferences.
-
removeUserPreferences
public void removeUserPreferences(UserIdentity user, String storageContext, Map<String,String> contextVars) throws UserPreferencesException
Description copied from interface:UserPreferencesStorage
Remove the stored user preferences for a login in a given context.- Specified by:
removeUserPreferences
in interfaceUserPreferencesStorage
- Parameters:
user
- the user.storageContext
- the preferences storage context.contextVars
- the context variables.- Throws:
UserPreferencesException
- if an error occurred
-
setUserPreferences
public void setUserPreferences(UserIdentity user, String storageContext, Map<String,String> contextVars, Map<String,String> preferences) throws UserPreferencesException
Description copied from interface:UserPreferencesStorage
Set a user's preferences for a given context.- Specified by:
setUserPreferences
in interfaceUserPreferencesStorage
- Parameters:
user
- the user.storageContext
- the preferences storage context.contextVars
- The context variables including environment elementspreferences
- a Map of the preference values indexed by ID.- Throws:
UserPreferencesException
- if an error occurred
-
getUserPreferenceAsString
public String getUserPreferenceAsString(UserIdentity user, String storageContext, Map<String,String> contextVars, String id) throws UserPreferencesException
Description copied from interface:UserPreferencesStorage
Get a single string user preference value for a given context.- Specified by:
getUserPreferenceAsString
in interfaceUserPreferencesStorage
- Parameters:
user
- the user.storageContext
- the preferences storage context.contextVars
- The context variables including environment elementsid
- the preference ID.- Returns:
- the user preference value as a String.
- Throws:
UserPreferencesException
- if an error occurred
-
getUserPreferenceAsLong
public Long getUserPreferenceAsLong(UserIdentity user, String storageContext, Map<String,String> contextVars, String id) throws UserPreferencesException
Description copied from interface:UserPreferencesStorage
Get a single long user preference value for a given context.- Specified by:
getUserPreferenceAsLong
in interfaceUserPreferencesStorage
- Parameters:
user
- the user.storageContext
- the preferences storage context.contextVars
- The context variables including environment elementsid
- the preference ID.- Returns:
- the user preference value as a Long.
- Throws:
UserPreferencesException
- if an error occurred
-
getUserPreferenceAsDate
public Date getUserPreferenceAsDate(UserIdentity user, String storageContext, Map<String,String> contextVars, String id) throws UserPreferencesException
Description copied from interface:UserPreferencesStorage
Get a single date user preference value for a given context.- Specified by:
getUserPreferenceAsDate
in interfaceUserPreferencesStorage
- Parameters:
user
- the user.storageContext
- the preferences storage context.contextVars
- The context variables including environment elementsid
- the preference ID.- Returns:
- the user preference value as a Date.
- Throws:
UserPreferencesException
- if an error occurred
-
getUserPreferenceAsBoolean
public Boolean getUserPreferenceAsBoolean(UserIdentity user, String storageContext, Map<String,String> contextVars, String id) throws UserPreferencesException
Description copied from interface:UserPreferencesStorage
Get a single boolean user preference value for a given context.- Specified by:
getUserPreferenceAsBoolean
in interfaceUserPreferencesStorage
- Parameters:
user
- the user.storageContext
- the preferences storage context.contextVars
- The context variables including environment elementsid
- the preference ID.- Returns:
- the user preference value as a Boolean.
- Throws:
UserPreferencesException
- if an error occurred
-
getUserPreferenceAsDouble
public Double getUserPreferenceAsDouble(UserIdentity user, String storageContext, Map<String,String> contextVars, String id) throws UserPreferencesException
Description copied from interface:UserPreferencesStorage
Get a single double user preference value for a given context.- Specified by:
getUserPreferenceAsDouble
in interfaceUserPreferencesStorage
- Parameters:
user
- the user.storageContext
- the preferences storage context.contextVars
- The context variables including environment elementsid
- the preference ID.- Returns:
- the user preference value as a Double.
- Throws:
UserPreferencesException
- if an error occurred
-
isColumnValid
protected boolean isColumnValid(String name)
Test if the given column corresponds to a preference value.- Parameters:
name
- the column name.- Returns:
- true if the column corresponds to a preference value, false otherwise.
-
getPreferenceValue
protected String getPreferenceValue(ResultSet rs, int columnIndex, int jdbcType) throws SQLException
Get a preference value as a String.- Parameters:
rs
- The result set, must be set on the right record.columnIndex
- The column index.jdbcType
- The JDBC type.- Returns:
- The preference value as a String, can be null.
- Throws:
SQLException
- if an error occurs.
-
dataExists
protected boolean dataExists(Connection connection, UserIdentity user, String storageContext) throws SQLException
Test if a record exists for this user and context.- Parameters:
connection
- The database connection.user
- The user.storageContext
- The storage context.- Returns:
- true if data exists, false otherwise.
- Throws:
SQLException
- if an error occurs.
-
insertPreferences
protected void insertPreferences(Connection connection, Map<String,String> preferences, UserIdentity user, String storageContext) throws SQLException
Insert preferences into the database.- Parameters:
connection
- The database connection.preferences
- The preference values, indexed by preference id.user
- The user.storageContext
- The preference storage context.- Throws:
SQLException
- if an error occurs.
-
updatePreferences
protected void updatePreferences(Connection connection, Map<String,String> preferences, UserIdentity user, String storageContext) throws SQLException
Update existing preferences.- Parameters:
connection
- The database connection.preferences
- The preference values, indexed by preference id.user
- The user.storageContext
- The preference storage context.- Throws:
SQLException
- if an error occurs.
-
-