View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.services.wcm.publication;
18  
19  import org.exoplatform.commons.exception.ExoMessageException;
20  import org.exoplatform.commons.utils.PageList;
21  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  /**
27   * Created by The eXo Platform SAS
28   * Author : Hoa Pham
29   * hoa.phamvu@exoplatform.com
30   * Oct 17, 2008
31   */
32  @SuppressWarnings({ "deprecation", "unchecked" })
33  public class PaginatedResultIterator extends PageList {
34  
35    /** The nodes. */
36    protected Result result;
37  
38    /**
39     * Instantiates a new paginated node iterator.
40     *
41     * @param pageSize the page size
42     */
43    public PaginatedResultIterator(int pageSize) {
44      super(pageSize);
45    }
46  
47    public PaginatedResultIterator(Result result, int pageSize) {
48       super(pageSize);
49       this.result = result;
50       this.setAvailablePage((int)result.getNumTotal());
51       this.currentListPage_ = null;
52    }
53  
54    /* (non-Javadoc)
55     * @see org.exoplatform.commons.utils.PageList#populateCurrentPage(int)
56     */
57    protected void populateCurrentPage(int page) throws Exception {
58      if(page == currentPage_) {
59        if(currentListPage_ != null)
60          return;
61      }
62      currentListPage_ = new ArrayList();
63  
64      WCMComposer composer = WCMCoreUtils.getService(WCMComposer.class);
65      result.getFiltersDescriber().put(WCMComposer.FILTER_LIMIT, ""+this.getPageSize());
66      result.getFiltersDescriber().put(WCMComposer.FILTER_OFFSET, ""+(this.getPageSize()*(page-1)));
67      result.getFiltersDescriber().put(WCMComposer.FILTER_TOTAL, ""+this.result.getNumTotal());
68      result = composer.getPaginatedContents(result.getNodeLocationDescriber(),
69                                             result.getFiltersDescriber(),
70                                             WCMCoreUtils.getUserSessionProvider());
71  
72      currentListPage_ = result.getNodes();
73  
74      currentPage_ = page;
75    }
76  
77    /**
78     * Retrieve the total pages.
79     * 
80     * @return the total pages
81     */
82    public int getTotalPages() { return getAvailablePage(); }  
83  
84    /**
85     * Retrieve the nodes per page.
86     * 
87     * @return the nodes per page
88     */
89    public int getNodesPerPage() { return getPageSize(); }    
90  
91    /**
92     * Retrieve the total nodes.
93     * 
94     * @return the total nodes
95     */
96    public long getTotalNodes() {
97      return result.getNumTotal();
98    }
99  
100   /**
101    * Retrieve the nodes of current page.
102    * 
103    * @return the current page data
104    * 
105    * @throws Exception the exception
106    */
107   public List getCurrentPageData() throws Exception {
108     return currentPage();
109   }
110   
111   /* (non-Javadoc)
112    * @see org.exoplatform.commons.utils.PageList#getPage(int)
113    */
114   public List getPage(int page) throws Exception {
115     if (page < 1 || page > availablePage_) {
116       Object[] args = { Integer.toString(page), Integer.toString(availablePage_) };
117       throw new ExoMessageException("PageList.page-out-of-range", args);
118     }
119     populateCurrentPage(page);
120     while (currentListPage_ != null && currentListPage_.size() == 0 && page > 1) {
121       page--;
122       result.setNumTotal(page * this.getPageSize());
123       populateCurrentPage(page);
124       availablePage_ = page;
125     }
126 
127     return currentListPage_;
128     
129   }
130 
131   /**
132    * Change page.
133    * 
134    * @param page the page
135    * 
136    * @throws Exception the exception
137    */
138   public void changePage(int page) throws Exception {
139     populateCurrentPage(page);
140   }
141 
142   /* (non-Javadoc)
143    * @see org.exoplatform.commons.utils.PageList#getAll()
144    */
145   public List getAll() throws Exception {
146     throw new UnsupportedOperationException();
147   }
148 }