public class JdbcPlainUserPreferencesStorage extends AbstractLogEnabled implements UserPreferencesStorage, ThreadSafe, Configurable
UserPreferencesStorage
,
which stores preferences in database, one preference by column.<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>
JdbcXmlUserPreferencesStorage
as it does not implement DefaultUserPreferencesStorage
,
and because the latter imposes the DB table structure and stores the preferences as an XML binary.Modifier and Type | Field and 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 and Description |
---|
JdbcPlainUserPreferencesStorage() |
Modifier and Type | Method and 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.
|
enableLogging, getLogger, setupLogger, setupLogger, setupLogger
protected String _dataSourceId
protected String _databaseTable
protected String _loginColumn
protected String _populationColumn
protected String _contextColumn
protected Pattern _columnPattern
protected Map<String,String> _prefIdToColumn
protected Map<String,String> _columnToPrefId
public JdbcPlainUserPreferencesStorage()
public void configure(Configuration configuration) throws ConfigurationException
configure
in interface Configurable
ConfigurationException
public void configureMappings(Configuration configuration) throws ConfigurationException
configuration
- the mapping configuration root.ConfigurationException
- if an error occurs.public Map<String,String> getUnTypedUserPrefs(UserIdentity user, String storageContext, Map<String,String> contextVars) throws UserPreferencesException
UserPreferencesStorage
getUnTypedUserPrefs
in interface UserPreferencesStorage
user
- the user.storageContext
- the preferences storage context.contextVars
- The context variables including environment elementsUserPreferencesException
- if an error occurs getting the preferences.public void removeUserPreferences(UserIdentity user, String storageContext, Map<String,String> contextVars) throws UserPreferencesException
UserPreferencesStorage
removeUserPreferences
in interface UserPreferencesStorage
user
- the user.storageContext
- the preferences storage context.contextVars
- the context variables.UserPreferencesException
- if an error occurredpublic void setUserPreferences(UserIdentity user, String storageContext, Map<String,String> contextVars, Map<String,String> preferences) throws UserPreferencesException
UserPreferencesStorage
setUserPreferences
in interface UserPreferencesStorage
user
- the user.storageContext
- the preferences storage context.contextVars
- The context variables including environment elementspreferences
- a Map of the preference values indexed by ID.UserPreferencesException
- if an error occurredpublic String getUserPreferenceAsString(UserIdentity user, String storageContext, Map<String,String> contextVars, String id) throws UserPreferencesException
UserPreferencesStorage
getUserPreferenceAsString
in interface UserPreferencesStorage
user
- the user.storageContext
- the preferences storage context.contextVars
- The context variables including environment elementsid
- the preference ID.UserPreferencesException
- if an error occurredpublic Long getUserPreferenceAsLong(UserIdentity user, String storageContext, Map<String,String> contextVars, String id) throws UserPreferencesException
UserPreferencesStorage
getUserPreferenceAsLong
in interface UserPreferencesStorage
user
- the user.storageContext
- the preferences storage context.contextVars
- The context variables including environment elementsid
- the preference ID.UserPreferencesException
- if an error occurredpublic Date getUserPreferenceAsDate(UserIdentity user, String storageContext, Map<String,String> contextVars, String id) throws UserPreferencesException
UserPreferencesStorage
getUserPreferenceAsDate
in interface UserPreferencesStorage
user
- the user.storageContext
- the preferences storage context.contextVars
- The context variables including environment elementsid
- the preference ID.UserPreferencesException
- if an error occurredpublic Boolean getUserPreferenceAsBoolean(UserIdentity user, String storageContext, Map<String,String> contextVars, String id) throws UserPreferencesException
UserPreferencesStorage
getUserPreferenceAsBoolean
in interface UserPreferencesStorage
user
- the user.storageContext
- the preferences storage context.contextVars
- The context variables including environment elementsid
- the preference ID.UserPreferencesException
- if an error occurredpublic Double getUserPreferenceAsDouble(UserIdentity user, String storageContext, Map<String,String> contextVars, String id) throws UserPreferencesException
UserPreferencesStorage
getUserPreferenceAsDouble
in interface UserPreferencesStorage
user
- the user.storageContext
- the preferences storage context.contextVars
- The context variables including environment elementsid
- the preference ID.UserPreferencesException
- if an error occurredprotected boolean isColumnValid(String name)
name
- the column name.protected String getPreferenceValue(ResultSet rs, int columnIndex, int jdbcType) throws SQLException
rs
- The result set, must be set on the right record.columnIndex
- The column index.jdbcType
- The JDBC type.SQLException
- if an error occurs.protected boolean dataExists(Connection connection, UserIdentity user, String storageContext) throws SQLException
connection
- The database connection.user
- The user.storageContext
- The storage context.SQLException
- if an error occurs.protected void insertPreferences(Connection connection, Map<String,String> preferences, UserIdentity user, String storageContext) throws SQLException
connection
- The database connection.preferences
- The preference values, indexed by preference id.user
- The user.storageContext
- The preference storage context.SQLException
- if an error occurs.protected void updatePreferences(Connection connection, Map<String,String> preferences, UserIdentity user, String storageContext) throws SQLException
connection
- The database connection.preferences
- The preference values, indexed by preference id.user
- The user.storageContext
- The preference storage context.SQLException
- if an error occurs.