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.service.prefs;
35
36 import fr.paris.lutece.portal.business.prefs.IPreferencesDAO;
37
38 import org.springframework.beans.factory.InitializingBean;
39
40 import java.util.List;
41
42
43
44
45
46
47 public class BaseUserPreferencesServiceImpl implements IUserPreferencesService, InitializingBean
48 {
49 private static final String TRUE = "true";
50 private static final String FALSE = "false";
51 private static BaseUserPreferencesCacheService _cache;
52 private IPreferencesDAO _dao;
53
54
55
56
57 protected BaseUserPreferencesServiceImpl( )
58 {
59 }
60
61
62
63
64
65 public void setDao( IPreferencesDAO dao )
66 {
67 _dao = dao;
68 }
69
70
71
72
73 @Override
74 public String get( String strUserId, String strKey, String strDefault )
75 {
76 String strCacheKey = _cache.getCacheKey( strUserId, strKey );
77 String strValue = (String) _cache.getFromCache( strCacheKey );
78
79 if ( strValue == null )
80 {
81 strValue = _dao.load( strUserId, strKey, strDefault );
82 _cache.putInCache( strCacheKey, strValue );
83 }
84
85 return strValue;
86 }
87
88
89
90
91 @Override
92 public int getInt( String strUserId, String strKey, int nDefault )
93 {
94 return Integer.parseInt( get( strUserId, strKey, String.valueOf( nDefault ) ) );
95 }
96
97
98
99
100 @Override
101 public boolean getBoolean( String strUserId, String strKey, boolean bDefault )
102 {
103 String strDefault = bDefault ? TRUE : FALSE;
104 String strValue = get( strUserId, strKey, strDefault );
105
106 return strValue.equals( TRUE );
107 }
108
109
110
111
112 @Override
113 public void put( String strUserId, String strKey, String strValue )
114 {
115 _dao.store( strUserId, strKey, strValue );
116 _cache.putInCache( _cache.getCacheKey( strUserId, strKey ), strValue );
117 }
118
119
120
121
122 @Override
123 public void putInt( String strUserId, String strKey, int nValue )
124 {
125 put( strUserId, strKey, String.valueOf( nValue ) );
126 }
127
128
129
130
131 @Override
132 public void putBoolean( String strUserId, String strKey, boolean bValue )
133 {
134 String strValue = bValue ? TRUE : FALSE;
135 put( strUserId, strKey, strValue );
136 }
137
138
139
140
141 @Override
142 public List<String> keys( String strUserId )
143 {
144 return _dao.keys( strUserId );
145 }
146
147
148
149
150 @Override
151 public void clear( String strUserId )
152 {
153 _dao.remove( strUserId );
154 _cache.removeCacheValuesOfUser( strUserId );
155 }
156
157
158
159
160 @Override
161 public void clearKey( String strUserId, String strKey )
162 {
163 _dao.removeKey( strUserId, strKey );
164 _cache.removeCacheValuesOfUser( strUserId );
165 }
166
167
168
169
170 @Override
171 public void clearKeyPrefix( String strUserId, String strPrefix )
172 {
173 _dao.removeKeyPrefix( strUserId, strPrefix );
174 _cache.removeCacheValuesOfUser( strUserId );
175 }
176
177
178
179
180 @Override
181 public boolean existsKey( String strUserId, String strKey )
182 {
183 return _dao.existsKey( strUserId, strKey );
184 }
185
186
187
188
189 @Override
190 public List<String> getUsers( String strKey, String strValue )
191 {
192 return _dao.getUserId( strKey, strValue );
193 }
194
195
196
197
198 @Override
199 public void afterPropertiesSet( ) throws Exception
200 {
201 synchronized ( BaseUserPreferencesServiceImpl.class )
202 {
203 if ( _cache == null )
204 {
205 _cache = new BaseUserPreferencesCacheService( );
206 _cache.initCache( );
207 }
208 }
209 }
210
211 @Override
212 public boolean existsValueForKey(String strKey, String strValue) {
213
214 return _dao.existsValueForKey(strKey, strValue);
215 }
216 }