AdminWorkgroupDAO.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.workgroup;

  35. import fr.paris.lutece.portal.business.user.AdminUser;
  36. import fr.paris.lutece.portal.business.user.AdminUserHome;
  37. import fr.paris.lutece.util.ReferenceList;
  38. import fr.paris.lutece.util.sql.DAOUtil;

  39. import java.util.ArrayList;
  40. import java.util.Collection;

  41. /**
  42.  * This class provides Data Access methods for AdminWorkgroup objects
  43.  */
  44. public final class AdminWorkgroupDAO implements IAdminWorkgroupDAO
  45. {
  46.     // Constants
  47.     private static final String CONSTANT_PERCENT = "%";
  48.     private static final String SQL_QUERY_SELECT = " SELECT workgroup_key, workgroup_description FROM core_admin_workgroup WHERE workgroup_key = ?  ";
  49.     private static final String SQL_QUERY_INSERT = " INSERT INTO core_admin_workgroup ( workgroup_key, workgroup_description ) VALUES ( ?, ? ) ";
  50.     private static final String SQL_QUERY_DELETE = " DELETE FROM core_admin_workgroup WHERE workgroup_key = ?  ";
  51.     private static final String SQL_QUERY_UPDATE = " UPDATE core_admin_workgroup SET workgroup_description = ? WHERE workgroup_key = ?  ";
  52.     private static final String SQL_QUERY_SELECTALL = " SELECT workgroup_key, workgroup_description FROM core_admin_workgroup ORDER BY workgroup_key";
  53.     private static final String SQL_QUERY_SELECT_USER_WORKGROUP = " SELECT workgroup_key FROM core_admin_workgroup_user WHERE id_user = ? AND workgroup_key = ? ";
  54.     private static final String SQL_QUERY_USER_IN_WORKGROUP = " SELECT id_user FROM core_admin_workgroup_user WHERE id_user = ? ";
  55.     private static final String SQL_QUERY_SELECT_USER_WORKGROUPS = " SELECT a.workgroup_key, a.workgroup_description "
  56.             + " FROM core_admin_workgroup a, core_admin_workgroup_user b " + " WHERE a.workgroup_key = b.workgroup_key AND b.id_user = ?  ";
  57.     private static final String SQL_QUERY_SELECT_USERS_LIST_FOR_WORKGROUP = " SELECT b.id_user " + " FROM core_admin_workgroup a, core_admin_workgroup_user b "
  58.             + " WHERE a.workgroup_key = b.workgroup_key AND a.workgroup_key = ?";
  59.     private static final String SQL_QUERY_DELETE_ALL_USERS_WORKGROUP = " DELETE FROM core_admin_workgroup_user WHERE workgroup_key = ?  ";
  60.     private static final String SQL_QUERY_INSERT_USER_WORKGROUP = " INSERT INTO core_admin_workgroup_user ( workgroup_key, id_user ) VALUES ( ?, ? ) ";
  61.     private static final String SQL_QUERY_DELETE_USER_FROM_WORKGROUP = " DELETE FROM core_admin_workgroup_user WHERE workgroup_key = ?  AND id_user = ?";
  62.     private static final String SQL_QUERY_SELECT_WORKGROUP_FROM_SEARCH = " SELECT workgroup_key, workgroup_description FROM core_admin_workgroup "
  63.             + " WHERE workgroup_key LIKE ? AND workgroup_description LIKE ? ORDER BY workgroup_key ";

  64.     /**
  65.      * {@inheritDoc}
  66.      */
  67.     public void insert( AdminWorkgroup workgroup )
  68.     {
  69.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT ) )
  70.         {
  71.             daoUtil.setString( 1, workgroup.getKey( ) );
  72.             daoUtil.setString( 2, workgroup.getDescription( ) );

  73.             daoUtil.executeUpdate( );
  74.         }
  75.     }

  76.     /**
  77.      * {@inheritDoc}
  78.      */
  79.     public AdminWorkgroup load( String strWorkgroupKey )
  80.     {
  81.         AdminWorkgroup workgroup = null;
  82.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
  83.         {
  84.             daoUtil.setString( 1, strWorkgroupKey );
  85.             daoUtil.executeQuery( );

  86.             if ( daoUtil.next( ) )
  87.             {
  88.                 workgroup = new AdminWorkgroup( );
  89.                 workgroup.setKey( daoUtil.getString( 1 ) );
  90.                 workgroup.setDescription( daoUtil.getString( 2 ) );
  91.             }

  92.         }

  93.         return workgroup;
  94.     }

  95.     /**
  96.      * {@inheritDoc}
  97.      */
  98.     public void delete( String strWorkgroupKey )
  99.     {
  100.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
  101.         {
  102.             daoUtil.setString( 1, strWorkgroupKey );

  103.             daoUtil.executeUpdate( );
  104.         }
  105.     }

  106.     /**
  107.      * {@inheritDoc}
  108.      */
  109.     public void store( AdminWorkgroup workgroup )
  110.     {
  111.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
  112.         {
  113.             daoUtil.setString( 1, workgroup.getDescription( ) );
  114.             daoUtil.setString( 2, workgroup.getKey( ) );

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

  118.     /**
  119.      * {@inheritDoc}
  120.      */
  121.     public Collection<AdminWorkgroup> selectWorkgroupList( )
  122.     {
  123.         Collection<AdminWorkgroup> listWorkgroups = new ArrayList<>( );
  124.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) )
  125.         {
  126.             daoUtil.executeQuery( );

  127.             while ( daoUtil.next( ) )
  128.             {
  129.                 AdminWorkgroup workgroup = new AdminWorkgroup( );
  130.                 workgroup.setKey( daoUtil.getString( 1 ) );
  131.                 workgroup.setDescription( daoUtil.getString( 2 ) );

  132.                 listWorkgroups.add( workgroup );
  133.             }

  134.         }

  135.         return listWorkgroups;
  136.     }

  137.     /**
  138.      * {@inheritDoc}
  139.      */
  140.     public boolean checkExistWorkgroup( String strWorkgroupKey )
  141.     {
  142.         boolean check = false;
  143.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
  144.         {
  145.             daoUtil.setString( 1, strWorkgroupKey );
  146.             daoUtil.executeQuery( );

  147.             if ( daoUtil.next( ) )
  148.             {
  149.                 check = true;
  150.             }
  151.         }
  152.         return check;
  153.     }

  154.     /**
  155.      * {@inheritDoc}
  156.      */
  157.     public boolean isUserInWorkgroup( int nIdUser, String strWorkgroupKey )
  158.     {
  159.         boolean bInWorkgroup = false;
  160.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_USER_WORKGROUP ) )
  161.         {
  162.             daoUtil.setInt( 1, nIdUser );
  163.             daoUtil.setString( 2, strWorkgroupKey );
  164.             daoUtil.executeQuery( );

  165.             if ( daoUtil.next( ) )
  166.             {
  167.                 bInWorkgroup = true;
  168.             }

  169.         }

  170.         return bInWorkgroup;
  171.     }

  172.     /**
  173.      * {@inheritDoc}
  174.      */
  175.     public boolean checkUserHasWorkgroup( int nIdUser )
  176.     {
  177.         boolean check = false;
  178.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_USER_IN_WORKGROUP ) )
  179.         {
  180.             daoUtil.setInt( 1, nIdUser );
  181.             daoUtil.executeQuery( );

  182.             if ( daoUtil.next( ) )
  183.             {
  184.                 check = true;
  185.             }
  186.         }
  187.         return check;
  188.     }

  189.     /**
  190.      * {@inheritDoc}
  191.      */
  192.     public ReferenceList getUserWorkgroups( int nIdUser )
  193.     {
  194.         ReferenceList listWorkgroups = new ReferenceList( );
  195.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_USER_WORKGROUPS ) )
  196.         {
  197.             daoUtil.setInt( 1, nIdUser );
  198.             daoUtil.executeQuery( );

  199.             while ( daoUtil.next( ) )
  200.             {
  201.                 listWorkgroups.addItem( daoUtil.getString( 1 ), daoUtil.getString( 2 ) );
  202.             }

  203.         }

  204.         return listWorkgroups;
  205.     }

  206.     /**
  207.      * {@inheritDoc}
  208.      */
  209.     public Collection<AdminUser> getUsersListForWorkgroup( String strWorkgroupKey )
  210.     {
  211.         Collection<AdminUser> listUsers = new ArrayList<>( );
  212.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_USERS_LIST_FOR_WORKGROUP ) )
  213.         {
  214.             daoUtil.setString( 1, strWorkgroupKey );
  215.             daoUtil.executeQuery( );

  216.             AdminUser adminUser = null;

  217.             while ( daoUtil.next( ) )
  218.             {
  219.                 adminUser = AdminUserHome.findByPrimaryKey( daoUtil.getInt( 1 ) );

  220.                 if ( adminUser != null )
  221.                 {
  222.                     listUsers.add( adminUser );
  223.                 }
  224.             }

  225.         }

  226.         return listUsers;
  227.     }

  228.     /**
  229.      * {@inheritDoc}
  230.      */
  231.     public void deleteAllUsersForWorkgroup( String strWorkgroupKey )
  232.     {
  233.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_ALL_USERS_WORKGROUP ) )
  234.         {
  235.             daoUtil.setString( 1, strWorkgroupKey );
  236.             daoUtil.executeUpdate( );
  237.         }
  238.     }

  239.     /**
  240.      * {@inheritDoc}
  241.      */
  242.     public void insertUserForWorkgroup( AdminUser user, String strWorkgroupKey )
  243.     {
  244.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_USER_WORKGROUP ) )
  245.         {
  246.             daoUtil.setString( 1, strWorkgroupKey );
  247.             daoUtil.setInt( 2, user.getUserId( ) );
  248.             daoUtil.executeUpdate( );
  249.         }
  250.     }

  251.     /**
  252.      * {@inheritDoc}
  253.      */
  254.     public void deleteUserFromWorkgroup( int nUserId, String strWorkgroupKey )
  255.     {
  256.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_USER_FROM_WORKGROUP ) )
  257.         {
  258.             daoUtil.setString( 1, strWorkgroupKey );
  259.             daoUtil.setInt( 2, nUserId );
  260.             daoUtil.executeUpdate( );
  261.         }
  262.     }

  263.     /**
  264.      * Find workgroups from a filter
  265.      *
  266.      * @param awFilter
  267.      *            the filter
  268.      * @return the list of workgroups
  269.      */
  270.     public Collection<AdminWorkgroup> selectWorkgroupsByFilter( AdminWorkgroupFilter awFilter )
  271.     {
  272.         Collection<AdminWorkgroup> listFilteredWorkgroups = new ArrayList<>( );
  273.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_WORKGROUP_FROM_SEARCH ) )
  274.         {

  275.             daoUtil.setString( 1, CONSTANT_PERCENT + awFilter.getKey( ) + CONSTANT_PERCENT );
  276.             daoUtil.setString( 2, CONSTANT_PERCENT + awFilter.getDescription( ) + CONSTANT_PERCENT );

  277.             daoUtil.executeQuery( );

  278.             while ( daoUtil.next( ) )
  279.             {
  280.                 AdminWorkgroup workgroup = new AdminWorkgroup( );
  281.                 workgroup.setKey( daoUtil.getString( 1 ) );
  282.                 workgroup.setDescription( daoUtil.getString( 2 ) );

  283.                 listFilteredWorkgroups.add( workgroup );
  284.             }

  285.         }

  286.         return listFilteredWorkgroups;
  287.     }
  288. }