/*
* This file is part of rockframework.
*
* rockframework is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* rockframework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>;.
*/
package br.net.woodstock.rockframework.web.faces.primefaces;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.primefaces.model.SortOrder;
import br.net.woodstock.rockframework.domain.Entity;
import br.net.woodstock.rockframework.persistence.orm.Page;
import br.net.woodstock.rockframework.persistence.orm.QueryResult;
import br.net.woodstock.rockframework.persistence.orm.impl.PageImpl;
import br.net.woodstock.rockframework.util.Assert;
import br.net.woodstock.rockframework.utils.CollectionUtils;
public class EntityDataModel<E extends Entity<Integer>> extends BaseLazyDataModel<E> {
private static final long serialVersionUID = -1013531104655352691L;
private EntityRepository repository;
private QueryResult queryResult;
public EntityDataModel(final int pageSize, final EntityRepository repository) {
super();
Assert.greaterThan(pageSize, 0, "pageSize");
Assert.notNull(repository, "repository");
this.repository = repository;
this.setPageSize(pageSize);
}
@Override
@SuppressWarnings("unchecked")
public E getRowData(final String rowKey) {
return (E) this.repository.getEntity(Integer.valueOf(rowKey));
}
@Override
public Object getRowKey(final E e) {
return e.getId();
}
@Override
public List<E> load(final int first, final int pageSize, final String sortField, final SortOrder sortOrder, final Map<String, String> filters) {
Page page = this.toPage(first, pageSize);
if ((this.queryResult == null) || (!page.equals(this.queryResult.getCurrentPage()))) {
this.queryResult = this.repository.getResult(page);
}
Collection<E> collection = this.queryResult.getResult();
List<E> list = CollectionUtils.toList(collection);
this.setRowCount(this.queryResult.getTotal());
this.setWrappedData(list);
return list;
}
@Override
public int getRowCount() {
if (this.queryResult == null) {
Page page = new PageImpl(1, this.getPageSize());
this.queryResult = this.repository.getResult(page);
Collection<E> collection = this.queryResult.getResult();
List<E> list = CollectionUtils.toList(collection);
this.setRowCount(this.queryResult.getTotal());
this.setWrappedData(list);
}
return super.getRowCount();
}
@Override
public int getRowIndex() {
int page = (this.queryResult.getCurrentPage().getPageNumber() - 1) * this.queryResult.getCurrentPage().getResultsPerPage();
return super.getRowIndex() + page;
}
protected Page toPage(final int first, final int pageSize) {
int div = first / pageSize;
div++;
Page page = new PageImpl(div, pageSize);
return page;
}
}