View Javadoc
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.portalcomponent;
35  
36  import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  /**
40   * This class provides Data Access methods for PortalComponent objects
41   */
42  public final class PortalComponentDAO implements IPortalComponentDAO
43  {
44      // Constants
45      private static final String SQL_QUERY_SELECT = " SELECT name FROM core_portal_component WHERE id_portal_component = ?";
46      private static final String SQL_QUERY_INSERT = " INSERT INTO core_portal_component ( id_portal_component, name ) VALUES ( ?, ? )";
47      private static final String SQL_QUERY_DELETE = " DELETE FROM core_portal_component WHERE id_portal_component = ?";
48      private static final String SQL_QUERY_UPDATE = " UPDATE core_portal_component SET id_portal_component = ?, name = ? " + " WHERE id_portal_component = ?";
49      private static final String SQL_QUERY_SELECTXSL = " SELECT a.id_stylesheet , a.description , a.file_name, a.source "
50              + " FROM core_stylesheet a, core_portal_component b, core_style_mode_stylesheet c, core_style d "
51              + " WHERE a.id_stylesheet = c.id_stylesheet AND d.id_style = c.id_style "
52              + " AND b.id_portal_component = d.id_portal_component AND d.id_portal_component = ? " + " AND c.id_mode = ? ";
53  
54      // /////////////////////////////////////////////////////////////////////////////////////
55      // Access methods to data
56  
57      /**
58       * {@inheritDoc}
59       */
60      public synchronized void insert( PortalComponent portalComponent )
61      {
62          try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT ) )
63          {
64  
65              daoUtil.setInt( 1, portalComponent.getId( ) );
66              daoUtil.setString( 2, portalComponent.getName( ) );
67  
68              daoUtil.executeUpdate( );
69          }
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      public PortalComponent load( int nPortalComponentId )
76      {
77          PortalComponent portalComponent = null;
78          try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
79          {
80              daoUtil.setInt( 1, nPortalComponentId );
81  
82              daoUtil.executeQuery( );
83  
84              if ( daoUtil.next( ) )
85              {
86                  portalComponent = new PortalComponent( );
87  
88                  portalComponent.setId( nPortalComponentId );
89                  portalComponent.setName( daoUtil.getString( 1 ) );
90              }
91  
92          }
93  
94          return portalComponent;
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     public void delete( int nPortalComponentId )
101     {
102         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
103         {
104             daoUtil.setInt( 1, nPortalComponentId );
105 
106             daoUtil.executeUpdate( );
107         }
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     public void store( PortalComponent portalComponent )
114     {
115         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
116         {
117 
118             daoUtil.setInt( 1, portalComponent.getId( ) );
119             daoUtil.setString( 2, portalComponent.getName( ) );
120             daoUtil.setInt( 3, portalComponent.getId( ) );
121 
122             daoUtil.executeUpdate( );
123         }
124     }
125 
126     /**
127      * {@inheritDoc}
128      */
129     public StyleSheet selectXslFile( int nPortalComponentId, int nIdMode )
130     {
131         StyleSheets/stylesheet/StyleSheet.html#StyleSheet">StyleSheet stylesheet = new StyleSheet( );
132         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTXSL ) )
133         {
134 
135             daoUtil.setInt( 1, nPortalComponentId );
136             daoUtil.setInt( 2, nIdMode );
137 
138             daoUtil.executeQuery( );
139 
140             if ( daoUtil.next( ) )
141             {
142                 stylesheet.setId( daoUtil.getInt( 1 ) );
143                 stylesheet.setDescription( daoUtil.getString( 2 ) );
144                 stylesheet.setFile( daoUtil.getString( 3 ) );
145                 stylesheet.setSource( daoUtil.getBytes( 4 ) );
146             }
147 
148         }
149 
150         return stylesheet;
151     }
152 }