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/siprp/ficha/FichaWindow.java

746 lines
21 KiB

/*
* FichaWindow.java
*
* Created on 29 de Março de 2004, 11:50
*/
package siprp.ficha;
import siprp.*;
import siprp.data.*;
import siprp.importer.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.text.*;
import com.evolute.utils.*;
import com.evolute.utils.data.*;
import com.evolute.utils.jdo.*;
import com.evolute.utils.metadb.*;
import com.evolute.utils.strings.*;
import com.evolute.utils.tracker.*;
import com.evolute.utils.ui.*;
import com.evolute.utils.ui.panel.*;
import com.evolute.utils.ui.search.*;
import com.evolute.utils.ui.window.*;
/**
*
* @author fpalma
*/
public class FichaWindow extends TabbedWindow
implements ListAction
{
private JDOProvider JDO;
private UpperPanel upperPanel;
private EmpresaPanel empresaPanel;
private TrabalhadorPanel trabalhadorPanel;
private ObservacoesPanel observacoesPanel;
private ExamePanel examePanel;
private FichaDataProvider fdpProvider;
private Integer empresaID;
private Integer estabelecimentoID;
private Integer trabalhadorID;
private static int permissions[][] =
new int[][]{ { NEW_INDEX, CANCEL_INDEX, SAVE_INDEX } };
/** Creates a new instance of FichaWindow */
public FichaWindow()
throws Exception
{
super( new UpperPanel(), new String[]{ "Empresa/Trabalhador", "Exame" },
createPermissions( permissions ) );
setEditorManagerFactory( FichaAptidaoEditorManager.getEditorManagerFactory() );
upperPanel = (UpperPanel) getUpperPanel();
upperPanel.owner = this;
fdpProvider = ( FichaDataProvider ) FichaDataProvider.getProvider();
JDO = ( JDOProvider ) Singleton.getInstance( Singleton.DEFAULT_JDO_PROVIDER );
setupComponents();
}
private void setupComponents()
throws Exception
{
setSize( 700, 640 );
setResizable( false );
setTitle( "Ficha de Aptid\u00e3o" );
JPanel empresaTrabalhadorPanel = getTab( 0 );
JPanel exameRecomendacoesPanel = getTab( 1 );
GridBagLayout gridbag = new GridBagLayout();
empresaTrabalhadorPanel.setLayout( gridbag );
GridBagConstraints constraints = new GridBagConstraints();
constraints.insets = new Insets( 0, 1, 0, 1 );
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1;
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 1;
constraints.weighty = 0;
empresaPanel = new EmpresaPanel();
empresaPanel.setEnabled( false );
gridbag.setConstraints( empresaPanel, constraints );
empresaTrabalhadorPanel.add( empresaPanel );
constraints.weighty = 0;
trabalhadorPanel = new TrabalhadorPanel();
gridbag.setConstraints( trabalhadorPanel, constraints );
empresaTrabalhadorPanel.add( trabalhadorPanel );
constraints.weighty = 1;
observacoesPanel = new ObservacoesPanel();
gridbag.setConstraints( observacoesPanel, constraints );
empresaTrabalhadorPanel.add( observacoesPanel );
gridbag = new GridBagLayout();
exameRecomendacoesPanel.setLayout( gridbag );
constraints = new GridBagConstraints();
constraints.insets = new Insets( 0, 1, 0, 1 );
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1;
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 1;
constraints.weighty = 0;
constraints.weighty = 1;
examePanel = new ExamePanel();
gridbag.setConstraints( examePanel, constraints );
exameRecomendacoesPanel.add( examePanel );
registerAction( new ActionHandler(){
public void execute()
{
print();
}
public boolean activate( boolean newAction, boolean editAction,
boolean cancelAction, boolean saveAction,
boolean deleteAction, boolean selectAction )
{
return saveAction || editAction;
}
}, "Imprimir",
"Imprimir Ficha", "print",
0, true );
registerAction( new ActionHandler(){
public void execute()
{
printOld();
}
public boolean activate( boolean newAction, boolean editAction,
boolean cancelAction, boolean saveAction,
boolean deleteAction, boolean selectAction )
{
return saveAction || editAction;
}
}, "Imprimir Fichas Anteriores",
"Imprimir Fichas Anteriores", null,
0, false );
registerAction( new ActionHandler(){
public void execute()
{
deleteOld();
}
public boolean activate( boolean newAction, boolean editAction,
boolean cancelAction, boolean saveAction,
boolean deleteAction, boolean selectAction )
{
return saveAction || editAction;
}
}, "Apagar Fichas Anteriores",
"Apagar Fichas Anteriores", null,
0, false );
// registerAction( new ActionHandler(){
// public void execute()
// {
// excel();
// }
//
// public boolean activate( boolean newAction, boolean editAction,
// boolean cancelAction, boolean saveAction,
// boolean deleteAction, boolean selectAction )
// {
// return saveAction;
// }
// }, "Importar",
// "Importar de Excel", "excel",
// 0, true );
}
public boolean save( int index )
{
new ProgressDialog( this, "A gravar...",
new Runnable(){
public void run()
{
}
});
StringBuffer msg = new StringBuffer();
MedicoData medico;
try
{
medico = (MedicoData)upperPanel.save();
}
catch( ValuesException vex )
{
msg.append( vex.getMessage() );
medico = null;
}
EstabelecimentoData estabelecimento;
try
{
estabelecimento = (EstabelecimentoData)empresaPanel.save();
}
catch( ValuesException vex )
{
msg.append( vex.getMessage() );
estabelecimento = null;
}
TrabalhadorData trabalhador;
try
{
trabalhador = (TrabalhadorData)trabalhadorPanel.save();
}
catch( ValuesException vex )
{
msg.append( vex.getMessage() );
trabalhador = null;
}
ExameData exame;
try
{
exame = (ExameData)examePanel.save();
}
catch( ValuesException vex )
{
msg.append( vex.getMessage() );
exame = null;
}
if( estabelecimento == null || trabalhador == null || exame == null || medico == null )
{
JOptionPane.showMessageDialog( this, msg.toString(), "Erro...", JOptionPane.ERROR_MESSAGE );
return false;
}
estabelecimento.set( EstabelecimentoData.NOME_PLAIN,
com.evolute.utils.strings.StringPlainer.convertString( ( String )
estabelecimento.get( EstabelecimentoData.NOME ) ) );
// trabalhador.setProperty( FichaDataProvider.R_TRABALHADOR_ESTABELECIMENTO, estabelecimento );
// trabalhador.setProperty( FichaDataProvider.OBSERVACOES, observacoesPanel.save() );
trabalhador.set( TrabalhadorData.ESTABELECIMENTO, estabelecimento );
trabalhador.set( TrabalhadorData.OBSERVACOES, observacoesPanel.save() );
trabalhador.set( TrabalhadorData.NOME_PLAIN,
com.evolute.utils.strings.StringPlainer.convertString( ( String )
trabalhador.get( TrabalhadorData.NOME ) ) );
// exame.setProperty( FichaDataProvider.R_EXAME_MEDICO, medico );
// exame.setProperty( FichaDataProvider.R_EXAME_TRABALHADOR, trabalhador );
exame.set( ExameData.MEDICO, medico );
exame.set( ExameData.TRABALHADOR, trabalhador );
try
{
exame.set( FichaDataProvider.PDF, createPDF( exame ) );
// empresa.save();
// estabelecimento.save();
exame.save();
trabalhador.save();
try
{
if( medico != null )
{
fdpProvider.setMedicoForEstabelecimento( ( Integer ) estabelecimento.get( EstabelecimentoData.ID ),
( Integer ) medico.get( MedicoData.ID ) );
}
}
catch( Exception mex )
{
}
// fdpProvider.savePDF( exame );
// DBKey key = trabalhador.getPrimaryKeyValue();
// DBField fields[] = FichaDataProvider.TRABALHADORES.getPrimaryKey();
// trabalhadorID = new Integer( ((Number)key.getFieldValue( fields[ 0 ] )).intValue() );
trabalhadorID = ( Integer ) trabalhador.get( TrabalhadorData.ID );
try
{
Marcacao.saveMarcacaoConsultaForTrabalhadorID( trabalhadorID, (Date)exame.get( ExameData.PROXIMO_EXAME ) );
}
catch( Exception iex )
{
iex.printStackTrace();
}
//
// createPDF( exame );
// print();
}
catch( Exception ex )
{
DialogException.showExceptionMessage( ex, "Erro a guardar", true );
return false;
}
return true;
}
public boolean newItem( int index )
{
fdpProvider.setSearch( FichaDataProvider.SEARCH_EMPRESAS );
SearchDialog search;
Integer oldID = empresaID;
search = new SearchDialog( fdpProvider, null, false, null, true );
empresaID = ( Integer )search.getSelected();
if( empresaID == null )
{
if( search.getIsNew() )
{
estabelecimentoID = null;
trabalhadorID = null;
clear( 0 );
return true;
}
else
{
return false;
}
}
fdpProvider.setSearch( FichaDataProvider.SEARCH_ESTABELECIMENTOS );
fdpProvider.setSearchID( FichaDataProvider.SEARCH_EMPRESAS, empresaID.intValue() );
search = new SearchDialog( fdpProvider, null, false, null, true );
estabelecimentoID = ( Integer )search.getSelected();
if( estabelecimentoID == null )
{
if( search.getIsNew() )
{
trabalhadorID = null;
reload(0);
return true;
}
else
{
return false;
}
}
fdpProvider.setSearch( FichaDataProvider.SEARCH_TRABALHADORES );
fdpProvider.setSearchID( FichaDataProvider.SEARCH_ESTABELECIMENTOS, estabelecimentoID.intValue() );
search = new SearchDialog( fdpProvider, null, false, null, true );
trabalhadorID = ( Integer )search.getSelected();
if( trabalhadorID == null )
{
if( search.getIsNew() )
{
reload(0);
return true;
}
else
{
return false;
}
}
reload(0);
return true;
}
public boolean delete( int index )
{
return true;
}
public void deleteOld()
{
if( trabalhadorID == null )
{
return;
}
Vector linhas;
try
{
IDObject fichas[] = fdpProvider.getAllFichasForTrabalhador( trabalhadorID );
linhas = new Vector( Arrays.asList( fichas ) );
}
catch( Exception ex )
{
DialogException.showExceptionMessage( ex, "Erro a criar lista de Fichas anteriores", true );
return;
}
ListActionDialog dialog =
new ListActionDialog( this, "Apagar Fichas Anteriores", new String[]{ "Data" }, linhas,
new ListAction(){
public String getListActionName()
{
return "Apagar";
}
public boolean executeListAction( int line, Object value )
{
if( value == null )
{
return true;
}
try
{
delete( ( ( IDObject )value ).getID() );
}
catch( Exception ex )
{
DialogException.showExceptionMessage( ex, "Erro a Apagar Ficha", true );
return false;
}
return true;
}
} );
dialog.setSize( 250, 200 );
dialog.setVisible( true );
}
public void reload( int index )
{
Integer upperData[] = new Integer[]{ empresaID, estabelecimentoID, trabalhadorID };
upperPanel.fill( upperData );
Object empresaData[] = new Object[]{ empresaID, estabelecimentoID };
empresaPanel.fill( empresaData );
trabalhadorPanel.fill( trabalhadorID );
observacoesPanel.fill( trabalhadorID );
examePanel.fill( trabalhadorID );
}
public void enableComponents( int index, boolean enable )
{
upperPanel.setEnabled( enable );
empresaPanel.setEnabled( enable );
trabalhadorPanel.setEnabled( enable );
observacoesPanel.setEnabled( enable );
examePanel.setEnabled( enable );
}
public void clear( int index )
{
upperPanel.clear();
empresaPanel.clear();
trabalhadorPanel.clear();
observacoesPanel.clear();
examePanel.clear();
}
public void print()
{
try
{
if( trabalhadorID == null )
{
System.out.println( "no trab" );
return;
}
Integer id = fdpProvider.getLastExameIDForTrabalhador( trabalhadorID );
if( id == null )
{
System.out.println( "no id" );
return;
}
print( id );
}
catch( Exception ex )
{
DialogException.showExceptionMessage( ex, "N\u00e3o foi poss\u00edvel imprimir", true );
}
}
private void print( Integer exameID )
throws Exception
{
// MetaObject exame = fdpProvider.load( fdpProvider.EXAMES, new DBKey( exameID ) );
ExameData exame = (ExameData) JDO.load( ExameData.class, exameID );
byte pdf[] = (byte []) exame.get( ExameData.PDF );
if( exame == null )
{
throw new Exception( "N\u00e3o existe exame" );
}
TrabalhadorData trabalhadorExame = ( TrabalhadorData ) exame.get( ExameData.TRABALHADOR );
String nomeFicheiro = StringPlainer.convertString( ( String ) trabalhadorExame.get( TrabalhadorData.NOME ) );
nomeFicheiro.replaceAll( " ", "_" );
ExamePDF ePDF = new ExamePDF();
ePDF.print( pdf, nomeFicheiro );
}
public byte[] createPDF( ExameData exame )
throws Exception
{
MedicoData medico = (MedicoData)exame.get( ExameData.MEDICO );
TrabalhadorData trabalhador = (TrabalhadorData) exame.get( ExameData.TRABALHADOR );
EstabelecimentoData estabelecimento = (EstabelecimentoData) trabalhador.get( TrabalhadorData.ESTABELECIMENTO );
EmpresaData empresa = (EmpresaData) estabelecimento.get( EstabelecimentoData.EMPRESA );
// empresa.load( empresa.getPrimaryKeyValue() );
Hashtable ht = new Hashtable();
ht.put( "designacao_social", empresa.get( EmpresaData.DESIGNACAO_SOCIAL ) );
ht.put( "estabelecimentos.nome", estabelecimento.get( EstabelecimentoData.NOME ) );
String aux = (String)estabelecimento.get( EstabelecimentoData.LOCALIDADE );
ht.put( "estabelecimentos.localidade", aux != null ? aux : "" );
Integer tipo = (Integer)empresa.get( EmpresaData.SERVICO_SAUDE_TIPO );
switch( tipo.intValue() )
{
case 1:
ht.put( "servico_saude_tipo_interno", "" );
break;
case 2:
ht.put( "servico_saude_tipo_interempresas", "" );
break;
case 3:
ht.put( "servico_saude_tipo_externo", "" );
break;
case 4:
ht.put( "servico_saude_tipo_sns", "" );
break;
}
ht.put( "servico_saude_designacao", empresa.get( EmpresaData.SERVICO_SAUDE_DESIGNACAO ) );
tipo = (Integer)empresa.get( EmpresaData.SERVICO_HIGIENE_TIPO );
switch( tipo.intValue() )
{
case 1:
ht.put( "servico_higiene_tipo_interno", "" );
break;
case 2:
ht.put( "servico_higiene_tipo_interempresas", "" );
break;
case 3:
ht.put( "servico_higiene_tipo_externo", "" );
break;
case 4:
ht.put( "servico_higiene_outro", "" );
break;
}
ht.put( "servico_higiene_designacao", empresa.get( EmpresaData.SERVICO_HIGIENE_DESIGNACAO ) );
ht.put( "trabalhadores.nome", trabalhador.get( TrabalhadorData.NOME ) );
aux = (String)trabalhador.get( TrabalhadorData.SEXO );
switch( aux.charAt( 0 ) )
{
case 'm':
ht.put( "sexo", "Masculino" );
break;
case 'f':
ht.put( "sexo", "Feminino" );
break;
}
DateFormat df = DateFormat.getDateInstance( DateFormat.SHORT );
Date data = ( Date )trabalhador.get( TrabalhadorData.DATA_NASCIMENTO );
ht.put( "data_nascimento", data != null ? df.format( data ) : "" );
aux = (String)trabalhador.get( TrabalhadorData.NACIONALIDADE );
ht.put( "nacionalidade", aux != null ? aux : "" );
aux = (String)trabalhador.get( TrabalhadorData.NUMERO_MECANOGRAFICO );
ht.put( "numero_mecanografico", aux != null ? aux : "" );
data = ( Date )trabalhador.get( TrabalhadorData.DATA_ADMISSAO );
ht.put( "data_admissao", data != null ? df.format( data ) : "" );
aux = (String)trabalhador.get( TrabalhadorData.CATEGORIA );
ht.put( "categoria", aux != null ? aux : "" );
aux = (String)trabalhador.get( TrabalhadorData.LOCAL_TRABALHO );
ht.put( "local_trabalho", aux != null ? aux : "" );
aux = (String)trabalhador.get( TrabalhadorData.FUNCAO_PROPOSTA );
ht.put( "funcao_proposta", aux != null ? aux : "" );
data = ( Date )trabalhador.get( TrabalhadorData.DATA_ADMISSAO_FUNCAO );
ht.put( "data_admissao_funcao", data != null ? df.format( data ) : "" );
aux = (String)trabalhador.get( TrabalhadorData.OBSERVACOES );
ht.put( "observacoes", aux != null ? aux : "" );
data = ( Date )exame.get( ExameData.DATA );
ht.put( "exames.data", data != null ? df.format( data ) : "" );
tipo = (Integer)exame.get( ExameData.TIPO );
switch( tipo.intValue() )
{
case 1:
ht.put( "tipo_admissao", "" );
break;
case 2:
ht.put( "tipo_periodico", "" );
break;
case 3:
ht.put( "tipo_ocasional", "" );
tipo = (Integer)exame.get( ExameData.OCASIONAL );
switch( tipo.intValue() )
{
case 1:
ht.put( "tipo_apos_doenca", "" );
break;
case 2:
ht.put( "tipo_apos_acidente", "" );
break;
case 3:
ht.put( "tipo_pedido_trabalhador", "" );
break;
case 4:
ht.put( "tipo_pedido_empresa", "" );
break;
case 5:
ht.put( "tipo_mudanca_funcao", "" );
break;
case 6:
ht.put( "tipo_trabalho", "" );
break;
case 10:
aux = (String)exame.get( ExameData.OUTRO_TIPO );
ht.put( "tipo_outro", aux != null ? aux : "" );
break;
}
break;
// case 4:
// aux = (String)exame.get( ExameData.OUTRO_TIPO );
// ht.put( "tipo_outro", aux != null ? aux : "" );
// break;
}
tipo = (Integer)exame.get( ExameData.RESULTADO );
switch( tipo.intValue() )
{
case 1:
ht.put( "resultado_apto", "" );
break;
case 2:
ht.put( "resultado_apto_condicionalmente", "" );
break;
case 3:
ht.put( "resultado_inapto_temp", "" );
break;
case 4:
ht.put( "resultado_inapto_def", "" );
break;
}
aux = (String)exame.get( ExameData.OUTRA_FUNCAO_1 );
ht.put( "outra_funcao_1", aux != null ? aux : "" );
aux = (String)exame.get( ExameData.OUTRA_FUNCAO_2 );
ht.put( "outra_funcao_2", aux != null ? aux : "" );
aux = (String)exame.get( ExameData.OUTRA_FUNCAO_3 );
ht.put( "outra_funcao_3", aux != null ? aux : "" );
aux = (String)exame.get( ExameData.OUTRA_FUNCAO_4 );
ht.put( "outra_funcao_4", aux != null ? aux : "" );
aux = (String)exame.get( ExameData.OUTRAS_RECOMENDACOES );
ht.put( "outras_recomendacoes", aux != null ? aux : "" );
data = ( Date )exame.get( ExameData.PROXIMO_EXAME );
ht.put( "proximo_exame", data != null ? df.format( data ) : "n/d" );
aux = (String)medico.get( MedicoData.NOME );
ht.put( "medicos.nome", aux != null ? aux : "" );
aux = (String)medico.get( MedicoData.NUMERO_CEDULA );
ht.put( "numero_cedula", aux != null ? aux : "" );
ExamePDF ePDF = new ExamePDF();
return ePDF.createPDF( ht );
}
private void excel()
{
FileDialog fd = new FileDialog( this, "Escolha um ficheiro Excel:", FileDialog.LOAD );
fd.setDirectory( System.getProperty( "user.home" ) );
fd.setFilenameFilter( new FilenameFilter() {
public boolean accept( File dir, String name )
{
return (name!=null) && (name.indexOf( ".xls" ) != -1);
}
} );
fd.setVisible( true );
//String filename = "c:\\test.xls";
String filename = fd.getFile();
if( filename != null )
{
filename = fd.getDirectory() + File.separator + filename;
try
{
Importer importer = new Importer( this, filename, false, Importer.TYPE_ADMISSAO );
Hashtable hash = importer.getData();
if( hash != null )
{
trabalhadorPanel.setData( hash );
}
}
catch( Exception ex )
{
ex.printStackTrace();
JOptionPane.showMessageDialog( this, "Erro a importar", "Erro...", JOptionPane.ERROR_MESSAGE );
}
}
}
private void printOld()
{
if( trabalhadorID == null )
{
return;
}
Vector linhas;
try
{
IDObject fichas[] = fdpProvider.getAllFichasForTrabalhador( trabalhadorID );
linhas = new Vector( Arrays.asList( fichas ) );
}
catch( Exception ex )
{
DialogException.showExceptionMessage( ex, "Erro a criar lista de Fichas anteriores", true );
return;
}
ListActionDialog dialog = new ListActionDialog( this, "Imprimir Fichas Anteriores", new String[]{ "Data" },
linhas, this );
dialog.setSize( 250, 200 );
dialog.show();
}
public boolean executeListAction( int line, Object value )
{
if( value == null )
{
return true;
}
try
{
print( ( ( IDObject )value ).getID() );
}
catch( Exception ex )
{
DialogException.showExceptionMessage( ex, "Erro a imprimir Ficha", true );
return false;
}
return true;
}
public String getListActionName()
{
return "Imprimir";
}
private void delete( Integer exameID )
throws Exception
{
// MetaObject exame = fdpProvider.load( fdpProvider.EXAMES, new DBKey( exameID ) );
ExameData exame = (ExameData) JDO.load( ExameData.class, exameID );
if( exame == null )
{
throw new Exception( "N\u00e3o existe exame" );
}
if( JOptionPane.showConfirmDialog( this,
"Tem a certeza que quer apagar o exame de " + exame.get( ExameData.DATA ) + "?",
"...", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null ) == 0 )
{
// exame.set( ExameData.INACTIVO, "y" );
// exame.save();
exame.delete();
}
}
}