/* * ECDsPanel.java * * Created on 13 de Maio de 2007, 17:07 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package siprp.medicina.processo.detalhes; import com.evolute.utils.Singleton; import com.evolute.utils.data.IDObject; import com.evolute.utils.jdo.JDOProvider; import com.evolute.utils.ui.DialogException; import com.evolute.utils.ui.calendar.JCalendarPanel; import info.clearthought.layout.TableLayout; import info.clearthought.layout.TableLayoutConstraints; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Date; import java.util.HashMap; import java.util.Vector; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import siprp.medicina.MedicinaConstants; import siprp.medicina.processo.ProcessoConstants; import siprp.medicina.processo.ProcessoDataProvider; import siprp.medicina.processo.ProcessoEvent; import siprp.medicina.processo.ProcessoListener; import siprp.medicina.processo.data.TrabalhadoresConsultasData; import siprp.medicina.processo.data.TrabalhadoresEcdsData; import siprp.medicina.processo.data.TrabalhadoresEcdsDatasData; /** * * @author Frederico */ public class ECDsPanel extends JPanel implements ActionListener, MedicinaConstants, ChangeListener { protected JFrame owner; protected JCalendarPanel dataEcdsPanel; protected JButton estadoButton; protected JButton emailButton; protected JButton observacoesButton; protected ObservacoesDialog observacoesDialog; protected final Vector PROCESSO_LISTENERS = new Vector(); protected JDOProvider JDO; protected ProcessoDataProvider provider; protected Integer trabalhadorID; protected Integer processoID; protected TrabalhadoresEcdsData ecds; protected TrabalhadoresEcdsDatasData ecdsData; /** * Creates a new instance of ECDsPanel */ public ECDsPanel( JFrame owner ) throws Exception { this.owner = owner; JDO = ( JDOProvider ) Singleton.getInstance( Singleton.DEFAULT_JDO_PROVIDER ); provider = ProcessoDataProvider.getProvider(); setupComponents(); } private void setupComponents() { JLabel dataLabel = new JLabel( "Data" ); dataConsultaPanel = new JCalendarPanel( null ); dataConsultaPanel.addChangeListener( this ); estadoButton = new JButton( " " ); estadoButton.addActionListener( this ); emailButton = new JButton( "Email" ); emailButton.addActionListener( this ); observacoesButton = new JButton( "Coment\u00e1rio" ); observacoesButton.addActionListener( this ); double cols[] = new double[]{ TableLayout.MINIMUM, TableLayout.FILL }; double rows[] = new double[]{ TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.MINIMUM }; TableLayout tableLayout = new TableLayout( cols, rows ); tableLayout.setVGap( 5 ); setLayout( tableLayout ); add( dataLabel, new TableLayoutConstraints( 0, 0 ) ); add( dataEcdsPanel, new TableLayoutConstraints( 1, 0 ) ); add( estadoButton, new TableLayoutConstraints( 0, 1, 1, 1 ) ); add( emailButton, new TableLayoutConstraints( 0, 2, 1, 2 ) ); add( observacoesButton, new TableLayoutConstraints( 0, 3, 1, 3 ) ); } public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if( source.equals( observacoesButton ) ) { editarObservacoes(); } else if( source.equals( emailButton ) ) { enviarEmail(); } else if( source.equals( estadoButton ) ) { mudarEstado(); } } public void stateChanged(ChangeEvent e) { Object source = e.getSource(); if( source.equals( dataConsultaPanel ) ) { Date data = dataConsultaPanel.getDate(); if( data == null ) { return; } try { boolean nova = false; if( ecds == null ) { nova = true; Integer ecdsID = provider.getEcdsEmAbertoForProcesso( processoID ); if( ecdsID == null ) { ecds = new TrabalhadoresEcdsData(); ecds.set( TrabalhadoresEcdsData.TRABALHADOR_ID, trabalhadorID ); ecds.set( TrabalhadoresEcdsData.PROCESSO_ID, processoID ); ecds.set( TrabalhadoresEcdsData.ESTADO, new Integer( MedicinaConstants.ESTADO_POR_REALIZAR ) ); } else { ecds = provider.getEcdsByID( ecdsID ); ecds.set( TrabalhadoresEcdsData.ESTADO, new Integer( MedicinaConstants.ESTADO_POR_REALIZAR ) ); } } ecds.set( TrabalhadoresConsultasData.DATA, data ); provider.saveEcds( ecds ); if( ecdsData == null ) { Integer ecdsID = ( Integer )ecds.get( TrabalhadoresEcdsData.ID ); Integer ecdsDataID = provider.getLastEcdsDataIDForEcds( ecdsID ); if( ecdsDataID == null || nova ) { ecdsData = new TrabalhadoresEcdsDatasData(); ecdsData.set( TrabalhadoresEcdsDatasData.TRABALHADORES_ECDS_ID, ecdsID ); ecdsData.set( TrabalhadoresEcdssDatasData.ESTADO, new Integer( MedicinaConstants.ESTADO_POR_REALIZAR ) ); } else { ecdsData = provider.getEcdsDataByID( ecdsDataID ); } } ecdsData.set( TrabalhadoresEcdsDatasData.DATA, data ); provider.saveEcdsData( ecdsData ); notifyListeners( ProcessoEvent.ACCAO_MUDAR_DATA_MARCACAO ); } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a gravar dados dos ECDs", true ); return; } } } protected void editarObservacoes() { if( observacoesDialog == null ) { observacoesDialog = new ObservacoesDialog( owner ); } String texto = ""; try { Integer dataID = provider.getLastEcdsDataIDForEcds( ( Integer ) ecds.get( TrabalhadoresEcdssData.ID ) ); IDObject observacoes = provider.getObservacoesEcdsData( dataID ); if( observacoes != null ) { texto = observacoes.toString(); } texto = observacoesDialog.editarObservacao( texto ); if( texto != null ) { provider.saveObservacoesEcdsData( dataID, texto ); notifyListeners( ProcessoEvent.ACCAO_ESCREVER_OBSERVACOES ); } } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a carregar dados", true ); return; } } protected void enviarEmail() { if( ecds == null ) { return; } try { Integer ecdsDataID; if( ecdsData == null ) { Integer ecdsID = ( Integer )ecds.get( TrabalhadoresEcdsData.ID ); ecdsDataID = provider.getLastEcdsDataIDForEcds( ecdsID ); ecdsData = provider.getEcdsDataByID( ecdsDataID ); } ecdsDataID = ( Integer ) ecdsData.get( TrabalhadoresEcdsDatasData.ID ); provider.saveEmailEcdsData( ecdsDataID, "teste@siprp.pt", "Email de teste.", "" ); notifyListeners( ProcessoEvent.ACCAO_ESCREVER_MAIL ); } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a gravar dados", true ); return; } } protected void mudarEstado() { if( ecds == null ) { return; } Integer estado = ( Integer ) ecds.get( TrabalhadoresEcdsData.ESTADO ); EstadoChooser chooser = new EstadoChooser( owner, ProcessoConstants.TIPO_ECDS, estado != null ? estado.intValue() : MedicinaConstants.ESTADO_POR_REALIZAR ); chooser.setVisible( true ); int novoEstado = chooser.getEstado(); if( estado == null || estado.intValue() != novoEstado ) { try { ecds.set( TrabalhadoresEcdsData.ESTADO, new Integer( novoEstado ) ); provider.saveEcds( ecds ); if( ecdsData == null ) { Integer ecdsID = ( Integer )ecds.get( TrabalhadoresEcdsData.ID ); Integer ecdsDataID = provider.getLastEcdsDataIDForEcds( ecdsID ); ecdsData = provider.getEcdsDataByID( ecdsDataID ); } ecdsData.set( TrabalhadoresEcdsDatasData.ESTADO, new Integer( novoEstado ) ); provider.saveEcdsData( ecdsData ); notifyListeners( ProcessoEvent.ACCAO_MUDAR_ESTADO_MARCACAO ); } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a gravar dados", true ); return; } } } public void addProcessoListener( ProcessoListener listener ) { PROCESSO_LISTENERS.add( listener ); } public void removeProcessoListener( ProcessoListener listener ) { PROCESSO_LISTENERS.remove( listener ); } public void clear() { dataConsultaPanel.setDate( null ); estadoButton.setText( " " ); ecds = null; ecdsData = null; } public void fill( Object value ) { clear(); if( value != null ) { try { ecds = ( TrabalhadoresEcdsData ) JDO.load( TrabalhadoresEcdsData.class, ( Integer ) value ); Date data = ( Date ) ecds.get( TrabalhadoresEcdsData.DATA ); Integer estado = ( Integer ) ecds.get( TrabalhadoresEcdsData.ESTADO ); dataConsultaPanel.setDate( data ); if( estado != null ) { estadoButton.setText( ESTADOS_EXAME_STR[ estado.intValue() ] ); } else { estadoButton.setText( " " ); } } catch( Exception ex ) { DialogException.showExceptionMessage( ex, "Erro a carregar dados", true ); } } enableButtons(); } protected void enableButtons() { if( ecds != null ) { Integer estado = ( Integer ) ecds.get( TrabalhadoresEcdsData.ESTADO ); dataEcdsPanel.setEnabled( estado == null || estado.intValue() == ESTADO_POR_REALIZAR ); estadoButton.setEnabled( ecds.get( TrabalhadoresEcdsData.DATA ) != null ); emailButton.setEnabled( ecds.get( TrabalhadoresEcdsData.DATA ) != null ); observacoesButton.setEnabled( ecds.get( TrabalhadoresEcdsData.DATA ) != null ); } else { dataConsultaPanel.setEnabled( true ); estadoButton.setEnabled( false ); emailButton.setEnabled( false ); observacoesButton.setEnabled( false ); } } public void setTrabalhadorID( Integer trabalhadorID ) { this.trabalhadorID = trabalhadorID; } public void setProcessoID( Integer processoID ) { this.processoID = processoID; } protected void notifyListeners( int accao ) { HashMap ids = new HashMap(); ids.put( ProcessoEvent.TIPO_PROCESSO, processoID ); if( ecds != null ) { ids.put( ProcessoEvent.TIPO_CONSULTA, ( Integer ) ecds.get( TrabalhadoresEcdsData.ID ) ); } if( ecdsData != null ) { ids.put( ProcessoEvent.TIPO_DATA, ( Integer ) ecds.get( TrabalhadoresEcdsData.ID ) ); } ProcessoEvent event = new ProcessoEvent( this, accao, ids ); for( int n = 0; n < PROCESSO_LISTENERS.size(); n++ ) { PROCESSO_LISTENERS.get( n ).processoStateChanged( event ); } } }