1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.portal.business.prefs;
35
36 import fr.paris.lutece.util.sql.DAOUtil;
37
38 import java.util.ArrayList;
39 import java.util.List;
40
41
42
43
44 public abstract class AbstractUserPreferencesDAO implements IPreferencesDAO
45 {
46 private static final String SQL_COUNT = "SELECT COUNT(*) FROM ";
47 private final String _strSqlSelect = "SELECT pref_value FROM " + getPreferencesTable( ) + " WHERE id_user = ? AND pref_key = ?";
48 private final String _strSqlInsert = "INSERT INTO " + getPreferencesTable( ) + " ( pref_value , id_user, pref_key ) VALUES ( ?, ?, ? ) ";
49 private final String _strSqlUpdate = "UPDATE " + getPreferencesTable( ) + " SET pref_value = ? WHERE id_user = ? AND pref_key = ?";
50 private final String _strSqlDelete = "DELETE FROM " + getPreferencesTable( ) + " WHERE id_user = ? ";
51 private final String _strSqlSelectAll = "SELECT pref_key FROM " + getPreferencesTable( ) + " WHERE id_user = ?";
52 private final String _strSqlSelectByValue = "SELECT id_user FROM " + getPreferencesTable( ) + " WHERE pref_key = ? AND pref_value = ? ";
53 private final String _strSqlDeleteKey = _strSqlDelete + " AND pref_key = ? ";
54 private final String _strSqlDeleteKeyPrefix = _strSqlDelete + " AND pref_key LIKE ? ";
55 private final String _strSqlSelectCount = SQL_COUNT + getPreferencesTable( ) + " WHERE id_user = ? AND pref_key = ?";
56 private final String _strSqlSelectCountPrefValue = SQL_COUNT + getPreferencesTable( ) + " WHERE pref_key = ? AND pref_value = ?";
57
58
59
60
61
62
63 abstract String getPreferencesTable( );
64
65
66
67
68 @Override
69 public String load( String strUserId, String strKey, String strDefault )
70 {
71 String strValue = strDefault;
72 try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( _strSqlSelect ) )
73 {
74 daoUtil.setString( 1, strUserId );
75 daoUtil.setString( 2, strKey );
76 daoUtil.executeQuery( );
77
78 if ( daoUtil.next( ) )
79 {
80 strValue = ( daoUtil.getString( 1 ) );
81 }
82
83 }
84
85 return strValue;
86 }
87
88
89
90
91 @Override
92 public List<String> getUserId( String strKey, String strValue )
93 {
94 List<String> listUserId = new ArrayList<>( );
95 try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( _strSqlSelectByValue ) )
96 {
97 daoUtil.setString( 1, strKey );
98 daoUtil.setString( 2, strValue );
99 daoUtil.executeQuery( );
100
101 while ( daoUtil.next( ) )
102 {
103 listUserId.add( daoUtil.getString( 1 ) );
104 }
105
106 }
107
108 return listUserId;
109 }
110
111
112
113
114 @Override
115 public void store( String strUserId, String strKey, String strValue )
116 {
117 String strSQL = _strSqlInsert;
118
119 if ( existsKey( strUserId, strKey ) )
120 {
121 strSQL = _strSqlUpdate;
122 }
123
124 try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( strSQL ) )
125 {
126
127 daoUtil.setString( 1, strValue );
128 daoUtil.setString( 2, strUserId );
129 daoUtil.setString( 3, strKey );
130
131 daoUtil.executeUpdate( );
132 }
133 }
134
135
136
137
138 @Override
139 public List<String> keys( String strUserId )
140 {
141 List<String> list = new ArrayList<>( );
142 try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( _strSqlSelectAll ) )
143 {
144 daoUtil.setString( 1, strUserId );
145 daoUtil.executeQuery( );
146
147 while ( daoUtil.next( ) )
148 {
149 list.add( daoUtil.getString( 1 ) );
150 }
151
152 }
153
154 return list;
155 }
156
157
158
159
160 @Override
161 public void remove( String strUserId )
162 {
163 try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( _strSqlDelete ) )
164 {
165 daoUtil.setString( 1, strUserId );
166 daoUtil.executeUpdate( );
167 }
168 }
169
170
171
172
173 @Override
174 public void removeKey( String strUserId, String strKey )
175 {
176 try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( _strSqlDeleteKey ) )
177 {
178 daoUtil.setString( 1, strUserId );
179 daoUtil.setString( 2, strKey );
180 daoUtil.executeUpdate( );
181 }
182 }
183
184
185
186
187 @Override
188 public void removeKeyPrefix( String strUserId, String strKeyPrefix )
189 {
190 try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( _strSqlDeleteKeyPrefix ) )
191 {
192 daoUtil.setString( 1, strUserId );
193 daoUtil.setString( 2, "%" + strKeyPrefix );
194 daoUtil.executeUpdate( );
195 }
196 }
197
198
199
200
201 @Override
202 public boolean existsKey( String strUserId, String strKey )
203 {
204 int nValue = 0;
205 try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( _strSqlSelectCount ) )
206 {
207 daoUtil.setString( 1, strUserId );
208 daoUtil.setString( 2, strKey );
209 daoUtil.executeQuery( );
210
211 if ( daoUtil.next( ) )
212 {
213 nValue = ( daoUtil.getInt( 1 ) );
214 }
215
216 }
217
218 return ( nValue != 0 );
219 }
220
221
222
223
224 @Override
225 public boolean existsValueForKey( String strKey, String strValue )
226 {
227 int nValue = 0;
228 try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( _strSqlSelectCountPrefValue ) )
229 {
230 daoUtil.setString( 1, strKey );
231 daoUtil.setString( 2, strValue );
232 daoUtil.executeQuery( );
233
234 if ( daoUtil.next( ) )
235 {
236 nValue = ( daoUtil.getInt( 1 ) );
237 }
238
239 }
240
241 return ( nValue != 0 );
242 }
243 }