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