View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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.document.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 DocumentPageTemplate objects
43   */
44  public final class DocumentPageTemplateDAO implements IDocumentPageTemplateDAO
45  {
46      // Constants
47      private static final String SQL_QUERY_NEW_PK = " SELECT max( id_page_template_document ) FROM document_page_template";
48      private static final String SQL_QUERY_SELECT = " SELECT id_page_template_document, description, page_template_path, picture_path FROM document_page_template WHERE id_page_template_document = ?";
49      private static final String SQL_QUERY_INSERT = " INSERT INTO document_page_template ( id_page_template_document, description, page_template_path, picture_path ) VALUES ( ?, ?, ?, ? )";
50      private static final String SQL_QUERY_DELETE = " DELETE FROM document_page_template WHERE id_page_template_document = ?";
51      private static final String SQL_QUERY_UPDATE = " UPDATE document_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 document_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          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK );
66          daoUtil.executeQuery( );
67  
68          int nKey;
69  
70          if ( !daoUtil.next( ) )
71          {
72              // if the table is empty
73              nKey = 1;
74          }
75  
76          nKey = daoUtil.getInt( 1 ) + 1;
77  
78          daoUtil.free( );
79  
80          return nKey;
81      }
82  
83      /**
84       * Insert a new record in the table.
85       * 
86       * @param documentPageTemplate
87       *            The Instance of the object DocumentPageTemplate
88       */
89      public synchronized void insert( DocumentPageTemplate documentPageTemplate )
90      {
91          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
92  
93          documentPageTemplate.setId( newPrimaryKey( ) );
94  
95          daoUtil.setInt( 1, documentPageTemplate.getId( ) );
96          daoUtil.setString( 2, documentPageTemplate.getDescription( ) );
97          daoUtil.setString( 3, documentPageTemplate.getFile( ) );
98          daoUtil.setString( 4, documentPageTemplate.getPicture( ) );
99  
100         daoUtil.executeUpdate( );
101         daoUtil.free( );
102     }
103 
104     /**
105      * load the data of DocumentPageTemplate from the table
106      *
107      * @param nPageTemplateId
108      *            The indentifier of the object DocumentPageTemplate
109      * @return The Instance of the object PageTemplate
110      */
111     public DocumentPageTemplate load( int nPageTemplateId )
112     {
113         DocumentPageTemplate documentPageTemplate = null;
114         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
115         daoUtil.setInt( 1, nPageTemplateId );
116 
117         daoUtil.executeQuery( );
118 
119         if ( daoUtil.next( ) )
120         {
121             documentPageTemplate = new DocumentPageTemplate( );
122             documentPageTemplate.setId( daoUtil.getInt( 1 ) );
123             documentPageTemplate.setDescription( daoUtil.getString( 2 ) );
124             documentPageTemplate.setFile( daoUtil.getString( 3 ) );
125             documentPageTemplate.setPicture( daoUtil.getString( 4 ) );
126         }
127 
128         daoUtil.free( );
129 
130         return documentPageTemplate;
131     }
132 
133     /**
134      * Delete a record from the table
135      * 
136      * @param nPageTemplateId
137      *            The indentifier of the object PageTemplate
138      */
139     public void delete( int nPageTemplateId )
140     {
141         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
142         daoUtil.setInt( 1, nPageTemplateId );
143         daoUtil.executeUpdate( );
144         daoUtil.free( );
145     }
146 
147     /**
148      * Update the record in the table
149      * 
150      * @param documentPageTemplate
151      *            The instance of the PageTemplate to update
152      */
153     public void store( DocumentPageTemplate documentPageTemplate )
154     {
155         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
156 
157         daoUtil.setInt( 1, documentPageTemplate.getId( ) );
158         daoUtil.setString( 2, documentPageTemplate.getDescription( ) );
159         daoUtil.setString( 3, documentPageTemplate.getFile( ) );
160         daoUtil.setString( 4, documentPageTemplate.getPicture( ) );
161         daoUtil.setInt( 5, documentPageTemplate.getId( ) );
162 
163         daoUtil.executeUpdate( );
164         daoUtil.free( );
165     }
166 
167     /**
168      * Returns a list of all the page templates
169      * 
170      * @return A list of PageTemplates objects
171      */
172     public List<DocumentPageTemplate> selectPageTemplatesList( )
173     {
174         List<DocumentPageTemplate> listDocumentPageTemplates = new ArrayList<DocumentPageTemplate>( );
175         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL );
176         daoUtil.executeQuery( );
177 
178         while ( daoUtil.next( ) )
179         {
180             DocumentPageTemplatePageTemplate.html#DocumentPageTemplate">DocumentPageTemplate documentPageTemplate = new DocumentPageTemplate( );
181 
182             documentPageTemplate.setId( daoUtil.getInt( 1 ) );
183             documentPageTemplate.setDescription( daoUtil.getString( 2 ) );
184             documentPageTemplate.setFile( daoUtil.getString( 3 ) );
185             documentPageTemplate.setPicture( daoUtil.getString( 4 ) );
186             listDocumentPageTemplates.add( documentPageTemplate );
187         }
188 
189         daoUtil.free( );
190 
191         return listDocumentPageTemplates;
192     }
193 }