All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.gov.frameworkdemoiselle.template.AbstractListPageBean Maven / Gradle / Ivy

There is a newer version: 2.5.4
Show newest version
/*
 * Demoiselle Framework
 * Copyright (C) 2010 SERPRO
 * ----------------------------------------------------------------------------
 * This file is part of Demoiselle Framework.
 * 
 * Demoiselle Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.
 * 
 * This program 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 Lesser General Public License version 3
 * along with this program; if not,  see 
 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA  02110-1301, USA.
 * ----------------------------------------------------------------------------
 * Este arquivo é parte do Framework Demoiselle.
 * 
 * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
 * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
 * do Software Livre (FSF).
 * 
 * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
 * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
 * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
 * para maiores detalhes.
 * 
 * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
 * "LICENCA.txt", junto com esse programa. Se não, acesse 
 * ou escreva para a Fundação do Software Livre (FSF) Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
 */
package br.gov.frameworkdemoiselle.template;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.inject.Inject;

import br.gov.frameworkdemoiselle.pagination.Pagination;
import br.gov.frameworkdemoiselle.pagination.PaginationContext;
import br.gov.frameworkdemoiselle.util.Reflections;
/**
 * Template Managed AuthenticationBean class that implements the methods defined by the interface ListPageBean.
 * 
 * @param 
 *            bean object type
 * @param 
 *            bean id type
 *
 * @author SERPRO
 * @see ListPageBean
 */
public abstract class AbstractListPageBean extends AbstractPageBean implements ListPageBean {

	private static final long serialVersionUID = 1L;

	private List resultList;

	private transient DataModel dataModel;

	private Map selection = new HashMap();

	@Inject
	private PaginationContext paginationContext;

	public void clear() {
		this.dataModel = null;
		this.resultList = null;
	}

	private Class beanClass;

	protected Class getBeanClass() {
		if (this.beanClass == null) {
			this.beanClass = Reflections.getGenericTypeArgument(this.getClass(), 0);
		}

		return this.beanClass;
	}

	@Override
	public DataModel getDataModel() {
		if (this.dataModel == null) {
			this.dataModel = new ListDataModel(this.getResultList());
		}

		return this.dataModel;
	}

	@Override
	public List getResultList() {
		if (this.resultList == null) {
			this.resultList = handleResultList();
		}

		return this.resultList;
	}

	protected abstract List handleResultList();

	@Override
	public String list() {
		clear();
		return getCurrentView();
	}

	public Map getSelection() {
		return selection;
	}

	public void setSelection(Map selection) {
		this.selection = selection;
	}

	public void clearSelection() {
		this.selection = new HashMap();
	}

	public List getSelectedList() {
		List selectedList = new ArrayList();
		Iterator iter = getSelection().keySet().iterator();
		while (iter.hasNext()) {
			I id = iter.next();
			if (getSelection().get(id)) {
				selectedList.add(id);
			}
		}
		return selectedList;
	}

	public Pagination getPagination() {
		return paginationContext.getPagination(getBeanClass(), true);
	}
}