View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.jasper.business.portlet;
35  
36  import fr.paris.lutece.portal.business.portlet.Portlet;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  /**
40   * this class provides Data Access methods for JasperPortlet objects
41   */
42  public final class JasperPortletDAO implements IJasperPortletDAO
43  {
44      // //////////////////////////////////////////////////////////////////////////
45      // Constants
46      private static final String SQL_QUERY_SELECT = "SELECT id_portlet, jasper_feed_id FROM jasper_portlet WHERE id_portlet = ? ";
47      private static final String SQL_QUERY_INSERT = "INSERT INTO jasper_portlet ( id_portlet, jasper_feed_id ) VALUES ( ?, ? )";
48      private static final String SQL_QUERY_DELETE = "DELETE FROM jasper_portlet WHERE id_portlet = ? ";
49      private static final String SQL_QUERY_UPDATE = "UPDATE jasper_portlet SET id_portlet = ?, jasper_feed_id = ? WHERE id_portlet = ? ";
50      private static final String SQL_QUERY_CHECK_PORTLET_LINKED = "SELECT jasper_feed_id FROM jasper_portlet WHERE jasper_feed_id = ? ";
51  
52      // /////////////////////////////////////////////////////////////////////////////////////
53      // Access methods to data
54  
55      /**
56       * Insert a new record in the table.
57       *
58       * @param portlet
59       *            The Instance of the Portlet
60       */
61      public void insert( Portlet portlet )
62      {
63      	try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT ) )
64      	{      
65              JasperPortlet/../../../../../../fr/paris/lutece/plugins/jasper/business/portlet/JasperPortlet.html#JasperPortlet">JasperPortlet p = (JasperPortlet) portlet;
66  
67              daoUtil.setInt( 1, p.getId( ) );
68              daoUtil.setString( 2, p.getJasperFeedId( ) );
69              daoUtil.executeUpdate( ); 
70      	}
71      }
72  
73      /**
74       * Delete record from table
75       *
76       * @param nPortletId
77       *            The indentifier of the Portlet
78       */
79      public void delete( int nPortletId )
80      {
81      	try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
82      	{      
83              daoUtil.setInt( 1, nPortletId );
84              daoUtil.executeUpdate( ); 
85      	}
86      }
87  
88      /**
89       * Update the record in the table
90       *
91       * @param portlet
92       *            The reference of the portlet
93       */
94      public void store( Portlet portlet )
95      {
96      	try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
97      	{    
98              JasperPortlet/../../../../../../fr/paris/lutece/plugins/jasper/business/portlet/JasperPortlet.html#JasperPortlet">JasperPortlet p = (JasperPortlet) portlet;
99  
100             daoUtil.setInt( 1, p.getId( ) );
101             daoUtil.setString( 2, p.getJasperFeedId( ) );
102             daoUtil.setInt( 3, p.getId( ) );
103 
104             daoUtil.executeUpdate( );   
105     	}
106     }
107 
108     /**
109      * load the data of dbpagePortlet from the table
110      * 
111      * @return portlet The instance of the object portlet
112      * @param nIdPortlet
113      *            The identifier of the portlet
114      */
115     public Portlet load( int nIdPortlet )
116     { 
117         JasperPortletper/business/portlet/JasperPortlet.html#JasperPortlet">JasperPortlet portlet = new JasperPortlet( );
118         
119     	try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
120     	{  
121             daoUtil.setInt( 1, nIdPortlet );
122             daoUtil.executeQuery( );
123 
124             if ( daoUtil.next( ) )
125             {
126                 portlet.setId( daoUtil.getInt( 1 ) );
127                 portlet.setJasperFeedId( daoUtil.getString( 2 ) );
128             }   
129     	}
130 
131         return portlet;
132     }
133 
134     /**
135      * Checks if a feed is linked to a portlet
136      * 
137      * @return A boolean
138      * @param nIdJasperFeed
139      *            The identifier of the Jasper feed
140      */
141     public boolean checkNoPortletLinked( int nIdJasperFeed )
142     {
143     	try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_PORTLET_LINKED ) )
144     	{   
145             daoUtil.setInt( 1, nIdJasperFeed );
146             daoUtil.executeQuery( );
147 
148             if ( daoUtil.next( ) )
149             {
150                 daoUtil.free( );
151 
152                 return false;
153             }    
154     	}
155     	
156         return true;
157     }
158 }