/* * ClientesWindow.java * * Created on 12 de Maio de 2004, 18:11 */ package siprp.clientes; import java.awt.*; import java.io.*; import java.text.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; import com.evolute.utils.*; import com.evolute.utils.data.*; import com.evolute.utils.dataui.*; import com.evolute.utils.date.*; import com.evolute.utils.fop.*; import com.evolute.utils.jdo.*; import com.evolute.utils.strings.*; import com.evolute.utils.ui.*; import com.evolute.utils.ui.dialog.*; import com.evolute.utils.ui.panel.*; import com.evolute.utils.ui.search.*; import com.evolute.utils.ui.text.*; import com.evolute.utils.ui.window.*; import com.evolute.utils.xml.*; import siprp.*; import siprp.data.*; import siprp.ficha.*; import siprp.importer.*; /** * * @author fpalma */ public class ClientesWindow extends EditorWindow implements ListSelectionListener { 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, TrabalhadorData.NOME ); TR_IMPORT_NAMES_TO_TRAB.put( Importer.SEXO, TrabalhadorData.SEXO ); TR_IMPORT_NAMES_TO_TRAB.put( Importer.NACIONALIDADE, TrabalhadorData.NACIONALIDADE ); TR_IMPORT_NAMES_TO_TRAB.put( Importer.DATA_NASCIMENTO, TrabalhadorData.DATA_NASCIMENTO ); TR_IMPORT_NAMES_TO_TRAB.put( Importer.NUMERO_MECANOGRAFICO, TrabalhadorData.NUMERO_MECANOGRAFICO ); TR_IMPORT_NAMES_TO_TRAB.put( Importer.DATA_ADMISSAO, TrabalhadorData.DATA_ADMISSAO ); TR_IMPORT_NAMES_TO_TRAB.put( Importer.CATEGORIA, TrabalhadorData.CATEGORIA ); TR_IMPORT_NAMES_TO_TRAB.put( Importer.LOCAL_TRABALHO, TrabalhadorData.LOCAL_TRABALHO ); TR_IMPORT_NAMES_TO_TRAB.put( Importer.FUNCAO, TrabalhadorData.FUNCAO_PROPOSTA ); TR_IMPORT_NAMES_TO_TRAB.put( Importer.DATA_ADMISSAO_FUNCAO, TrabalhadorData.DATA_ADMISSAO_FUNCAO ); TR_IMPORT_NAMES_TO_DEMISSOES.put( Importer.NOME, TrabalhadorData.NOME ); TR_IMPORT_NAMES_TO_DEMISSOES.put( Importer.DATA_DEMISSAO, TrabalhadorData.DATA_DEMISSAO ); // JDOObject.registerOperationHandlerForClass( DisableDeleteHandler.INSTANCE, EmpresaData.class ); // JDOObject.registerOperationHandlerForClass( DisableDeleteHandler.INSTANCE, EstabelecimentoData.class ); // JDOObject.registerOperationHandlerForClass( DisableDeleteHandler.INSTANCE, TrabalhadorData.class ); // // JDOObject.registerOperationHandlerForClass( MarcacaoSaveAndDeleteHandler.INSTANCE, MarcacaoEmpresaData.class ); // JDOObject.registerOperationHandlerForClass( MarcacaoSaveAndDeleteHandler.INSTANCE, MarcacaoEstabelecimentoData.class ); // JDOObject.registerOperationHandlerForClass( MarcacaoSaveAndDeleteHandler.INSTANCE, MarcacaoTrabalhadorData.class ); } public static final int INDEX_EMPRESA = 0; public static final int INDEX_ESTABELECIMENTO = 1; public static final int INDEX_TRABALHADOR = 2; 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 JDOProvider 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 EmpresaData empresa; private Integer elementosProtocolo[][]; private EstabelecimentoData estabelecimento; private TrabalhadorData trabalhador; private AvisosPanel avisos; protected boolean webAware; // 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 = ( JDOProvider ) Singleton.getInstance( Singleton.DEFAULT_JDO_PROVIDER ); clientesProvider = ( ClientesDataProvider ) ClientesDataProvider.getProvider(); setEditorManagerHierarchy( hierarchy ); setupComponents(); this.setExtendedState(this.getExtendedState() | this.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() ); 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 ); 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( EmpresaData.class ), null, false, null, true ); empresaID = ( Integer )search.getSelected(); if( empresaID != null ) { try { empresa = (EmpresaData) JDO.load( EmpresaData.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 ); 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 = (EmpresaData) ( ( Object [] )object )[ 0 ]; if( empresa.get( EmpresaData.ID ) == null ) { isNew = true; } elementosProtocolo = (Integer [][]) ( ( Object [] )object )[ 1 ]; // System.out.println( empresa.debug() ); ContactoData c1 = ( ContactoData )empresa.get( EmpresaData.CONTACTO_1 ); ContactoData c2 = ( ContactoData )empresa.get( EmpresaData.CONTACTO_2 ); // empresa.set( EmpresaData.CONTACTO_1, null ); // empresa.set( EmpresaData.CONTACTO_2, null ); // empresa.save(); // empresa.set( EmpresaData.CONTACTO_1, c1 ); // empresa.set( EmpresaData.CONTACTO_2, c2 ); c1.save(); c2.save(); // empresa.set( EmpresaData.INACTIVO, "n" ); empresa.set( EmpresaData.DESIGNACAO_SOCIAL_PLAIN, com.evolute.utils.strings.StringPlainer.convertString( ( String ) empresa.get( EmpresaData.DESIGNACAO_SOCIAL ) ) ); empresa.save(); clientesProvider.setElementosProtocoloForEmpresa( ( Integer ) empresa.get( EmpresaData.ID ), elementosProtocolo ); if( isNew ) { estabelecimentoPanel.setEmpresa( empresa ); } ( ( EmpresaPanel )panels[ INDEX_EMPRESA ] ).reloadMarcacoes(); break; case INDEX_ESTABELECIMENTO: estabelecimento = (EstabelecimentoData) object; if( estabelecimento.get( EstabelecimentoData.ID ) == null ) { isNew = true; } ContactoData c = ( ContactoData )estabelecimento.get( EstabelecimentoData.CONTACTO ); c.save(); if( estabelecimento.get( EstabelecimentoData.EMPRESA ) == null ) { JOptionPane.showMessageDialog( this, "Erro a guardar estabelecimento (NULL).", "Erro...", JOptionPane.ERROR_MESSAGE ); return false; } estabelecimento.set( EstabelecimentoData.NOME_PLAIN, com.evolute.utils.strings.StringPlainer.convertString( ( String ) estabelecimento.get( EstabelecimentoData.NOME ) ) ); estabelecimento.save(); estabelecimentoPanel.postSave(); if( isNew ) { trabalhadorPanel.setEstabelecimento( estabelecimento ); } break; case INDEX_TRABALHADOR: trabalhador = (TrabalhadorData) object; trabalhador.set( TrabalhadorData.NOME_PLAIN, com.evolute.utils.strings.StringPlainer.convertString( ( String ) trabalhador.get( TrabalhadorData.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 = ( EstabelecimentoData ) JDO.load( EstabelecimentoData.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 = (TrabalhadorData) JDO.load( TrabalhadorData.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 { TrabalhadorData 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( EmpresaData.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( EmpresaData.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( TrabalhadorData.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() ); EstabelecimentoData esta; if( eid == null ) { // save this estab esta = new EstabelecimentoData(); esta.set( EstabelecimentoData.EMPRESA, empresa ); esta.set( EstabelecimentoData.NOME, value.toString().trim() ); esta.set( EstabelecimentoData.NOME_PLAIN, com.evolute.utils.strings.StringPlainer.convertString( value.toString().trim(), false, false ) ); esta.save(); estabelecimentos.put( value.toString().trim().toLowerCase(), esta.get( EstabelecimentoData.ID ) ); } else { esta = ( EstabelecimentoData )estabCache.get( value ); if( esta == null ) { esta = (EstabelecimentoData) JDO.load( EstabelecimentoData.class, eid ); estabCache.put( value, esta ); } } //System.out.println( "Put " + i + " esta " + esta.debug() ); hash[ i ].put( TrabalhadorData.ESTABELECIMENTO, 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 TrabalhadorData tr = new TrabalhadorData(); tr.setHashData( hash[ i ] ); // o estabelecimento nao faz parte da lista por causa do UI tr.set( TrabalhadorData.ESTABELECIMENTO, hash[ i ].get( TrabalhadorData.ESTABELECIMENTO ) ); tr.set( TrabalhadorData.PERFIL, new Integer( 1 ) ); // System.out.println( "T: " + i + "\n" + tr.debug() ); tr.save(); } for( int i = 0; i < activate.size(); i++ ) { TrabalhadorData trabalhadorR = (TrabalhadorData)JDO.load( TrabalhadorData.class, ( Integer ) activate.elementAt( i ) ); trabalhadorR.set( TrabalhadorData.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( EmpresaData.ID ) ); Hashtable trabalhadores = new Hashtable(); for( int i = 0; i < trab.length; ++i ) { trabalhadores.put( 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 ) ) { if( value != null && !trabalhadores.containsKey( 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( value.toString().trim().toLowerCase() ); hash[ i ].put( TrabalhadorData.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" ); TrabalhadorData tr = (TrabalhadorData)JDO.load( TrabalhadorData.class, ( Integer ) hash[ i ].get( TrabalhadorData.ID ) ); System.out.println( "TRABALHADORID: " + tr.get( TrabalhadorData.ID ) ); tr.set( TrabalhadorData.DATA_DEMISSAO, hash[ i ].get( TrabalhadorData.DATA_DEMISSAO ) ); System.out.println( "TRABALHADORDEMISSAO: " + tr.get( TrabalhadorData.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 { EstabelecimentoData eatabelecimentoNovo = (EstabelecimentoData)JDO.load( EstabelecimentoData.class, ( (IDObject)value ).getID() ); trabalhador.set( TrabalhadorData.ESTABELECIMENTO, 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( EmpresaData.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( JDOObject object ) { empresa = null; estabelecimento = null; trabalhador = null; reload(0); if( object instanceof TrabalhadorData ) { tabbedPane.setSelectedIndex( 1 ); trabalhador = (TrabalhadorData) object; estabelecimento = (EstabelecimentoData)trabalhador.get( TrabalhadorData.ESTABELECIMENTO ); empresa = (EmpresaData)estabelecimento.get( EstabelecimentoData.EMPRESA ); } else if( object instanceof EstabelecimentoData ) { tabbedPane.setSelectedIndex( 0 ); estabelecimento = (EstabelecimentoData)object; empresa = (EmpresaData)estabelecimento.get( EstabelecimentoData.EMPRESA ); } else if( object instanceof EmpresaData ) { empresa = (EmpresaData)object; } if( empresa != null ) { try { elementosProtocolo = clientesProvider.getElementosProtocoloForEmpresa( ( Integer ) empresa.get( EmpresaData.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 { EmpresaData empresaR = (EmpresaData)JDO.load( EmpresaData.class, ( (IDObject)value ).getID() ); empresaR.set( EmpresaData.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 { EstabelecimentoData estabelecimentoR = (EstabelecimentoData)JDO.load( EstabelecimentoData.class, ( (IDObject)value ).getID() ); estabelecimentoR.set( EstabelecimentoData.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( EmpresaData.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 { TrabalhadorData trabalhadorR = (TrabalhadorData)JDO.load( TrabalhadorData.class, ( (IDObject)value ).getID() ); trabalhadorR.set( TrabalhadorData.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( EstabelecimentoData.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( EmpresaData.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( TrabalhadorData.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 ) ); 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 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( EmpresaData.DESIGNACAO_SOCIAL ) ); String nomeTrabalhador = ( ( String ) trabalhador.get( TrabalhadorData.NOME ) ); String numeroMecanografico = ( ( String ) trabalhador.get( TrabalhadorData.NUMERO_MECANOGRAFICO ) ); String sexo = "m".equals( trabalhador.get( TrabalhadorData.SEXO ) ) ? "M" : "F"; Date data = ( Date ) trabalhador.get( TrabalhadorData.DATA_NASCIMENTO ); String dataNascimento = data != null ? DF.format( data ) : "--"; String nacionalidade = ( String ) trabalhador.get( TrabalhadorData.NACIONALIDADE ); data = ( Date ) trabalhador.get( TrabalhadorData.DATA_ADMISSAO ); String dataAdmissao = data != null ? DF.format( data ) : "--"; String categoria = ( String ) trabalhador.get( TrabalhadorData.CATEGORIA ); String funcao = ( String ) trabalhador.get( TrabalhadorData.FUNCAO_PROPOSTA ); try { Integer id = clientesProvider.getEtiquetaID(); EtiquetaData etiqueta = ( EtiquetaData ) JDO.load( EtiquetaData.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( EmpresaData.DESIGNACAO_SOCIAL ) ); String nomeEstabelecimento = ( ( String ) estabelecimento.get( EstabelecimentoData.NOME ) ); ContactoData contacto = ( ContactoData )estabelecimento.get( EstabelecimentoData.CONTACTO ); String nomeContacto = ""; if( contacto != null ) { nomeContacto = ( String )contacto.get( ContactoData.NOME ); } String morada = ( ( String ) estabelecimento.get( EstabelecimentoData.MORADA ) ); String codigoPostal = ( ( String ) estabelecimento.get( EstabelecimentoData.CODIGO_POSTAL ) ); String local = ( ( String ) estabelecimento.get( EstabelecimentoData.LOCALIDADE ) ); try { Integer id = clientesProvider.getEtiquetaID(); EtiquetaData etiqueta = ( EtiquetaData ) JDO.load( EtiquetaData.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 ); } } }