ResultWriter.cpp 9.62 KB
#include "ResultWriter.h"

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

using namespace TREPS::Common;
using namespace TREPS::TimeManager;

namespace TREPS
{
	namespace RequestManager
	{
		ResultWriterClass::ResultWriterClass(void):XMLManagerClass()
		{
		
		}
		
		bool ResultWriterClass::init(void)
		{
			return this->create("treps");
		}
		
		void ResultWriterClass::setError(const char *msg)
		{
			Node * resNode = this->getResultNode();
			
			if (resNode == NULL)
				return;
				
			this->addAttributeToNode("success","false",resNode);
			
			this->addStrArg("error",msg);
		}
		
		void ResultWriterClass::setSuccess(void)
		{
			Node * resNode = this->getResultNode();
			
			if (resNode == NULL)
				return;
				
			this->addAttributeToNode("success","true",resNode);
		}
	
		Node * ResultWriterClass::createArgNode(const char *tag_name)
		{
			Node * resNode = this->getResultNode();

                        if (resNode == NULL)
                                return NULL;

                        Node *newNode = this->addChildToNode(tag_name,resNode);

			return newNode;
		}
	
		Node * ResultWriterClass::addStrArg(const char *tag_name, const char *value)
		{
			Node *newNode = this->createArgNode(tag_name);
			if (newNode == NULL)
				return NULL;

			this->setNodeContent(value, newNode);

			return newNode;
				
		}

                Node * ResultWriterClass::addIntArg(const char *tag_name, int value)
		{
			Node *newNode = this->createArgNode(tag_name);
			if (newNode == NULL)
				return NULL;

			this->setNodeContent(intToStr(value).c_str(), newNode);

			this->addAttributeToNode("type", "int", newNode);

			return newNode;
		}

		Node * ResultWriterClass::addFileInfoArg(const char *tag_name, t_FileInfo info)
		{
			Node *newNode = this->createArgNode(tag_name);
			if (newNode == NULL)
				return NULL;

			//add type
			this->addAttributeToNode("type", "fileinfo", newNode);

			//add detected frames node
			Node *framesNode = this->addChildToNode("frames",newNode);

			if (framesNode == NULL)
				return NULL;

			for (t_StringList::iterator it=info.frames.begin(); it != info.frames.end(); ++it)
			{
				//add frame node
				Node *frameNode = this->addChildToNode("frame",framesNode);

				//add frame id
				this->addAttributeToNode("id",(*it).c_str(),frameNode);
			}

			//add number of records
			Node *recordsNode = this->addChildToNode("records",newNode);

			if (recordsNode == NULL)
				return NULL;

			this->addAttributeToNode("total", intToStr(info.nbRecords).c_str(), recordsNode);

			//add fields node
			Node *fieldsNode = this->addChildToNode("fields",newNode);

			if (fieldsNode == NULL)
				return NULL;

			this->addAttributeToNode("total", intToStr(info.fields.size()).c_str(), fieldsNode);

			//get time manager instance
			TimeManagerClass *timeMgr = TimeManagerClass::getInstance();

			for (t_FieldList::iterator it=info.fields.begin(); it != info.fields.end(); ++it)
			{
				t_Field crtField = (*it);

				//add field node
				Node *fieldNode = this->addChildToNode("field",fieldsNode);

				if (fieldNode == NULL)
					return NULL;

				//add field id
				this->addAttributeToNode("id",crtField.id.c_str(),fieldNode);

				//add type
				this->addAttributeToNode("type",fieldTypeToStr(crtField.type).c_str(),fieldNode);

				//add timeformat if type == FT_TIME
				if (crtField.type == FT_TIME)
				{
					string timeId = timeMgr->getTimeIdFromFormatAndPattern(crtField.timeformat, crtField.timepattern.c_str());

					this->addAttributeToNode("timeformat",timeId.c_str(),fieldNode);

					// add time pattern
					this->addAttributeToNode("timepattern",crtField.timepattern.c_str(),fieldNode);
				}

				//add field name
				Node *nameNode = this->addChildToNode("name",fieldNode);

				if (nameNode == NULL)
					return NULL;

				this->setNodeContent(crtField.name.c_str(), nameNode);

				//add dimensions
				Node *dimsNode = this->addChildToNode("dimensions",fieldNode);

				if (dimsNode == NULL)
					return NULL;

				for (t_DimensionList::iterator dim=crtField.dims.begin(); dim != crtField.dims.end(); ++dim)
				{
					Node *dimNode = this->addChildToNode("dimension",dimsNode);

					if (dimNode == NULL)
						return NULL;

					this->addAttributeToNode("id", (*dim).id.c_str(), dimNode);

					this->addAttributeToNode("size", intToStr((*dim).size).c_str(), dimNode);
				}

				//add field nbRecords
				recordsNode = this->addChildToNode("records",fieldNode);

				if (recordsNode == NULL)
					return NULL;

				this->addAttributeToNode("total",intToStr(crtField.nbRecords).c_str(),recordsNode);

				//add field attributes
				Node *attributesNode = this->addAttributesNode(fieldNode, &crtField.attributes); 

				if (attributesNode == NULL)
					return NULL;

				//add field ucd
				Node *ucdNode = this->addChildToNode("ucd",fieldNode);

				if (ucdNode == NULL)
					return NULL;

				this->setNodeContent(crtField.ucd.c_str(), ucdNode);

				//add field unit
				Node *unitNode = this->addChildToNode("unit",fieldNode);

				if (unitNode == NULL)
					return NULL;

				this->setNodeContent(crtField.unit.c_str(), unitNode);
			}

			//add vectors node
			Node *vectorsNode = this->addVectorsNode(newNode, &info.vectors);

			if (vectorsNode == NULL)
				return NULL;

			//add file attributes
			Node *attributesNode = this->addAttributesNode(newNode, &info.attributes);
			if (attributesNode == NULL)
				return NULL;

			return newNode;
		}

		Node * ResultWriterClass::addDataRecordListArg(const char *tag_name, DataRecordListClass *data)
		{
			Node *newNode = this->createArgNode(tag_name);
                        if (newNode == NULL)
                                return NULL;

			this->addAttributeToNode("type", "data", newNode);

			DataRecordClass *crtRecord = data->getFirstRecord();

			while (crtRecord != NULL)
			{
				Node *recordNode = this->addChildToNode("rec",newNode);

				map<string, string> valList = crtRecord->getValuesMap();

				for (map<string,string>::iterator it2 = valList.begin(); it2 != valList.end(); ++it2)
				{
					string id  = (it2->first);
					string val = (it2->second);
					Node *valNode = this->addChildToNode("val",recordNode);
					this->addAttributeToNode("id",id.c_str(),valNode);
					this->setNodeContent(val.c_str(),valNode);
				}

				crtRecord = crtRecord->getNextRecord();
			}

			return newNode;
		}

		Node * ResultWriterClass::addTransformationInfoArg(const char *tag_name, t_TransformationInfo info)
		{
			Node *newNode = this->createArgNode(tag_name);

			if (newNode == NULL)
				return NULL;

			//argument type
			this->addAttributeToNode("type", "runinfo", newNode);

			//frames node
			Node *framesNode = this->addChildToNode("frames",newNode);

			if (framesNode == NULL)
				return NULL;

			this->addAttributeToNode("src", info.srcFrame.c_str(), framesNode);

			this->addAttributeToNode("dst", info.dstFrame.c_str(), framesNode);

			//time node
			Node *timeNode = this->addChildToNode("time",newNode);

			if (timeNode == NULL)
				return NULL;

			this->addAttributeToNode("fieldId", info.timeFieldId.c_str(), timeNode);

			this->addAttributeToNode("formatId", info.timeFormatId.c_str(), timeNode);

			this->addAttributeToNode("pattern", info.timePattern.c_str(), timeNode);

			//source vectors node
			Node *vectorsNode = this->addVectorsNode(newNode, &info.srcVectors);

			if (vectorsNode == NULL)
				return NULL;

			return newNode;
		}
		
		Node * ResultWriterClass::getResultNode(void)
		{
			Node *resNode = this->getChildFromRoot("result");
			
			if (!resNode)
				resNode = this->addChildToRoot("result");
			
			return resNode;
		}

		Node * ResultWriterClass::addVectorsNode(Node *parent, t_VectorList *vectors)
		{
			//add vectors node
			Node *vectorsNode = this->addChildToNode("vectors", parent);

			if (vectorsNode == NULL)
				return NULL;

			this->addAttributeToNode("total", intToStr(vectors->size()).c_str(), vectorsNode);

			for (t_VectorList::iterator it=vectors->begin(); it != vectors->end(); ++it)
			{
				t_Vector crtVector = (*it);

				//add vector node
				Node *vectorNode = this->addChildToNode("vector",vectorsNode);

				if (vectorNode == NULL)
					return NULL;

				//add vector info
				this->addAttributeToNode("id",crtVector.id.c_str(),vectorNode);

				this->addAttributeToNode("comp_1",crtVector.fieldId[0].c_str(),vectorNode);

				this->addAttributeToNode("comp_2",crtVector.fieldId[1].c_str(),vectorNode);

				this->addAttributeToNode("comp_3",crtVector.fieldId[2].c_str(),vectorNode);
	
				//add nbRecords
				Node *recordsNode = this->addChildToNode("records",vectorNode);
				if (recordsNode == NULL)
					return NULL;
				this->addAttributeToNode("total",intToStr(crtVector.nbRecords).c_str(),recordsNode);
			}

			return vectorsNode;
		}

		Node *ResultWriterClass::addAttributesNode(Node *parent, map<string,string> *attributes)
		{
			Node *attributesNode = this->addChildToNode("attributes",parent);

			if (attributesNode == NULL)
				return NULL;

			for (map<string,string>::iterator att=attributes->begin(); att != attributes->end(); ++att)
			{
				string attName  = (att->first);
				string attVal   = (att->second);

				Node *attributeNode = this->addChildToNode("attribute",attributesNode);

				if (attributeNode == NULL)
					return NULL;

				this->addAttributeToNode("name",attName.c_str(),attributeNode);

				this->setNodeContent(attVal.c_str(),attributeNode);
			}

			return attributesNode;
		}
	}
}