AbstractUserPreferencesDAO.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.business.prefs;

  35. import fr.paris.lutece.util.sql.DAOUtil;

  36. import java.util.ArrayList;
  37. import java.util.List;

  38. /**
  39.  * User Preferences DAO
  40.  */
  41. public abstract class AbstractUserPreferencesDAO implements IPreferencesDAO
  42. {
  43.     private static final String SQL_COUNT = "SELECT COUNT(*) FROM ";
  44.     private final String _strSqlSelect = "SELECT pref_value FROM " + getPreferencesTable( ) + " WHERE id_user = ? AND pref_key = ?";
  45.     private final String _strSqlInsert = "INSERT INTO " + getPreferencesTable( ) + " ( pref_value , id_user, pref_key ) VALUES ( ?, ?, ? ) ";
  46.     private final String _strSqlUpdate = "UPDATE " + getPreferencesTable( ) + " SET pref_value = ? WHERE id_user = ? AND pref_key = ?";
  47.     private final String _strSqlDelete = "DELETE FROM " + getPreferencesTable( ) + " WHERE id_user = ? ";
  48.     private final String _strSqlSelectAll = "SELECT pref_key FROM " + getPreferencesTable( ) + " WHERE id_user = ?";
  49.     private final String _strSqlSelectByValue = "SELECT id_user FROM " + getPreferencesTable( ) + " WHERE pref_key = ? AND pref_value = ? ";
  50.     private final String _strSqlDeleteKey = _strSqlDelete + " AND pref_key = ? ";
  51.     private final String _strSqlDeleteKeyPrefix = _strSqlDelete + " AND pref_key LIKE ? ";
  52.     private final String _strSqlSelectCount = SQL_COUNT + getPreferencesTable( ) + " WHERE id_user = ? AND pref_key = ?";
  53.     private final String _strSqlSelectCountPrefValue = SQL_COUNT + getPreferencesTable( ) + " WHERE pref_key = ? AND pref_value = ?";

  54.     /**
  55.      * Gets the preferences table
  56.      *
  57.      * @return The table name that stores preferences
  58.      */
  59.     abstract String getPreferencesTable( );

  60.     /**
  61.      * {@inheritDoc }
  62.      */
  63.     @Override
  64.     public String load( String strUserId, String strKey, String strDefault )
  65.     {
  66.         String strValue = strDefault;
  67.         try ( DAOUtil daoUtil = new DAOUtil( _strSqlSelect ) )
  68.         {
  69.             daoUtil.setString( 1, strUserId );
  70.             daoUtil.setString( 2, strKey );
  71.             daoUtil.executeQuery( );

  72.             if ( daoUtil.next( ) )
  73.             {
  74.                 strValue = ( daoUtil.getString( 1 ) );
  75.             }

  76.         }

  77.         return strValue;
  78.     }

  79.     /**
  80.      * {@inheritDoc }
  81.      */
  82.     @Override
  83.     public List<String> getUserId( String strKey, String strValue )
  84.     {
  85.         List<String> listUserId = new ArrayList<>( );
  86.         try ( DAOUtil daoUtil = new DAOUtil( _strSqlSelectByValue ) )
  87.         {
  88.             daoUtil.setString( 1, strKey );
  89.             daoUtil.setString( 2, strValue );
  90.             daoUtil.executeQuery( );

  91.             while ( daoUtil.next( ) )
  92.             {
  93.                 listUserId.add( daoUtil.getString( 1 ) );
  94.             }

  95.         }

  96.         return listUserId;
  97.     }

  98.     /**
  99.      * {@inheritDoc }
  100.      */
  101.     @Override
  102.     public void store( String strUserId, String strKey, String strValue )
  103.     {
  104.         String strSQL = _strSqlInsert;

  105.         if ( existsKey( strUserId, strKey ) )
  106.         {
  107.             strSQL = _strSqlUpdate;
  108.         }

  109.         try ( DAOUtil daoUtil = new DAOUtil( strSQL ) )
  110.         {

  111.             daoUtil.setString( 1, strValue );
  112.             daoUtil.setString( 2, strUserId );
  113.             daoUtil.setString( 3, strKey );

  114.             daoUtil.executeUpdate( );
  115.         }
  116.     }

  117.     /**
  118.      * {@inheritDoc }
  119.      */
  120.     @Override
  121.     public List<String> keys( String strUserId )
  122.     {
  123.         List<String> list = new ArrayList<>( );
  124.         try ( DAOUtil daoUtil = new DAOUtil( _strSqlSelectAll ) )
  125.         {
  126.             daoUtil.setString( 1, strUserId );
  127.             daoUtil.executeQuery( );

  128.             while ( daoUtil.next( ) )
  129.             {
  130.                 list.add( daoUtil.getString( 1 ) );
  131.             }

  132.         }

  133.         return list;
  134.     }

  135.     /**
  136.      * {@inheritDoc }
  137.      */
  138.     @Override
  139.     public void remove( String strUserId )
  140.     {
  141.         try ( DAOUtil daoUtil = new DAOUtil( _strSqlDelete ) )
  142.         {
  143.             daoUtil.setString( 1, strUserId );
  144.             daoUtil.executeUpdate( );
  145.         }
  146.     }

  147.     /**
  148.      * {@inheritDoc }
  149.      */
  150.     @Override
  151.     public void removeKey( String strUserId, String strKey )
  152.     {
  153.         try ( DAOUtil daoUtil = new DAOUtil( _strSqlDeleteKey ) )
  154.         {
  155.             daoUtil.setString( 1, strUserId );
  156.             daoUtil.setString( 2, strKey );
  157.             daoUtil.executeUpdate( );
  158.         }
  159.     }

  160.     /**
  161.      * {@inheritDoc }
  162.      */
  163.     @Override
  164.     public void removeKeyPrefix( String strUserId, String strKeyPrefix )
  165.     {
  166.         try ( DAOUtil daoUtil = new DAOUtil( _strSqlDeleteKeyPrefix ) )
  167.         {
  168.             daoUtil.setString( 1, strUserId );
  169.             daoUtil.setString( 2, "%" + strKeyPrefix );
  170.             daoUtil.executeUpdate( );
  171.         }
  172.     }

  173.     /**
  174.      * {@inheritDoc }
  175.      */
  176.     @Override
  177.     public boolean existsKey( String strUserId, String strKey )
  178.     {
  179.         int nValue = 0;
  180.         try ( DAOUtil daoUtil = new DAOUtil( _strSqlSelectCount ) )
  181.         {
  182.             daoUtil.setString( 1, strUserId );
  183.             daoUtil.setString( 2, strKey );
  184.             daoUtil.executeQuery( );

  185.             if ( daoUtil.next( ) )
  186.             {
  187.                 nValue = ( daoUtil.getInt( 1 ) );
  188.             }

  189.         }

  190.         return ( nValue != 0 );
  191.     }

  192.     /**
  193.      * {@inheritDoc }
  194.      */
  195.     @Override
  196.     public boolean existsValueForKey( String strKey, String strValue )
  197.     {
  198.         int nValue = 0;
  199.         try ( DAOUtil daoUtil = new DAOUtil( _strSqlSelectCountPrefValue ) )
  200.         {
  201.             daoUtil.setString( 1, strKey );
  202.             daoUtil.setString( 2, strValue );
  203.             daoUtil.executeQuery( );

  204.             if ( daoUtil.next( ) )
  205.             {
  206.                 nValue = ( daoUtil.getInt( 1 ) );
  207.             }

  208.         }

  209.         return ( nValue != 0 );
  210.     }
  211. }