You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
4.7 KiB

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pagination;
import db.entidades.PlanoActuacao;
import db.entidades.Risco;
import db.entidades.Utilizador;
import db.providers.RiscoLogic;
import utils.Global;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import planosactuacao.SessionBean1.PAGINATION_ENUM;
public abstract class Pagination implements Serializable
{
// private static final String COLOR_TRATADO = "#000000";
private static final String DEFAULT_COLOR_NAO_TRATADO = "#CC9966";
private static final Map< Integer, String > COLOR_NAO_TRATADO = new HashMap< Integer, String >();
static
{
COLOR_NAO_TRATADO.put( Global.RESPONSAVEL_SEGURANCA, "#FF5858" );
}
private final PAGINATION_ENUM pagination;
private final PlanoActuacao planoActuacao;
private final Utilizador user;
private List< Risco > riscos = new LinkedList< Risco >();
private List< Pagina > pages = new LinkedList< Pagina >();
private int currentPageNumber = 1;
public Pagination( PAGINATION_ENUM pagination, PlanoActuacao p, Utilizador user )
{
this.pagination = pagination;
this.planoActuacao = p;
this.user = user;
setup();
}
public PAGINATION_ENUM getPagination()
{
return pagination;
}
public PlanoActuacao getPlanoActuacao()
{
return planoActuacao;
}
public abstract void setup();
public void addRisco( Risco r )
{
if ( r != null )
{
if ( riscos == null )
{
riscos = new LinkedList< Risco >();
}
riscos.add( r );
}
}
public void addPage( Pagina page )
{
if ( page != null )
{
if ( pages == null )
{
pages = new LinkedList< Pagina >();
}
pages.add( page );
}
}
public List< Pagina > getPages( Risco riscoActual )
{
pages = new LinkedList< Pagina >();
if ( getCurrentPageNumber() > 1 )
{
Pagina page = new Pagina();
page.setId( "prev" );
page.setPrevious( new Boolean( true ) );
page.setText( "<<" );
addPage( page );
}
Iterator< Risco > it = riscos.iterator();
int pageNumber = 1;
while ( it.hasNext() )
{
Risco risco = it.next();
Pagina page = new Pagina();
page.setId( "valor" + risco.getId() );
page.setPageNumber( pageNumber );
page.setText( pageNumber );
if ( isCurrentPage( page, risco, riscoActual ) )
{
setDisabled( page );
}
else
{
setColor( page, risco );
}
addPage( page );
pageNumber++;
}
if ( getCurrentPageNumber() < getTotalPages() )
{
Pagina page = new Pagina();
page.setId( "next" );
page.setNext( new Boolean( true ) );
page.setText( ">>" );
addPage( page );
}
return pages;
}
private boolean isCurrentPage( Pagina page, Risco risco, Risco riscoActual )
{
return ( page.getPageNumber() == getCurrentPageNumber() ) && ( ( riscoActual.getIsPatrimonial() && risco.getIsPatrimonial() && getPagination().equals( PAGINATION_ENUM.PATRIMONIAIS ) )
|| ( ! riscoActual.getIsPatrimonial() && ! risco.getIsPatrimonial() && getPagination().equals( PAGINATION_ENUM.NORMAL ) ) );
}
private void setColor( Pagina page, Risco risco )
{
String color = getColor( risco );
if ( color != null )
{
String style = page.getStyle();
style = (style == null ? "" : style) + "; color: " + color + ";";
page.setStyle( style );
}
}
private void setDisabled( Pagina page )
{
String style = page.getStyle();
style = ( style == null ? "" : style ) + "background: #FFFFFF; border: 1px solid #000000; padding-left: 5px; padding-right: 5px;";
page.setStyle( style );
page.setDisabled( true );
}
private String getColor( Risco risco )
{
String result = null;
boolean isRiscoTratado = RiscoLogic.getInstance().isRiscoTratado( risco, user, true );
if ( ! isRiscoTratado )
{
result = COLOR_NAO_TRATADO.get( user.getTipo() );
if ( result == null )
{
result = DEFAULT_COLOR_NAO_TRATADO;
}
}
return result;
}
public Risco getRisco( Pagina p )
{
if ( p.isNext() )
{
currentPageNumber++;
}
else if ( p.isPrevious() )
{
currentPageNumber--;
}
else
{
currentPageNumber = p.getPageNumber().intValue();
}
return riscos.get( currentPageNumber - 1 );
}
public Risco getNextRisco()
{
Risco r = null;
if ( currentPageNumber < getTotalPages() )
{
currentPageNumber++;
r = riscos.get( currentPageNumber - 1 );
}
return r;
}
public int getTotalPages()
{
return pages == null ? 0 : pages.size() - 1;
}
public int getCurrentPageNumber()
{
return currentPageNumber;
}
}