package com.rapid.utils;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import com.rapid.core.Application;
import com.rapid.data.ConnectionAdapter;
import com.rapid.data.ConnectionAdapter.ConnectionAdapterException;
import com.rapid.data.DataFactory;
import com.rapid.data.DatabaseConnection;
import com.rapid.data.SimpleConnectionAdapter;
import com.rapid.server.RapidHttpServlet;
import com.rapid.server.RapidRequest;

public class DataFactoryBuilder {

	public static DataFactory createDataFactory(RapidRequest rapidRequest, DatabaseConnection connection) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
		DataFactory dataFactory = null;

		RapidHttpServlet rapidServlet = rapidRequest.getRapidServlet();
		if(rapidServlet!=null) {
			ServletContext context = rapidServlet.getServletContext();
			Application application = rapidRequest.getApplication();
			ConnectionAdapter connectionAdapter = connection.getConnectionAdapter(context, application);
			dataFactory = new DataFactory(connectionAdapter, false);
		}

		return dataFactory;
	}

	public static DataFactory createDataFactory(RapidRequest rapidRequest) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
		DatabaseConnection connection = rapidRequest.getApplication().getDatabaseConnections().get(0);
		return createDataFactory(rapidRequest, connection);
	}

	public static DataFactory createDataFactory(RapidRequest rapidRequest, int connectionNumber) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
		DatabaseConnection connection = rapidRequest.getApplication().getDatabaseConnections().get(connectionNumber);
		return createDataFactory(rapidRequest, connection);
	}

	public static DataFactory createMasterDataFactory(RapidRequest rapidRequest) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
		DataFactory dataFactory = null;

		Application application = rapidRequest.getApplication();
		List<DatabaseConnection> connections = application.getDatabaseConnections();
		DatabaseConnection connection = connections.get(0);
		HttpServletRequest request = rapidRequest.getRequest();
		if(request!=null) {
			ServletContext context = request.getServletContext();
			if(context!=null) {
				String driverClass = connection.getDriverClass();
				String connectionString = connection.getConnectionString();
				String username = connection.getUserName();
				String password = connection.getPassword();
				ConnectionAdapter connectionAdapter = new SimpleConnectionAdapter(context, driverClass, connectionString, username, password);
				dataFactory = new DataFactory(connectionAdapter, false);
			}
		}

		return dataFactory;
	}

	public static DataFactory createMasterDataFactory(ServletContext context, Application application) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
		DatabaseConnection connection = application.getDatabaseConnections().get(0);
		return createMasterDataFactory(context, application, connection);
	}

	public static DataFactory createMasterDataFactory(ServletContext context, Application application, DatabaseConnection connection) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
		DataFactory dataFactory = null;

		String driverClass = connection.getDriverClass();
		String connectionString = connection.getConnectionString();
		String username = connection.getUserName();
		String password = connection.getPassword();
		ConnectionAdapter connectionAdapter = new SimpleConnectionAdapter(context, driverClass, connectionString, username, password);
		dataFactory = new DataFactory(connectionAdapter, false);

		return dataFactory;
	}

	public static DataFactory checkDataFactoryConnection(RapidRequest rapidRequest, DataFactory dataFactory) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, SQLException, ConnectionAdapterException {
		if(dataFactory.getConnection(rapidRequest).isClosed())
			dataFactory = createMasterDataFactory(rapidRequest.getRapidServlet().getServletContext(), rapidRequest.getApplication());
		return dataFactory;
	}
}
@@ -863,32 +863,33 @@
 rgetException %7B%0A
+%0A
 %09%09DataFactory da
@@ -906,17 +906,116 @@
 = null;%0A
-%0A
+%09%09ServletContext context = null;%0A%09%09Application application = null;%0A%0A%09%09if (rapidRequest != null) %7B%0A%0A%09
 %09%09RapidH
@@ -1072,20 +1072,23 @@
 vlet();%0A
+%0A%09
 %09%09if
+ 
 (rapidSe
@@ -1092,18 +1092,20 @@
 dServlet
+ 
 !=
+ 
 null) %7B%0A
@@ -1103,39 +1103,25 @@
  null) %7B%0A%09%09%09
-ServletContext 
+%09
 context = ra
@@ -1155,28 +1155,17 @@
 t();%0A%09%09%09
-Application 
+%09
 applicat
@@ -1202,16 +1202,26 @@
 ion();%0A%09
+%09%09%7D%0A%0A%09%09%7D%0A%0A
 %09%09Connec
@@ -1309,17 +1309,16 @@
 ion);%0A%09%09
-%09
 dataFact
@@ -1365,20 +1365,16 @@
  false);
-%0A%09%09%7D
 %0A%0A%09%09retu

Update to DataFactoryBuilder to remove dependency on rapidRequest