DataEntityDAO.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.datastore;

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

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

  38. /**
  39.  * This class provides Data Access methods for Entity objects
  40.  */
  41. public final class DataEntityDAO implements IDataEntityDAO
  42. {
  43.     // Constants
  44.     private static final String SQL_QUERY_SELECT = "SELECT entity_key, entity_value FROM core_datastore WHERE entity_key = ?";
  45.     private static final String SQL_QUERY_INSERT = "INSERT INTO core_datastore ( entity_key, entity_value ) VALUES ( ?, ? ) ";
  46.     private static final String SQL_QUERY_DELETE = "DELETE FROM core_datastore WHERE entity_key = ? ";
  47.     private static final String SQL_QUERY_UPDATE = "UPDATE core_datastore SET entity_value = ? WHERE entity_key = ?";
  48.     private static final String SQL_QUERY_SELECTALL = "SELECT entity_key, entity_value FROM core_datastore";
  49.     private static final String SQL_QUERY_SELECTBYPREFIX = "SELECT entity_key, entity_value FROM core_datastore WHERE entity_key LIKE ?";

  50.     /**
  51.      * Insert a new record in the table.
  52.      *
  53.      * @param entity
  54.      *            instance of the Entity object to insert
  55.      */
  56.     @Override
  57.     public void insert( DataEntity entity )
  58.     {
  59.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT ) )
  60.         {

  61.             daoUtil.setString( 1, entity.getKey( ) );
  62.             daoUtil.setString( 2, entity.getValue( ) );

  63.             daoUtil.executeUpdate( );
  64.         }
  65.     }

  66.     /**
  67.      * Load the data of the entity from the table
  68.      *
  69.      * @param strKey
  70.      *            The identifier of the entity
  71.      * @return the instance of the Entity
  72.      */
  73.     @Override
  74.     public DataEntity load( String strKey )
  75.     {
  76.         DataEntity entity = null;
  77.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
  78.         {
  79.             daoUtil.setString( 1, strKey );
  80.             daoUtil.executeQuery( );

  81.             if ( daoUtil.next( ) )
  82.             {
  83.                 entity = new DataEntity( );

  84.                 entity.setKey( daoUtil.getString( 1 ) );
  85.                 entity.setValue( daoUtil.getString( 2 ) );
  86.             }

  87.         }

  88.         return entity;
  89.     }

  90.     /**
  91.      * Delete a record from the table
  92.      *
  93.      * @param strKey
  94.      *            The identifier of the entity
  95.      */
  96.     @Override
  97.     public void delete( String strKey )
  98.     {
  99.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
  100.         {
  101.             daoUtil.setString( 1, strKey );
  102.             daoUtil.executeUpdate( );
  103.         }
  104.     }

  105.     /**
  106.      * Update the record in the table
  107.      *
  108.      * @param entity
  109.      *            The reference of the entity
  110.      */
  111.     @Override
  112.     public void store( DataEntity entity )
  113.     {
  114.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
  115.         {

  116.             daoUtil.setString( 1, entity.getValue( ) );
  117.             daoUtil.setString( 2, entity.getKey( ) );

  118.             daoUtil.executeUpdate( );
  119.         }
  120.     }

  121.     /**
  122.      * Load the data of all the entitys and returns them as a List
  123.      *
  124.      * @return The List which contains the data of all the entitys
  125.      */
  126.     @Override
  127.     public List<DataEntity> selectEntitiesList( )
  128.     {
  129.         List<DataEntity> entityList = new ArrayList<>( );
  130.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) )
  131.         {
  132.             daoUtil.executeQuery( );

  133.             while ( daoUtil.next( ) )
  134.             {
  135.                 DataEntity entity = new DataEntity( );

  136.                 entity.setKey( daoUtil.getString( 1 ) );
  137.                 entity.setValue( daoUtil.getString( 2 ) );

  138.                 entityList.add( entity );
  139.             }

  140.         }

  141.         return entityList;
  142.     }

  143.     @Override
  144.     public List<DataEntity> selectEntitiesByPrefix( String strPrefix )
  145.     {
  146.         List<DataEntity> entityList = new ArrayList<>( );
  147.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTBYPREFIX ) )
  148.         {
  149.             daoUtil.setString( 1, strPrefix + "%" );
  150.             daoUtil.executeQuery( );

  151.             while ( daoUtil.next( ) )
  152.             {
  153.                 DataEntity entity = new DataEntity( );

  154.                 entity.setKey( daoUtil.getString( 1 ) );
  155.                 entity.setValue( daoUtil.getString( 2 ) );

  156.                 entityList.add( entity );
  157.             }

  158.         }
  159.         return entityList;
  160.     }
  161. }