View Javadoc
1   /*
2    * Copyright (C) 2003-2011 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.search.base;
18  
19  import java.util.Comparator;
20  import java.util.List;
21  
22  import org.exoplatform.commons.exception.ExoMessageException;
23  import org.exoplatform.commons.utils.PageList;
24  import org.exoplatform.services.log.ExoLogger;
25  import org.exoplatform.services.log.Log;
26  
27  /**
28   * Created by The eXo Platform SAS
29   * Author : Nguyen Anh Vu
30   *          anhvurz90@gmail.com
31   * Jun 17, 2011  
32   */
33  @SuppressWarnings("deprecation")
34  public abstract class AbstractPageList<E> extends PageList<E> {
35    
36    public static final int DEFAULT_PAGE_SIZE = 10;
37    
38    public static final int DEAFAULT_BUFFER_SIZE = 100;
39    
40    public static final int RESULT_SIZE_SEPARATOR = 100;
41    
42    private static final Log LOG  = ExoLogger.getLogger(AbstractPageList.class.getName());
43  
44    /** The spell suggestion. */
45    protected String spellSuggestion;
46    /** The query time. */
47    protected long queryTime;
48    /** The node filter */
49    protected NodeSearchFilter filter;
50    /** The data creator */
51    protected SearchDataCreator<E> searchDataCreator;
52    /** The comparator for searching */
53    protected Comparator<E> comparator;
54    /** Sort by */
55    protected String sortByField;
56    /** oder to sort */
57    protected String order;
58    /** The offset */
59    protected int offset_;
60    
61    protected boolean loadedAllData_ = true;
62    
63    public Comparator<E> getComparator() {
64      return comparator;
65    }
66  
67    public void setComparator(Comparator<E> comparator) {
68      this.comparator = comparator;
69    }
70  
71    public String getSortByField() {
72      return sortByField;
73    }
74  
75    public void setSortByField(String sortByField) {
76      this.sortByField = sortByField;
77    }
78  
79    public String getOrder() {
80      return order;
81    }
82  
83    public void setOrder(String order) {
84      this.order = order;
85    }
86  
87    /** The constructor */
88    public AbstractPageList(int pageSize) {
89      super(pageSize);
90    }
91    
92    /** The constructor */
93    public AbstractPageList(int pageSize, NodeSearchFilter filter, SearchDataCreator<E> creator) {
94      this(pageSize);
95      this.filter = filter;
96      this.searchDataCreator = creator;
97    }
98    
99    public String getSpellSuggestion() {
100     return spellSuggestion;
101   }
102 
103   public void setSpellSuggestion(String spellSuggestion) {
104     this.spellSuggestion = spellSuggestion;
105   }
106   
107   public long getQueryTime() {
108     return queryTime;
109   }
110 
111   public void setQueryTime(long queryTime) {
112     this.queryTime = queryTime;
113   }
114 
115   public NodeSearchFilter getFilter() {
116     return filter;
117   }
118 
119   public void setFilter(NodeSearchFilter filter) {
120     this.filter = filter;
121   }
122 
123   public SearchDataCreator<E> getSearchDataCreator() {
124     return searchDataCreator;
125   }
126   
127   public void setSearchDataCreator(SearchDataCreator<E> searchDataCreator) {
128     this.searchDataCreator = searchDataCreator;
129   }
130   
131   @SuppressWarnings("unchecked")
132   public List currentPage() throws Exception
133   {
134     populateCurrentPage(currentPage_);
135     return currentListPage_;
136   }
137 
138   @Override
139   protected void checkAndSetPage(int page) throws Exception
140   {
141      currentPage_ = page;
142      if (page < 1 || page > availablePage_ + offset_/getPageSize())
143      {
144         Object[] args = {Integer.toString(page), Integer.toString(availablePage_)};
145         throw new ExoMessageException("PageList.page-out-of-range", args);
146      }
147   }
148   
149   
150   public abstract void sortData();
151   
152   public abstract List<E> getPageWithOffsetCare(int page) throws Exception;
153   
154   protected void removeRedundantPages(int availablePage) {
155     for (int i = 1; i < Math.min(availablePage, this.getAvailablePage()); i++) {
156       try {
157         int currentPageSize = this.getPage(i).size();
158         if (currentPageSize == 0) {
159           this.setAvailablePage((i-1)*this.getPageSize());
160           break;
161         }
162         if (currentPageSize < this.getPageSize()) {
163           this.setAvailablePage(i*this.getPageSize());
164           break;
165         }
166       } catch (Exception e) {
167         if (LOG.isWarnEnabled()) {
168           LOG.warn("Error in getPage.", e);
169         }
170       }
171     }
172     try {
173       getPageWithOffsetCare(1);
174     } catch (Exception e) {
175       if (LOG.isWarnEnabled()) {
176         LOG.warn("Error in getPageWithOffsetCare.", e);
177       }
178     }
179   }
180   
181   public boolean loadedAllData() {
182     return loadedAllData_;
183   }
184   
185 //  @SuppressWarnings("unchecked")
186 //  protected List<NodeLocation> filterNodes(List<Node> nodes) {
187 //    if (filter == null) {
188 //      return NodeLocation.getLocationsByNodeList(nodes);
189 //    }
190 //    List<NodeLocation> ret = new ArrayList<NodeLocation>(); 
191 //    for (Node node : nodes) {
192 //      Node filteredNode = filter.filterNodeToDisplay(node);
193 //      if (filteredNode != null) ret.add(NodeLocation.getNodeLocationByNode(filteredNode));
194 //    }
195 //    return ret;
196 //  }
197 //
198 //  protected List<NodeLocation> filterNodes(NodeIterator iter) {
199 //    if (filter == null) {
200 //      return NodeLocation.getLocationsByIterator(iter);
201 //    }
202 //    List<NodeLocation> ret = new ArrayList<NodeLocation>(); 
203 //    while (iter.hasNext()) {
204 //      Node filteredNode = filter.filterNodeToDisplay(iter.nextNode());
205 //      if (filteredNode != null) ret.add(NodeLocation.getNodeLocationByNode(filteredNode));
206 //    }
207 //    return ret;
208 //  }
209 
210 }