/*
 * 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(), new siprp.update.updates.V7_0_To_V7_2(),
							new siprp.update.updates.V7_2_To_V7_4(), new siprp.update.updates.V7_4_To_V7_5(),
							new siprp.update.updates.V7_5_To_V7_6(), new siprp.update.updates.V7_6_To_V7_7(),
							new siprp.update.updates.V7_7_To_V7_8(), new siprp.update.updates.V7_8_To_V7_9(),
							new siprp.update.updates.V7_9_To_V8_0(), new siprp.update.updates.V8_0_To_V8_1(),
							new siprp.update.updates.V8_1_To_V8_2(), new siprp.update.updates.V8_2_To_V8_3(),
							new siprp.update.updates.V8_3_To_V8_4(), new siprp.update.updates.V8_4_To_V8_5(),
							new siprp.update.updates.V8_5_To_V8_6(), new siprp.update.updates.V8_6_To_V8_7(),
							new siprp.update.updates.V8_7_To_V8_8(), new siprp.update.updates.V8_8_To_V8_9(),
							new siprp.update.updates.V8_9_To_V9_0(), new siprp.update.updates.V9_0To_V9_1() };
	
	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<Update> validUpdates = new Vector<Update>();
		for( int n = 0; n < UPDATE_LIST.length; n++ )
		{
			if( version <= UPDATE_LIST[ n ].getStartVersion() )
			{
				validUpdates.add( UPDATE_LIST[ n ] );
			}
		}
		return 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 );
	}
}
