View Javadoc
1   /*
2    * Copyright (c) 2002-2018, Mairie de 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  
35  package fr.paris.lutece.plugins.html.business.portlet;
36  
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 HtmlPortlet objects
42   */
43  public abstract class BaseHtmlPortletDAO implements IHtmlPortletDAO
44  {
45      // Constants
46      private static final String SQL_QUERY_SELECT = "SELECT id_portlet, html FROM html_portlet WHERE id_portlet = ? ";
47      private static final String SQL_QUERY_INSERT = "INSERT INTO html_portlet ( id_portlet, html ) VALUES ( ?, ? )";
48      private static final String SQL_QUERY_DELETE = "DELETE FROM html_portlet WHERE id_portlet = ? ";
49      private static final String SQL_QUERY_UPDATE = "UPDATE html_portlet SET id_portlet = ?, html = ? WHERE id_portlet = ? ";
50  
51      // /////////////////////////////////////////////////////////////////////////////////////
52      // Access methods to data
53  
54      /**
55       * Insert a new record in the table.
56       *
57       * @param portlet
58       *            The Instance of the Portlet
59       */
60      @Override
61      public void insert( Portlet portlet )
62      {
63          IHtmlPortlet./../../../../../../fr/paris/lutece/plugins/html/business/portlet/IHtmlPortlet.html#IHtmlPortlet">IHtmlPortlet p = (IHtmlPortlet) portlet;
64          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
65          daoUtil.setInt( 1, p.getId( ) );
66          daoUtil.setString( 2, p.getHtml( ) );
67  
68          daoUtil.executeUpdate( );
69          daoUtil.free( );
70      }
71  
72      /**
73       * Delete record from table
74       *
75       * @param nPortletId
76       *            The indentifier of the Portlet
77       */
78      @Override
79      public void delete( int nPortletId )
80      {
81          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
82          daoUtil.setInt( 1, nPortletId );
83  
84          daoUtil.executeUpdate( );
85          daoUtil.free( );
86      }
87  
88      /**
89       * Update the record in the table
90       *
91       * @param portlet
92       *            The reference of the portlet
93       */
94      @Override
95      public void store( Portlet portlet )
96      {
97          IHtmlPortlet./../../../../../../fr/paris/lutece/plugins/html/business/portlet/IHtmlPortlet.html#IHtmlPortlet">IHtmlPortlet p = (IHtmlPortlet) portlet;
98          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
99          daoUtil.setInt( 1, p.getId( ) );
100         daoUtil.setString( 2, p.getHtml( ) );
101         daoUtil.setInt( 3, p.getId( ) );
102 
103         daoUtil.executeUpdate( );
104         daoUtil.free( );
105     }
106 
107     /**
108      * Load the portlet
109      * 
110      * @param nIdPortlet
111      *            The portlet ID
112      * @param portlet
113      *            The portlet
114      * @return The portlet
115      */
116     protected Portlet load( int nIdPortlet, IHtmlPortlet portlet )
117     {
118         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
119         daoUtil.setInt( 1, nIdPortlet );
120         daoUtil.executeQuery( );
121 
122         if ( daoUtil.next( ) )
123         {
124             portlet.setId( daoUtil.getInt( 1 ) );
125             portlet.setHtml( daoUtil.getString( 2 ) );
126         }
127 
128         daoUtil.free( );
129 
130         return (Portlet) portlet;
131     }
132 }