All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.genexus.db.LocalUserInformation Maven / Gradle / Ivy

Go to download

Core classes for the runtime used by Java and Android apps generated with GeneXus

There is a newer version: 4.7.0
Show newest version
package com.genexus.db;

import java.sql.SQLException;
import java.util.Enumeration;

import com.genexus.*;
import com.genexus.db.driver.DataSource;
import com.genexus.db.driver.GXConnection;

public class LocalUserInformation extends UserInformation
{
	public LocalUserInformation(Namespace namespace)
	{
		super(namespace);

		ClientPreferences preferences = new ClientPreferences(namespace.iniFile);

		setLocalUtil(   preferences.getDECIMAL_POINT(),
						preferences.getDATE_FMT(),
						preferences.getTIME_FMT(),
						preferences.getYEAR_LIMIT(),
						preferences.getLANGUAGE());
	}
/*
	public LocalUserInformation(ModelContext context, Namespace namespace)
	{
		super(context, namespace);
	}
*/
/*
	public void setLocalUtil(char decimalPoint, String dateFormat, String timeFormat, int firstYear2K, String language)
	{
		localUtil = LocalUtil.getLocalUtil(decimalPoint, dateFormat, timeFormat, firstYear2K, language);
		messages  = localUtil.getMessages();
	}
*/
	public GXConnection getConnection(ModelContext context, String dataSourceName, String user, String password) throws SQLException
	{
		ConnectionInformation info = getConnectionSQLite(dataSourceName);

		if	(info.rwConnection == null)
		{
			info = new ConnectionInformation();
			info.user = user;
			info.password = password;
			connections.put(dataSourceName, info);
			createConnection2(context, namespace.getDataSource(dataSourceName), info);
		}

		return (GXConnection) info.rwConnection;
	}

	public boolean isConnected(String dataSourceName)
	{
		ConnectionInformation info = getConnectionSQLite(dataSourceName);
		return info != null && info.rwConnection != null;
	}

	public GXConnection getConnection(ModelContext context, String dataSourceName)
	{
		ConnectionInformation info = getConnectionSQLite(dataSourceName);

		if	(info.rwConnection == null || ((GXConnection) info.rwConnection).getError())
		{
			info = new ConnectionInformation();
			connections.put(dataSourceName, info);

			createConnection(context, namespace.getDataSource(dataSourceName), info);
		}

		return (GXConnection) info.rwConnection;
	}
	
	private ConnectionInformation getConnectionSQLite(String dataSourceName) 
	{
		ConnectionInformation info = (ConnectionInformation) connections.get(dataSourceName);
		if (info.rwConnection == null || ((GXConnection) info.rwConnection).getError())
		{
			//try get default connection, that be should used in Android.
			info = (ConnectionInformation) connections.get("DEFAULT");
		}
		return info;
	}
/*
	protected void putConnection(String dataSourceName, GXConnection rwConnection, GXConnection roConnection, String user, String password)
	{
		connections.put(dataSourceName, new ConnectionInformation(rwConnection, roConnection, user, password));
	}
*/
	public void disconnect() throws SQLException
	{
		for (Enumeration en = connections.elements(); en.hasMoreElements(); )
		{
			ConnectionInformation info = (ConnectionInformation) en.nextElement();

			if	(info.rwConnection != null)
			{
				try
				{
					info.rwConnection.close();
				}
				catch (SQLException e)
				{
					System.err.println("Error while closing a db connection " + e.getMessage());
				}
				finally
				{
					// Si no pongo esto en null siempre, si da un error al desconectarse despues
					// quiere volver a usar esta conexion. Es mejor que quede la otra conexion
					// en banda que que cancele le aplicacion...
					info.rwConnection = null;
				}
			}
		}

	}

	public void disconnectOnException() throws SQLException
	{
		disconnect();
	}

	private boolean setUserAndPassword(DataSource dataSource, String[] user, String[] password)
	{
		if	(dataSource.loginInServer)
		{
			user[0]     = dataSource.defaultUser;
			password[0] = dataSource.defaultPassword;
			return true;
		}

		return false;
	}

	private void createConnection2(ModelContext context, DataSource dataSource, ConnectionInformation info) throws SQLException
	{
		info.rwConnection = new GXConnection(context, info.user, info.password, dataSource);
	}


	private void createConnection(ModelContext context, DataSource dataSource, ConnectionInformation info) //throws SQLException
	{
		String[] user	  = new String[1];
		String[] password = new String[1];

		int retries = 0;
		boolean ui = com.genexus.ApplicationContext.getInstance().isMsgsToUI();

		while (info.rwConnection == null && retries++ < 3 )
		{
			if	(setUserAndPassword(dataSource, user, password))
			{
				try
				{
					info.password 	  = password[0];
					info.user     	  = user[0];
					info.rwConnection = new GXConnection(context, info.user, info.password, dataSource);
				}
				catch (SQLException e)
				{
					CommonUtil.msg(null, e.getMessage());
				}
			}
			else
			{
				//if	(ui)
					//Application.exit();
			}

			if	(!ui)
				break;
		}

		if	(ui)
		{
			if	(retries > 3)
			{
				System.out.println("Can't connect to to database. Exiting ...");
			 	//Application.exit();
			}
		}
	}


	public String getUser(String dataSource)
	{
		return ((ConnectionInformation) connections.get(dataSource)).user;
	}

	public String getPassword(String dataSource)
	{
		return ((ConnectionInformation) connections.get(dataSource)).password;
	}

	public LocalUtil getLocalUtil()
	{
		return super.getLocalUtil();
	}

	public Messages getMessages()
	{
		return messages;
	}

	public Namespace getNamespace()
	{
		return namespace;
	}

	public void dropAllCursors()
	{
		for (Enumeration en = connections.elements(); en.hasMoreElements(); )
		{
			ConnectionInformation info = (ConnectionInformation) en.nextElement();

			if	(info.rwConnection != null)
				info.rwConnection.dropAllCursors();

			if	(info.roConnection != null)
				info.rwConnection.dropAllCursors();
		}
	}
	
	public void disconnectUser()
	{
	  try
	  {
		DBConnectionManager.getInstance().disconnect(getHandle());
	  }
	  catch(java.sql.SQLException e)
	  {
		  System.out.println(e.toString());
	  }
	}	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy