View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.blog.business;
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 BlogPageTemplate objects
44   */
45  public final class BlogPageTemplateDAO implements IBlogPageTemplateDAO
46  {
47      // Constants
48      private static final String SQL_QUERY_SELECT = " SELECT id_page_template_document, description, page_template_path, picture_path FROM blog_page_template WHERE id_page_template_document = ?";
49      private static final String SQL_QUERY_INSERT = " INSERT INTO blog_page_template ( description, page_template_path, picture_path ) VALUES ( ?, ?, ? )";
50      private static final String SQL_QUERY_DELETE = " DELETE FROM blog_page_template WHERE id_page_template_document = ?";
51      private static final String SQL_QUERY_UPDATE = " UPDATE blog_page_template SET id_page_template_document = ?, description = ?, page_template_path = ?, picture_path = ? "
52              + " WHERE id_page_template_document = ?";
53      private static final String SQL_QUERY_SELECTALL = " SELECT id_page_template_document , description, page_template_path, picture_path FROM blog_page_template ORDER BY id_page_template_document ";
54  
55      // /////////////////////////////////////////////////////////////////////////////////////
56      // Access methods to data
57  
58      /**
59       * Insert a new record in the table.
60       * 
61       * @param blogPageTemplate
62       *            The Instance of the object BlogPageTemplate
63       */
64      @Override
65      public synchronized void insert( BlogPageTemplate blogPageTemplate )
66      {
67          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS ) )
68          {
69              daoUtil.setString( 1, blogPageTemplate.getDescription( ) );
70              daoUtil.setString( 2, blogPageTemplate.getFile( ) );
71              daoUtil.setString( 3, blogPageTemplate.getPicture( ) );
72  
73              daoUtil.executeUpdate( );
74  
75              if ( daoUtil.nextGeneratedKey( ) )
76              {
77              	blogPageTemplate.setId( daoUtil.getGeneratedKeyInt( 1 ) );
78              }
79          }
80      }
81  
82      /**
83       * load the data of BlogPageTemplate from the table
84       *
85       * @param nPageTemplateId
86       *            The indentifier of the object BlogPageTemplate
87       * @return The Instance of the object PageTemplate
88       */
89      @Override
90      public BlogPageTemplate load( int nPageTemplateId )
91      {
92          BlogPageTemplate blogPageTemplate = null;
93          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
94          {
95              daoUtil.setInt( 1, nPageTemplateId );
96  
97              daoUtil.executeQuery( );
98  
99              if ( daoUtil.next( ) )
100             {
101                 blogPageTemplate = new BlogPageTemplate( );
102                 blogPageTemplate.setId( daoUtil.getInt( 1 ) );
103                 blogPageTemplate.setDescription( daoUtil.getString( 2 ) );
104                 blogPageTemplate.setFile( daoUtil.getString( 3 ) );
105                 blogPageTemplate.setPicture( daoUtil.getString( 4 ) );
106             }
107         }
108 
109         return blogPageTemplate;
110     }
111 
112     /**
113      * Delete a record from the table
114      * 
115      * @param nPageTemplateId
116      *            The indentifier of the object PageTemplate
117      */
118     @Override
119     public void delete( int nPageTemplateId )
120     {
121         try ( 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 blogPageTemplate
132      *            The instance of the PageTemplate to update
133      */
134     @Override
135     public void store( BlogPageTemplate blogPageTemplate )
136     {
137         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
138         {
139             daoUtil.setInt( 1, blogPageTemplate.getId( ) );
140             daoUtil.setString( 2, blogPageTemplate.getDescription( ) );
141             daoUtil.setString( 3, blogPageTemplate.getFile( ) );
142             daoUtil.setString( 4, blogPageTemplate.getPicture( ) );
143             daoUtil.setInt( 5, blogPageTemplate.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     @Override
155     public List<BlogPageTemplate> selectPageTemplatesList( )
156     {
157         List<BlogPageTemplate> listBlogPageTemplates = new ArrayList<>( );
158         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) )
159         {
160             daoUtil.executeQuery( );
161 
162             while ( daoUtil.next( ) )
163             {
164                 BlogPageTemplatePageTemplate.html#BlogPageTemplate">BlogPageTemplate blogPageTemplate = new BlogPageTemplate( );
165 
166                 blogPageTemplate.setId( daoUtil.getInt( 1 ) );
167                 blogPageTemplate.setDescription( daoUtil.getString( 2 ) );
168                 blogPageTemplate.setFile( daoUtil.getString( 3 ) );
169                 blogPageTemplate.setPicture( daoUtil.getString( 4 ) );
170                 listBlogPageTemplates.add( blogPageTemplate );
171             }
172 
173         }
174         return listBlogPageTemplates;
175     }
176 }