StatisticNode.cc 7.99 KB
/*
 * StatisticNode.cc
 *
 *  Created on: 04 nov 2014
 *      Author: AKKA
 */

#include <string.h>


// Parameters module includes
#include "ServicesServer.hh"
#include "Parameter.hh"
#include "FileConfigurator.hh"
#include "ParameterManager.hh"
#include "TimeTable.hh"
#include "AsciiWriter.hh"
#include "InternalXMLWriter.hh"
#include "VOTableWriter.hh"
#include "PostProcessingNode.hh"

// XMLConfigurator module includes
#include "Constant.hh"

// Statistic output
#include "StatisticNode.hh"
#include "StatisticOutput.hh"

using namespace AMDA::Parameters;
using namespace AMDA::XMLConfigurator;

namespace AMDA {
	namespace ParamOutputImpl {
		namespace Statistic {
			class TimeFormatNode: public AMDA::XMLConfigurator::NodeCfg {
				public:
				void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {
					LOG4CXX_DEBUG(gLogger, "TimeFormatNode::proceed")
					StatisticProperties *statisticProperties =  pContext.get<StatisticProperties *>();

					if (strcmp ((const char*)pNode->children->content, "ISO") == 0) {
						statisticProperties->setTimeFormat(TimeTableCatalog::TimeTable::TIME_FORMAT::YYYYMMDDThhmmssmsk);
					} else {
						ERROR_EXCEPTION("Time Format not supported : " << (const char*)pNode->children->content);
					}
				}
			};

			class FileFormatNode: public AMDA::XMLConfigurator::NodeCfg {
				public:
				void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {
					LOG4CXX_DEBUG(gLogger, "FileFormatNode::proceed")
					StatisticProperties *statisticProperties =  pContext.get<StatisticProperties *>();

					if (strcmp ((const char*)pNode->children->content, "ASCII") == 0) {
						statisticProperties->setFileFormatKey(TimeTableCatalog::AsciiWriter::FORMAT);
					} else if (strcmp ((const char*)pNode->children->content, "XML") == 0) {
						statisticProperties->setFileFormatKey(TimeTableCatalog::InternalXMLWriter::FORMAT);
					} else if (strcmp ((const char*)pNode->children->content, "VOT") == 0) {
						statisticProperties->setFileFormatKey(TimeTableCatalog::VOTableWriter::FORMAT);
					} else {
						ERROR_EXCEPTION("File Format not supported : " << (const char*)pNode->children->content);
					}
				}
			};

			class FileNameNode: public AMDA::XMLConfigurator::NodeCfg {
				public:
				void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {
					LOG4CXX_DEBUG(gLogger, "FileNameNode::proceed")
					StatisticProperties *statisticProperties =  pContext.get<StatisticProperties *>();
					statisticProperties->setFileName((const char*) pNode->children->content);
				}
			};

			class OutputStructureNode: public AMDA::XMLConfigurator::NodeCfg {
				public:
				void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {
					LOG4CXX_DEBUG(gLogger, "OutputStructureNode::proceed")
					StatisticProperties *statisticProperties =  pContext.get<StatisticProperties *>();

					if (strcmp ((const char*)pNode->children->content, "one-file") == 0) {
						statisticProperties->setOutputStructure(OutputStructure::ONE_FILE);
					} else if (strcmp ((const char*)pNode->children->content, "one-file-per-parameter") == 0) {
						statisticProperties->setOutputStructure(OutputStructure::ONE_FILE_PER_PARAMETER);
					} else {
						ERROR_EXCEPTION("Output structure not supported : " << (const char*)pNode->children->content);
					}
				}
			};

			class FunctionArgumentNode: public AMDA::XMLConfigurator::NodeCfg {
				public:
				void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {
					LOG4CXX_DEBUG(gLogger, "FunctionArgumentNode::proceed")
					FunctionProperties *functionProperties =  pContext.get<FunctionProperties *>();

					xmlChar *lArgName = xmlGetProp(pNode, (const xmlChar *) "name");
					xmlChar *lValName = xmlGetProp(pNode, (const xmlChar *) "value");

					if ((lArgName != NULL) && (lValName != NULL))
						functionProperties->addArgument((const char*) lArgName, (const char*) lValName);

					if (lArgName)
						xmlFree(lArgName);
					if (lValName)
						xmlFree(lValName);
				}
			};

			class FunctionNode: public AMDA::XMLConfigurator::NodeGrpCfg {
				public:
				FunctionNode(void) : NodeGrpCfg() {
					getChildList()["argument"] = NodeCfgSPtr(new FunctionArgumentNode());
				}

				void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {
					LOG4CXX_DEBUG(gLogger, "FunctionNode::proceed")
					ParamProperties *paramProperties =  pContext.get<ParamProperties *>();

					xmlChar *lFuncName = xmlGetProp(pNode, (const xmlChar *) "name");
					if (lFuncName == NULL) {
						ERROR_EXCEPTION(
								AMDA::XMLConfigurator::ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@name")
					}

					FunctionProperties* functionProperties = new FunctionProperties();

					functionProperties->setName((const char*) lFuncName);

					xmlFree(lFuncName);

					paramProperties->addFunctionProperties(functionProperties);

					CfgContext lContext;
					lContext.push<FunctionProperties *>(functionProperties);

					NodeGrpCfg::proceed(pNode, lContext);
				}
			};

			class ParamNode: public AMDA::XMLConfigurator::NodeGrpCfg {
				public:
				ParamNode(void) : NodeGrpCfg() {
					getChildList()["function"] = NodeCfgSPtr(new FunctionNode());
				}

				void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {
					LOG4CXX_DEBUG(gLogger, "ParamNode::proceed");
					StatisticProperties *statisticProperties =  pContext.get<StatisticProperties *>();

					xmlChar *lParamId = xmlGetProp(pNode, (const xmlChar *) "id");
					if (lParamId == NULL) {
						ERROR_EXCEPTION(
								AMDA::XMLConfigurator::ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@" << AMDA::XMLConfigurator::PARAMNAME)
					}

					ParamProperties* paramProperties = new ParamProperties();

					paramProperties->setId((const char*) lParamId);

					xmlFree(lParamId);

					xmlChar *lParamIndex = xmlGetProp(pNode, (const xmlChar *) "index");
					if (lParamIndex != NULL) {
						paramProperties->setIndex(atoi( (const char*) lParamIndex));
						xmlFree(lParamIndex);
					}

					CfgContext lContext;
					lContext.push<ParamProperties *>(paramProperties);

					NodeGrpCfg::proceed(pNode, lContext);

					statisticProperties->addParamProperties(paramProperties);
				}
			};


			class ParamsNode: public AMDA::XMLConfigurator::NodeGrpCfg {
				public:
				ParamsNode(void) : NodeGrpCfg() {
					getChildList()["param"] = NodeCfgSPtr(new ParamNode());
				}

				void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {
					LOG4CXX_DEBUG(gLogger, "ParamsNode::proceed");
					StatisticProperties *statisticProperties =  pContext.get<StatisticProperties *>();

					CfgContext lContext;
					lContext.push<StatisticProperties *>(statisticProperties);

					NodeGrpCfg::proceed(pNode, lContext);
				}
			};

			StatisticNode::StatisticNode(void) : NodeGrpCfg() {
				getChildList()["timeFormat"] = NodeCfgSPtr(new TimeFormatNode());
				getChildList()["fileFormat"] = NodeCfgSPtr(new FileFormatNode());
				getChildList()["outputStructure"] = NodeCfgSPtr(new OutputStructureNode());
				getChildList()["fileName"] = NodeCfgSPtr(new FileNameNode());
				getChildList()["params"] = NodeCfgSPtr(new ParamsNode());
				getChildList()["postProcess"]=NodeCfgSPtr(new postprocessing::PostProcessingNode<StatisticOutput>());
			}


			void StatisticNode::proceed(xmlNodePtr pNode,
					const AMDA::Parameters::CfgContext& pContext) {
				LOG4CXX_DEBUG(gLogger, "StatisticNode::proceed");

				ParameterManager* lParameterManager = pContext.get<ParameterManager*>();
				ParamOutput *lParamOutputTmp;
				CfgContext lContext;

				StatisticOutput* lstatisticOutput = new StatisticOutput(*lParameterManager);

				lContext.push<StatisticOutput *>(lstatisticOutput);
				lContext.push<StatisticProperties *>(&lstatisticOutput->getStatisticProperties());

				lParamOutputTmp = lstatisticOutput;
				ParamOutputSPtr  lParamOutput(lParamOutputTmp);
				NodeGrpCfg::proceed(pNode, lContext);
				lParameterManager->getParamOutputList().push_back(lParamOutput);
			}

		} // namespace Statistic
	} // namespace ParamOutputImpl
} // namespace AMDA