/*
 * UserDataProvider.java
 *
 * Created on 8 de Novembro de 2004, 18:38
 */

package siprp.clientes;

import java.util.*;

import com.evolute.utils.*;
import com.evolute.utils.arrays.*;
import com.evolute.utils.data.*;
import com.evolute.utils.db.*;
import com.evolute.utils.metadb.*;
import com.evolute.utils.ui.search.*;
import com.evolute.utils.sql.*;

import siprp.*;
/**
 *
 * @author  fpalma
 */
public class UserDataProvider extends MetaProvider
{
	private static final String RESERVED_ROLES_MANAGER = "manager";
	private static final String RESERVED_ROLES_ADMIN = "admin";
	private static final Object LOCK = new Object();
	private static UserDataProvider instance = null;
	private final Executer executer;
	
	protected final Expression excludeReserved;

	/** Creates a new instance of UserDataProvider */
	public UserDataProvider()
		throws Exception
	{
		boolean webAware = ( ( Boolean ) Singleton.getInstance( SingletonConstants.WEB_AWARE ) ).booleanValue();
		DBManager dbm;
		if( webAware )
		{
			String url = ( String ) Singleton.getInstance( SingletonConstants.WEB_URL_PREFIX );
			url += ( String ) Singleton.getInstance( SingletonConstants.WEB_URL ) + "/";
			url += ( String ) Singleton.getInstance( SingletonConstants.WEB_DB_NAME ) + "/";
			String user = ( String ) Singleton.getInstance( SingletonConstants.WEB_USER );
			String pwd = ( String ) Singleton.getInstance( SingletonConstants.WEB_PASSWORD );
			dbm = new JDBCManager( url, user, pwd, 10, 8, 8, null );
			executer = dbm.getSharedExecuter( this );
		}
		else
		{
			dbm = null;
			executer = null;
		}
		excludeReserved = new Field( "role" ).isDifferent( RESERVED_ROLES_MANAGER ).and(
			new Field( "role" ).isDifferent( RESERVED_ROLES_ADMIN ) );
	}
	
	public static MetaProvider getProvider()
		throws Exception
	{
		synchronized( LOCK )
		{
			if( instance == null )
			{
				instance = new UserDataProvider();
			}
		}
		return instance;
	}
	
	public String getUserName( Integer empresaID )
		throws Exception
	{
System.out.println( "getUserName( " +empresaID+ " )" ) ;
		Select select = new Select( new String[]{ "roles" }, new String[]{ "username" },
				new Field( "role" ).isEqual( empresaID == null ? "siprpuser" : empresaID.toString() ) );
		Virtual2DArray array = executer.executeQuery( select );
		if( array.columnLength() == 0 )
		{
			return null;
		}
		return array.get( 0, 0 ).toString();
	}
	
	public void saveUser( String userName, String password, Integer empresaID, boolean isNew )
		throws Exception
	{
		if( isNew )
		{
			Insert insert = 
				new Insert( "roles", 
					new Assignment[]{ new Assignment( new Field( "username" ), userName ),
										new Assignment( new Field( "role" ), empresaID == null ? "siprpuser" : empresaID.toString() ) } );
			executer.executeQuery( insert, null );
			insert = 
				new Insert( "users", 
					new Assignment[]{ new Assignment( new Field( "username" ), userName ),
										new Assignment( new Field( "password" ), password ) } );
			executer.executeQuery( insert, null );
		}
		else
		{
			Select select = new Select( new String[]{ "roles" }, new String[]{ "username" },
				new Field( "role" ).isEqual( empresaID == null ? "siprpuser" : empresaID.toString() ) );
			Virtual2DArray array = executer.executeQuery( select );
			if( array.columnLength() == 0 )
			{
				saveUser( userName, password, empresaID, true );
				return;
			}
			String oldName = array.get( 0, 0 ).toString();
			Update update = 
				new Update( "roles", 
					new Assignment[]{ new Assignment( new Field( "username" ), userName ),
										new Assignment( new Field( "role" ), empresaID == null ? "siprpuser" : empresaID.toString() ) },
				new Field( "username" ).isEqual( oldName ).and( excludeReserved ) );
			executer.executeQuery( update );
			update = 
				new Update( "users", 
					new Assignment[]{ new Assignment( new Field( "username" ), userName ),
										new Assignment( new Field( "password" ), password ) },
				new Field( "username" ).isEqual( oldName ) );
			executer.executeQuery( update );
		}
	}
	
	public boolean checkNewName( String name, Integer empresaID )
		throws Exception
	{
		Select select = new Select( new String[]{ "roles" }, new String[]{ "username" },
				new Field( "role" ).isDifferent( empresaID == null ? "siprpuser" : empresaID.toString() ).and(
				new Field( "lower( username )" ).isEqual( new Field( "lower( '" + name + "' )" ) ) ) );
		Virtual2DArray array = executer.executeQuery( select );
		if( array.columnLength() != 0 )
		{
			if( empresaID != null )
			{
				return false;
			}
			else
			{
				select = new Select( new String[]{ "roles" }, new String[]{ "username" },
					new Field( "role" ).isEqual( empresaID == null ? "siprpuser" : empresaID.toString() ).and(
					new Field( "lower( username )" ).isEqual( new Field( "lower( '" + name + "' )" ) ) ) );
				array = executer.executeQuery( select );
				if( array.columnLength() == 0 )
				{
					return false;
				}
			}
		}
		
		return true;
	}
}
