BaseUserPreferencesServiceImpl.java

  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. import fr.paris.lutece.portal.business.prefs.IPreferencesDAO;

  36. import org.springframework.beans.factory.InitializingBean;

  37. import java.util.List;

  38. /**
  39.  * Abstract User Preferences Service
  40.  *
  41.  * @since 4.0
  42.  */
  43. public class BaseUserPreferencesServiceImpl implements IUserPreferencesService, InitializingBean
  44. {
  45.     private static final String TRUE = "true";
  46.     private static final String FALSE = "false";
  47.     private static BaseUserPreferencesCacheService _cache;
  48.     private IPreferencesDAO _dao;

  49.     /**
  50.      * Constructor
  51.      */
  52.     protected BaseUserPreferencesServiceImpl( )
  53.     {
  54.     }

  55.     /**
  56.      * Sets the DAO
  57.      *
  58.      * @param dao
  59.      *            The DAO
  60.      */
  61.     public void setDao( IPreferencesDAO dao )
  62.     {
  63.         _dao = dao;
  64.     }

  65.     /**
  66.      * {@inheritDoc }
  67.      */
  68.     @Override
  69.     public String get( String strUserId, String strKey, String strDefault )
  70.     {
  71.         String strCacheKey = _cache.getCacheKey( strUserId, strKey );
  72.         String strValue = (String) _cache.getFromCache( strCacheKey );

  73.         if ( strValue == null )
  74.         {
  75.             strValue = _dao.load( strUserId, strKey, strDefault );
  76.             _cache.putInCache( strCacheKey, strValue );
  77.         }

  78.         return strValue;
  79.     }

  80.     /**
  81.      * {@inheritDoc }
  82.      */
  83.     @Override
  84.     public int getInt( String strUserId, String strKey, int nDefault )
  85.     {
  86.         return Integer.parseInt( get( strUserId, strKey, String.valueOf( nDefault ) ) );
  87.     }

  88.     /**
  89.      * {@inheritDoc }
  90.      */
  91.     @Override
  92.     public boolean getBoolean( String strUserId, String strKey, boolean bDefault )
  93.     {
  94.         String strDefault = bDefault ? TRUE : FALSE;
  95.         String strValue = get( strUserId, strKey, strDefault );

  96.         return strValue.equals( TRUE );
  97.     }

  98.     /**
  99.      * {@inheritDoc }
  100.      */
  101.     @Override
  102.     public void put( String strUserId, String strKey, String strValue )
  103.     {
  104.         _dao.store( strUserId, strKey, strValue );
  105.         _cache.putInCache( _cache.getCacheKey( strUserId, strKey ), strValue );
  106.     }

  107.     /**
  108.      * {@inheritDoc }
  109.      */
  110.     @Override
  111.     public void putInt( String strUserId, String strKey, int nValue )
  112.     {
  113.         put( strUserId, strKey, String.valueOf( nValue ) );
  114.     }

  115.     /**
  116.      * {@inheritDoc }
  117.      */
  118.     @Override
  119.     public void putBoolean( String strUserId, String strKey, boolean bValue )
  120.     {
  121.         String strValue = bValue ? TRUE : FALSE;
  122.         put( strUserId, strKey, strValue );
  123.     }

  124.     /**
  125.      * {@inheritDoc }
  126.      */
  127.     @Override
  128.     public List<String> keys( String strUserId )
  129.     {
  130.         return _dao.keys( strUserId );
  131.     }

  132.     /**
  133.      * {@inheritDoc }
  134.      */
  135.     @Override
  136.     public void clear( String strUserId )
  137.     {
  138.         _dao.remove( strUserId );
  139.         _cache.removeCacheValuesOfUser( strUserId );
  140.     }

  141.     /**
  142.      * {@inheritDoc }
  143.      */
  144.     @Override
  145.     public void clearKey( String strUserId, String strKey )
  146.     {
  147.         _dao.removeKey( strUserId, strKey );
  148.         _cache.removeCacheValuesOfUser( strUserId );
  149.     }

  150.     /**
  151.      * {@inheritDoc }
  152.      */
  153.     @Override
  154.     public void clearKeyPrefix( String strUserId, String strPrefix )
  155.     {
  156.         _dao.removeKeyPrefix( strUserId, strPrefix );
  157.         _cache.removeCacheValuesOfUser( strUserId );
  158.     }

  159.     /**
  160.      * {@inheritDoc }
  161.      */
  162.     @Override
  163.     public boolean existsKey( String strUserId, String strKey )
  164.     {
  165.         return _dao.existsKey( strUserId, strKey );
  166.     }

  167.     /**
  168.      * {@inheritDoc }
  169.      */
  170.     @Override
  171.     public List<String> getUsers( String strKey, String strValue )
  172.     {
  173.         return _dao.getUserId( strKey, strValue );
  174.     }

  175.     /**
  176.      * {@inheritDoc}
  177.      */
  178.     @Override
  179.     public void afterPropertiesSet( ) throws Exception
  180.     {
  181.         synchronized( BaseUserPreferencesServiceImpl.class )
  182.         {
  183.             if ( _cache == null )
  184.             {
  185.                 _cache = new BaseUserPreferencesCacheService( );
  186.                 _cache.initCache( );
  187.             }
  188.         }
  189.     }

  190.     @Override
  191.     public boolean existsValueForKey( String strKey, String strValue )
  192.     {

  193.         return _dao.existsValueForKey( strKey, strValue );
  194.     }
  195. }