View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.document.modules.multirootindexers.util;
35  
36  import fr.paris.lutece.portal.business.page.Page;
37  import fr.paris.lutece.portal.business.page.PageHome;
38  import fr.paris.lutece.portal.service.portal.PortalService;
39  import fr.paris.lutece.portal.service.util.AppPropertiesService;
40  
41  import java.util.ArrayList;
42  import java.util.Collection;
43  import java.util.List;
44  import java.util.Set;
45  import java.util.TreeSet;
46  
47  
48  /**
49   * This class provides utility methods.
50   */
51  public final class PageTreeUtils
52  {
53      /** Regexp used to skip attributes */
54      public static final String NOT_INDEXED_REGEXP = AppPropertiesService.getProperty( 
55              "document-multirootindexers.document.attributes.notindexed" );
56  
57      /**
58       * Private constructor.
59       */
60      private PageTreeUtils(  )
61      {
62          // nothing
63      }
64  
65      /**
66       * Gets all pages from root page (instead of all db page)
67       * @return all pages from root id
68       */
69      public static List<Page> getListPagesFromRoot(  )
70      {
71          int nRootId = PortalService.getRootPageId(  );
72          List<Page> listPages = new ArrayList<Page>(  );
73  
74          Page rootPage = PageHome.findByPrimaryKey( nRootId );
75  
76          listPages.add( rootPage );
77          listPages.addAll( getChildPages( nRootId ) );
78  
79          return listPages;
80      }
81  
82      /**
83       * Gets all pages id from root page
84       * @return all pages id from root id.
85       */
86      public static Set<Integer> getListPagesIdsFromRoot(  )
87      {
88          int nRootId = PortalService.getRootPageId(  );
89          Set<Integer> setPagesIds = new TreeSet<Integer>(  );
90  
91          setPagesIds.add( nRootId );
92          setPagesIds.addAll( getListChildPagesids( nRootId ) );
93  
94          return setPagesIds;
95      }
96  
97      /**
98       * Gets children pages page (recursively)
99       * @param nIdPage root page
100      * @return all childrenpages
101      */
102     private static Collection<Page> getChildPages( int nIdPage )
103     {
104         Collection<Page> collectionPages = PageHome.getChildPages( nIdPage );
105         List<Page> listChildsPage = new ArrayList<Page>(  );
106 
107         for ( Page page : collectionPages )
108         {
109             listChildsPage.addAll( getChildPages( page.getId(  ) ) );
110         }
111 
112         collectionPages.addAll( listChildsPage );
113 
114         return collectionPages;
115     }
116 
117     /**
118      * Gets children pages id page (recursively)
119      * @param nIdPage root page
120      * @return all childrenpages
121      */
122     private static Set<Integer> getListChildPagesids( int nIdPage )
123     {
124         Set<Integer> setPagesIds = new TreeSet<Integer>(  );
125         Collection<Page> collectionPages = PageHome.getChildPagesMinimalData( nIdPage );
126         Set<Integer> setChildPagesIds = new TreeSet<Integer>(  );
127 
128         for ( Page page : collectionPages )
129         {
130             setChildPagesIds.addAll( getListChildPagesids( page.getId(  ) ) );
131             setPagesIds.add( page.getId(  ) );
132         }
133 
134         setPagesIds.addAll( setChildPagesIds );
135 
136         return setPagesIds;
137     }
138 }