View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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  /**
44   * Abstract User Preferences Service
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       * @param dao The DAO
64       */
65      public void setDao( IPreferencesDAO dao )
66      {
67          _dao = dao;
68      }
69  
70      /**
71       * {@inheritDoc }
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       * {@inheritDoc }
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       * {@inheritDoc }
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      * {@inheritDoc }
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      * {@inheritDoc }
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      * {@inheritDoc }
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      * {@inheritDoc }
140      */
141     @Override
142     public List<String> keys( String strUserId )
143     {
144         return _dao.keys( strUserId );
145     }
146 
147     /**
148      * {@inheritDoc }
149      */
150     @Override
151     public void clear( String strUserId )
152     {
153         _dao.remove( strUserId );
154         _cache.removeCacheValuesOfUser( strUserId );
155     }
156 
157     /**
158      * {@inheritDoc }
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      * {@inheritDoc }
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      * {@inheritDoc }
179      */
180     @Override
181     public boolean existsKey( String strUserId, String strKey )
182     {
183         return _dao.existsKey( strUserId, strKey );
184     }
185 
186     /**
187      * {@inheritDoc }
188      */
189     @Override
190     public List<String> getUsers( String strKey, String strValue )
191     {
192         return _dao.getUserId( strKey, strValue );
193     }
194 
195     /**
196      * {@inheritDoc}
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 }