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;
		ServletContext context = null;
		Application application = null;

		if (rapidRequest != null) {

			RapidHttpServlet rapidServlet = rapidRequest.getRapidServlet();

			if (rapidServlet != null) {
				context = rapidServlet.getServletContext();
				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;
	}
}
@@ -3734,32 +3734,441 @@
 onnection);%0A%09%7D%0A%0A
+%09public static DataFactory createMasterDataFactory(ServletContext context, Application application, ConnectionAdapter connectionAdapter) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException %7B%0A%09%09DataFactory dataFactory = new DataFactory(connectionAdapter, false);%0A%09%09return dataFactory;%0A%09%7D%0A%0A
 %09public static D
@@ -4461,43 +4461,8 @@
 n %7B%0A
-%09%09DataFactory dataFactory = null;%0A%0A
 %09%09St
@@ -4790,24 +4790,36 @@
 assword);%0A%09%09
+DataFactory 
 dataFactory 
@@ -4812,36 +4812,44 @@
 y dataFactory = 
-new 
+createMaster
 DataFactory(conn
@@ -4836,32 +4836,54 @@
 sterDataFactory(
+context, application, 
 connectionAdapte
@@ -4879,35 +4879,27 @@
 ctionAdapter
-, false
 );%0A
-%0A
 %09%09return dat
@@ -5239,16 +5239,17 @@
 n %7B%0A%09%09if
+ 
 (dataFac

Bring in line with RapidCustom version