ParamMgr.cc 2 KB
/*
 * ParamMgr.cc
 *
 *  Created on: Oct 6, 2014
 *      Author: m.mazel
 */

#include "Helper.hh"
#include "Properties.hh"
#include "ParamMgr.hh"
#include "ParamParser.hh"
#include "InfoLogger.hh"

namespace AMDA {
namespace Info {

const ParamInfoSPtr ParamMgr::getParamInfoFromId (const std::string &id, bool forceCreateIfNoExist) {
	if (id.empty())
		return ParamInfoSPtr();

	// If Param id already loaded, return it
	if (_paramInfoMap.find (id) != _paramInfoMap.end()) {
		return _paramInfoMap [id];
	}

	// Otherwise, build Param path & filename using the id
	AMDA::helpers::Properties lProperties("app.properties");

	std::string xmlFileName = lProperties["app.param.path"] + "/" + id + ".xml";
	_paramInfoMap [id] = getParamInfoFromFile (id, xmlFileName);

	if (forceCreateIfNoExist && (_paramInfoMap [id] == nullptr))
	{
		_paramInfoMap [id] = ParamInfoSPtr(new ParamInfo);
		_paramInfoMap [id]->setId(id);
	}

	return _paramInfoMap [id];
}

const ParamInfoSPtr ParamMgr::cloneParamInfoFromId(const std::string &originId, const std::string &dstId)
{
	if (originId.empty() || dstId.empty()) {
		return ParamInfoSPtr();
	}

	//if param dst id exist, return it
	if (_paramInfoMap.find (dstId) != _paramInfoMap.end()) {
		return _paramInfoMap [dstId];
	}

	//if Param origin id no exist, return nullptr
	if (_paramInfoMap.find (originId) == _paramInfoMap.end()) {
		return ParamInfoSPtr();
	}

	if (_paramInfoMap[originId] == nullptr) {
		return ParamInfoSPtr();
	}

	//copy param info
	_paramInfoMap [dstId] = ParamInfoSPtr(new ParamInfo(*_paramInfoMap[originId].get()));

	_paramInfoMap [dstId]->setId(dstId);

	return _paramInfoMap [dstId];
}

ParamInfoSPtr ParamMgr::getParamInfoFromFile (const std::string &paramId,
		const std::string &xmlFilename) {
	AMDA::helpers::Properties lProperties("app.properties");

	// Retrieve XSD file for Param and build a parser
	ParamParser parser(lProperties["app.paramInfo.xsd"].c_str());

	// Parse XML file
	return parser.parse (paramId, xmlFilename);
}

} /* namespace Info */
} /* namespace AMDA */