/* * ClientesWindow.java * * Created on 12 de Maio de 2004, 18:11 */ package siprp.clientes; import static com.evolute.utils.strings.UnicodeLatin1Map.atilde; import static com.evolute.utils.strings.UnicodeLatin1Map.ccedil; import java.awt.Dimension; import java.awt.FileDialog; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.io.File; import java.io.FilenameFilter; import java.text.DateFormat; import java.util.Arrays; import java.util.Date; import java.util.Hashtable; import java.util.Locale; import java.util.Vector; import javax.swing.BorderFactory; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTabbedPane; import javax.swing.JTextField; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import siprp.FichaDataProvider; import siprp.SIPRPTracker; import siprp.SingletonConstants; import siprp.data.AvisoConstants; import siprp.data.SearchExecuterFactory; import siprp.data.outer.ContactosData; import siprp.data.outer.EmpresasData; import siprp.data.outer.EstabelecimentosData; import siprp.data.outer.EtiquetasData; import siprp.data.outer.ExamesData; import siprp.data.outer.TrabalhadoresData; import siprp.ficha.ExamePDF; import siprp.importer.Importer; import siprp.ui.SIPRPEditorWindow; import com.evolute.entity.ProviderInterface; import com.evolute.utils.Singleton; import com.evolute.utils.data.IDObject; import com.evolute.utils.dataedition.persistence.Persistent; import com.evolute.utils.dataui.ControllableComponent; import com.evolute.utils.date.DateUtils; import com.evolute.utils.jdo.JDOObject; import com.evolute.utils.strings.StringPlainer; import com.evolute.utils.ui.DialogException; import com.evolute.utils.ui.dialog.ListAction; import com.evolute.utils.ui.dialog.ListActionDialog; import com.evolute.utils.ui.dialog.LongMessageDialog; import com.evolute.utils.ui.search.SearchDialog; import com.evolute.utils.ui.text.CopyPasteHandler; import com.evolute.utils.ui.window.ActionHandler; /** * * @author fpalma */ public class ClientesWindow extends SIPRPEditorWindow implements ListSelectionListener { /** * */ private static final long serialVersionUID = 1L; private static final DateFormat DF = DateFormat.getDateInstance( DateFormat.SHORT, new Locale( "pt", "PT" ) ); public static final Hashtable TR_IMPORT_NAMES_TO_TRAB = new Hashtable(); public static final Hashtable TR_IMPORT_NAMES_TO_DEMISSOES = new Hashtable(); static { TR_IMPORT_NAMES_TO_TRAB.put( Importer.NOME, TrabalhadoresData.NOME ); TR_IMPORT_NAMES_TO_TRAB.put( Importer.SEXO, TrabalhadoresData.SEXO ); TR_IMPORT_NAMES_TO_TRAB.put( Importer.NACIONALIDADE, TrabalhadoresData.NACIONALIDADE ); TR_IMPORT_NAMES_TO_TRAB.put( Importer.DATA_NASCIMENTO, TrabalhadoresData.DATA_NASCIMENTO ); TR_IMPORT_NAMES_TO_TRAB.put( Importer.NUMERO_MECANOGRAFICO, TrabalhadoresData.NUMERO_MECANOGRAFICO ); TR_IMPORT_NAMES_TO_TRAB.put( Importer.DATA_ADMISSAO, TrabalhadoresData.DATA_ADMISSAO ); TR_IMPORT_NAMES_TO_TRAB.put( Importer.CATEGORIA, TrabalhadoresData.CATEGORIA ); TR_IMPORT_NAMES_TO_TRAB.put( Importer.LOCAL_TRABALHO, TrabalhadoresData.LOCAL_TRABALHO ); TR_IMPORT_NAMES_TO_TRAB.put( Importer.FUNCAO, TrabalhadoresData.FUNCAO_PROPOSTA ); TR_IMPORT_NAMES_TO_TRAB.put( Importer.DATA_ADMISSAO_FUNCAO, TrabalhadoresData.DATA_ADMISSAO_FUNCAO ); TR_IMPORT_NAMES_TO_DEMISSOES.put( Importer.NOME, TrabalhadoresData.NOME ); TR_IMPORT_NAMES_TO_DEMISSOES.put( Importer.DATA_DEMISSAO, TrabalhadoresData.DATA_DEMISSAO ); // JDOObject.registerOperationHandlerForClass( DisableDeleteHandler.INSTANCE, EmpresasData.class ); // JDOObject.registerOperationHandlerForClass( DisableDeleteHandler.INSTANCE, EstabelecimentosData.class ); // JDOObject.registerOperationHandlerForClass( DisableDeleteHandler.INSTANCE, TrabalhadoresData.class ); // // JDOObject.registerOperationHandlerForClass( MarcacaoSaveAndDeleteHandler.INSTANCE, MarcacaoEmpresasData.class ); // JDOObject.registerOperationHandlerForClass( MarcacaoSaveAndDeleteHandler.INSTANCE, MarcacaoEstabelecimentosData.class ); // JDOObject.registerOperationHandlerForClass( MarcacaoSaveAndDeleteHandler.INSTANCE, MarcacoesTrabalhadorData.class ); } public static final int INDEX_EMPRESA = 0; public static final int INDEX_ESTABELECIMENTO = 1; public static final int INDEX_TRABALHADOR = 2; private static final String ESTABELECIMENTO_PLANO_ACTUACAO_TAB_NOME = "Plano de Actua"+ccedil+atilde+"o"; private static int hierarchy[] = new int[]{0,0,1}; private static int permissions[][] = new int[][]{ { NEW_INDEX, EDIT_INDEX, CANCEL_INDEX, SAVE_INDEX, SELECT_BYNAME_INDEX, DELETE_INDEX }, { NEW_INDEX, EDIT_INDEX, CANCEL_INDEX, SAVE_INDEX, DELETE_INDEX }, { NEW_INDEX, EDIT_INDEX, CANCEL_INDEX, SAVE_INDEX, DELETE_INDEX } }; private ProviderInterface JDO; private ClientesDataProvider clientesProvider; private ControllableComponent panels[] = new ControllableComponent[ 4 ]; private EmpresaPanel empresaPanel; private JPanel estabelecimentoOuterPanel; private JTabbedPane tabbedPane; private JTextField estabelecimentoText; private EstabelecimentoPanel estabelecimentoPanel; private TrabalhadorPanel trabalhadorPanel; private SIPRPTracker tracker; private EmpresasData empresa; private Integer elementosProtocolo[][]; private EstabelecimentosData estabelecimento; private TrabalhadoresData trabalhador; private AvisosPanel avisos; protected boolean webAware; // private EstabelecimentoPlanoActuacaoPanel planoActuacaoPanel; // public static void main( String args[] ) // throws Exception // { // ClientesWindow window = new ClientesWindow(); // window.show(); // } /** Creates a new instance of ClientesWindow */ public ClientesWindow( AvisosPanel avisos ) throws Exception { super( permissions ); this.avisos = avisos; webAware = ( ( Boolean ) Singleton.getInstance( SingletonConstants.WEB_AWARE ) ).booleanValue(); JDO = ( ProviderInterface ) Singleton.getInstance( Singleton.DEFAULT_EVO_DATA_PROVIDER ); clientesProvider = ( ClientesDataProvider ) ClientesDataProvider.getProvider(); setEditorManagerHierarchy( hierarchy ); setupComponents(); this.setExtendedState(this.getExtendedState() | ClientesWindow.MAXIMIZED_BOTH); } private void setupComponents() throws Exception { setSize( 1000, 740 ); setTitle( "Gest\u00e3o de Clientes" ); changeGroupName( INDEX_ESTABELECIMENTO, "Estabelecimento" ); JPanel toolbarEstabelecimento = getToolbarPanel( INDEX_ESTABELECIMENTO ); changeGroupName( INDEX_TRABALHADOR, "Trabalhador" ); JPanel toolbarTrabalhador = getToolbarPanel( INDEX_TRABALHADOR ); tabbedPane = new JTabbedPane(); empresaPanel = new EmpresaPanel(); empresaPanel.setBorder( BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), "Empresa/Entidade" ) ); estabelecimentoText = new JTextField(); estabelecimentoPanel = new EstabelecimentoPanel( toolbarEstabelecimento, estabelecimentoText, empresaPanel.getEstabelecimentosTable() ); estabelecimentoOuterPanel = new JPanel(); estabelecimentoOuterPanel.setBorder( BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), "Estabelecimento" ) ); JLabel estabelecimentoLabel = new JLabel( "Designa\u00e7\u00e3o" ); trabalhadorPanel = new TrabalhadorPanel( toolbarTrabalhador, empresaPanel.getEstabelecimentosTable() ); // planoActuacaoPanel = new EstabelecimentoPlanoActuacaoPanel(); GridBagLayout gridbag = new GridBagLayout(); getContentPane().setLayout( gridbag ); GridBagConstraints constraints = new GridBagConstraints(); constraints.insets = new Insets( 1, 1, 1, 1 ); constraints.fill = GridBagConstraints.BOTH; constraints.weightx = 1; constraints.gridwidth = GridBagConstraints.REMAINDER; constraints.weighty = 0.5; constraints.gridheight = 1; gridbag.setConstraints( empresaPanel, constraints ); getContentPane().add( empresaPanel ); constraints.weighty = 0.5; constraints.gridheight = GridBagConstraints.REMAINDER; gridbag.setConstraints( estabelecimentoOuterPanel, constraints ); getContentPane().add( estabelecimentoOuterPanel ); gridbag = new GridBagLayout(); estabelecimentoOuterPanel.setLayout( gridbag ); constraints = new GridBagConstraints(); constraints.insets = new Insets( 1, 1, 1, 1 ); constraints.fill = GridBagConstraints.BOTH; constraints.weightx = 0; constraints.gridwidth = 1; constraints.weighty = 0; constraints.gridheight = 1; gridbag.setConstraints( estabelecimentoLabel, constraints ); estabelecimentoOuterPanel.add( estabelecimentoLabel ); constraints.weightx = 1; constraints.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints( estabelecimentoText, constraints ); estabelecimentoOuterPanel.add( estabelecimentoText ); constraints.weightx = 1; constraints.gridwidth = GridBagConstraints.REMAINDER; constraints.weighty = 1; constraints.gridheight = GridBagConstraints.REMAINDER; gridbag.setConstraints( tabbedPane, constraints ); estabelecimentoOuterPanel.add( tabbedPane ); tabbedPane.add( "Dados", estabelecimentoPanel ); tabbedPane.add( "Trabalhadores", trabalhadorPanel ); // tabbedPane.add( ESTABELECIMENTO_PLANO_ACTUACAO_TAB_NOME, planoActuacaoPanel ); panels[ INDEX_EMPRESA ] = empresaPanel; panels[ INDEX_ESTABELECIMENTO ] = estabelecimentoPanel; panels[ INDEX_TRABALHADOR ] = trabalhadorPanel; //empresaPanel.addListSelectionListener( estabelecimentoPanel ); empresaPanel.addListSelectionListener( trabalhadorPanel ); empresaPanel.addListSelectionListener( this ); trabalhadorPanel.addListSelectionListener( this ); registerAction( new ActionHandler(){ public void execute() { excel(); } public boolean activate( boolean newAction, boolean editAction, boolean cancelAction, boolean saveAction, boolean deleteAction, boolean selectAction ) { return editAction; } }, "Importar", "Importar de Excel", "excel", 0, true ); registerAction( new ActionHandler(){ public void execute() { excelDemissoes(); } public boolean activate( boolean newAction, boolean editAction, boolean cancelAction, boolean saveAction, boolean deleteAction, boolean selectAction ) { return editAction; } }, "Demiss\u00f5es", "Demiss\u00f5es por Excel", null, 0, true ); registerAction( new ActionHandler(){ public void execute() { mudarEstabelecimento(); } public boolean activate( boolean newAction, boolean editAction, boolean cancelAction, boolean saveAction, boolean deleteAction, boolean selectAction ) { return editAction; } }, "Mudar Estabelecimento", "Mudar Estabelecimento", null, INDEX_TRABALHADOR, false ); registerAction( new ActionHandler(){ public void execute() { imprimirFicha(); } public boolean activate( boolean newAction, boolean editAction, boolean cancelAction, boolean saveAction, boolean deleteAction, boolean selectAction ) { return editAction ; } }, "Imprimir Ficha de Aptid\u00e3o", "Imprimir Ficha de Aptid\u00e3o", null, INDEX_TRABALHADOR, false ); registerAction( new ActionHandler(){ public void execute() { imprimirEtiquetaTrabalhador(); } public boolean activate( boolean newAction, boolean editAction, boolean cancelAction, boolean saveAction, boolean deleteAction, boolean selectAction ) { return editAction ; } }, "Imprimir Etiqueta", "Imprimir Etiqueta de Trabalhador", null, INDEX_TRABALHADOR, false ); registerAction( new ActionHandler(){ public void execute() { imprimirEtiquetaEstabelecimento(); } public boolean activate( boolean newAction, boolean editAction, boolean cancelAction, boolean saveAction, boolean deleteAction, boolean selectAction ) { return editAction ; } }, "Imprimir Etiqueta", "Imprimir Etiqueta de Estabelecimento", null, INDEX_ESTABELECIMENTO, false ); registerAction( new ActionHandler(){ public void execute() { restaurarEmpresa(); } public boolean activate( boolean newAction, boolean editAction, boolean cancelAction, boolean saveAction, boolean deleteAction, boolean selectAction ) { return true; } }, "Restaurar Empresa", "Restaurar Empresa", null, INDEX_EMPRESA, false ); if( webAware ) { registerAction( new ActionHandler(){ public void execute() { utilizadorWeb( false ); } public boolean activate( boolean newAction, boolean editAction, boolean cancelAction, boolean saveAction, boolean deleteAction, boolean selectAction ) { return editAction; } }, "Utilizador Web da Empresa", "Utilizador Web da Empresa", null, INDEX_EMPRESA, false ); registerAction( new ActionHandler(){ public void execute() { utilizadorWeb( true ); } public boolean activate( boolean newAction, boolean editAction, boolean cancelAction, boolean saveAction, boolean deleteAction, boolean selectAction ) { return true; } }, "Utilizador Web", "Utilizador Web", null, INDEX_EMPRESA, false ); registerAction( new ActionHandler(){ public void execute() { noticias(); } public boolean activate( boolean newAction, boolean editAction, boolean cancelAction, boolean saveAction, boolean deleteAction, boolean selectAction ) { return true; } }, "Not\u00edcias", "Not\u00edcias", null, INDEX_EMPRESA, false ); } registerAction( new ActionHandler(){ public void execute() { restaurarEstabelecimento(); } public boolean activate( boolean newAction, boolean editAction, boolean cancelAction, boolean saveAction, boolean deleteAction, boolean selectAction ) { return newAction || saveAction; } }, "Restaurar Estabelecimento", "Restaurar Estabelecimento", null, INDEX_ESTABELECIMENTO, false ); registerAction( new ActionHandler(){ public void execute() { restaurarTrabalhador(); } public boolean activate( boolean newAction, boolean editAction, boolean cancelAction, boolean saveAction, boolean deleteAction, boolean selectAction ) { return newAction || saveAction; } }, "Restaurar Trabalhador", "Restaurar Trabalhador", null, INDEX_TRABALHADOR, false ); new CopyPasteHandler( estabelecimentoText ); } public boolean searchByName( ) { SearchDialog search; Integer empresaID; // switch( index ) // { // case INDEX_EMPRESA: search = new SearchDialog( SearchExecuterFactory.getSearchExecuter( EmpresasData.class ), null, false, null, true ); empresaID = ( Integer )search.getSelected(); if( empresaID != null ) { try { empresa = (EmpresasData) JDO.load( EmpresasData.class, empresaID ); elementosProtocolo = clientesProvider.getElementosProtocoloForEmpresa( empresaID, 2 ); } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a carregar a empresa", true ); empresa = null; } reload( INDEX_EMPRESA ); } // break; // } return true; } public boolean newItem( int index ) { return true; } public void reload( int index ) { // System.out.println( "RELOAD: " + index + " e: " + empresa + " e: " +estabelecimento + " t: " + trabalhador ); switch( index ) { case INDEX_EMPRESA: empresaPanel.fill( new Object[]{ empresa, elementosProtocolo } ); estabelecimentoPanel.setEmpresa( empresa ); break; case INDEX_ESTABELECIMENTO: // trabalhadorPanel.fill( null ); estabelecimentoPanel.fill( estabelecimento ); trabalhadorPanel.setEstabelecimento( estabelecimento ); // planoActuacaoPanel.fill( estabelecimento == null ? null : estabelecimento.get( EstabelecimentosData.ID ) ); break; case INDEX_TRABALHADOR: trabalhadorPanel.fill( trabalhador ); // trabalhadorPanel.reload(); break; } } public boolean save( int index ) { boolean isNew = false; Object object = panels[ index ].save(); try { switch( index ) { case INDEX_EMPRESA: empresa = (EmpresasData) ( ( Object [] )object )[ 0 ]; if( empresa.get( EmpresasData.ID ) == null ) { isNew = true; } elementosProtocolo = (Integer [][]) ( ( Object [] )object )[ 1 ]; // System.out.println( empresa.debug() ); ContactosData c1 = empresa.toContacto_1(); ContactosData c2 = empresa.toContacto_2(); // empresa.set( EmpresasData.CONTACTO_1, null ); // empresa.set( EmpresasData.CONTACTO_2, null ); // empresa.save(); // empresa.set( EmpresasData.CONTACTO_1, c1 ); // empresa.set( EmpresasData.CONTACTO_2, c2 ); if( c1 != null ) { c1.save(); } if( c2 != null ) { c2.save(); } // empresa.set( EmpresasData.INACTIVO, "n" ); empresa.set( EmpresasData.DESIGNACAO_SOCIAL_PLAIN, com.evolute.utils.strings.StringPlainer.convertString( ( String ) empresa.get( EmpresasData.DESIGNACAO_SOCIAL ) ) ); empresa.save(); clientesProvider.setElementosProtocoloForEmpresa( ( Integer ) empresa.get( EmpresasData.ID ), elementosProtocolo ); if( isNew ) { estabelecimentoPanel.setEmpresa( empresa ); } ( ( EmpresaPanel )panels[ INDEX_EMPRESA ] ).reloadMarcacoes(); break; case INDEX_ESTABELECIMENTO: estabelecimento = (EstabelecimentosData) object; if( estabelecimento.get( EstabelecimentosData.ID ) == null ) { isNew = true; } ContactosData c = ( ContactosData )estabelecimento.toContacto_id(); c.save(); if( estabelecimento.toEmpresa_id() == null ) { JOptionPane.showMessageDialog( this, "Erro a guardar estabelecimento (NULL).", "Erro...", JOptionPane.ERROR_MESSAGE ); return false; } estabelecimento.set( EstabelecimentosData.NOME_PLAIN, com.evolute.utils.strings.StringPlainer.convertString( ( String ) estabelecimento.get( EstabelecimentosData.NOME ) ) ); estabelecimento.save(); estabelecimentoPanel.postSave(); if( isNew ) { trabalhadorPanel.setEstabelecimento( estabelecimento ); } break; case INDEX_TRABALHADOR: trabalhador = (TrabalhadoresData) object; trabalhador.set( TrabalhadoresData.NOME_PLAIN, com.evolute.utils.strings.StringPlainer.convertString( ( String ) trabalhador.get( TrabalhadoresData.NOME ) ) ); trabalhador.save(); trabalhadorPanel.postSave(); break; default: ( ( JDOObject )object ).save(); } // object.save(); } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a gravar", true ); return false; } //reload( index ); return true; } public void enableComponents( int index, boolean enable ) { panels[ index ].setEnabled( enable ); avisos.setEnabled( !empresaPanel.isEnabled() && !estabelecimentoPanel.isEnabled() && !trabalhadorPanel.isEnabled(), AvisoConstants.TIPO_EMPRESA ); } public void clear( int index ) { panels[ index ].fill( null ); } public void valueChanged( ListSelectionEvent e ) { Object source = e.getSource(); Integer id; if( source.equals( empresaPanel ) ) { id = empresaPanel.getSelectedEstabelecimento(); if( id != null ) { select( INDEX_ESTABELECIMENTO ); try { estabelecimento = ( EstabelecimentosData ) JDO.load( EstabelecimentosData.class, id ); } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a carregar o estabelecimento", true ); estabelecimento = null; } } else { estabelecimento = null; } reload( INDEX_ESTABELECIMENTO ); } else if( source.equals( trabalhadorPanel ) ) { id = trabalhadorPanel.getSelectedTrabalhador(); //System.out.println( "trabalhadorid: " + id ); if( id != null ) { try { trabalhador = (TrabalhadoresData) JDO.load( TrabalhadoresData.class, id ); } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a carregar o trabalhador", true ); trabalhador = null; } select( INDEX_TRABALHADOR ); } else { trabalhador = null; } reload( INDEX_TRABALHADOR ); } } public boolean delete( int index ) { String yes = "Sim"; String no = "N\u00e3o"; switch( index ) { case INDEX_EMPRESA: if( empresa == null ) { return false; } if( JOptionPane.showOptionDialog( this, "Tem a certeza que quer apagar esta Empresa?", "Apagar Empresa...", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, new String[]{ no, yes }, no ) == 1 ) { try { empresa.delete(); empresa = null; } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a apagar", true ); return false; } return true; } break; case INDEX_ESTABELECIMENTO: if( estabelecimento == null ) { return false; } if( JOptionPane.showOptionDialog( this, "Tem a certeza que quer apagar este Estabelecimento?", "Apagar Estabelecimento...", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, new String[]{ no, yes }, no ) == 1 ) { try { estabelecimento.delete(); estabelecimento = null; reload( INDEX_EMPRESA ); } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a apagar", true ); return false; } return true; } break; case INDEX_TRABALHADOR: if( trabalhador == null ) { return false; } if( JOptionPane.showOptionDialog( this, "Tem a certeza que quer apagar este Trabalhador?", "Apagar Trabalhador...", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, new String[]{ no, yes }, no ) == 1 ) { try { TrabalhadoresData trabalhadorAux = trabalhador; trabalhador = null; trabalhadorAux.delete(); reload( INDEX_ESTABELECIMENTO ); try { trabalhadorPanel.reloaList(); } catch( Exception ex ) { ex.printStackTrace(); } } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a apagar", true ); return false; } return true; } break; } return false; } 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, true, Importer.TYPE_ADMISSAO ); Hashtable hash[] = importer.getMultipleData(); Vector activate = new Vector(); if( hash != null ) { // load all (active) trabalhadores for this company IDObject trab[] = ( ( FichaDataProvider )FichaDataProvider.getProvider() ).getAllTrabalhadoresForEmpresa( ( Integer )empresa.get( EmpresasData.ID ) ); Hashtable trabalhadores = new Hashtable(); for( int i = 0; i < trab.length; ++i ) { trabalhadores.put( trab[ i ].toString().trim().toLowerCase(), trab[ i ].getID() ); } // load all active estabelecimentos for this company IDObject estab[] = ( ( FichaDataProvider )FichaDataProvider.getProvider() ).getAllEstabelecimentosForEmpresa( ( Integer )empresa.get( EmpresasData.ID ) ); Hashtable estabelecimentos = new Hashtable(); for( int i = 0; i < estab.length; ++i ) { estabelecimentos.put( estab[ i ].toString().trim().toLowerCase(), estab[ i ].getID() ); } Hashtable estabCache = new Hashtable(); for( int i = 0; i < hash.length; ++i ) { // if( hash[ i ] == null ) // { // continue; // } for( int j = 0; j < Importer.NAMES.length; ++j ) { if( hash[ i ] == null ) { continue; } Object value = hash[ i ].remove( Importer.NAMES[ j ] ); if( Importer.NAMES[ j ].equals( Importer.NOME ) ) { if( value != null && trabalhadores.get( value.toString().trim().toLowerCase() ) == null ) { trabalhadores.put( value.toString().trim().toLowerCase(), value ); hash[ i ].put( TR_IMPORT_NAMES_TO_TRAB.get( Importer.NOME ), value.toString().trim() ); hash[ i ].put( TrabalhadoresData.NOME_PLAIN, com.evolute.utils.strings.StringPlainer.convertString( value.toString().trim(), false, false ) ); } else { Integer tID = (Integer) trabalhadores.get( value.toString().trim().toLowerCase() ); if( tID != null ) { activate.add( tID ); } hash[ i ] = null; } } else if( Importer.NAMES[ j ].equals( Importer.LOCAL_TRABALHO ) ) { if( value == null ) { value = "sede"; } Integer eid = ( Integer )estabelecimentos.get( value.toString().trim().toLowerCase() ); EstabelecimentosData esta; if( eid == null ) { // save this estab esta = new EstabelecimentosData(); esta.setToEmpresa_id( empresa ); esta.set( EstabelecimentosData.NOME, value.toString().trim() ); esta.set( EstabelecimentosData.NOME_PLAIN, com.evolute.utils.strings.StringPlainer.convertString( value.toString().trim(), false, false ) ); esta.save(); estabelecimentos.put( value.toString().trim().toLowerCase(), esta.get( EstabelecimentosData.ID ) ); } else { esta = ( EstabelecimentosData )estabCache.get( value ); if( esta == null ) { esta = (EstabelecimentosData) JDO.load( EstabelecimentosData.class, eid ); estabCache.put( value, esta ); } } hash[ i ].put( TrabalhadoresData.TO_ESTABELECIMENTO_ID, esta ); hash[ i ].put( TR_IMPORT_NAMES_TO_TRAB.get( Importer.LOCAL_TRABALHO ), value.toString().trim() ); } else { if( Importer.NAMES[ j ].indexOf( Importer.DATA_PREFIX ) == 0 ) { value = DateUtils.guessDate( (String)value ); if( value != null ) { hash[ i ].put( TR_IMPORT_NAMES_TO_TRAB.get( Importer.NAMES[ j ] ), value ); } } else { if( Importer.NAMES[ j ].equals( Importer.SEXO ) ) { if( value == null || ( (String) value ).length() == 0 ) { value = "m"; } else { value = ( (String) value ).substring(0,1).toLowerCase(); if( !value.equals( "m" ) && !value.equals( "f" ) ) { value = "m"; } } } if( value != null ) { hash[ i ].put( TR_IMPORT_NAMES_TO_TRAB.get( Importer.NAMES[ j ] ), value.toString().trim() ); } } } } } for( int i = 0; i < hash.length; ++i ) { if( hash[ i ] == null ) { continue; } // save trab TrabalhadoresData tr = new TrabalhadoresData(); tr.setHashData( hash[ i ] ); // o estabelecimento nao faz parte da lista por causa do UI tr.set( TrabalhadoresData.TO_ESTABELECIMENTO_ID, hash[ i ].get( TrabalhadoresData.TO_ESTABELECIMENTO_ID ) ); tr.set( TrabalhadoresData.PERFIL, new Integer( 1 ) ); // System.out.println( "T: " + i + "\n" + tr.debug() ); tr.save(); } for( int i = 0; i < activate.size(); i++ ) { TrabalhadoresData trabalhadorR = (TrabalhadoresData)JDO.load( TrabalhadoresData.class, ( Integer ) activate.elementAt( i ) ); trabalhadorR.set( TrabalhadoresData.INACTIVO, "n" ); trabalhadorR.save(); } } reload( INDEX_EMPRESA ); } catch( Exception ex ) { ex.printStackTrace(); JOptionPane.showMessageDialog( this, "Erro a importar", "Erro...", JOptionPane.ERROR_MESSAGE ); } } } private void excelDemissoes() { 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, true, Importer.TYPE_DEMISSAO ); Hashtable hash[] = importer.getMultipleData(); Vector invalidos = new Vector(); Vector validos = new Vector(); Vector semData = new Vector(); if( hash != null ) { // load all (active) trabalhadores for this company IDObject trab[] = ( ( FichaDataProvider )FichaDataProvider.getProvider() ).getAllTrabalhadoresForEmpresa( ( Integer )empresa.get( EmpresasData.ID ) ); Hashtable trabalhadores = new Hashtable(); for( int i = 0; i < trab.length; ++i ) { trabalhadores.put( StringPlainer.convertString( trab[ i ].toString().trim().toLowerCase() ), trab[ i ].getID() ); } Hashtable estabCache = new Hashtable(); for( int i = 0; i < hash.length; ++i ) { // if( hash[ i ] == null ) // { // continue; // } for( int j = 0; j < Importer.NAMES_DEMISSAO.length; ++j ) { if( hash[ i ] == null ) { continue; } Object value = hash[ i ].remove( Importer.NAMES_DEMISSAO[ j ] ); if( Importer.NAMES_DEMISSAO[ j ].equals( Importer.NOME ) ) { System.out.println( "VALUE NOME: " + value ); if( value != null && !trabalhadores.containsKey( StringPlainer.convertString( value.toString().trim().toLowerCase() ) ) ) { invalidos.add( new String[]{ value.toString().trim(), StringPlainer.convertString( value.toString().trim(), false, false ) } ); hash[ i ] = null; } else if( value != null ) { Integer tID = (Integer) trabalhadores.get( StringPlainer.convertString( value.toString().trim().toLowerCase() ) ); hash[ i ].put( TrabalhadoresData.ID, tID ); validos.add( new String[]{ value.toString().trim(), StringPlainer.convertString( value.toString().trim(), false, false ) } ); } else { hash[ i ] = null; } } else if( Importer.NAMES_DEMISSAO[ j ].equals( Importer.DATA_DEMISSAO ) ) { value = DateUtils.guessDate( (String)value ); if( value != null ) { hash[ i ].put( TR_IMPORT_NAMES_TO_DEMISSOES.get( Importer.NAMES_DEMISSAO[ j ] ), value ); } else { hash[ i ] = null; validos.removeElementAt( validos.size() - 1 ); semData.add( new String[]{ value.toString().trim(), StringPlainer.convertString( value.toString().trim(), false, false ) } ); } } } } //System.out.println( "HASH.LENGTH: " + hash.length ); for( int i = 0; i < hash.length; ++i ) { //System.out.println( "HASH[ " + i + " ]: " + hash[ i ] ); if( hash[ i ] == null ) { //System.out.println( "TRABALHADOR:continue" ); continue; } //System.out.println( "TRABALHADOR:not continue" ); TrabalhadoresData tr = (TrabalhadoresData)JDO.load( TrabalhadoresData.class, ( Integer ) hash[ i ].get( TrabalhadoresData.ID ) ); //System.out.println( "TRABALHADORID: " + tr.get( TrabalhadoresData.ID ) ); tr.set( TrabalhadoresData.DATA_DEMISSAO, hash[ i ].get( TrabalhadoresData.DATA_DEMISSAO ) ); //System.out.println( "TRABALHADORDEMISSAO: " + tr.get( TrabalhadoresData.DATA_DEMISSAO ) ); tr.save(); } StringBuffer buff = new StringBuffer( "" ); buff.append( "Demitidos:\n" ); for( int i = 0; i < validos.size(); i++ ) { buff.append( ( ( String [] ) validos.elementAt( i ) )[ 1 ] + "\n" ); } buff.append( "\nSem data ou data inv\u00e1lida:\n" ); for( int i = 0; i < semData.size(); i++ ) { buff.append( ( ( String [] ) semData.elementAt( i ) )[ 1 ] + "\n" ); } buff.append( "\nN\u00e3o encontrados na base de dados:\n" ); for( int i = 0; i < invalidos.size(); i++ ) { buff.append( ( ( String [] ) invalidos.elementAt( i ) )[ 1 ] + "\n" ); } new LongMessageDialog( this, false, buff.toString(), new Dimension( 600, 500 ) ); } reload( INDEX_EMPRESA ); } catch( Exception ex ) { ex.printStackTrace(); JOptionPane.showMessageDialog( this, "Erro a importar", "Erro...", JOptionPane.ERROR_MESSAGE ); } } } public void mudarEstabelecimento() { ListAction lAction = new ListAction(){ public String getListActionName() { return "Escolher"; } public boolean executeListAction( int line, Object value ) { if( trabalhador == null ) { return false; } try { EstabelecimentosData eatabelecimentoNovo = (EstabelecimentosData)JDO.load( EstabelecimentosData.class, ( (IDObject)value ).getID() ); trabalhador.set( TrabalhadoresData.TO_ESTABELECIMENTO_ID, eatabelecimentoNovo ); trabalhador.save(); trabalhador = null; reload( INDEX_ESTABELECIMENTO ); trabalhadorPanel.reloaList(); } catch( Exception ex ) { ex.printStackTrace(); return false; } return true; } }; try { IDObject estab[] = ( ( FichaDataProvider )FichaDataProvider.getProvider() ).getAllEstabelecimentosForEmpresa( ( Integer )empresa.get( EmpresasData.ID ) ); ListActionDialog dialog = new ListActionDialog( this, "Escolher Estabelecimento", new String []{ "Designa\u00e7\u00e3o" }, new Vector( Arrays.asList( estab ) ), lAction ); dialog.setSize( 250, 200 ); dialog.setVisible( true ); } catch( Exception ex ) { ex.printStackTrace(); } } public void setJDOObject( Persistent object ) { empresa = null; estabelecimento = null; trabalhador = null; reload(0); if( object instanceof TrabalhadoresData ) { tabbedPane.setSelectedIndex( 1 ); trabalhador = (TrabalhadoresData) object; estabelecimento = (EstabelecimentosData)trabalhador.toEstabelecimento_id(); empresa = (EmpresasData)estabelecimento.toEmpresa_id(); } else if( object instanceof EstabelecimentosData ) { tabbedPane.setSelectedIndex( 0 ); estabelecimento = (EstabelecimentosData)object; empresa = (EmpresasData)estabelecimento.toEmpresa_id(); } else if( object instanceof EmpresasData ) { empresa = (EmpresasData)object; } if( empresa != null ) { try { elementosProtocolo = clientesProvider.getElementosProtocoloForEmpresa( ( Integer ) empresa.get( EmpresasData.ID ), 2 ); } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a carregar dados do protocolo.", true ); elementosProtocolo = new Integer[2][0]; } } reload(0); if( empresa != null ) { select( INDEX_EMPRESA ); } if( estabelecimento != null ) { empresaPanel.setSelectedEstabelecimento( estabelecimento ); } if( trabalhador != null ) { trabalhadorPanel.setSelectedTrabalhador( trabalhador ); } } public void postSetup() { } public void restaurarEmpresa() { ListAction lAction = new ListAction(){ public String getListActionName() { return "Restaurar"; } public boolean executeListAction( int line, Object value ) { try { EmpresasData empresaR = (EmpresasData)JDO.load( EmpresasData.class, ( (IDObject)value ).getID() ); empresaR.set( EmpresasData.INACTIVO, "n" ); empresaR.save(); } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a restaurar a Empresa", true ); return false; } return true; } }; try { IDObject empresas[] = ( ( FichaDataProvider )FichaDataProvider.getProvider() ).getAllEmpresasDeleted(); ListActionDialog dialog = new ListActionDialog( this, "Restaurar Empresa", new String []{ "Designa\u00e7\u00e3o Social" }, new Vector( Arrays.asList( empresas ) ), lAction ); dialog.setSize( 250, 200 ); dialog.setVisible( true ); } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a criar lista de Empresas", true ); ex.printStackTrace(); } } public void restaurarEstabelecimento() { ListAction lAction = new ListAction(){ public String getListActionName() { return "Restaurar"; } public boolean executeListAction( int line, Object value ) { try { EstabelecimentosData estabelecimentoR = (EstabelecimentosData)JDO.load( EstabelecimentosData.class, ( (IDObject)value ).getID() ); estabelecimentoR.set( EstabelecimentosData.INACTIVO, "n" ); estabelecimentoR.save(); } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a restaurar o Estabelecimento", true ); return false; } return true; } }; if( empresa == null ) { return; } try { IDObject estabelecimentos[] = ( ( FichaDataProvider )FichaDataProvider.getProvider() ).getAllEstabelecimentosDeletedForEmpresa( ( Integer ) empresa.get( EmpresasData.ID ) ); ListActionDialog dialog = new ListActionDialog( this, "Restaurar Estabelecimento", new String []{ "Nome" }, new Vector( Arrays.asList( estabelecimentos ) ), lAction ); dialog.setSize( 250, 200 ); dialog.setVisible( true ); } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a criar lista de Estabelecimentos", true ); ex.printStackTrace(); } } public void restaurarTrabalhador() { ListAction lAction = new ListAction(){ public String getListActionName() { return "Restaurar"; } public boolean executeListAction( int line, Object value ) { try { TrabalhadoresData trabalhadorR = (TrabalhadoresData)JDO.load( TrabalhadoresData.class, ( (IDObject)value ).getID() ); trabalhadorR.set( TrabalhadoresData.INACTIVO, "n" ); trabalhadorR.save(); } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a restaurar o Trabalhador", true ); return false; } return true; } }; if( estabelecimento == null ) { return; } try { IDObject trabalhadores[] = ( ( FichaDataProvider )FichaDataProvider.getProvider() ).getAllTrabalhadoresDeletedForEstabelecimento( ( Integer ) estabelecimento.get( EstabelecimentosData.ID ) ); ListActionDialog dialog = new ListActionDialog( this, "Restaurar Trabalhador", new String []{ "Nome" }, new Vector( Arrays.asList( trabalhadores ) ), lAction ); dialog.setSize( 250, 200 ); dialog.setVisible( true ); } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a criar lista de Trabalhadores", true ); ex.printStackTrace(); } } protected void utilizadorWeb( boolean siprp ) { // new Exception().printStackTrace(); if( !siprp && empresa == null ) { JOptionPane.showMessageDialog( this, "Tem de escolher uma empresa", "Erro...", JOptionPane.ERROR_MESSAGE ); } try { UserWindow window = new UserWindow( this, siprp ? null : ( Integer ) empresa.get( EmpresasData.ID ) ); window.setVisible( true ); } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a estabelecer a liga\u00e7\u00e3o com a p\u00e1gina", true ); } } public void noticias() { try { NoticiasDialog dialog = new NoticiasDialog( this ); dialog.setVisible( true ); } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a carregar not\u00edcias", true ); } } public void imprimirFicha() { ListAction lAction = new ListAction(){ public String getListActionName() { return "Escolher"; } public boolean executeListAction( int line, Object value ) { if( trabalhador == null ) { return false; } try { printFicha(( (IDObject)value ).getID() ); } catch( Exception ex ) { ex.printStackTrace(); return false; } return true; } }; if( trabalhador == null ) { return; } Vector linhas; try { IDObject fichas[] = ( ( FichaDataProvider )FichaDataProvider.getProvider() ).getAllFichasForTrabalhador( ( Integer ) trabalhador.get( TrabalhadoresData.ID ) ); 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, lAction ); dialog.setSize( 250, 200 ); dialog.setVisible( true ); } private void printFicha( Integer exameID ) throws Exception { // MetaObject exame = fdpProvider.load( fdpProvider.EXAMES, new DBKey( exameID ) ); ExamesData exame = (ExamesData) JDO.load( ExamesData.class, exameID ); byte pdf[] = (byte []) exame.get( ExamesData.PDF ); if( exame == null ) { throw new Exception( "N\u00e3o existe exame" ); } TrabalhadoresData trabalhadorExame = exame.toTrabalhador_id(); String nomeFicheiro = StringPlainer.convertString( ( String ) trabalhadorExame.get( TrabalhadoresData.NOME ) ); nomeFicheiro.replaceAll( " ", "_" ); ExamePDF ePDF = new ExamePDF(); ePDF.print( pdf, nomeFicheiro ); } public void setTracker( SIPRPTracker tracker ) { this.tracker = tracker; estabelecimentoPanel.setTracker( tracker ); trabalhadorPanel.setTracker( tracker ); } public void imprimirEtiquetaTrabalhador() { if( trabalhador == null ) { return; } String nomeEmpresa = ( ( String ) empresa.get( EmpresasData.DESIGNACAO_SOCIAL ) ); String nomeTrabalhador = ( ( String ) trabalhador.get( TrabalhadoresData.NOME ) ); String numeroMecanografico = ( ( String ) trabalhador.get( TrabalhadoresData.NUMERO_MECANOGRAFICO ) ); String sexo = "m".equals( trabalhador.get( TrabalhadoresData.SEXO ) ) ? "M" : "F"; Date data = ( Date ) trabalhador.get( TrabalhadoresData.DATA_NASCIMENTO ); String dataNascimento = data != null ? DF.format( data ) : "--"; String nacionalidade = ( String ) trabalhador.get( TrabalhadoresData.NACIONALIDADE ); data = ( Date ) trabalhador.get( TrabalhadoresData.DATA_ADMISSAO ); String dataAdmissao = data != null ? DF.format( data ) : "--"; String categoria = ( String ) trabalhador.get( TrabalhadoresData.CATEGORIA ); String funcao = ( String ) trabalhador.get( TrabalhadoresData.FUNCAO_PROPOSTA ); try { Integer id = clientesProvider.getEtiquetaID(); EtiquetasData etiqueta = (EtiquetasData) JDO.load( EtiquetasData.class, id ); EtiquetaPrinter printer = new EtiquetaPrinter( etiqueta ); Vector linhas = new Vector(); linhas.add( new Object[]{ nomeEmpresa, new Double( 1.0 ) } ); linhas.add( new Object[]{ nomeTrabalhador, new Double( 1.0 ) } ); linhas.add( new Object[]{ "N\u00ba mec.:" + ( numeroMecanografico != null ? numeroMecanografico.toString() : "" ), new Double( 0.80 ), "Sexo:" + sexo, new Double( 0.20 ) } ); linhas.add( new Object[]{ "Data nasc:" + dataNascimento, new Double( 0.4 ), "Nacionalidade:" + nacionalidade, new Double( 0.85 ) } ); linhas.add( new Object[]{ "Data adm.:" + dataAdmissao, new Double( 0.4 ), "Categoria:" + categoria, new Double( 0.6 ) } ); linhas.add( new Object[]{ "Fun\u00e7\u00e3o:" + funcao, new Double( 1.0 ) } ); printer.print( linhas ); } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a carregar etiqueta.", true ); } } public void imprimirEtiquetaEstabelecimento() { if( estabelecimento == null ) { return; } String nomeEmpresa = ( ( String ) empresa.get( EmpresasData.DESIGNACAO_SOCIAL ) ); String nomeEstabelecimento = ( ( String ) estabelecimento.get( EstabelecimentosData.NOME ) ); ContactosData contacto = estabelecimento.toContacto_id(); String nomeContacto = ""; if( contacto != null ) { nomeContacto = ( String )contacto.get( ContactosData.NOME ); } String morada = ( ( String ) estabelecimento.get( EstabelecimentosData.MORADA ) ); String codigoPostal = ( ( String ) estabelecimento.get( EstabelecimentosData.CODIGO_POSTAL ) ); String local = ( ( String ) estabelecimento.get( EstabelecimentosData.LOCALIDADE ) ); try { Integer id = clientesProvider.getEtiquetaID(); EtiquetasData etiqueta = ( EtiquetasData ) JDO.load( EtiquetasData.class, id ); EtiquetaPrinter printer = new EtiquetaPrinter( etiqueta ); Vector linhas = new Vector(); linhas.add( new Object[]{ nomeEmpresa, new Double( 1.0 ) } ); linhas.add( new Object[]{ nomeContacto, new Double( 1.0 ) } ); linhas.add( new Object[]{ morada, new Double( 1.0 ) } ); linhas.add( new Object[]{ codigoPostal + " " + local, new Double( 1.0 ) } ); printer.print( linhas ); } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a carregar etiqueta.", true ); } } }