View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * Abstract User Preferences Service
44   * 
45   * @since 4.0
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       * Constructor
56       */
57      protected BaseUserPreferencesServiceImpl( )
58      {
59      }
60  
61      /**
62       * Sets the DAO
63       * 
64       * @param dao
65       *            The DAO
66       */
67      public void setDao( IPreferencesDAO dao )
68      {
69          _dao = dao;
70      }
71  
72      /**
73       * {@inheritDoc }
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       * {@inheritDoc }
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      * {@inheritDoc }
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      * {@inheritDoc }
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      * {@inheritDoc }
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      * {@inheritDoc }
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      * {@inheritDoc }
142      */
143     @Override
144     public List<String> keys( String strUserId )
145     {
146         return _dao.keys( strUserId );
147     }
148 
149     /**
150      * {@inheritDoc }
151      */
152     @Override
153     public void clear( String strUserId )
154     {
155         _dao.remove( strUserId );
156         _cache.removeCacheValuesOfUser( strUserId );
157     }
158 
159     /**
160      * {@inheritDoc }
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      * {@inheritDoc }
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      * {@inheritDoc }
181      */
182     @Override
183     public boolean existsKey( String strUserId, String strKey )
184     {
185         return _dao.existsKey( strUserId, strKey );
186     }
187 
188     /**
189      * {@inheritDoc }
190      */
191     @Override
192     public List<String> getUsers( String strKey, String strValue )
193     {
194         return _dao.getUserId( strKey, strValue );
195     }
196 
197     /**
198      * {@inheritDoc}
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 }