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.style;
35  
36  import fr.paris.lutece.util.sql.DAOUtil;
37  
38  import java.sql.Statement;
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  /**
43   * This class provides Data Access methods for PageTemplate objects
44   */
45  public final class PageTemplateDAO implements IPageTemplateDAO
46  {
47      // Constants
48      private static final String SQL_QUERY_SELECT = " SELECT id_template, description, file_name, picture FROM core_page_template WHERE id_template = ?";
49      private static final String SQL_QUERY_INSERT = " INSERT INTO core_page_template ( description, file_name, picture ) VALUES ( ?, ?, ? )";
50      private static final String SQL_QUERY_DELETE = " DELETE FROM core_page_template WHERE id_template = ?";
51      private static final String SQL_QUERY_UPDATE = " UPDATE core_page_template SET id_template = ?, description = ?, file_name = ?, picture = ? "
52              + " WHERE id_template = ?";
53      private static final String SQL_QUERY_SELECTALL = " SELECT id_template , description, file_name, picture FROM core_page_template ORDER BY id_template ";
54      private static final String SQL_CHECK_PAGE_TEMPLATE_IS_USED = " SELECT id_template FROM core_page WHERE id_template = ? ";
55  
56      // /////////////////////////////////////////////////////////////////////////////////////
57      // Access methods to data
58  
59      /**
60       * Insert a new record in the table.
61       * 
62       * @param pageTemplate
63       *            The Instance of the object PageTemplate
64       */
65      public void insert( PageTemplate pageTemplate )
66      {
67          try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS ) )
68          {
69              int nIndex = 1;
70              daoUtil.setString( nIndex++, pageTemplate.getDescription( ) );
71              daoUtil.setString( nIndex++, pageTemplate.getFile( ) );
72              daoUtil.setString( nIndex, pageTemplate.getPicture( ) );
73  
74              daoUtil.executeUpdate( );
75  
76              if ( daoUtil.nextGeneratedKey( ) )
77              {
78                  pageTemplate.setId( daoUtil.getGeneratedKeyInt( 1 ) );
79              }
80          }
81      }
82  
83      /**
84       * load the data of PageTemplate from the table
85       *
86       * @param nPageTemplateId
87       *            The indentifier of the object PageTemplate
88       * @return The Instance of the object PageTemplate
89       */
90      public PageTemplate load( int nPageTemplateId )
91      {
92          PageTemplate pageTemplate = null;
93          try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
94          {
95              daoUtil.setInt( 1, nPageTemplateId );
96  
97              daoUtil.executeQuery( );
98  
99              if ( daoUtil.next( ) )
100             {
101                 pageTemplate = new PageTemplate( );
102                 pageTemplate.setId( daoUtil.getInt( 1 ) );
103                 pageTemplate.setDescription( daoUtil.getString( 2 ) );
104                 pageTemplate.setFile( daoUtil.getString( 3 ) );
105                 pageTemplate.setPicture( daoUtil.getString( 4 ) );
106             }
107 
108         }
109 
110         return pageTemplate;
111     }
112 
113     /**
114      * Delete a record from the table
115      * 
116      * @param nPageTemplateId
117      *            The indentifier of the object PageTemplate
118      */
119     public void delete( int nPageTemplateId )
120     {
121         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
122         {
123             daoUtil.setInt( 1, nPageTemplateId );
124             daoUtil.executeUpdate( );
125         }
126     }
127 
128     /**
129      * Update the record in the table
130      * 
131      * @param pageTemplate
132      *            The instance of the PageTemplate to update
133      */
134     public void store( PageTemplate pageTemplate )
135     {
136         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
137         {
138 
139             daoUtil.setInt( 1, pageTemplate.getId( ) );
140             daoUtil.setString( 2, pageTemplate.getDescription( ) );
141             daoUtil.setString( 3, pageTemplate.getFile( ) );
142             daoUtil.setString( 4, pageTemplate.getPicture( ) );
143             daoUtil.setInt( 5, pageTemplate.getId( ) );
144 
145             daoUtil.executeUpdate( );
146         }
147     }
148 
149     /**
150      * Returns a list of all the page templates
151      * 
152      * @return A list of PageTemplates objects
153      */
154     public List<PageTemplate> selectPageTemplatesList( )
155     {
156         List<PageTemplate> listPageTemplates = new ArrayList<>( );
157         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) )
158         {
159             daoUtil.executeQuery( );
160 
161             while ( daoUtil.next( ) )
162             {
163                 PageTemplateyle/PageTemplate.html#PageTemplate">PageTemplate pageTemplate = new PageTemplate( );
164 
165                 pageTemplate.setId( daoUtil.getInt( 1 ) );
166                 pageTemplate.setDescription( daoUtil.getString( 2 ) );
167                 pageTemplate.setFile( daoUtil.getString( 3 ) );
168                 pageTemplate.setPicture( daoUtil.getString( 4 ) );
169                 listPageTemplates.add( pageTemplate );
170             }
171 
172         }
173 
174         return listPageTemplates;
175     }
176 
177     /**
178      * Checks if a page template has been used by a page
179      * 
180      * @param nPageTemplateId
181      *            The identifier of the page template
182      * @return true if a page template is used by a page, false otherwise
183      */
184     public boolean checkPageTemplateIsUsed( int nPageTemplateId )
185     {
186         boolean check = false;
187         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_CHECK_PAGE_TEMPLATE_IS_USED ) )
188         {
189 
190             daoUtil.setInt( 1, nPageTemplateId );
191             daoUtil.executeQuery( );
192 
193             if ( !daoUtil.next( ) )
194             {
195                 check = true;
196             }
197 
198         }
199 
200         return check;
201     }
202 }