RequestManager.php 6.02 KB
<?php

//autoload configuration
function amdaintegration_autoload($class_name)
{
	$dirs = array(
			'InputOutput',
			'InputOutput/IHMImpl',
			'InputOutput/IHMImpl/Config',
			'InputOutput/IHMImpl/Params',
			'InputOutput/IHMImpl/Params/DataMiningImpl',
			'InputOutput/IHMImpl/Params/StatisticsImpl',
			'InputOutput/IHMImpl/Params/DownloadImpl',
			'InputOutput/IHMImpl/Params/PlotImpl',
			'InputOutput/IHMImpl/Params/GeneratorImpl',
			'InputOutput/IHMImpl/Process',
			'InputOutput/IHMImpl/ParamInfo',
			'InputOutput/IHMImpl/Tools',
			'InputOutput/IHMImpl/TimeTables',
			'InputOutput/TestImpl',
			'Request',
			'Request/Config',
			'Request/ParamsRequestImpl',
			'Request/ParamsRequestImpl/Nodes',
			'Request/ParamsRequestImpl/Nodes/Infos',
			'Request/ParamsRequestImpl/Nodes/Params',
			'Request/ParamsRequestImpl/Nodes/Requests',
			'Request/ProcessRequestImpl',
			'Request/ProcessRequestImpl/Process',
			'Request/TTRequestImpl' 
	);
	
	$ihm_dirs = array(
			IHM_SRC_DIR.'/php/classes',
			IHM_SRC_DIR.'/php/RemoteDataCenter'
	);
   
   $find = false;
	
	foreach($dirs as $dir)
	{
		$file = __DIR__.'/'.$dir.'/'.$class_name.'.php';
		if (file_exists($file))
		{
			require $file;
			$find = true;
			break;
		}
	}
	
	if (!$find)
		foreach($ihm_dirs as $dir)
		{
			$file = $dir.'/'.$class_name.'.php';
			if (file_exists($file))
			{
				require $file;			 
				break;
			}
		}		
}

spl_autoload_register('amdaintegration_autoload');

/**
 * @class FunctionTypeEnumClass
 * @brief Abstract class used as an enumerate for function type.
 * @details
*/
abstract class FunctionTypeEnumClass
{
	const PARAMS             = "params";
	const PARAMSGEN          = "params_gen";
	const ACTION             = "action";
	const PROCESSDELETE      = "process_delete";
	const PROCESSRUNNINGINFO = "process_running_info";
	const PROCESSGETINFO     = "process_get_info";
	const PROCESSCLEAN       = "process_clean";
	const PROCESSGETREQUEST  = "process_get_request";
	const TTMERGE            = "tt_merge";
	const TTUNION            = "tt_union";
	const TTCONVERT          = "tt_convert";
	const PARAMINFO          = "param_info";
}

/**
 * @class ClientTypeEnumClass
 * @brief Abstract class used as an enumerate for AMDA_Integration client.
 * @details
 */
abstract class ClientTypeEnumClass
{
	const IHM  = "ihm";
	const TEST = "test";
}

/**
 * @class RequestManagerClass
 * @brief Main class to manage a request that's come from an AMDA client
 * @details
 */
Class RequestManagerClass
{
	public static $version = "1.6.0";

/*
	* @brief Constructor
*/
	function __construct()
	{
	}

/*
	* @brief Treat a request that's come from the IHM
*/
	public function runIHMRequest($user, $userHost, $function, $input)
	{
		return $this->runGenericRequest(ClientTypeEnumClass::IHM,$user,$userHost,$function,$input);
	}

/*
	* @brief Treat a request that's come from a test script
*/
	public function runTestRequest($user, $function, $input)
	{
		$userHost = "";
		return $this->runGenericRequest(ClientTypeEnumClass::TEST,$user,$userHost,$function,$input);
	}

/*
	* @brief Create the request instance in relation with the function type
*/
	private function createRequest($user, $userHost, $function)
	{
		switch ($function)
		{
			case FunctionTypeEnumClass::PARAMS :
			case FunctionTypeEnumClass::PARAMSGEN :
			case FunctionTypeEnumClass::ACTION :
				return new ParamsRequestClass($user, $userHost);
			case FunctionTypeEnumClass::PROCESSDELETE :
			case FunctionTypeEnumClass::PROCESSRUNNINGINFO :
			case FunctionTypeEnumClass::PROCESSGETINFO :
			case FunctionTypeEnumClass::PROCESSCLEAN :
			case FunctionTypeEnumClass::PROCESSGETREQUEST :
				return new ProcessRequestClass($user, $userHost);
			case FunctionTypeEnumClass::TTMERGE :
			case FunctionTypeEnumClass::TTUNION :
			case FunctionTypeEnumClass::TTCONVERT :
				return new TTRequestClass($user, $userHost);
			case FunctionTypeEnumClass::PARAMINFO :
				return new ParamInfoRequestClass($user, $userHost);
			default :
				throw new Exception('Request '.$function.' not implemented.');
		}
	}

/*
	*  @brief Create an instance of the InputOutput interface in relation with the client
*/
	private function createInputOutput($user,$userHost = null,$client)
	{
		switch ($client)
		{
			case ClientTypeEnumClass::IHM :
				return new IHMInputOutputClass($user, $userHost);
			case ClientTypeEnumClass::TEST :
				return new TestInputOutputClass($user);
			default :
				throw new Exception('Client '.$client.' not implemented.');
		}
	}

/*
	* @brief Sequence used to run a request
*/
	private function runGenericRequest($client,$user,$userHost,$function,$input)
	{
		//create an instance of the InputOutput interface
		$inputOutput = $this->createInputOutput($user, $userHost, $client);

		//get the input data from the request input data by using the InputOutput interface
		$inputdata = $inputOutput->getInputData($input,$function);
		
		//create an instance of the RequestClass to run the request
		$request = $this->createRequest($user, $userHost, $function);
		
		if (is_array($inputdata))
		{
			$outputdata = array();
			foreach ($inputdata as $data)
				//execute the request
				$outputdata[] = $this->runSingleRequest($request,$data);
		}
		else
			$outputdata = $this->runSingleRequest($request,$inputdata);
				
		//get the request output data from the output data by using the InputOutput interface
		return $inputOutput->getOutput($outputdata);
	}

/*
	*  @brief Run a single request
*/
	private function runSingleRequest($request,$data)
	{
		//link the request to the input data
		$request->setData($data);

		//init the request
		if (!$request->init())
		{
			$crtData = $request->getData();
			if (isset($crtData))
				throw new Exception('Request initialization error  : '.$crtData->getLastErrorMessage());
			else
				throw new Exception('Cannot init request. Unknown error.');
		}

		//run the request
		if (!$request->run())
		{
			$crtData = $request->getData();
			if ($crtData)
				throw new Exception('Request execution error  : '.$crtData->getLastErrorMessage());
			else
				throw new Exception('Cannot run request. Unknown error.');
		}

		return $request->getData();
	}
}

?>