View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.plugins.announce.business.portlet;
35  
36  import fr.paris.lutece.plugins.announce.service.AnnouncePlugin;
37  import fr.paris.lutece.portal.business.portlet.Portlet;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  
40  /**
41   * this class provides Data Access methods for LastAnnouncePortlet objects
42   */
43  public final class LastAnnouncesPortletDAO implements ILastAnnouncesPortletDAO
44  {
45      private static final String SQL_QUERY_INSERT = " INSERT INTO announce_portlet_last_announces ( id_portlet, nb_announces ) VALUES (?,?) ";
46      private static final String SQL_QUERY_DELETE = " DELETE FROM announce_portlet_last_announces WHERE id_portlet = ? ";
47      private static final String SQL_QUERY_UPDATE = " UPDATE announce_portlet_last_announces SET nb_announces = ? WHERE id_portlet = ? ";
48      private static final String SQL_QUERY_SELECT = " SELECT nb_announces FROM announce_portlet_last_announces WHERE id_portlet = ? ";
49  
50      /**
51       * {@inheritDoc}
52       */
53      @Override
54      public void insert( Portlet portlet )
55      {
56          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, AnnouncePlugin.getPlugin( ) ) )
57          {
58              LastAnnouncesPortlets/lutece/plugins/announce/business/portlet/LastAnnouncesPortlet.html#LastAnnouncesPortlet">LastAnnouncesPortlet lastAnnouncesPortlet = (LastAnnouncesPortlet) portlet;
59              int nIndex = 1;
60              daoUtil.setInt( nIndex++, lastAnnouncesPortlet.getId( ) );
61              daoUtil.setInt( nIndex, lastAnnouncesPortlet.getNbAnnouncesToDisplay( ) );
62  
63              daoUtil.executeUpdate( );
64          }
65      }
66  
67      /**
68       * {@inheritDoc}
69       */
70      @Override
71      public void delete( int nPortletId )
72      {
73          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, AnnouncePlugin.getPlugin( ) ) )
74          {
75              daoUtil.setInt( 1, nPortletId );
76              daoUtil.executeUpdate( );
77          }
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      public void store( Portlet portlet )
85      {
86          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, AnnouncePlugin.getPlugin( ) ) )
87          {
88              LastAnnouncesPortlets/lutece/plugins/announce/business/portlet/LastAnnouncesPortlet.html#LastAnnouncesPortlet">LastAnnouncesPortlet lastAnnouncesPortlet = (LastAnnouncesPortlet) portlet;
89              int nIndex = 1;
90              daoUtil.setInt( nIndex++, lastAnnouncesPortlet.getNbAnnouncesToDisplay( ) );
91              daoUtil.setInt( nIndex, lastAnnouncesPortlet.getId( ) );
92  
93              daoUtil.executeUpdate( );
94          }
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public Portlet load( int nIdPortlet )
102     {
103         LastAnnouncesPortletusiness/portlet/LastAnnouncesPortlet.html#LastAnnouncesPortlet">LastAnnouncesPortlet portlet = new LastAnnouncesPortlet( );
104         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, AnnouncePlugin.getPlugin( ) ) )
105         {
106             daoUtil.setInt( 1, nIdPortlet );
107             portlet.setId( nIdPortlet );
108 
109             if ( daoUtil.next( ) )
110             {
111                 portlet.setNbAnnouncesToDisplay( daoUtil.getInt( 1 ) );
112             }
113         }
114         return portlet;
115     }
116 }