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.securityheader;
35  
36  import java.sql.Statement;
37  import java.util.ArrayList;
38  import java.util.List;
39  
40  import fr.paris.lutece.util.sql.DAOUtil;
41  
42  /**
43   * This class provides Data Access methods for SecurityHeader objects
44   */
45  public final class SecurityHeaderDAO implements ISecurityHeaderDAO
46  {
47        // Constants
48        private static final String SQL_QUERY_SELECT = "SELECT id_security_header, name, value, description, type, page_category, is_active FROM core_admin_security_header WHERE id_security_header = ?";
49        private static final String SQL_QUERY_INSERT = "INSERT INTO core_admin_security_header ( name, value, description, type, page_category ) VALUES ( ?, ?, ?, ?, ? ) ";
50        private static final String SQL_QUERY_DELETE = "DELETE FROM core_admin_security_header WHERE id_security_header = ? ";
51        private static final String SQL_QUERY_UPDATE = "UPDATE core_admin_security_header SET value = ?, description = ?, type = ?, page_category = ? WHERE id_security_header = ?";
52        private static final String SQL_QUERY_UPDATE_IS_ACTIVE = "UPDATE core_admin_security_header SET is_active = ? WHERE id_security_header = ?";
53        private static final String SQL_QUERY_SELECTALL = "SELECT id_security_header, name, value, description, type, page_category, is_active FROM core_admin_security_header";
54        private static final String SQL_QUERY_SELECT_ACTIVE_BY_TYPE = "SELECT id_security_header, name, value, description, type, page_category, is_active FROM core_admin_security_header WHERE is_active = 'true' and type = ? ";
55  
56      /**
57       * Insert a new record in the table.
58       *
59       * @param securityHeader
60       *            instance of the SecurityHeader object to insert
61       */
62      @Override
63      public void insert( SecurityHeader securityHeader )
64      {
65          try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS ) )
66          {
67              int nIndex = 1;
68              daoUtil.setString( nIndex++, securityHeader.getName( ) );
69              daoUtil.setString( nIndex++, securityHeader.getValue( ) );
70              daoUtil.setString( nIndex++, securityHeader.getDescription( ) );
71              daoUtil.setString( nIndex++, securityHeader.getType( ) );
72              daoUtil.setString( nIndex, securityHeader.getPageCategory() );
73  
74              daoUtil.executeUpdate( );
75  
76              if ( daoUtil.nextGeneratedKey( ) )
77              {
78              	securityHeader.setId( daoUtil.getGeneratedKeyInt( 1 ) );
79              }
80          }
81      }
82  
83      /**
84       * Loads the data of the security header from the table
85       *
86       * @param nId
87       *            The identifier of the security header
88       * @return the instance of the Security header
89       */
90      @Override
91      public SecurityHeader load( int nId )
92      {
93      	SecurityHeader securityHeader = null;
94          try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
95          {
96              daoUtil.setInt( 1, nId );
97              daoUtil.executeQuery( );
98  
99              if ( daoUtil.next( ) )
100             {
101             	securityHeader = new SecurityHeader( );
102 
103                 securityHeader.setId( daoUtil.getInt( 1 ) );
104             	securityHeader.setName( daoUtil.getString( 2 ) );            	
105             	securityHeader.setValue( daoUtil.getString( 3 ) );
106             	securityHeader.setDescription( daoUtil.getString( 4 ) );
107             	securityHeader.setType( daoUtil.getString( 5 ) );
108             	securityHeader.setPageCategory(daoUtil.getString( 6 ));
109             	securityHeader.setActive( daoUtil.getBoolean( 7 ) );
110             }
111 
112         }
113 
114         return securityHeader;
115     }
116 
117     /**
118      * Delete a record from the table
119      *
120      * @param nSecurityHeaderId
121      *            The identifier of the security header
122      */
123     @Override
124     public void delete( int nSecurityHeaderId )
125     {
126         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
127         {
128             daoUtil.setInt( 1, nSecurityHeaderId );
129             daoUtil.executeUpdate( );
130         }
131     }
132 
133     /**
134      * Update the record in the table
135      *
136      * @param securityHeader
137      *            The reference of the security header
138      */
139     @Override
140     public void store( SecurityHeader securityHeader )
141     {
142         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
143         {
144 
145             daoUtil.setString( 1, securityHeader.getValue( ) );
146             daoUtil.setString( 2, securityHeader.getDescription( ) );
147             daoUtil.setString( 3, securityHeader.getType( ) );
148             daoUtil.setString( 4, securityHeader.getPageCategory( ) );
149             daoUtil.setInt( 5, securityHeader.getId( ) );
150 
151             daoUtil.executeUpdate( );
152         }
153     }
154     
155     /**
156      * Update of "is_active" column value of security header which is specified in parameter
157      * 
158      * @param nSecurityHeaderId
159      *            The securityHeader Id
160      * @param isActiveValue
161      *            The value to set to is_active column
162      */
163     @Override
164     public void updateIsActive( int nSecurityHeaderId, boolean isActiveValue )
165     {
166     	try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_IS_ACTIVE ) )
167         {
168 
169             daoUtil.setBoolean( 1, isActiveValue );
170             daoUtil.setInt( 2, nSecurityHeaderId );
171 
172             daoUtil.executeUpdate( );
173         }
174     }
175     
176     /**
177      * Loads the data of all the security headers and returns them in form of a collection
178      *
179      * @return The Collection which contains the data of all the security headers
180      */
181     @Override
182     public List<SecurityHeader> selectAll( )
183     {
184         List<SecurityHeader> securityHeadersList = new ArrayList<SecurityHeader>( );
185         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) )
186         {
187             daoUtil.executeQuery( );
188 
189             while ( daoUtil.next( ) )
190             {
191             	SecurityHeader securityHeader = new SecurityHeader( );
192 
193             	securityHeader.setId( daoUtil.getInt( 1 ) );
194             	securityHeader.setName( daoUtil.getString( 2 ) );            	
195             	securityHeader.setValue( daoUtil.getString( 3 ) );
196             	securityHeader.setDescription( daoUtil.getString( 4 ) );
197             	securityHeader.setType( daoUtil.getString( 5 ) );
198             	securityHeader.setPageCategory(daoUtil.getString( 6 ) );
199             	securityHeader.setActive( daoUtil.getBoolean( 7 ) );
200 
201             	securityHeadersList.add( securityHeader );
202             }
203 
204         }
205 
206         return securityHeadersList;
207     }
208 
209     /**
210      * Returns all active security headers from a specified type (page or REST api)
211      *
212      * @param strType
213      *            The type
214      * @return the list which contains the data of all the securityHeaders
215      */
216     @Override
217     public List<SecurityHeader> selectActiveByType( String strType )
218     {
219         List<SecurityHeader> securityHeadersList = new ArrayList<>( );
220         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ACTIVE_BY_TYPE ) )
221         {
222             daoUtil.setString( 1, strType );
223             daoUtil.executeQuery( );
224 
225             while ( daoUtil.next( ) )
226             {
227             	SecurityHeader securityHeader = new SecurityHeader( );
228 
229             	securityHeader.setId( daoUtil.getInt( 1 ) );
230             	securityHeader.setName( daoUtil.getString( 2 ) );
231             	securityHeader.setDescription( daoUtil.getString( 3 ) );
232             	securityHeader.setValue( daoUtil.getString( 4 ) );
233             	securityHeader.setType( daoUtil.getString( 5 ) );
234             	securityHeader.setPageCategory(daoUtil.getString( 6 ) );
235             	securityHeader.setActive( daoUtil.getBoolean( 7 ) );
236 
237             	securityHeadersList.add( securityHeader );
238             }
239         }
240 
241         return securityHeadersList;
242     }
243 }