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.ArrayList;
20  import java.util.Collections;
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import javax.jcr.Node;
27  import javax.jcr.NodeIterator;
28  import javax.jcr.RepositoryException;
29  import javax.jcr.query.QueryResult;
30  import javax.jcr.query.Row;
31  import javax.jcr.query.RowIterator;
32  
33  import org.exoplatform.services.log.ExoLogger;
34  import org.exoplatform.services.log.Log;
35  import org.exoplatform.services.security.ConversationState;
36  import org.exoplatform.services.wcm.search.SiteSearchService;
37  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
38  
39  /**
40   * Created by The eXo Platform SAS
41   * Author : Nguyen Anh Vu
42   *          anhvurz90@gmail.com
43   * Jun 17, 2011  
44   */
45  public class ArrayNodePageList<E> extends AbstractPageList<E> {
46    
47    private static final Log LOG = ExoLogger.getLogger(ArrayNodePageList.class.getName());
48  
49    /** The nodes. */
50    protected List<E> dataList;
51    
52    /** Constructor */
53    public ArrayNodePageList(int pageSize) {
54      super(pageSize);
55      removeRedundantPages(0);
56    }
57  
58    /** Constructor */
59    public ArrayNodePageList(List<E> results, int pageSize) {
60      super(pageSize);
61      dataList = results;
62      setAvailablePage(dataList.size());
63      removeRedundantPages(dataList.size() / pageSize);
64      currentListPage_ = null;
65      loadedAllData_ = true;
66    }
67    
68    /** Constructor */
69    public ArrayNodePageList(List<Node> nodes, int pageSize, 
70                             NodeSearchFilter filter, SearchDataCreator<E> dataCreator) {
71      super(pageSize, filter, dataCreator);
72      dataList = new ArrayList<>();
73      try {
74        for (Node node : nodes) {
75          if (filter != null) {
76            node = filter.filterNodeToDisplay(node);
77          }
78          if (searchDataCreator != null && node != null) { 
79            E data = searchDataCreator.createData(node, null, null);
80            if (data != null) {
81              dataList.add(data);
82            }
83          }
84        }
85      } catch (Exception e) {
86        if (LOG.isWarnEnabled()) {
87          LOG.warn(e.getMessage());
88        }
89      }
90      setAvailablePage(dataList.size());
91      removeRedundantPages(dataList.size() / pageSize);
92      currentListPage_ = null;
93    }
94    
95    /** Nodes getter */
96    public List<E> getDataList() {
97      return dataList;
98    }
99    
100   @Override
101   public List<E> getAll() throws Exception {
102     return getDataList();
103   }
104 
105   @Override
106   protected void populateCurrentPage(int page) throws Exception {
107     currentListPage_ = new ArrayList<>();
108     int count = 0;
109     if (dataList != null) {
110         for(int i = ((page - 1)*this.getPageSize()); i < dataList.size(); i++) {
111           currentListPage_.add(dataList.get(i));
112           count++;
113           if(count == getPageSize()) {
114              break;
115           }
116       }
117     }
118     currentPage_ = page;
119   }
120 
121   @Override
122   public void sortData() {
123     //sort by given comparator
124     if (comparator != null) {
125       Collections.sort(dataList, comparator);
126       try {
127         populateCurrentPage(currentPage_);
128       } catch (Exception e) {
129         if (LOG.isWarnEnabled()) {
130           LOG.warn(e.getMessage());
131         }
132       }
133     }  
134   }
135 
136   @Override
137   public List<E> getPageWithOffsetCare(int page) throws Exception {
138     return getPage(page);
139   }
140 }