/*
* PesquisasWindow.java
*
* Created on 3 de Junho de 2004, 18:13
*/
package siprp.pesquisas;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import com.evolute.utils.*;
import com.evolute.utils.data.*;
import com.evolute.utils.documents.*;
import com.evolute.utils.jdo.*;
import com.evolute.utils.tables.*;
import com.evolute.utils.tracker.*;
import com.evolute.utils.ui.*;
import com.evolute.utils.ui.text.*;
import siprp.data.*;
/**
*
* @author fpalma
*/
public class PesquisasWindow extends JFrame
implements TrackableWindow, ListSelectionListener, ActionListener
{
private JDOProvider JDO;
private PesquisasProvider provider;
private JTextField anoText;
private BaseTable empresasTable;
private VectorTableModel empresasModel;
private BaseTable estabelecimentosTable;
private VectorTableModel estabelecimentosModel;
private JButton pesquisarButton;
private JButton exportarButton;
private JEditorPane resultadoText;
// public static void main( String args[] )
// throws Exception
// {
// new PesquisasWindow().show();
// }
/** Creates a new instance of PesquisasWindow */
public PesquisasWindow()
throws Exception
{
provider = (PesquisasProvider)PesquisasProvider.getProvider();
JDO =( JDOProvider ) Singleton.getInstance( Singleton.DEFAULT_JDO_PROVIDER );
setupComponents();
}
private void setupComponents()
{
setSize( 1000, 700 );
setTitle( "Relat\u00f3rio Anual" );
JLabel anoLabel = new JLabel( "Ano" );
anoText = new JTextField();
anoText.setDocument( new YearDocument() );
anoText.setPreferredSize( new Dimension( 50, 20 ) );
new CopyPasteHandler( anoText );
empresasModel = new VectorTableModel( new String[]{ "Designa\u00e7\u00e3o Social" } );
empresasTable = new BaseTable( empresasModel );
empresasTable.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
empresasTable.setNonResizableNorReordable();
empresasTable.getSelectionModel().addListSelectionListener( this );
JScrollPane empresasScroll = new JScrollPane();
empresasScroll.setBorder( BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Empresa" ) );
empresasScroll.setViewportView( empresasTable );
empresasScroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
empresasScroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
empresasScroll.getVerticalScrollBar().setBlockIncrement(20);
estabelecimentosModel = new VectorTableModel( new String[]{ "Nome" } );
estabelecimentosTable = new BaseTable( estabelecimentosModel );
estabelecimentosTable.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
estabelecimentosTable.setNonResizableNorReordable();
estabelecimentosTable.getSelectionModel().addListSelectionListener( this );
JScrollPane estabelecimentosScroll = new JScrollPane();
estabelecimentosScroll.setBorder( BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Estabelecimento" ) );
estabelecimentosScroll.setViewportView( estabelecimentosTable );
estabelecimentosScroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
estabelecimentosScroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
pesquisarButton = new JButton( "Pesquisar" );
pesquisarButton.addActionListener( this );
exportarButton = new JButton( "Exportar" );
exportarButton.addActionListener( this );
resultadoText = new JEditorPane( "text/html", "" );
resultadoText.setEditable( false );
// resultadoText.setLineWrap( true );
// resultadoText.setWrapStyleWord( true );
new CopyPasteHandler( resultadoText );
JScrollPane resultadoScroll = new JScrollPane();
resultadoScroll.setViewportView( resultadoText );
resultadoScroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
resultadoScroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
JPanel pad;
GridBagLayout gridbag = new GridBagLayout();
getContentPane().setLayout( gridbag );
GridBagConstraints constraints = new GridBagConstraints();
constraints.insets = new Insets( 5, 5, 5, 5 );
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.weighty = 0;
constraints.gridheight = 1;
constraints.weightx = 1;
constraints.gridwidth = GridBagConstraints.REMAINDER;
JPanel anoPanel = new JPanel();
anoPanel.setLayout( new FlowLayout( FlowLayout.LEFT ) );
anoPanel.add( anoLabel );
anoPanel.add( anoText );
gridbag.setConstraints( anoPanel, constraints );
getContentPane().add( anoPanel );
constraints.fill = GridBagConstraints.BOTH;
constraints.weighty = 0.2;
constraints.gridheight = 2;
constraints.weightx = 0.3;
constraints.gridwidth = 3;
gridbag.setConstraints( empresasScroll, constraints );
getContentPane().add( empresasScroll );
gridbag.setConstraints( estabelecimentosScroll, constraints );
getContentPane().add( estabelecimentosScroll );
constraints.weighty = 0;
constraints.gridheight = 1;
constraints.weightx = 0.4;
constraints.gridwidth = GridBagConstraints.REMAINDER;
pad = new JPanel();
gridbag.setConstraints( pad, constraints );
getContentPane().add( pad );
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.weighty = 0;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.gridwidth = 1;
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout( new GridLayout( 1, 2 ) );
buttonPanel.add( pesquisarButton );
buttonPanel.add( exportarButton );
gridbag.setConstraints( buttonPanel, constraints );
getContentPane().add( buttonPanel );
constraints.weighty = 0;
constraints.gridheight = 1;
constraints.weightx = 0.4;
constraints.gridwidth = GridBagConstraints.REMAINDER;
pad = new JPanel();
gridbag.setConstraints( pad, constraints );
getContentPane().add( pad );
constraints.fill = GridBagConstraints.BOTH;
constraints.weighty = 0.8;
constraints.gridheight = GridBagConstraints.REMAINDER;
constraints.weightx = 1;
constraints.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints( resultadoScroll, constraints );
getContentPane().add( resultadoScroll );
}
private boolean close()
{
setVisible( false );
dispose();
return true;
}
public boolean closeIfPossible()
{
return close();
}
public void open()
{
empresasTable.clearSelection();
try
{
IDObject empresas[] = provider.getAllEmpresas();
empresasModel.setValues( new Vector( Arrays.asList( empresas ) ) );
}
catch( Exception ex )
{
DialogException.showExceptionMessage( ex, "Erro a carregar dados", true );
}
setVisible( true );
}
public void refresh()
{
}
public void valueChanged( ListSelectionEvent e )
{
Object source = e.getSource();
if( source.equals( empresasTable.getSelectionModel() ) )
{
estabelecimentosTable.clearSelection();
int selected = empresasTable.getSelectedRow();
if( selected == -1 )
{
return;
}
IDObject empresa = (IDObject) empresasModel.getRowAt( selected );
estabelecimentosModel.clearAll();
IDObject estabelecimentos[];
try
{
estabelecimentos = provider.getAllEstabelecimentosForEmpresa( empresa.getID() );
}
catch( Exception ex )
{
DialogException.showExceptionMessage( ex, "Erro a carregar os estabelecimentos", true );
return;
}
Vector v = new Vector( Arrays.asList( estabelecimentos ) );
v.add( new MappableObject( new Integer( -1 ), "TODOS" ) );
estabelecimentosModel.setValues( v );
}
/* else if( source.equals( estabelecimentosTable.getSelectionModel() ) )
{
}*/
}
public void actionPerformed( ActionEvent e )
{
Object source = e.getSource();
if( source.equals( pesquisarButton ) )
{
int sEmpresa = empresasTable.getSelectedRow();
int sEstabelecimento = estabelecimentosTable.getSelectedRow();
String anoStr = anoText.getText();
int ano = ( anoStr.length() != 0 )? Integer.parseInt( anoStr ): -1;
if( sEmpresa == -1 || ano == -1 )
{
JOptionPane.showMessageDialog( this, "Tem de escolher ano e empresa", "Erro...",
JOptionPane.ERROR_MESSAGE );
return;
}
StringBuffer buff = new StringBuffer();
buff.append( "ANO: " );
buff.append( "" + ano + "
" );
buff.append( "
EMPRESA
" );
try
{
Integer idEmpresa = ( (IDObject)empresasModel.getRowAt( sEmpresa ) ).getID();
EmpresaData empresa = (EmpresaData)JDO.load( EmpresaData.class, idEmpresa );
buff.append( " DESIGNA\u00C7\u00C3O SOCIAL: " );
String designacao = (String)empresa.get( EmpresaData.DESIGNACAO_SOCIAL );
buff.append( "" + ( designacao == null ? "" : designacao.trim() ) + "
" );
buff.append( " MORADA: " );
String morada = (String)empresa.get( EmpresaData.MORADA );
buff.append( "" + ( morada == null ? "" : morada.trim() ) + "
" );
buff.append( " LOCALIDADE: " );
String localidade = ( String ) empresa.get( EmpresaData.LOCALIDADE );
buff.append( "" + ( localidade == null ? "" : localidade.trim() ) + "
" );
buff.append( " C\u00D3DIGO POSTAL: " );
String codigoPostal = ( String ) empresa.get( EmpresaData.CODIGO_POSTAL );
buff.append( "" + ( codigoPostal == null ? "" : codigoPostal.trim() ) + "
" );
ContactoData contacto = (ContactoData)empresa.get( EmpresaData.CONTACTO_2 );
if( contacto == null )
{
buff.append( " TELEFONE:
" );
buff.append( " FAX:
" );
}
else
{
buff.append( " TELEFONE: " );
String telefone = ( String ) contacto.get( ContactoData.TELEFONE );
buff.append( "" + ( telefone == null ? "" : telefone.trim() ) + "
" );
buff.append( " FAX: " );
String fax = ( String ) contacto.get( ContactoData.TELEFONE );
buff.append( "" + ( fax == null ? "" : fax.trim() ) + "
" );
}
buff.append( " DISTRITO: " );
String distrito = ( String ) empresa.get( EmpresaData.DISTRITO );
buff.append( "" + ( distrito == null ? "" : distrito ) + "
" );
buff.append( " CONCELHO: " );
String concelho = ( String ) empresa.get( EmpresaData.CONCELHO );
buff.append( "" + ( concelho == null ? "" : concelho ) + "
" );
buff.append( " N\u00DAMERO PESSOA COLECTIVA: " );
String contribuinte = ( String ) empresa.get( EmpresaData.CONTRIBUINTE );
buff.append( "" + ( contribuinte == null ? "" : contribuinte ) + "
" );
buff.append( " N\u00DAMERO SEGURAN\u00C7A SOCIAL: " );
String segSocial = ( String ) empresa.get( EmpresaData.SEGURANCA_SOCIAL );
buff.append( "" + ( segSocial == null ? "" : segSocial ) + "
" );
buff.append( " CAE: " );
String CAE = ( String ) empresa.get( EmpresaData.CAE );
buff.append( "" + ( CAE == null ? "" : CAE ) + "
" );
buff.append( "
N\u00DAMERO M\u00C9DIO DE TRABALHADORES DURANTE O ANO" );
double contagemMedia[] = provider.countNumeroMedioTrabalhadoresEmpresa( idEmpresa, ano );
buff.append( "
" );
buff.append( "" );
buff.append( "| | | | TOTAL | | HOMENS | | MULHERES | " );
buff.append( "
" );
buff.append( "| | TOTAL | | " + (int)( Math.round( contagemMedia[0] ) + Math.round( contagemMedia[1] ) ) + " | ");
buff.append( " | " + ( (int)Math.round( contagemMedia[0] ) ) + " | " );
buff.append( " | " + ( (int)Math.round( contagemMedia[1] ) ) + " | " );
buff.append( "
" );
buff.append( "
" );
buff.append( "
N\u00DAMERO TOTAL DE TRABALHADORES DURANTE O ANO" );
double contagemTotal[] = provider.countNumeroTotalTrabalhadoresEmpresa( idEmpresa, ano );
buff.append( "" );
buff.append( "" );
buff.append( "| | | | TOTAL | | HOMENS | | MULHERES | " );
buff.append( "
" );
buff.append( "| | TOTAL | | " + (int)( Math.round( contagemTotal[0] ) + Math.round( contagemTotal[1] ) ) + " | ");
buff.append( " | " + ( (int)Math.round( contagemTotal[0] ) ) + " | " );
buff.append( " | " + ( (int)Math.round( contagemTotal[1] ) ) + " | " );
buff.append( "
" );
buff.append( "
" );
IDObject estabelecimentos[];
if( sEstabelecimento == -1 || ( ( IDObject )estabelecimentosModel.getRowAt( sEstabelecimento ) ).getID().equals( new Integer( -1 ) ) )
{
estabelecimentos = provider.getAllEstabelecimentosForEmpresa( idEmpresa );
}
else
{
estabelecimentos = new IDObject[ 1 ];
estabelecimentos[ 0 ] = ( IDObject )estabelecimentosModel.getRowAt( sEstabelecimento );
}
buff.append( "
" );
buff.append( "
ESTABELECIMENTOS
" );
buff.append( "
" );
for( int n = 0; n < estabelecimentos.length; n++ )
{
EstabelecimentoData estabelecimento = (EstabelecimentoData)JDO.load( EstabelecimentoData.class, estabelecimentos[ n ].getID() );
buff.append( " NOME: " );
String nome = (String)estabelecimento.get( EstabelecimentoData.NOME );
buff.append( "" + ( nome == null ? "" : nome.trim() ) + "
" );
buff.append( " MORADA: " );
morada = (String)estabelecimento.get( EstabelecimentoData.MORADA );
buff.append( "" + ( morada == null ? "" : morada.trim() ) + "
" );
buff.append( " LOCALIDADE: " );
localidade = (String)estabelecimento.get( EstabelecimentoData.LOCALIDADE );
buff.append( "" + ( localidade == null ? "" : localidade.trim() ) + "
" );
buff.append( " C\u00D3DIGO POSTAL: " );
codigoPostal = (String)estabelecimento.get( EstabelecimentoData.CODIGO_POSTAL );
buff.append( "" + ( codigoPostal == null ? "" : codigoPostal.trim() ) + "
" );
contacto = (ContactoData)estabelecimento.get( EstabelecimentoData.CONTACTO );
if( contacto == null )
{
buff.append( " TELEFONE:
" );
buff.append( " FAX:
" );
}
else
{
buff.append( " TELEFONE: " );
String telefone = ( String ) contacto.get( ContactoData.TELEFONE );
buff.append( "" + ( telefone == null ? "" : telefone.trim() ) + "
" );
buff.append( " FAX: " );
String fax = ( String ) contacto.get( ContactoData.TELEFONE );
buff.append( "" + ( fax == null ? "" : fax.trim() ) + "
" );
}
int countTrabalhadores[] = provider.countTrabalhadoresEstabelecimentoDezembro( estabelecimentos[ n ].getID(), ano );
buff.append( "
" );
buff.append( " TRABALHADORES A 31 DE DEZEMBRO DE " + ano + ": " );
buff.append( "
" );
buff.append( "" );
buff.append( "" );
buff.append( "| |   | TOTAL | HOMENS | MULHERES | " );
buff.append( "
" );
buff.append( "" );
buff.append( "| | TOTAL | "
+ " " + ( countTrabalhadores[0] + countTrabalhadores[1] ) + " | "
+ " " + ( countTrabalhadores[0] ) + " | "
+ " " + ( countTrabalhadores[1] ) + " | " );
buff.append( "
" );
int countTrabalhadoresEtario[][] = provider.countTrabalhadoresEstabelecimentoDezembroPorGrupoEtario( estabelecimentos[ n ].getID(), ano );
buff.append( "" );
buff.append( "| | MENOS DE 18 ANOS | "
+ " " + ( countTrabalhadoresEtario[0][0] + countTrabalhadoresEtario[0][1] ) + " | "
+ " " + ( countTrabalhadoresEtario[0][0] ) + " | "
+ " " + ( countTrabalhadoresEtario[0][1] ) + " | " );
buff.append( "
" );
buff.append( "" );
buff.append( "| | 18 A 49 ANOS | "
+ " " + ( countTrabalhadoresEtario[1][0] + countTrabalhadoresEtario[1][1] ) + " | "
+ " " + ( countTrabalhadoresEtario[1][0] ) + " | "
+ " " + ( countTrabalhadoresEtario[1][1] ) + " | " );
buff.append( "
" );
buff.append( "" );
buff.append( "| | MAIS DE 50 ANOS | "
+ " " + ( countTrabalhadoresEtario[2][0] + countTrabalhadoresEtario[2][1] ) + " | "
+ " " + ( countTrabalhadoresEtario[2][0] ) + " | "
+ " " + ( countTrabalhadoresEtario[2][1] ) + " | " );
buff.append( "
" );
buff.append( "
" );
buff.append( "
" );
int countExames[][] = provider.countExamesEstabelecimentoDezembroPorGrupoEtario( estabelecimentos[ n ].getID(), ano );
for( int cE = 0; cE < countExames[ countExames.length - 2 ].length; cE++ )
{
countExames[ countExames.length - 2 ][ cE ] += countExames[ countExames.length - 1 ][ cE ];
}
int totais[] = new int[ 6 ];
for( int cE1 = 0; cE1 < totais.length; cE1++ )
{
for( int cE2 = 0; cE2 < countExames.length; cE2++ )
{
totais[ cE1 ] += countExames[ cE2 ][ cE1 ];
}
}
buff.append( "
" );
buff.append( " N\u00DAMERO DE EXAMES DE ADMISS\u00C3O, PERI\u00D3DICOS E OCASIONAIS EFECTUADOS" );
buff.append( "
" );
buff.append( "" );
buff.append( "| | | | | TOTAL | | INFERIOR A 18 ANOS | "
+ " | 18 A 49 ANOS | | 50 E MAIS ANOS | |
" );
buff.append( "| | TOTAL DE EXAMES | | H | " );
buff.append( "" + ( totais[0] + totais[1] + totais[2] ) + " | | " );
for( int t = 0; t < 3; t++ )
{
buff.append( "" + totais[t] + " | | " );
}
buff.append( "
" );
buff.append( "| | | | M | " );
buff.append( "" + ( totais[3] + totais[4] + totais[5] ) + " | | " );
for( int t = 3; t < 6; t++ )
{
buff.append( "" + totais[t] + " | | " );
}
buff.append( "
" );
String sexos[] = new String[]{ "H", "M" };
for( int t = 0; t < provider.DESCRICAO_TIPOS_EXAME.length; t++ )
{
buff.append( "| | "+provider.DESCRICAO_TIPOS_EXAME[t]+" | " );
for( int s = 0; s < 2; s++ )
{
buff.append( " | " + sexos[ s ] + " | " );
buff.append( "" + (countExames[t][s*3]+countExames[t][s*3+1]+countExames[t][s*3+2]) + " | | " );
for( int d = 0; d < 3; d++ )
{
buff.append( "" + countExames[t][s*3+d] + " | | " );
}
if( s == 0 )
{
buff.append( "
| | | " );
}
else
{
buff.append( "
" );
}
}
}
int countExamesOcasionais[][] = provider.countExamesOcasionaisEstabelecimentoDezembroPorGrupoEtario( estabelecimentos[ n ].getID(), ano );
for( int t = 0; t < provider.DESCRICAO_TIPOS_OCASIONAL.length; t++ )
{
buff.append( "| | "+provider.DESCRICAO_TIPOS_OCASIONAL[t]+" | " );
for( int s = 0; s < 2; s++ )
{
buff.append( " | " + sexos[ s ] + " | " );
int sum = countExamesOcasionais[t][s*3]+countExamesOcasionais[t][s*3+1]+countExamesOcasionais[t][s*3+2];
buff.append( "" + ( sum < 0 ? "-" : "" + sum ) + " | | " );
for( int d = 0; d < 3; d++ )
{
int val = countExamesOcasionais[t][s*3+d];
if( val < 0 )
{
countExamesOcasionais[t][s*3+d] = 0;
}
buff.append( "" + ( val < 0 ? "-" : "" + val ) + " | | " );
}
if( s == 0 )
{
buff.append( "
| | | " );
}
else
{
buff.append( "
" );
}
}
}
buff.append( "
" );
buff.append( "
" );
buff.append( " EXAMES COMPLEMENTARES REALIZADOS" );
buff.append( "
" );
buff.append( "" );
buff.append( "| | TIPO DE EXAME | | " );
buff.append( "N\u00BA TOTAL DE EXAMES |
" );
String nomesExamesComp[] = provider.getNomesExames();
// int countExamesComp[] = provider.countExamesComplementaresEmpresa( idEmpresa, ano );
int countExamesComp[] = provider.countExamesComplementaresEstabelecimento( estabelecimentos[ n ].getID(), ano );
for( int nec = 0; nec < nomesExamesComp.length; nec++ )
{
buff.append( "" );
buff.append( "| | " + nomesExamesComp[nec] + " | " );
buff.append( " | " + countExamesComp[nec] + " | " );
buff.append( "
" );
}
buff.append( "
" );
buff.append("
");
}
resultadoText.setText( buff.toString() );
}
catch( Exception ex )
{
DialogException.showExceptionMessage( ex, "Erro a carregar dados", true );
return;
}
}
else if( source.equals( exportarButton ) )
{
exportar();
}
}
public void exportar()
{
FileDialog dialog = new FileDialog( this, "Ficheiro de destino", FileDialog.SAVE );
dialog.setDirectory( System.getProperty( "user.home" ) );
dialog.setVisible( true );
String fileName;
String dirName;
fileName = dialog.getFile();
dirName = dialog.getDirectory();
if( fileName != null )
{
int index = fileName.indexOf( '.' );
if( index == -1 )
{
fileName += ".html";
}
if( index == fileName.length() - 1 )
{
fileName += "html";
}
String fullName = dirName + fileName;
String text = resultadoText.getText();
String title = "S.I.P.R.P. - Sociedade Ibérica de Prevenção de Riscos Profissionais";
String style = "";
text = text.replace( "", "\n\t\t" + title + "\n" + style );
text = text.replace( "", "" );
text = text.replace( "", "
" );
text = text.replace( "", "\nRelatório Anual
" );
// String title = "S.I.P.R.P. - Sociedade Ibérica de Prevenção de Riscos Profissionais";
// text = text.replace( "", "\n\t\t" + title + "" );
// System.out.println( text );
try
{
FileWriter writer = new FileWriter( new File( fullName ) );
writer.write( text );
writer.close();
}
catch( IOException ex )
{
DialogException.showException( ex );
return;
}
}
}
}