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