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.util.ArrayList;
39  import java.util.List;
40  
41  /**
42   * This class provides Data Access methods for BlogPageTemplate objects
43   */
44  public final class BlogPageTemplateDAO implements IBlogPageTemplateDAO
45  {
46      // Constants
47      private static final String SQL_QUERY_NEW_PK = " SELECT max( id_page_template_document ) FROM blog_page_template";
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 ( id_page_template_document, 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       * Generates a new primary key
60       * 
61       * @return The new primary key
62       */
63      int newPrimaryKey( )
64      {
65          int nKey = 1;
66          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK ) )
67          {
68              daoUtil.executeQuery( );
69              if ( daoUtil.next( ) )
70              {
71                  nKey = daoUtil.getInt( 1 ) + 1;
72              }
73          }
74          return nKey;
75      }
76  
77      /**
78       * Insert a new record in the table.
79       * 
80       * @param blogPageTemplate
81       *            The Instance of the object BlogPageTemplate
82       */
83      @Override
84      public synchronized void insert( BlogPageTemplate blogPageTemplate )
85      {
86          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT ) )
87          {
88              blogPageTemplate.setId( newPrimaryKey( ) );
89  
90              daoUtil.setInt( 1, blogPageTemplate.getId( ) );
91              daoUtil.setString( 2, blogPageTemplate.getDescription( ) );
92              daoUtil.setString( 3, blogPageTemplate.getFile( ) );
93              daoUtil.setString( 4, blogPageTemplate.getPicture( ) );
94  
95              daoUtil.executeUpdate( );
96          }
97      }
98  
99      /**
100      * load the data of BlogPageTemplate from the table
101      *
102      * @param nPageTemplateId
103      *            The indentifier of the object BlogPageTemplate
104      * @return The Instance of the object PageTemplate
105      */
106     @Override
107     public BlogPageTemplate load( int nPageTemplateId )
108     {
109         BlogPageTemplate blogPageTemplate = null;
110         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
111         {
112             daoUtil.setInt( 1, nPageTemplateId );
113 
114             daoUtil.executeQuery( );
115 
116             if ( daoUtil.next( ) )
117             {
118                 blogPageTemplate = new BlogPageTemplate( );
119                 blogPageTemplate.setId( daoUtil.getInt( 1 ) );
120                 blogPageTemplate.setDescription( daoUtil.getString( 2 ) );
121                 blogPageTemplate.setFile( daoUtil.getString( 3 ) );
122                 blogPageTemplate.setPicture( daoUtil.getString( 4 ) );
123             }
124         }
125 
126         return blogPageTemplate;
127     }
128 
129     /**
130      * Delete a record from the table
131      * 
132      * @param nPageTemplateId
133      *            The indentifier of the object PageTemplate
134      */
135     @Override
136     public void delete( int nPageTemplateId )
137     {
138         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
139         {
140             daoUtil.setInt( 1, nPageTemplateId );
141             daoUtil.executeUpdate( );
142         }
143     }
144 
145     /**
146      * Update the record in the table
147      * 
148      * @param blogPageTemplate
149      *            The instance of the PageTemplate to update
150      */
151     @Override
152     public void store( BlogPageTemplate blogPageTemplate )
153     {
154         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
155         {
156             daoUtil.setInt( 1, blogPageTemplate.getId( ) );
157             daoUtil.setString( 2, blogPageTemplate.getDescription( ) );
158             daoUtil.setString( 3, blogPageTemplate.getFile( ) );
159             daoUtil.setString( 4, blogPageTemplate.getPicture( ) );
160             daoUtil.setInt( 5, blogPageTemplate.getId( ) );
161 
162             daoUtil.executeUpdate( );
163         }
164     }
165 
166     /**
167      * Returns a list of all the page templates
168      * 
169      * @return A list of PageTemplates objects
170      */
171     @Override
172     public List<BlogPageTemplate> selectPageTemplatesList( )
173     {
174         List<BlogPageTemplate> listBlogPageTemplates = new ArrayList<>( );
175         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) )
176         {
177             daoUtil.executeQuery( );
178 
179             while ( daoUtil.next( ) )
180             {
181                 BlogPageTemplatePageTemplate.html#BlogPageTemplate">BlogPageTemplate blogPageTemplate = new BlogPageTemplate( );
182 
183                 blogPageTemplate.setId( daoUtil.getInt( 1 ) );
184                 blogPageTemplate.setDescription( daoUtil.getString( 2 ) );
185                 blogPageTemplate.setFile( daoUtil.getString( 3 ) );
186                 blogPageTemplate.setPicture( daoUtil.getString( 4 ) );
187                 listBlogPageTemplates.add( blogPageTemplate );
188             }
189 
190         }
191         return listBlogPageTemplates;
192     }
193 }