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