AliasPortletDAO.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.portlet;

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

  38. import fr.paris.lutece.util.ReferenceList;
  39. import fr.paris.lutece.util.sql.DAOUtil;

  40. /**
  41.  * This class provides Data Access methods for AliasPortlet objects
  42.  */
  43. public final class AliasPortletDAO implements IAliasPortletDAO
  44. {
  45.     // sql queries
  46.     private static final String SQL_QUERY_INSERT = "INSERT INTO core_portlet_alias ( id_portlet , id_alias ) VALUES ( ?, ? )";
  47.     private static final String SQL_QUERY_DELETE = "DELETE FROM core_portlet_alias WHERE id_portlet = ?";
  48.     private static final String SQL_QUERY_SELECT = "SELECT id_alias FROM core_portlet_alias WHERE id_portlet = ? ";
  49.     private static final String SQL_QUERY_UPDATE = "UPDATE core_portlet_alias SET id_alias=? WHERE id_portlet = ?";
  50.     private static final String SQL_QUERY_SELECT_PORTLETS_BY_TYPE = "SELECT  id_portlet, name FROM core_portlet WHERE id_portlet_type = ? ORDER BY name";
  51.     private static final String SQL_QUERY_SELECT_ALIAS_ID = "SELECT id_alias FROM core_portlet_alias WHERE id_portlet= ? ";
  52.     private static final String SQL_QUERY_SELECT_ACCEPT_ALIAS_PORTLET_LIST = "SELECT id_portlet, name FROM core_portlet WHERE accept_alias = 1 ";
  53.     private static final String SQL_QUERY_SELECT_ACCEPT_ALIAS_PORTLET_LIST_DETAIL = "SELECT id_portlet, id_page, name FROM core_portlet WHERE accept_alias = 1 ORDER BY id_page";

  54.     // /////////////////////////////////////////////////////////////////////////////////////
  55.     // Access methods to data

  56.     /**
  57.      * {@inheritDoc}
  58.      */
  59.     public synchronized void insert( Portlet portlet )
  60.     {
  61.         AliasPortlet alias = (AliasPortlet) portlet;

  62.         // insert into the table portlet_alias
  63.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT ) )
  64.         {
  65.             daoUtil.setInt( 1, alias.getId( ) );
  66.             daoUtil.setInt( 2, alias.getAliasId( ) );

  67.             daoUtil.executeUpdate( );
  68.         }
  69.     }

  70.     /**
  71.      * {@inheritDoc}
  72.      */
  73.     public void delete( int nPortletId )
  74.     {
  75.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
  76.         {
  77.             daoUtil.setInt( 1, nPortletId );

  78.             daoUtil.executeUpdate( );
  79.         }
  80.     }

  81.     /**
  82.      * {@inheritDoc}
  83.      */
  84.     public Portlet load( int nIdPortlet )
  85.     {
  86.         AliasPortlet portlet = new AliasPortlet( );
  87.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
  88.         {
  89.             daoUtil.setInt( 1, nIdPortlet );
  90.             daoUtil.executeQuery( );

  91.             if ( daoUtil.next( ) )
  92.             {
  93.                 portlet.setAliasId( daoUtil.getInt( 1 ) );
  94.             }

  95.         }

  96.         return portlet;
  97.     }

  98.     /**
  99.      * {@inheritDoc}
  100.      */
  101.     public void store( Portlet portlet )
  102.     {
  103.         AliasPortlet r = (AliasPortlet) portlet;

  104.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
  105.         {
  106.             daoUtil.setInt( 1, r.getAliasId( ) );
  107.             daoUtil.setInt( 2, portlet.getId( ) );

  108.             daoUtil.executeUpdate( );
  109.         }
  110.     }

  111.     /**
  112.      * {@inheritDoc}
  113.      */
  114.     public ReferenceList selectPortletsByTypeList( String strPortletTypeId )
  115.     {
  116.         ReferenceList list = new ReferenceList( );
  117.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PORTLETS_BY_TYPE ) )
  118.         {
  119.             daoUtil.setString( 1, strPortletTypeId );
  120.             daoUtil.executeQuery( );

  121.             while ( daoUtil.next( ) )
  122.             {
  123.                 list.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
  124.             }

  125.         }

  126.         return list;
  127.     }

  128.     /**
  129.      * {@inheritDoc}
  130.      */
  131.     public int selectAliasId( int nIdPortlet )
  132.     {
  133.         int nAliasId = 0;
  134.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALIAS_ID ) )
  135.         {
  136.             daoUtil.setInt( 1, nIdPortlet );
  137.             daoUtil.executeQuery( );

  138.             if ( daoUtil.next( ) )
  139.             {
  140.                 nAliasId = daoUtil.getInt( 1 );
  141.             }

  142.         }

  143.         return nAliasId;
  144.     }

  145.     /**
  146.      * {@inheritDoc}
  147.      */
  148.     public ReferenceList selectAcceptAliasPortletRefList( )
  149.     {
  150.         ReferenceList list = new ReferenceList( );
  151.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ACCEPT_ALIAS_PORTLET_LIST ) )
  152.         {
  153.             daoUtil.executeQuery( );

  154.             while ( daoUtil.next( ) )
  155.             {
  156.                 list.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
  157.             }

  158.         }

  159.         return list;
  160.     }

  161.     /**
  162.      * {@inheritDoc}
  163.      */
  164.     public Collection<Portlet> selectAcceptAliasPortletList( )
  165.     {
  166.         List<Portlet> list = new ArrayList<>( );
  167.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ACCEPT_ALIAS_PORTLET_LIST_DETAIL ) )
  168.         {
  169.             daoUtil.executeQuery( );

  170.             while ( daoUtil.next( ) )
  171.             {
  172.                 PortletImpl portlet = new PortletImpl( );
  173.                 portlet.setId( daoUtil.getInt( 1 ) );
  174.                 portlet.setPageId( daoUtil.getInt( 2 ) );
  175.                 portlet.setName( daoUtil.getString( 3 ) );
  176.                 list.add( portlet );
  177.             }

  178.         }

  179.         return list;
  180.     }

  181. }