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.

304 lines
7.3 KiB

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package analiseacidentestrabalho;
import beans.AnaliseAcidente;
import beans.Utilizador;
import com.evolute.utils.error.ErrorLogger;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.rave.web.ui.component.Hyperlink;
import com.sun.rave.web.ui.component.StaticText;
import com.sun.rave.web.ui.component.TextArea;
import db.AnalisesDataProvider;
import global.Global;
import java.util.Date;
import javax.faces.FacesException;
import javax.faces.context.FacesContext;
import utils.Utils;
/**
* <p>Page bean that corresponds to a similarly named JSP page. This
* class contains component definitions (and initialization code) for
* all components that you have defined on this page, as well as
* lifecycle methods and event handlers where you may add behavior
* to respond to incoming events.</p>
*
* @version ConfirmarDesactivar.java
* @version Created on Oct 11, 2010, 3:38:04 PM
* @author dneves
*/
public class ConfirmarDesactivar extends AbstractPageBean
{
private Hyperlink lnkPlanosActuacao = new Hyperlink();
public Hyperlink getLnkPlanosActuacao()
{
return lnkPlanosActuacao;
}
public void setLnkPlanosActuacao( Hyperlink h )
{
this.lnkPlanosActuacao = h;
}
private StaticText lblUser = new StaticText();
public StaticText getLblUser()
{
return lblUser;
}
public void setLblUser( StaticText st )
{
this.lblUser = st;
}
private StaticText txtMsg = new StaticText();
public StaticText getTxtMsg()
{
return txtMsg;
}
public void setTxtMsg( StaticText st )
{
this.txtMsg = st;
}
private StaticText textAnalise = new StaticText();
public StaticText getTextAnalise()
{
return textAnalise;
}
public void setTextAnalise( StaticText textAnalise )
{
this.textAnalise = textAnalise;
}
private TextArea textComentario = new TextArea();
public TextArea getTextComentario()
{
return textComentario;
}
public void setTextComentario( TextArea textComentario )
{
this.textComentario = textComentario;
}
private void _init() throws Exception
{
}
public ConfirmarDesactivar()
{
}
@Override
public void init()
{
super.init();
try
{
_init();
}
catch ( Exception e )
{
log( "ConfirmarDesactivar Initialization Failure", e );
throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
}
if ( ! canAccess() )
{
try
{
getExternalContext().redirect( "Login.jsp" );
}
catch( Exception e )
{
ErrorLogger.logException( e );
}
}
}
/**
* <p>Callback method that is called after the component tree has been
* restored, but before any event processing takes place. This method
* will <strong>only</strong> be called on a postback request that
* is processing a form submit. Customize this method to allocate
* resources that will be required in your event handlers.</p>
*/
@Override
public void preprocess()
{
txtMsg.setText( getSessionBean1().getMsg() );
getSessionBean1().setMsg( "" );
}
/**
* <p>Callback method that is called just before rendering takes place.
* This method will <strong>only</strong> be called for the page that
* will actually be rendered (and not, for example, on a page that
* handled a postback and then navigated to a different page). Customize
* this method to allocate resources that will be required for rendering
* this page.</p>
*/
@Override
public void prerender()
{
System.out.println( "=== CONFIRMAR_DESACTIVACAO ===" );
lblUser.setText( getSessionBean1().getCurrentUser().getLogin() );
AnaliseAcidente analise = getSessionBean1().getCurrentAnalise();
if ( analise != null )
{
textAnalise.setText( "Análise nº " + analise.getAnalise_nr() );
}
else
{
textAnalise.setText( "Análise inválida." );
}
}
/**
* <p>Callback method that is called after rendering is completed for
* this request, if <code>init()</code> was called (regardless of whether
* or not this was the page that was actually rendered). Customize this
* method to release resources acquired in the <code>init()</code>,
* <code>preprocess()</code>, or <code>prerender()</code> methods (or
* acquired during execution of an event handler).</p>
*/
@Override
public void destroy()
{
}
/**
* <p>Return a reference to the scoped data bean.</p>
*
* @return reference to the scoped data bean
*/
protected SessionBean1 getSessionBean1() {
return (SessionBean1) getBean("SessionBean1");
}
/**
* <p>Return a reference to the scoped data bean.</p>
*
* @return reference to the scoped data bean
*/
protected RequestBean1 getRequestBean1() {
return (RequestBean1) getBean("RequestBean1");
}
/**
* <p>Return a reference to the scoped data bean.</p>
*
* @return reference to the scoped data bean
*/
protected ApplicationBean1 getApplicationBean1() {
return (ApplicationBean1) getBean("ApplicationBean1");
}
private boolean canAccess()
{
boolean canAccess = false;
if ( getSessionBean1().isLoggedIn() )
{
Utilizador u = getSessionBean1().getCurrentUser();
if ( u != null && u.getTipo().intValue() == Global.TIPO_UTILIZADOR_DIRECTOR_SIPRP )
{
canAccess = true;
}
}
return canAccess;
}
public String lnkPlanosActuacao_action()
{
Utilizador u = getSessionBean1().getCurrentUser();
FacesContext fc = getFacesContext().getCurrentInstance();
Global.redirectToPlanos(fc, u);
return null;
}
public String lnkNewUser_action()
{
getSessionBean1().setNavFrom( "EstatisticasProcesso" );
return "gestao_utilizadores";
}
public String lnkEditUser_action()
{
getSessionBean1().setNavFrom( "EstatisticasProcesso" );
getSessionBean1().setModoEdicaoUtilizador( "edit" );
return "user";
}
public String lnkLogout_action()
{
Utils.doLogout( getExternalContext(), true );
return null;
}
public String buttonCancelar_action()
{
getSessionBean1().setCurrentAnalise( null );
return "formDirSiprp";
}
public String buttonConfirmar_action()
{
Utilizador currentUser = getSessionBean1().getCurrentUser();
AnaliseAcidente analise = getSessionBean1().getCurrentAnalise();
if ( userCanDesactivarAnalise( currentUser ) && analise != null )
{
try
{
String comentario = null;
if ( textComentario.getText() != null )
{
comentario = ( ( String ) textComentario.getText() ).trim();
comentario = "".equals( comentario ) ? null : comentario;
}
analise.setEstado( Global.ESTADO_CONCLUIDO );
analise.setConcluido_por_desactivacao( Boolean.TRUE );
analise.setData_desactivacao( new Date() );
analise.setComentario_desactivacao( comentario );
AnalisesDataProvider adp = new AnalisesDataProvider();
adp.updateAnalise( analise );
}
catch ( Exception e )
{
ErrorLogger.logException( e );
getSessionBean1().setMsg( "Erro a desactivar análise.\n" + e.getMessage() );
}
}
return "formDirSiprp";
}
private boolean userCanDesactivarAnalise( Utilizador user )
{
boolean canDesactivar = false;
if ( user != null )
{
int userType = user.getTipo() == null ? -1 : user.getTipo().intValue();
canDesactivar = userType == Global.TIPO_UTILIZADOR_DIRECTOR_SIPRP;
}
return canDesactivar;
}
}