IndexerActionDAO.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.indexeraction;

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

  36. import java.sql.Statement;
  37. import java.util.ArrayList;
  38. import java.util.List;

  39. /**
  40.  * This class provides Data Access methods for Indexer Action objects
  41.  */
  42. public final class IndexerActionDAO implements IIndexerActionDAO
  43. {
  44.     // Constants
  45.     /**
  46.      * SQL Where constant
  47.      */
  48.     public static final String CONSTANT_WHERE = " WHERE ";

  49.     /**
  50.      * SQL And constant
  51.      */
  52.     public static final String CONSTANT_AND = " AND ";
  53.     private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_action,id_document,id_task,indexer_name, id_portlet"
  54.             + " FROM core_indexer_action WHERE id_action = ?";
  55.     private static final String SQL_QUERY_INSERT = "INSERT INTO core_indexer_action( id_document,id_task ,indexer_name,id_portlet)" + " VALUES(?,?,?,?)";
  56.     private static final String SQL_QUERY_DELETE = "DELETE FROM core_indexer_action WHERE id_action = ? ";
  57.     private static final String SQL_QUERY_DELETE_ALL = "DELETE FROM core_indexer_action";
  58.     private static final String SQL_QUERY_UPDATE = "UPDATE core_indexer_action SET id_action=?,id_document=?,id_task=?,indexer_name=?,id_portlet=? WHERE id_action = ? ";
  59.     private static final String SQL_QUERY_SELECT = "SELECT id_action,id_document,id_task,indexer_name,id_portlet" + " FROM core_indexer_action  ";
  60.     private static final String SQL_FILTER_ID_TASK = " id_task = ? ";

  61.     /**
  62.      * {@inheritDoc}
  63.      */
  64.     @Override
  65.     public void insert( IndexerAction indexerAction )
  66.     {
  67.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS ) )
  68.         {
  69.             int nIndex = 1;
  70.             daoUtil.setString( nIndex++, indexerAction.getIdDocument( ) );
  71.             daoUtil.setInt( nIndex++, indexerAction.getIdTask( ) );
  72.             daoUtil.setString( nIndex++, indexerAction.getIndexerName( ) );
  73.             daoUtil.setInt( nIndex, indexerAction.getIdPortlet( ) );

  74.             daoUtil.executeUpdate( );

  75.             if ( daoUtil.nextGeneratedKey( ) )
  76.             {
  77.                 indexerAction.setIdAction( daoUtil.getGeneratedKeyInt( 1 ) );
  78.             }
  79.         }
  80.     }

  81.     /**
  82.      * {@inheritDoc}
  83.      */
  84.     @Override
  85.     public IndexerAction load( int nId )
  86.     {
  87.         IndexerAction indexerAction = null;

  88.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY ) )
  89.         {
  90.             daoUtil.setInt( 1, nId );
  91.             daoUtil.executeQuery( );

  92.             if ( daoUtil.next( ) )
  93.             {
  94.                 indexerAction = new IndexerAction( );
  95.                 indexerAction.setIdAction( daoUtil.getInt( 1 ) );
  96.                 indexerAction.setIdDocument( daoUtil.getString( 2 ) );
  97.                 indexerAction.setIdTask( daoUtil.getInt( 3 ) );
  98.                 indexerAction.setIndexerName( daoUtil.getString( 4 ) );
  99.                 indexerAction.setIdPortlet( daoUtil.getInt( 5 ) );
  100.             }

  101.         }

  102.         return indexerAction;
  103.     }

  104.     /**
  105.      * {@inheritDoc}
  106.      */
  107.     @Override
  108.     public void delete( int nId )
  109.     {
  110.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
  111.         {
  112.             daoUtil.setInt( 1, nId );
  113.             daoUtil.executeUpdate( );
  114.         }
  115.     }

  116.     /**
  117.      * {@inheritDoc}
  118.      */
  119.     @Override
  120.     public void deleteAll( )
  121.     {
  122.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_ALL ) )
  123.         {
  124.             daoUtil.executeUpdate( );
  125.         }
  126.     }

  127.     /**
  128.      * {@inheritDoc}
  129.      */
  130.     @Override
  131.     public void store( IndexerAction indexerAction )
  132.     {
  133.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
  134.         {
  135.             daoUtil.setInt( 1, indexerAction.getIdAction( ) );
  136.             daoUtil.setString( 2, indexerAction.getIdDocument( ) );
  137.             daoUtil.setInt( 3, indexerAction.getIdTask( ) );
  138.             daoUtil.setString( 4, indexerAction.getIndexerName( ) );
  139.             daoUtil.setInt( 5, indexerAction.getIdPortlet( ) );

  140.             daoUtil.executeUpdate( );
  141.         }
  142.     }

  143.     /**
  144.      * {@inheritDoc}
  145.      */
  146.     @Override
  147.     public List<IndexerAction> selectList( IndexerActionFilter filter )
  148.     {
  149.         List<IndexerAction> indexerActionList = new ArrayList<>( );
  150.         IndexerAction indexerAction = null;
  151.         List<String> listStrFilter = new ArrayList<>( );

  152.         if ( filter.containsIdTask( ) )
  153.         {
  154.             listStrFilter.add( SQL_FILTER_ID_TASK );
  155.         }

  156.         String strSQL = buildRequestWithFilter( SQL_QUERY_SELECT, listStrFilter, null );

  157.         try ( DAOUtil daoUtil = new DAOUtil( strSQL ) )
  158.         {

  159.             int nIndex = 1;

  160.             if ( filter.containsIdTask( ) )
  161.             {
  162.                 daoUtil.setInt( nIndex, filter.getIdTask( ) );
  163.             }

  164.             daoUtil.executeQuery( );

  165.             while ( daoUtil.next( ) )
  166.             {
  167.                 indexerAction = new IndexerAction( );
  168.                 indexerAction.setIdAction( daoUtil.getInt( 1 ) );
  169.                 indexerAction.setIdDocument( daoUtil.getString( 2 ) );
  170.                 indexerAction.setIdTask( daoUtil.getInt( 3 ) );
  171.                 indexerAction.setIndexerName( daoUtil.getString( 4 ) );
  172.                 indexerAction.setIdPortlet( daoUtil.getInt( 5 ) );
  173.                 indexerActionList.add( indexerAction );
  174.             }

  175.         }

  176.         return indexerActionList;
  177.     }

  178.     /**
  179.      * {@inheritDoc}
  180.      */
  181.     @Override
  182.     public List<IndexerAction> selectList( )
  183.     {
  184.         List<IndexerAction> indexerActionList = new ArrayList<>( );
  185.         IndexerAction indexerAction = null;

  186.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
  187.         {

  188.             daoUtil.executeQuery( );

  189.             while ( daoUtil.next( ) )
  190.             {
  191.                 indexerAction = new IndexerAction( );
  192.                 indexerAction.setIdAction( daoUtil.getInt( 1 ) );
  193.                 indexerAction.setIdDocument( daoUtil.getString( 2 ) );
  194.                 indexerAction.setIdTask( daoUtil.getInt( 3 ) );
  195.                 indexerAction.setIndexerName( daoUtil.getString( 4 ) );
  196.                 indexerAction.setIdPortlet( daoUtil.getInt( 5 ) );
  197.                 indexerActionList.add( indexerAction );
  198.             }

  199.         }

  200.         return indexerActionList;
  201.     }

  202.     /**
  203.      * Builds a query with filters placed in parameters
  204.      *
  205.      * @param strSelect
  206.      *            the select of the query
  207.      * @param listStrFilter
  208.      *            the list of filter to add in the query
  209.      * @param strOrder
  210.      *            the order by of the query
  211.      * @return a query
  212.      */
  213.     public static String buildRequestWithFilter( String strSelect, List<String> listStrFilter, String strOrder )
  214.     {
  215.         StringBuilder strBuffer = new StringBuilder( );
  216.         strBuffer.append( strSelect );

  217.         int nCount = 0;

  218.         for ( String strFilter : listStrFilter )
  219.         {
  220.             if ( ++nCount == 1 )
  221.             {
  222.                 strBuffer.append( CONSTANT_WHERE );
  223.             }

  224.             strBuffer.append( strFilter );

  225.             if ( nCount != listStrFilter.size( ) )
  226.             {
  227.                 strBuffer.append( CONSTANT_AND );
  228.             }
  229.         }

  230.         if ( strOrder != null )
  231.         {
  232.             strBuffer.append( strOrder );
  233.         }

  234.         return strBuffer.toString( );
  235.     }
  236. }