View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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.htmldocs.business;
35  
36  import fr.paris.lutece.util.sql.DAOUtil;
37  
38  import java.util.ArrayList;
39  import java.util.List;
40  
41  
42  /**
43   * This class provides Data Access methods for DocumentPageTemplate objects
44   */
45  public final class DocumentPageTemplateDAO implements IDocumentPageTemplateDAO
46  {
47      // Constants
48      private static final String SQL_QUERY_NEW_PK = " SELECT max( id_page_template_document ) FROM htmldocs_page_template";
49      private static final String SQL_QUERY_SELECT = " SELECT id_page_template_document, description, page_template_path, picture_path FROM htmldocs_page_template WHERE id_page_template_document = ?";
50      private static final String SQL_QUERY_INSERT = " INSERT INTO htmldocs_page_template ( id_page_template_document, description, page_template_path, picture_path ) VALUES ( ?, ?, ?, ? )";
51      private static final String SQL_QUERY_DELETE = " DELETE FROM htmldocs_page_template WHERE id_page_template_document = ?";
52      private static final String SQL_QUERY_UPDATE = " UPDATE htmldocs_page_template SET id_page_template_document = ?, description = ?, page_template_path = ?, picture_path = ? " +
53          " WHERE id_page_template_document = ?";
54      private static final String SQL_QUERY_SELECTALL = " SELECT id_page_template_document , description, page_template_path, picture_path FROM htmldocs_page_template ORDER BY id_page_template_document ";
55  
56      ///////////////////////////////////////////////////////////////////////////////////////
57      //Access methods to data
58  
59      /**
60       * Generates a new primary key
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       * @param documentPageTemplate The Instance of the object DocumentPageTemplate
86       */
87      public synchronized void insert( DocumentPageTemplate documentPageTemplate )
88      {
89          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
90  
91          documentPageTemplate.setId( newPrimaryKey(  ) );
92  
93          daoUtil.setInt( 1, documentPageTemplate.getId(  ) );
94          daoUtil.setString( 2, documentPageTemplate.getDescription(  ) );
95          daoUtil.setString( 3, documentPageTemplate.getFile(  ) );
96          daoUtil.setString( 4, documentPageTemplate.getPicture(  ) );
97  
98          daoUtil.executeUpdate(  );
99          daoUtil.free(  );
100     }
101 
102     /**
103      * load the data of DocumentPageTemplate from the table
104      *
105      * @param nPageTemplateId The indentifier of the object DocumentPageTemplate
106      * @return The Instance of the object PageTemplate
107      */
108     public DocumentPageTemplate load( int nPageTemplateId )
109     {
110         DocumentPageTemplate documentPageTemplate = null;
111         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
112         daoUtil.setInt( 1, nPageTemplateId );
113 
114         daoUtil.executeQuery(  );
115 
116         if ( daoUtil.next(  ) )
117         {
118             documentPageTemplate = new DocumentPageTemplate(  );
119             documentPageTemplate.setId( daoUtil.getInt( 1 ) );
120             documentPageTemplate.setDescription( daoUtil.getString( 2 ) );
121             documentPageTemplate.setFile( daoUtil.getString( 3 ) );
122             documentPageTemplate.setPicture( daoUtil.getString( 4 ) );
123         }
124 
125         daoUtil.free(  );
126 
127         return documentPageTemplate;
128     }
129 
130     /**
131      * Delete a record from the table
132      * @param nPageTemplateId The indentifier of the object PageTemplate
133      */
134     public void delete( int nPageTemplateId )
135     {
136         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
137         daoUtil.setInt( 1, nPageTemplateId );
138         daoUtil.executeUpdate(  );
139         daoUtil.free(  );
140     }
141 
142     /**
143      * Update the record in the table
144      * @param documentPageTemplate The instance of the PageTemplate to update
145      */
146     public void store( DocumentPageTemplate documentPageTemplate )
147     {
148         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
149 
150         daoUtil.setInt( 1, documentPageTemplate.getId(  ) );
151         daoUtil.setString( 2, documentPageTemplate.getDescription(  ) );
152         daoUtil.setString( 3, documentPageTemplate.getFile(  ) );
153         daoUtil.setString( 4, documentPageTemplate.getPicture(  ) );
154         daoUtil.setInt( 5, documentPageTemplate.getId(  ) );
155 
156         daoUtil.executeUpdate(  );
157         daoUtil.free(  );
158     }
159 
160     /**
161      * Returns a list of all the page templates
162      * @return A list of PageTemplates objects
163      */
164     public List<DocumentPageTemplate> selectPageTemplatesList(  )
165     {
166         List<DocumentPageTemplate> listDocumentPageTemplates = new ArrayList<DocumentPageTemplate>(  );
167         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL );
168         daoUtil.executeQuery(  );
169 
170         while ( daoUtil.next(  ) )
171         {
172             DocumentPageTemplate documentPageTemplate = new DocumentPageTemplate(  );
173 
174             documentPageTemplate.setId( daoUtil.getInt( 1 ) );
175             documentPageTemplate.setDescription( daoUtil.getString( 2 ) );
176             documentPageTemplate.setFile( daoUtil.getString( 3 ) );
177             documentPageTemplate.setPicture( daoUtil.getString( 4 ) );
178             listDocumentPageTemplates.add( documentPageTemplate );
179         }
180 
181         daoUtil.free(  );
182 
183         return listDocumentPageTemplates;
184     }
185 }