FeatureGroupDAO.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.right;

  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 feature group objects
  40.  */
  41. public final class FeatureGroupDAO implements IFeatureGroupDAO
  42. {
  43.     // Constants
  44.     private static final String SQL_SELECT = " SELECT id_feature_group, feature_group_description, feature_group_label, feature_group_order, feature_group_icon";
  45.     private static final String SQL_QUERY_SELECT = SQL_SELECT + " FROM core_feature_group " + " WHERE id_feature_group = ? ";
  46.     private static final String SQL_QUERY_INSERT = " INSERT INTO core_feature_group ( id_feature_group , feature_group_description, feature_group_label, feature_group_order, feature_group_icon ) "
  47.             + " VALUES ( ?, ?, ?, ?, ?  )";
  48.     private static final String SQL_QUERY_DELETE = " DELETE FROM core_feature_group WHERE id_feature_group = ? ";
  49.     private static final String SQL_QUERY_UPDATE = " UPDATE core_feature_group SET feature_group_description = ?, "
  50.             + " feature_group_label = ? , feature_group_order = ?, feature_group_icon = ? " + " WHERE id_feature_group = ?";
  51.     private static final String SQL_QUERY_SELECTALL = SQL_SELECT + " FROM core_feature_group ORDER BY feature_group_order ASC";
  52.     private static final String SQL_QUERY_COUNT_FEATUREGROUP = " SELECT COUNT(id_feature_group)FROM core_feature_group";

  53.     // /////////////////////////////////////////////////////////////////////////////////////
  54.     // Access methods to data

  55.     /**
  56.      * Insert a new record in the table.
  57.      *
  58.      * @param featureGroup
  59.      *            instance of the feature group to insert
  60.      */
  61.     public void insert( FeatureGroup featureGroup )
  62.     {
  63.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT ) )
  64.         {

  65.             daoUtil.setString( 1, featureGroup.getId( ) );
  66.             daoUtil.setString( 2, featureGroup.getDescriptionKey( ) );
  67.             daoUtil.setString( 3, featureGroup.getLabelKey( ) );
  68.             daoUtil.setInt( 4, featureGroup.getOrder( ) );
  69.             daoUtil.setString( 5, featureGroup.getIcon( ) );

  70.             daoUtil.executeUpdate( );
  71.         }
  72.     }

  73.     /**
  74.      * load the data of FeatureGroup from the table
  75.      *
  76.      * @param strIdFeatureGroup
  77.      *            The indentifier of the object FeatureGroup
  78.      * @return The Instance of the object Page
  79.      */
  80.     public FeatureGroup load( String strIdFeatureGroup )
  81.     {
  82.         FeatureGroup featureGroup = null;
  83.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
  84.         {
  85.             daoUtil.setString( 1, strIdFeatureGroup );

  86.             daoUtil.executeQuery( );

  87.             if ( daoUtil.next( ) )
  88.             {
  89.                 featureGroup = new FeatureGroup( );
  90.                 featureGroup.setId( daoUtil.getString( 1 ) );
  91.                 featureGroup.setDescriptionKey( daoUtil.getString( 2 ) );
  92.                 featureGroup.setLabelKey( daoUtil.getString( 3 ) );
  93.                 featureGroup.setOrder( daoUtil.getInt( 4 ) );
  94.                 featureGroup.setIcon( daoUtil.getString( 5 ) );
  95.             }

  96.         }

  97.         return featureGroup;
  98.     }

  99.     /**
  100.      * Delete a record from the table
  101.      *
  102.      * @param strIdFeatureGroup
  103.      *            The indentifier of the object FeatureGroup
  104.      */
  105.     public void delete( String strIdFeatureGroup )
  106.     {
  107.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
  108.         {
  109.             daoUtil.setString( 1, strIdFeatureGroup );
  110.             daoUtil.executeUpdate( );
  111.         }
  112.     }

  113.     /**
  114.      * Update the record in the table
  115.      *
  116.      * @param featureGroup
  117.      *            the reference of the feature group
  118.      */
  119.     public void store( FeatureGroup featureGroup )
  120.     {
  121.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
  122.         {

  123.             daoUtil.setString( 1, featureGroup.getDescriptionKey( ) );
  124.             daoUtil.setString( 2, featureGroup.getLabelKey( ) );
  125.             daoUtil.setInt( 3, featureGroup.getOrder( ) );
  126.             daoUtil.setString( 4, featureGroup.getIcon( ) );
  127.             daoUtil.setString( 5, featureGroup.getId( ) );

  128.             daoUtil.executeUpdate( );
  129.         }
  130.     }

  131.     /**
  132.      * Loads the data of all the feature groups and returns them in form of a collection
  133.      *
  134.      * @return the list which contains the data of all the feature groups
  135.      */
  136.     public List<FeatureGroup> selectFeatureGroupsList( )
  137.     {
  138.         List<FeatureGroup> featureGroupList = new ArrayList<>( );
  139.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) )
  140.         {
  141.             daoUtil.executeQuery( );

  142.             while ( daoUtil.next( ) )
  143.             {
  144.                 FeatureGroup featureGroup = new FeatureGroup( );

  145.                 featureGroup.setId( daoUtil.getString( 1 ) );
  146.                 featureGroup.setDescriptionKey( daoUtil.getString( 2 ) );
  147.                 featureGroup.setLabelKey( daoUtil.getString( 3 ) );
  148.                 featureGroup.setOrder( daoUtil.getInt( 4 ) );
  149.                 featureGroup.setIcon( daoUtil.getString( 5 ) );

  150.                 featureGroupList.add( featureGroup );
  151.             }

  152.         }

  153.         return featureGroupList;
  154.     }

  155.     /**
  156.      * Returns the count of groups
  157.      *
  158.      * @return the count of all the feature groups
  159.      */
  160.     public int selectFeatureGroupsCount( )
  161.     {
  162.         int nCount = 0;
  163.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_COUNT_FEATUREGROUP ) )
  164.         {

  165.             daoUtil.executeQuery( );
  166.             while ( daoUtil.next( ) )
  167.             {
  168.                 nCount = daoUtil.getInt( 1 );
  169.             }

  170.         }

  171.         return nCount;
  172.     }
  173. }