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.
SIPRP/trunk/PlanosActuacao/src/utils/Utils.java

174 lines
4.6 KiB

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package utils;
import com.evolute.utils.Singleton;
import com.evolute.utils.error.ErrorLogger;
import com.sun.webui.jsf.model.Option;
import db.entidades.Utilizador;
import db.providers.PlanosDataProvider;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import planosactuacao.SessionBean1;
/**
*
* @author lluis
*/
public class Utils
{
public static String parseToInsert( String str )
{
String result = str;
// if ( result != null )
// {
// result = UnicodeChecker.parseToUnicode( result, true, true );
// }
return result;
}
public static String unicodeToHTML( String text )
{
if( text == null || text.length() < 6 )
{
return text;
}
// text = UnicodeChecker.convertUnicode0100ToHTMLFromUser( text );
// text = UnicodeChecker.convertUnicode0200ToHTMLFromUser( text );
// text = UnicodeChecker.convertUnicode2000ToHTMLFromUser( text );
// text = UnicodeChecker.convertUnicodeLatin1ExtendedToHTMLFromUser( text );
// text = UnicodeChecker.convertUnicodeSpecialsToHTMLFromUser( text );
String output = text;
// output = output.replaceAll( "\\\\u0009", " " );
// output = output.replaceAll( "\\\\u000a", "<br>" );
//
// output = output.replaceAll( "\\\\u00a0", "&nbsp;" );
// output = output.replaceAll( "\\\\u00aa", "&ordf;" );
// output = output.replaceAll( "\\\\u00ba", "&ordm;" );
// output = output.replaceAll( "\\\\u0153", "&#156;" );
//
// output = output.replaceAll( "\\\\u2013", "-" );
// output = output.replaceAll( "\\\\u2014", "-" );
// output = output.replaceAll( "\\\\u2018|\\\\u2019", "'" );
// output = output.replaceAll( "\\\\u201c|\\\\u201d", "\"" );
//
// output = output.replaceAll( "\\\\u2022", "*" );
// output = output.replaceAll( "\\\\u2026", "..." );
return output;
}
public static void doLogin( Utilizador u )
{
if ( u != null )
{
Singleton.setInstance( Singleton.USERNAME, u.getLogin() );
}
else
{
ErrorLogger.logException( new Exception( "Utils.doLogin() : Utilizador is null !" ) );
}
}
public static void doLogout( FacesContext fc )
{
Singleton.setInstance( Singleton.USERNAME, null );
SessionBean1 sessionBean = JSFUtils.getSessionBean( fc );
sessionBean.setCurrentUser( null );
sessionBean.setLoggedIn( false );
String aatSessionID = sessionBean.getAatSessionID();
ExternalContext ec = fc.getExternalContext();
HttpSession session = ( HttpSession ) ec.getSession( false );
ServletContext paContext = session.getServletContext();
ServletContext aatContext = paContext.getContext( "/AnaliseAcidentesTrabalho" );
if ( aatContext != null && aatSessionID != null )
{
Map< String, Object > mapData = ( Map< String, Object > ) aatContext.getAttribute( aatSessionID );
if ( mapData != null )
{
HttpSession aatSession = ( HttpSession ) mapData.get( "session" );
try
{
aatSession.invalidate();
}
catch ( IllegalStateException e )
{
// ErrorLogger.logException( e );
System.out.println( "Session already invalidated." );
}
}
}
}
public static Integer getDefaultSelectedYear()
{
Integer result = null;
java.util.Calendar calendar = java.util.Calendar.getInstance();
calendar.setTime( new Date() );
result = calendar.get( java.util.Calendar.YEAR );
return result;
}
public static Option[] getYearDropValues()
{
Calendar calendar = Calendar.getInstance();
calendar.setTime( new Date() );
Integer currentYear = calendar.get( Calendar.YEAR );
Option[] opts = null;
try
{
int start = 0;
List< Integer > anosList = PlanosDataProvider.getInstance().getDistinctYears( null );
if ( anosList.contains( currentYear ) )
{
opts = new Option[ anosList.size() + 1 ];
opts[ start++ ] = new Option( 0, "Todos" );
}
else
{
opts = new Option[ anosList.size() + 2 ];
opts[ start++ ] = new Option( 0, "Todos" );
opts[ start++ ] = new Option( currentYear, currentYear.toString() );
}
Iterator< Integer > it = anosList.iterator();
for ( ; it.hasNext(); start++ )
{
Integer ano = it.next();
opts[ start ] = new Option( ano, ano.toString() );
}
}
catch ( Exception e )
{
ErrorLogger.logException( e );
}
return opts;
}
public static String getPageFrom(String referer)
{
String pageFrom = referer.substring(referer.lastIndexOf("/")+1);
return pageFrom;
}
}