FileLoaderManager.cpp 3.92 KB
#include "FileLoaderManager.h"

#include <cstring>

#include "FileFormatManager.h"
#include "FileLoaderASCII.h"
#include "FileLoaderVOTable.h"
#include "FileLoaderNetCDF.h"
#include "FileLoaderCDF.h"

#include "../FrameManager/FrameManager.h"
#include "../Common/Toolbox.h"

using namespace TREPS::Common;
using namespace TREPS::FrameManager;

namespace TREPS
{
	namespace File
	{
		FileLoaderManagerClass::FileLoaderManagerClass(void) : app(NULL), loader(NULL), errorMsg("")
		{
			this->app = ApplicationClass::getInstance();
		}
		
		FileLoaderManagerClass::~FileLoaderManagerClass(void)
		{
			if (this->loader != NULL)
			{
				this->loader->close();
				delete this->loader;
				this->loader = NULL;
			}
		}
		
		bool FileLoaderManagerClass::init(const char *file_path)
		{
			this->errorMsg = "";

			//detect file format
			string formatsFile = this->app->getConf()->getFormatsFilePath();

			if (formatsFile.compare("") == 0)
			{
				this->errorMsg = "Internal error - Cannot get files formats file path";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				return false;
			}

			//generate a file format manager to detect the file format
			FileFormatManagerClass *formatMgr = new FileFormatManagerClass();

			if (!formatMgr->init(formatsFile.c_str()))
			{
				this->errorMsg = "Cannot init file format manager";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				delete formatMgr;
				return false;
			}

			//init the file loader in relation with the file format
			t_FileFormat format = formatMgr->detectFileFormat(file_path);

			delete formatMgr;

			switch (format)
			{
				case FF_ASCII :
					this->loader = new FileLoaderASCIIClass();
					break;
				case FF_VOTABLE :
					this->loader = new FileLoaderVOTableClass();
					break;
				case FF_NETCDF :
					this->loader = new FileLoaderNetCDFClass();
					break;
				case FF_CDF :
					this->loader = new FileLoaderCDFClass();
					break;
				default :
					this->errorMsg = "Unknown file format";
					LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
					return false;
			}

			//open the file
			bool res = this->loader->open(file_path);

			if (!res)
			{
				this->errorMsg = "Cannot open file";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
			}

			return res;
		}

		bool FileLoaderManagerClass::getInfo(t_FileInfo &info)
		{
			this->errorMsg = "";

			if (!this->loader)
			{
				this->errorMsg = "Internal error - Cannot get file info. Loader not initialized.";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				return false;
			}

			if (!this->loader->isOpened())
			{
				this->errorMsg = "Internal error - Cannot get file info. File not open.";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				return false;
			}

			//get file structure information
			bool res = this->loader->getInfo(info);

			if (!res)
			{
				this->errorMsg = "Cannot get file info. Unable to load the file structure.";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
			}

			return res;
		}

		bool FileLoaderManagerClass::getData(int start, int limit, DataRecordListClass *data, int &total)
		{
			this->errorMsg = "";

			data->clear();
			
			if (!this->loader)
			{
				this->errorMsg = "Internal error - Cannot get file data. Loader not initialized!";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				return false;
			}

			if (!this->loader->isOpened())
			{
				this->errorMsg = "Internal error - Cannot get file data. File not open!";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				return false;
			}
			
			//get file data records
			if (!this->loader->getData(start,limit,data,total))
			{
				this->errorMsg = "Cannot get file data. Unable to load the file structure";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				return false;
			}
			
			return true;
		}

		string FileLoaderManagerClass::getErrorMsg(void)
		{
			return this->errorMsg;
		}
	}
}