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.page;
35  
36  import fr.paris.lutece.portal.business.portlet.Portlet;
37  import fr.paris.lutece.portal.business.portlet.PortletHome;
38  import fr.paris.lutece.portal.service.image.ImageResource;
39  import fr.paris.lutece.util.ReferenceList;
40  import fr.paris.lutece.util.sql.DAOUtil;
41  
42  import java.sql.Timestamp;
43  
44  import java.util.ArrayList;
45  import java.util.Collection;
46  import java.util.List;
47  
48  
49  /**
50   * This class porvides Data Access methods for Page objects
51   */
52  public final class PageDAO implements IPageDAO
53  {
54      // Constants
55      private static final String SQL_QUERY_NEW_PK = "SELECT max(id_page) FROM core_page";
56      private static final String SQL_QUERY_SELECT = "SELECT a.id_parent, a.name, a.description, a.id_template, b.file_name, " +
57          " a.page_order, a.status, a.role , a.code_theme , a.node_status , a.image_content, a.mime_type, " +
58          "  a.date_update, a.meta_keywords, a.meta_description, a.id_authorization_node FROM core_page a, core_page_template b WHERE a.id_template = b.id_template AND a.id_page = ? ";
59      private static final String SQL_QUERY_SELECT_WITHOUT_IMAGE_CONTENT = "SELECT a.id_parent, a.name, a.description, a.id_template, b.file_name, " +
60          " a.page_order, a.status, a.role , a.code_theme , a.node_status , a.mime_type, " +
61          "  a.date_update, a.meta_keywords, a.meta_description FROM core_page a INNER JOIN " +
62          " core_page_template b ON (a.id_template = b.id_template) WHERE a.id_page = ? ";
63      private static final String SQL_QUERY_SELECT_BY_ID_PORTLET = "SELECT a.id_page, a.id_parent, a.name, a.description, a.id_template, " +
64          " a.page_order, a.status, a.role , a.code_theme , a.node_status , a.image_content, a.mime_type, " +
65          "  a.meta_keywords, a.meta_description,a.id_authorization_node FROM core_page a,core_portlet b WHERE a.id_page = b.id_page AND b.id_portlet = ? ";
66      private static final String SQL_QUERY_INSERT = "INSERT INTO core_page ( id_page , id_parent , name , description, date_update, " +
67          " id_template,  page_order, status, role, date_creation, code_theme , node_status, image_content , mime_type ,  " +
68          " meta_keywords, meta_description,id_authorization_node ) " +
69          " VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )";
70      private static final String SQL_QUERY_DELETE = "DELETE FROM core_page WHERE id_page = ?";
71      private static final String SQL_QUERY_UPDATE = "UPDATE core_page SET id_parent = ?,  name = ?, description = ? , date_update = ? , " +
72          " id_template = ? , page_order = ? , status = ? , role = ? , code_theme = ? , node_status = ? , " +
73          " image_content = ? , mime_type = ? , meta_keywords = ?, meta_description = ? ,id_authorization_node=?" +
74          " WHERE id_page = ?";
75      private static final String SQL_QUERY_CHECKPK = "SELECT id_page FROM core_page WHERE id_page = ?";
76      private static final String SQL_QUERY_CHILDPAGE = "SELECT id_page , id_parent, name, description, " +
77          " page_order , status , role, code_theme, image_content, mime_type , meta_keywords, meta_description, date_update,id_authorization_node " +
78          " FROM core_page WHERE id_parent = ? ORDER BY page_order";
79      private static final String SQL_QUERY_CHILDPAGE_MINIMAL_DATA = "SELECT id_page ,id_parent, name, description, role FROM core_page " +
80          " WHERE id_parent = ? ORDER BY page_order";
81      private static final String SQL_QUERY_SELECTALL = "SELECT id_page , id_parent,  name, description, date_update, " +
82          " page_order, status, role, code_theme, image_content, mime_type , meta_keywords, meta_description,id_authorization_node  FROM core_page ";
83      private static final String SQL_QUERY_BY_ROLE_KEY = "SELECT id_page , id_parent,  name, description, date_update, " +
84          " page_order, status, role, code_theme, image_content, mime_type , meta_keywords, meta_description,id_authorization_node  FROM core_page WHERE role = ? ";
85      private static final String SQL_QUERY_SELECT_PORTLET = "SELECT id_portlet FROM core_portlet WHERE id_page = ? ORDER BY portlet_order";
86      private static final String SQL_QUERY_UPDATE_PAGE_DATE = "UPDATE core_page SET date_update = ? WHERE id_page = ?";
87      private static final String SQL_QUERY_SELECTALL_NODE_PAGE = "SELECT id_page, name FROM core_page WHERE node_status = 0";
88      private static final String SQL_QUERY_NEW_CHILD_PAGE_ORDER = "SELECT max(page_order) FROM core_page WHERE id_parent = ?";
89      private static final String SQL_QUERY_CHECK_PAGE_EXIST = "SELECT id_page FROM core_page " + " WHERE id_page = ? ";
90      private static final String SQL_QUERY_SELECT_LAST_MODIFIED_PAGE = "SELECT id_page, id_parent, name, description, id_template, " +
91          " page_order, status, role , code_theme , node_status , mime_type, " +
92          "  date_update, meta_keywords, meta_description,id_authorization_node FROM core_page " +
93          " ORDER BY date_update DESC LIMIT 1";
94  
95      // ImageResource queries
96      private static final String SQL_QUERY_SELECT_RESOURCE_IMAGE = " SELECT image_content , mime_type FROM core_page " +
97          " WHERE id_page = ? ";
98      private static final String SQL_QUERY_SELECT_CHILD_PAGE_FOR_MODIFY_AUTORISATION_NODE = "  SELECT id_page FROM core_page  " +
99          "WHERE id_parent=? AND( id_authorization_node IS NULL OR id_page != id_authorization_node ) ";
100     private static final String SQL_QUERY_UPDATE_AUTORISATION_NODE = " UPDATE core_page SET id_authorization_node = ? WHERE id_page=? ";
101 
102     ///////////////////////////////////////////////////////////////////////////////////////
103     //Access methods to data
104 
105     /**
106      * Generates a new primary key
107      * @return The new primary key
108      */
109     int newPrimaryKey(  )
110     {
111         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK );
112         daoUtil.executeQuery(  );
113 
114         int nKey;
115 
116         if ( !daoUtil.next(  ) )
117         {
118             // if the table is empty
119             nKey = 1;
120         }
121 
122         nKey = daoUtil.getInt( 1 ) + 1;
123 
124         daoUtil.free(  );
125 
126         return nKey;
127     }
128 
129     /**
130      * {@inheritDoc}
131      */
132     public synchronized void insert( Page page )
133     {
134         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
135 
136         int nNewPrimaryKey = newPrimaryKey(  );
137         page.setId( nNewPrimaryKey );
138         page.setOrigParentPageId( page.getParentPageId(  ) );
139 
140         daoUtil.setInt( 1, page.getId(  ) );
141         daoUtil.setInt( 2, page.getParentPageId(  ) );
142         daoUtil.setString( 3, page.getName(  ) );
143         daoUtil.setString( 4, page.getDescription(  ) );
144         page.setDateUpdate( new Timestamp( new java.util.Date(  ).getTime(  ) ) );
145         daoUtil.setTimestamp( 5, page.getDateUpdate(  ) );
146         daoUtil.setInt( 6, page.getPageTemplateId(  ) );
147         daoUtil.setInt( 7, page.getOrder(  ) );
148         daoUtil.setInt( 8, page.getStatus(  ) );
149         daoUtil.setString( 9, page.getRole(  ) );
150 
151         // For a new object, update time = creation time
152         daoUtil.setTimestamp( 10, page.getDateUpdate(  ) );
153         daoUtil.setString( 11, page.getCodeTheme(  ) );
154         daoUtil.setInt( 12, page.getNodeStatus(  ) );
155         daoUtil.setBytes( 13, page.getImageContent(  ) );
156         daoUtil.setString( 14, page.getMimeType(  ) );
157 
158         if ( ( page.getMetaKeywords(  ) != null ) && ( page.getMetaKeywords(  ).length(  ) > 0 ) )
159         {
160             daoUtil.setString( 15, page.getMetaKeywords(  ) );
161         }
162         else
163         {
164             daoUtil.setString( 15, null );
165         }
166 
167         if ( ( page.getMetaDescription(  ) != null ) && ( page.getMetaDescription(  ).length(  ) > 0 ) )
168         {
169             daoUtil.setString( 16, page.getMetaDescription(  ) );
170         }
171         else
172         {
173             daoUtil.setString( 16, null );
174         }
175 
176         if ( page.getIdAuthorizationNode(  ) != null )
177         {
178             daoUtil.setInt( 17, page.getIdAuthorizationNode(  ) );
179         }
180         else
181         {
182             daoUtil.setIntNull( 17 );
183         }
184 
185         daoUtil.executeUpdate(  );
186         daoUtil.free(  );
187     }
188 
189     /**
190      * {@inheritDoc}
191      */
192     public Page load( int nPageId, boolean bPortlets )
193     {
194         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
195         daoUtil.setInt( 1, nPageId );
196 
197         daoUtil.executeQuery(  );
198 
199         Page page = new Page(  );
200 
201         if ( daoUtil.next(  ) )
202         {
203             page.setId( nPageId );
204             page.setParentPageId( daoUtil.getInt( 1 ) );
205             page.setOrigParentPageId( daoUtil.getInt( 1 ) );
206             page.setName( daoUtil.getString( 2 ) );
207             page.setDescription( daoUtil.getString( 3 ) );
208             page.setPageTemplateId( daoUtil.getInt( 4 ) );
209             page.setTemplate( daoUtil.getString( 5 ) );
210             page.setOrder( daoUtil.getInt( 6 ) );
211             page.setStatus( daoUtil.getInt( 7 ) );
212             page.setRole( daoUtil.getString( 8 ) );
213             page.setCodeTheme( daoUtil.getString( 9 ) );
214             page.setNodeStatus( daoUtil.getInt( 10 ) );
215             page.setImageContent( daoUtil.getBytes( 11 ) );
216             page.setMimeType( daoUtil.getString( 12 ) );
217             page.setDateUpdate( daoUtil.getTimestamp( 13 ) );
218             page.setMetaKeywords( daoUtil.getString( 14 ) );
219             page.setMetaDescription( daoUtil.getString( 15 ) );
220 
221             if ( daoUtil.getObject( 16 ) != null )
222             {
223                 page.setIdAuthorizationNode( daoUtil.getInt( 16 ) );
224             }
225 
226             // Patch perfs : close connection before loadPortlets
227             daoUtil.free(  );
228 
229             // Loads the portlets contained into the page
230             if ( bPortlets )
231             {
232                 loadPortlets( page );
233             }
234         }
235 
236         daoUtil.free(  );
237 
238         return page;
239     }
240 
241     /**
242      * {@inheritDoc}
243      */
244     public Page loadWithoutImageContent( int nPageId, boolean bPortlets )
245     {
246         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_WITHOUT_IMAGE_CONTENT );
247         daoUtil.setInt( 1, nPageId );
248 
249         daoUtil.executeQuery(  );
250 
251         Page page = new Page(  );
252 
253         if ( daoUtil.next(  ) )
254         {
255             page.setId( nPageId );
256             page.setParentPageId( daoUtil.getInt( 1 ) );
257             page.setOrigParentPageId( daoUtil.getInt( 1 ) );
258             page.setName( daoUtil.getString( 2 ) );
259             page.setDescription( daoUtil.getString( 3 ) );
260             page.setPageTemplateId( daoUtil.getInt( 4 ) );
261             page.setTemplate( daoUtil.getString( 5 ) );
262             page.setOrder( daoUtil.getInt( 6 ) );
263             page.setStatus( daoUtil.getInt( 7 ) );
264             page.setRole( daoUtil.getString( 8 ) );
265             page.setCodeTheme( daoUtil.getString( 9 ) );
266             page.setNodeStatus( daoUtil.getInt( 10 ) );
267             page.setMimeType( daoUtil.getString( 11 ) );
268             page.setDateUpdate( daoUtil.getTimestamp( 12 ) );
269             page.setMetaKeywords( daoUtil.getString( 13 ) );
270             page.setMetaDescription( daoUtil.getString( 14 ) );
271 
272             if ( daoUtil.getObject( 15 ) != null )
273             {
274                 page.setIdAuthorizationNode( daoUtil.getInt( 15 ) );
275             }
276 
277             // Patch perfs : close connection before loadPortlets
278             daoUtil.free(  );
279 
280             // Loads the portlets contained into the page
281             if ( bPortlets )
282             {
283                 loadPortlets( page );
284             }
285         }
286 
287         daoUtil.free(  );
288 
289         return page;
290     }
291 
292     /**
293      * {@inheritDoc}
294      */
295     public Page loadPageByIdPortlet( int nPorletId )
296     {
297         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_ID_PORTLET );
298         daoUtil.setInt( 1, nPorletId );
299 
300         daoUtil.executeQuery(  );
301 
302         Page page = new Page(  );
303 
304         if ( daoUtil.next(  ) )
305         {
306             page.setId( daoUtil.getInt( 1 ) );
307             page.setParentPageId( daoUtil.getInt( 2 ) );
308             page.setOrigParentPageId( daoUtil.getInt( 2 ) );
309             page.setName( daoUtil.getString( 3 ) );
310             page.setDescription( daoUtil.getString( 4 ) );
311             page.setPageTemplateId( daoUtil.getInt( 5 ) );
312             page.setOrder( daoUtil.getInt( 6 ) );
313             page.setStatus( daoUtil.getInt( 7 ) );
314             page.setRole( daoUtil.getString( 8 ) );
315             page.setCodeTheme( daoUtil.getString( 9 ) );
316             page.setNodeStatus( daoUtil.getInt( 10 ) );
317             page.setImageContent( daoUtil.getBytes( 11 ) );
318             page.setMimeType( daoUtil.getString( 12 ) );
319             page.setMetaKeywords( daoUtil.getString( 13 ) );
320             page.setMetaDescription( daoUtil.getString( 14 ) );
321             page.setIdAuthorizationNode( daoUtil.getInt( 15 ) );
322         }
323 
324         daoUtil.free(  );
325 
326         return page;
327     }
328 
329     /**
330      * {@inheritDoc}
331      */
332     public void delete( int nPageId )
333     {
334         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
335         daoUtil.setInt( 1, nPageId );
336 
337         daoUtil.executeUpdate(  );
338         daoUtil.free(  );
339     }
340 
341     /**
342      * {@inheritDoc}
343      */
344     public void store( Page page )
345     {
346         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
347 
348         daoUtil.setInt( 1, page.getParentPageId(  ) );
349         daoUtil.setString( 2, page.getName(  ) );
350         daoUtil.setString( 3, page.getDescription(  ) );
351         page.setDateUpdate( new Timestamp( new java.util.Date(  ).getTime(  ) ) );
352         daoUtil.setTimestamp( 4, page.getDateUpdate(  ) );
353         daoUtil.setInt( 5, page.getPageTemplateId(  ) );
354         daoUtil.setInt( 6, page.getOrder(  ) );
355         daoUtil.setInt( 7, page.getStatus(  ) );
356         daoUtil.setString( 8, page.getRole(  ) );
357         daoUtil.setString( 9, page.getCodeTheme(  ) );
358         daoUtil.setInt( 10, page.getNodeStatus(  ) );
359         daoUtil.setBytes( 11, page.getImageContent(  ) );
360         daoUtil.setString( 12, page.getMimeType(  ) );
361 
362         if ( ( page.getMetaKeywords(  ) != null ) && ( page.getMetaKeywords(  ).length(  ) > 0 ) )
363         {
364             daoUtil.setString( 13, page.getMetaKeywords(  ) );
365         }
366         else
367         {
368             daoUtil.setString( 13, null );
369         }
370 
371         if ( ( page.getMetaDescription(  ) != null ) && ( page.getMetaDescription(  ).length(  ) > 0 ) )
372         {
373             daoUtil.setString( 14, page.getMetaDescription(  ) );
374         }
375         else
376         {
377             daoUtil.setString( 14, null );
378         }
379 
380         if ( page.getIdAuthorizationNode(  ) != null )
381         {
382             daoUtil.setInt( 15, page.getIdAuthorizationNode(  ) );
383         }
384         else
385         {
386             daoUtil.setIntNull( 15 );
387         }
388 
389         daoUtil.setInt( 16, page.getId(  ) );
390 
391         daoUtil.executeUpdate(  );
392         daoUtil.free(  );
393     }
394 
395     /**
396      * Checks if the page identifier exists
397      * @param nKey  The page identifier
398      * @return true if the identifier exists, false if not
399      */
400     boolean checkPrimaryKey( int nKey )
401     {
402         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECKPK );
403 
404         daoUtil.setInt( 1, nKey );
405         daoUtil.executeQuery(  );
406 
407         if ( !daoUtil.next(  ) )
408         {
409             daoUtil.free(  );
410 
411             return false;
412         }
413 
414         daoUtil.free(  );
415 
416         return true;
417     }
418 
419     /**
420      * loads the portlets list contained into the page
421      *
422      * @param page The object page
423      */
424     void loadPortlets( Page page )
425     {
426         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PORTLET );
427         daoUtil.setInt( 1, page.getId(  ) );
428 
429         daoUtil.executeQuery(  );
430 
431         // Patch perfs :  get query responses and close connection before getting portlet
432         ArrayList<Integer> portletIds = new ArrayList<Integer>(  );
433 
434         while ( daoUtil.next(  ) )
435         {
436             portletIds.add( Integer.valueOf( daoUtil.getInt( 1 ) ) );
437         }
438 
439         daoUtil.free(  );
440 
441         ArrayList<Portlet> pageColl = new ArrayList<Portlet>(  );
442 
443         for ( Integer nPortletId : portletIds )
444         {
445             Portlet portlet = PortletHome.findByPrimaryKey( nPortletId );
446             pageColl.add( portlet );
447         }
448 
449         page.setPortlets( pageColl );
450     }
451 
452     /**
453      * {@inheritDoc}
454      */
455     public Collection<Page> selectChildPages( int nParentPageId )
456     {
457         Collection<Page> pageList = new ArrayList<Page>(  );
458         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHILDPAGE );
459         daoUtil.setInt( 1, nParentPageId );
460 
461         daoUtil.executeQuery(  );
462 
463         while ( daoUtil.next(  ) )
464         {
465             Page page = new Page(  );
466 
467             page.setId( daoUtil.getInt( 1 ) );
468             page.setParentPageId( daoUtil.getInt( 2 ) );
469             page.setOrigParentPageId( daoUtil.getInt( 2 ) );
470             page.setName( daoUtil.getString( 3 ) );
471             page.setDescription( daoUtil.getString( 4 ) );
472             page.setOrder( daoUtil.getInt( 5 ) );
473             page.setStatus( daoUtil.getInt( 6 ) );
474             page.setRole( daoUtil.getString( 7 ) );
475             page.setCodeTheme( daoUtil.getString( 8 ) );
476             page.setImageContent( daoUtil.getBytes( 9 ) );
477             page.setMimeType( daoUtil.getString( 10 ) );
478             page.setMetaKeywords( daoUtil.getString( 11 ) );
479             page.setMetaDescription( daoUtil.getString( 12 ) );
480             page.setDateUpdate( daoUtil.getTimestamp( 13 ) );
481 
482             if ( daoUtil.getObject( 14 ) != null )
483             {
484                 page.setIdAuthorizationNode( daoUtil.getInt( 14 ) );
485             }
486 
487             pageList.add( page );
488         }
489 
490         daoUtil.free(  );
491 
492         return pageList;
493     }
494 
495     /**
496      * {@inheritDoc}
497      */
498     public Collection<Page> selectChildPagesMinimalData( int nParentPageId )
499     {
500         Collection<Page> pageList = new ArrayList<Page>(  );
501         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHILDPAGE_MINIMAL_DATA );
502         daoUtil.setInt( 1, nParentPageId );
503 
504         daoUtil.executeQuery(  );
505 
506         while ( daoUtil.next(  ) )
507         {
508             Page page = new Page(  );
509             page.setId( daoUtil.getInt( 1 ) );
510             page.setParentPageId( daoUtil.getInt( 2 ) );
511             page.setOrigParentPageId( daoUtil.getInt( 2 ) );
512             page.setName( daoUtil.getString( 3 ) );
513             page.setDescription( daoUtil.getString( 4 ) );
514             page.setRole( daoUtil.getString( 5 ) );
515             pageList.add( page );
516         }
517 
518         daoUtil.free(  );
519 
520         return pageList;
521     }
522 
523     /**
524      * {@inheritDoc}
525      */
526     public List<Page> selectAllPages(  )
527     {
528         List<Page> pageList = new ArrayList<Page>(  );
529         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL );
530         daoUtil.executeQuery(  );
531 
532         while ( daoUtil.next(  ) )
533         {
534             Page page = new Page(  );
535 
536             page.setId( daoUtil.getInt( 1 ) );
537             page.setParentPageId( daoUtil.getInt( 2 ) );
538             page.setOrigParentPageId( daoUtil.getInt( 2 ) );
539             page.setName( daoUtil.getString( 3 ) );
540             page.setDescription( daoUtil.getString( 4 ) );
541             page.setDateUpdate( daoUtil.getTimestamp( 5 ) );
542             page.setOrder( daoUtil.getInt( 6 ) );
543             page.setStatus( daoUtil.getInt( 7 ) );
544             page.setRole( daoUtil.getString( 8 ) );
545             page.setCodeTheme( daoUtil.getString( 9 ) );
546             page.setImageContent( daoUtil.getBytes( 10 ) );
547             page.setMimeType( daoUtil.getString( 11 ) );
548             page.setMetaKeywords( daoUtil.getString( 12 ) );
549             page.setMetaDescription( daoUtil.getString( 13 ) );
550 
551             if ( daoUtil.getObject( 14 ) != null )
552             {
553                 page.setIdAuthorizationNode( daoUtil.getInt( 14 ) );
554             }
555 
556             pageList.add( page );
557         }
558 
559         daoUtil.free(  );
560 
561         return pageList;
562     }
563 
564     /**
565      * {@inheritDoc}
566      */
567     public void invalidatePage( int nPageId )
568     {
569         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_PAGE_DATE );
570 
571         daoUtil.setTimestamp( 1, new Timestamp( new java.util.Date(  ).getTime(  ) ) );
572         daoUtil.setInt( 2, nPageId );
573 
574         daoUtil.executeUpdate(  );
575         daoUtil.free(  );
576     }
577 
578     /**
579      * {@inheritDoc}
580      */
581     public ReferenceList getPagesList(  )
582     {
583         ReferenceList listPages = new ReferenceList(  );
584         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_NODE_PAGE );
585         daoUtil.executeQuery(  );
586 
587         while ( daoUtil.next(  ) )
588         {
589             Page page = new Page(  );
590             page.setId( daoUtil.getInt( 1 ) );
591             page.setName( daoUtil.getString( 2 ) );
592             listPages.addItem( page.getId(  ), page.getName(  ) + " ( " + page.getId(  ) + " )" );
593         }
594 
595         daoUtil.free(  );
596 
597         return listPages;
598     }
599 
600     /**
601      * Return the list of all the pages filtered by Lutece Role specified in parameter
602      *
603      * @param strRoleKey The Lutece Role key
604      * @return a collection of pages
605      */
606     public Collection<Page> getPagesByRoleKey( String strRoleKey )
607     {
608         Collection<Page> pageList = new ArrayList<Page>(  );
609         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_BY_ROLE_KEY );
610         daoUtil.setString( 1, strRoleKey );
611 
612         daoUtil.executeQuery(  );
613 
614         while ( daoUtil.next(  ) )
615         {
616             Page page = new Page(  );
617 
618             page.setId( daoUtil.getInt( 1 ) );
619             page.setParentPageId( daoUtil.getInt( 2 ) );
620             page.setOrigParentPageId( daoUtil.getInt( 2 ) );
621             page.setName( daoUtil.getString( 3 ) );
622             page.setDescription( daoUtil.getString( 4 ) );
623             page.setDateUpdate( daoUtil.getTimestamp( 5 ) );
624             page.setOrder( daoUtil.getInt( 6 ) );
625             page.setStatus( daoUtil.getInt( 7 ) );
626             page.setRole( daoUtil.getString( 8 ) );
627             page.setCodeTheme( daoUtil.getString( 9 ) );
628             page.setImageContent( daoUtil.getBytes( 10 ) );
629             page.setMimeType( daoUtil.getString( 11 ) );
630             page.setMetaKeywords( daoUtil.getString( 12 ) );
631             page.setMetaDescription( daoUtil.getString( 13 ) );
632 
633             if ( daoUtil.getObject( 14 ) != null )
634             {
635                 page.setIdAuthorizationNode( daoUtil.getInt( 14 ) );
636             }
637 
638             pageList.add( page );
639         }
640 
641         daoUtil.free(  );
642 
643         return pageList;
644     }
645 
646     /**
647      * {@inheritDoc}
648      */
649     public int selectNewChildPageOrder( int nParentPageId )
650     {
651         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_CHILD_PAGE_ORDER );
652         daoUtil.setInt( 1, nParentPageId );
653         daoUtil.executeQuery(  );
654 
655         int nPageOrder;
656 
657         if ( !daoUtil.next(  ) )
658         {
659             // if the table is empty
660             nPageOrder = 1;
661         }
662 
663         nPageOrder = daoUtil.getInt( 1 ) + 1;
664 
665         daoUtil.free(  );
666 
667         return nPageOrder;
668     }
669 
670     /**
671      * {@inheritDoc}
672      */
673     public ImageResource loadImageResource( int nIdPage )
674     {
675         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_RESOURCE_IMAGE );
676         daoUtil.setInt( 1, nIdPage );
677         daoUtil.executeQuery(  );
678 
679         ImageResource image = null;
680 
681         if ( daoUtil.next(  ) )
682         {
683             image = new ImageResource(  );
684             image.setImage( daoUtil.getBytes( 1 ) );
685             image.setMimeType( daoUtil.getString( 2 ) );
686         }
687 
688         daoUtil.free(  );
689 
690         return image;
691     }
692 
693     /**
694      * Tests if page exist
695      *
696      * @param nPageId The identifier of the document
697      * @return true if the page existed, false otherwise
698      */
699     public boolean checkPageExist( int nPageId )
700     {
701         boolean bPageExisted = false;
702         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_PAGE_EXIST );
703 
704         daoUtil.setInt( 1, nPageId );
705         daoUtil.executeQuery(  );
706 
707         if ( daoUtil.next(  ) )
708         {
709             bPageExisted = true;
710         }
711 
712         daoUtil.free(  );
713 
714         return bPageExisted;
715     }
716 
717     /**
718      * {@inheritDoc}
719      */
720     public Page loadLastModifiedPage(  )
721     {
722         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_LAST_MODIFIED_PAGE );
723 
724         daoUtil.executeQuery(  );
725 
726         Page page = null;
727 
728         if ( daoUtil.next(  ) )
729         {
730             page = new Page(  );
731 
732             int nIndex = 1;
733             page.setId( daoUtil.getInt( nIndex++ ) );
734             page.setParentPageId( daoUtil.getInt( nIndex++ ) );
735             page.setOrigParentPageId( page.getParentPageId(  ) );
736             page.setName( daoUtil.getString( nIndex++ ) );
737             page.setDescription( daoUtil.getString( nIndex++ ) );
738             page.setPageTemplateId( daoUtil.getInt( nIndex++ ) );
739             page.setOrder( daoUtil.getInt( nIndex++ ) );
740             page.setStatus( daoUtil.getInt( nIndex++ ) );
741             page.setRole( daoUtil.getString( nIndex++ ) );
742             page.setCodeTheme( daoUtil.getString( nIndex++ ) );
743             page.setNodeStatus( daoUtil.getInt( nIndex++ ) );
744             page.setMimeType( daoUtil.getString( nIndex++ ) );
745             page.setDateUpdate( daoUtil.getTimestamp( nIndex++ ) );
746             page.setMetaKeywords( daoUtil.getString( nIndex++ ) );
747             page.setMetaDescription( daoUtil.getString( nIndex++ ) );
748 
749             if ( daoUtil.getObject( nIndex ) != null )
750             {
751                 page.setIdAuthorizationNode( daoUtil.getInt( nIndex ) );
752             }
753         }
754 
755         daoUtil.free(  );
756 
757         return page;
758     }
759 
760     /**
761      * {@inheritDoc }
762      */
763     public void updateAutorisationNode( int nIdPage, Integer nIdAutorisationNode )
764     {
765         StringBuilder strSQl = new StringBuilder(  );
766         strSQl.append( SQL_QUERY_UPDATE_AUTORISATION_NODE );
767 
768         DAOUtil daoUtil = new DAOUtil( strSQl.toString(  ) );
769 
770         if ( nIdAutorisationNode != null )
771         {
772             daoUtil.setInt( 1, nIdAutorisationNode );
773         }
774         else
775         {
776             daoUtil.setIntNull( 1 );
777         }
778 
779         daoUtil.setInt( 2, nIdPage );
780 
781         daoUtil.executeUpdate(  );
782         daoUtil.free(  );
783     }
784 
785     /**
786      * {@inheritDoc }
787      */
788     public List<Integer> selectPageForChangeAutorisationNode( int nIdParentPage )
789     {
790         List<Integer> listIdPage = new ArrayList<Integer>(  );
791         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CHILD_PAGE_FOR_MODIFY_AUTORISATION_NODE );
792 
793         daoUtil.setInt( 1, nIdParentPage );
794 
795         daoUtil.executeQuery(  );
796 
797         while ( daoUtil.next(  ) )
798         {
799             listIdPage.add( daoUtil.getInt( 1 ) );
800         }
801 
802         daoUtil.free(  );
803 
804         return listIdPage;
805     }
806 }