diff --git a/trunk/siprp/Main.java b/trunk/siprp/Main.java index 694ec960..736bb8d4 100644 --- a/trunk/siprp/Main.java +++ b/trunk/siprp/Main.java @@ -2,7 +2,6 @@ package siprp; import siprp.companydataloaders.*; import siprp.data.*; -import siprp.ficha.*; import siprp.clientes.*; import com.evolute.utils.*; @@ -15,21 +14,15 @@ import com.evolute.utils.sql.*; import com.evolute.utils.ui.*; import com.evolute.utils.ui.window.*; -import javax.jdo.JDOHelper; -import javax.jdo.PersistenceManager; -import javax.jdo.PersistenceManagerFactory; -import javax.jdo.Transaction; - import java.awt.*; -import java.io.*; -import java.text.*; import java.util.*; import javax.swing.*; import javax.swing.tree.*; +import siprp.update.UpdateWindow; public class Main implements com.evolute.utils.ui.window.Connector { - public final static String SHST_VERSION = "6.1"; + public final static String SHST_VERSION = "7.0"; private final static ClassLoader classLoader = new EVUtilsImageLib().getClass().getClassLoader(); @@ -116,6 +109,7 @@ public class Main implements com.evolute.utils.ui.window.Connector loginWindow.setExtendedState(loginWindow.getExtendedState() | loginWindow.MAXIMIZED_BOTH); loginWindow.setVisible( true ); loginWindow.expandAll(); + new UpdateWindow().update(); } public boolean connect( String user, String passwd ) diff --git a/trunk/siprp/update/Update.java b/trunk/siprp/update/Update.java new file mode 100644 index 00000000..a3298852 --- /dev/null +++ b/trunk/siprp/update/Update.java @@ -0,0 +1,22 @@ +/* + * Update.java + * + * Created on 26 de Setembro de 2006, 12:13 + * + * To change this template, choose Tools | Template Manager + * and open the template in the editor. + */ + +package siprp.update; + +/** + * + * @author fpalma + */ +public interface Update +{ + public double getStartVersion(); + public double getEndVersion(); + public String[] listChanges(); + public void doUpdate() throws Exception; +} diff --git a/trunk/siprp/update/UpdateList.java b/trunk/siprp/update/UpdateList.java new file mode 100644 index 00000000..12173ef1 --- /dev/null +++ b/trunk/siprp/update/UpdateList.java @@ -0,0 +1,104 @@ +/* + * Updater.java + * + * Created on 26 de Setembro de 2006, 11:49 + * + * To change this template, choose Tools | Template Manager + * and open the template in the editor. + */ + +package siprp.update; + +import java.util.*; + +import com.evolute.utils.*; +import com.evolute.utils.arrays.*; +import com.evolute.utils.db.*; +import com.evolute.utils.sql.*; + + +/** + * + * @author fpalma + */ +public class UpdateList +{ + protected static final Update UPDATE_LIST[] = + new Update[]{ new siprp.update.updates.V6_1_To_V7_0() }; + + protected static Executer EXECUTER; + protected static double version = -1; + + /** Creates a new instance of Updater */ + private UpdateList() + { + } + + public static double getCurrentVersion() + throws Exception + { + if( version < 0 ) + { + version = getVersion(); + } + return version; + } + + public static Update []getUpdates() + throws Exception + { + if( version < 0 ) + { + version = getVersion(); + } + Vector validUpdates = new Vector(); + for( int n = 0; n < UPDATE_LIST.length; n++ ) + { + if( version <= UPDATE_LIST[ n ].getStartVersion() ) + { + validUpdates.add( UPDATE_LIST[ n ] ); + } + } + return ( Update [] ) validUpdates.toArray( new Update[ validUpdates.size() ] ); + } + + protected static double getVersion() + throws Exception + { + DBManager dbm = ( DBManager ) Singleton.getInstance( Singleton.DEFAULT_DBMANAGER ); + EXECUTER = dbm.getSharedExecuter(); + try + { + return readVersion(); + } + catch( Exception ex ) + { + createVersionTable(); + } + return readVersion(); + } + + protected static double readVersion() + throws Exception + { + Select select = + new Select( new String[]{ "version" }, + new String[]{ "MAX(current_version)" }, + null ); + Virtual2DArray array = EXECUTER.executeQuery( select ); + return ( ( Number ) array.get( 0, 0 ) ).doubleValue(); + } + + protected static void createVersionTable() + throws Exception + { + com.evolute.utils.sql.Update update = + new com.evolute.utils.sql.Update( "CREATE TABLE version( current_version float );" ); + EXECUTER.executeQuery( update ); + Insert insert = + new Insert( "version", + new Assignment[]{ + new Assignment( new Field( "current_version" ), new Double( 0.1 ) ) } ); + EXECUTER.executeQuery( insert ); + } +} diff --git a/trunk/siprp/update/UpdateWindow.java b/trunk/siprp/update/UpdateWindow.java new file mode 100644 index 00000000..c432a9e7 --- /dev/null +++ b/trunk/siprp/update/UpdateWindow.java @@ -0,0 +1,188 @@ +/* + * UpdateWindow.java + * + * Created on 26 de Setembro de 2006, 11:54 + * + * To change this template, choose Tools | Template Manager + * and open the template in the editor. + */ + +package siprp.update; + +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import java.util.*; + +import com.evolute.utils.*; +import com.evolute.utils.date.*; +import com.evolute.utils.db.*; +import com.evolute.utils.sql.*; +import com.evolute.utils.ui.*; + + +/** + * + * @author fpalma + */ +public class UpdateWindow extends JFrame + implements ActionListener +{ + protected JTextArea logText; + protected JButton closeButton; + + protected Update updates[]; + protected String filename; + + public static void main( String args[] ) + { + UpdateWindow updateWindow = new UpdateWindow(); + updateWindow.update(); + } + + /** Creates a new instance of UpdateWindow */ + public UpdateWindow() + { + setupComponents(); + } + + private void setupComponents() + { + setTitle( "Actualiza\u00e7\u00e3o do Software" ); + setSize( 800, 600 ); + logText = new JTextArea(); + logText.setLineWrap( true ); + logText.setWrapStyleWord( true ); + logText.setEditable( true ); + JScrollPane scp = + new JScrollPane( logText, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, + JScrollPane.HORIZONTAL_SCROLLBAR_NEVER ); + closeButton = new JButton( "Fechar" ); + + setLayout( new BorderLayout() ); + add( scp, BorderLayout.CENTER ); + add( closeButton, BorderLayout.SOUTH ); + + closeButton.setEnabled( false ); + closeButton.addActionListener( this ); + + setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE ); + } + + public void actionPerformed( ActionEvent e ) + { + Object source = e.getSource(); + if( source.equals( closeButton ) ) + { + close(); + } + } + + public void close() + { + SwingUtilities.invokeLater( new Runnable(){ + public void run() + { + setVisible( false ); + dispose(); + } + } ); + } + + public void update() + { + log( "A verificar a exist\u00eancia de actualiza\u00e7\u00f5es...\n" ); + try + { + double version = UpdateList.getCurrentVersion(); + log( "Vers\u00e3o actual: " + ( version < 6.1 ? 6.1 : version ) + "\n" ); + updates = UpdateList.getUpdates(); + } + catch( Exception ex ) + { + setVisible( true ); + log( "Erro a verificar actualiza\u00e7\u00f5es!!!" ); + ex.printStackTrace(); + updates = new Update[ 0 ]; + } + if( updates.length > 0 ) + { + setVisible( true ); + log( "Existe " + ( updates.length > 1 ? "m " : " " ) + updates.length + + " actualiza\u00e7" + ( updates.length > 1 ? "\u00f5es... " : "\u00e3o\n" ) ); + boolean ok = doUpdates(); + if( ok ) + { + log( "**** Actualiza\u00e7\u00f5es terminadas ****\n" ); + } + else + { + log( "**** Actualiza\u00e7\u00f5es abortadas ****\n" ); + log( "\nPode favor contacte a EVOLUTE\n" ); + } + + } + closeButton.setEnabled( true ); + } + + protected void log( final String newLog ) + { + SwingUtilities.invokeLater( new Runnable(){ + public void run() + { + logText.append( newLog ); + } + } ); + } + + protected boolean doUpdates() + { + double last = 0.1; + log( "**** A realizar actualiza\u00e7\u00f5es... **** \n" ); + for( int n = 0; n < updates.length; n++ ) + { + last = updates[ n ].getEndVersion(); + String msg = " " + updates[ n ] + "... \n"; + String changes[] = updates[ n ].listChanges(); + for( int c = 0; c < changes.length; c++ ) + { + msg += " " + changes[ c ] + "\n"; + } + log( msg ); + try + { + updates[ n ].doUpdate(); + } + catch( Exception ex ) + { + log( "ERRO ! ! !\nA abortar actualiza\u00e7\u00f5es\n" ); + ex.printStackTrace(); + return false; + } + } + try + { + DBManager dbm = ( DBManager ) Singleton.getInstance( Singleton.DEFAULT_DBMANAGER ); + Executer executer = dbm.getSharedExecuter(); + com.evolute.utils.sql.Update update = + new com.evolute.utils.sql.Update( "version", + new Assignment[]{ + new Assignment( new Field( "current_version" ), new Double( last ) ) }, + null ); + executer.executeQuery( update ); + log( "Vers\u00e3o actualizada para: " + last + "\n" ); + return true; + } + catch( Exception ex ) + { + log( "ERRO ! ! !\nA abortar actualiza\u00e7\u00f5es\n" ); + ex.printStackTrace(); + return false; + } + } + + public int getUpdateCount() + { + return updates.length; + } +} diff --git a/trunk/siprp/update/updates/V6_1_To_V7_0.java b/trunk/siprp/update/updates/V6_1_To_V7_0.java new file mode 100644 index 00000000..b6a7d176 --- /dev/null +++ b/trunk/siprp/update/updates/V6_1_To_V7_0.java @@ -0,0 +1,68 @@ +/* + * V6_1_To_V7_0.java + * + * Created on 26 de Setembro de 2006, 11:48 + * + * To change this template, choose Tools | Template Manager + * and open the template in the editor. + */ + +package siprp.update.updates; + +import com.evolute.utils.*; +import com.evolute.utils.arrays.*; +import com.evolute.utils.db.*; +import com.evolute.utils.sql.*; + + +/** + * + * @author fpalma + */ +public class V6_1_To_V7_0 + implements siprp.update.Update +{ + + /** + * Creates a new instance of V6_1_To_V7_0 + */ + public V6_1_To_V7_0() + { + } + + public String []listChanges() + { + return new String[]{ "Acrescentar campo de contribuinte nos prestadores", + "Acrescentar campo de prestador nas marca\u00e7\u00f5es", + "Acrescentar campo de prestador no hist\u00f3rico das marca\u00e7\u00f5es"}; + } + + public double getStartVersion() + { + return 6.1; + } + + public double getEndVersion() + { + return 7.0; + } + + public void doUpdate() + throws Exception + { + DBManager dbm = ( DBManager ) Singleton.getInstance( Singleton.DEFAULT_DBMANAGER ); + Executer executer = dbm.getSharedExecuter(); + com.evolute.utils.sql.Update update = + new com.evolute.utils.sql.Update( "ALTER TABLE prestadores ADD contribuinte VARCHAR(64);" ); + executer.executeQuery( update ); + update = new com.evolute.utils.sql.Update( "ALTER TABLE marcacoes_trabalhador ADD prestador_id int4 REFERENCES prestadores( id );" ); + executer.executeQuery( update ); + update = new com.evolute.utils.sql.Update( "ALTER TABLE marcacoes_trabalhador_estados ADD prestador_id int4 REFERENCES prestadores( id );" ); + executer.executeQuery( update ); + } + + public String toString() + { + return "v6.1 para v7.0"; + } +}