BodiesCDPP3DView.cpp 6.95 KB
#include "BodiesCDPP3DView.h"

#include <vector>

//#include "../../CDPP3DViewSOAPClient/cdppBinder.nsmap"
#include "../../CDPP3DViewSOAPClient/cdpp3dviewcdppBinderProxy.h"
#include "../Common/Toolbox.h"
//#include "../DataRetriever/DataRetriever.h"
//#include "../File/FileLoaderManager.h"
#include "../TimeManager/TimeManager.h"

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

namespace TREPS
{
	namespace Bodies
	{
		BodiesCDPP3DViewClass::BodiesCDPP3DViewClass(RequestAbstractClass *trepsRequest):BodiesAbstractClass(trepsRequest),
						client(NULL)
		{
			//init SOAP client for 3DView Web Service
			this->client = new cdppBinderProxy(SOAP_IO_KEEPALIVE);
			soap_ssl_init();
			if (soap_ssl_client_context(this->client,
				SOAP_SSL_NO_AUTHENTICATION,
				NULL, // keyfile: required only when client must authenticate to server
				NULL, // password to read the key file (not used with GNUTLS)
				NULL, // cacert file to store trusted certificates
				NULL, // capath to directory with trusted certificates
				NULL  // if randfile!=NULL: use a file with random data to seed randomness
			)) {
				this->errorMsg = "Error during the SOAP SSL init: ";
				if (this->client->soap_fault_string() != NULL)
				{
					this->errorMsg += this->client->soap_fault_string();
				}
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
			}
		}

		BodiesCDPP3DViewClass::~BodiesCDPP3DViewClass(void)
		{
			if (this->client != NULL)
			{
				delete this->client;
				this->client = NULL;
			}
		}

		bool BodiesCDPP3DViewClass::run()
		{
			this->errorMsg = "";


			//for each vector
			//for (t_VectorList::iterator it=this->request->getSrcVectors()->begin(); it != this->request->getSrcVectors()->end(); ++it)
			//{

				string generalStatus = "";
				generalStatus += " - Running step : list bodies";
				this->trepsRequest->setStatus(generalStatus.c_str());
				t_Bodies bodies = this->callListBodiesRequest(request->getIsSc());
				
				generalStatus = "Bodies found: ";
				generalStatus += bodies.size();
                generalStatus += " - Running step : filter bodies result";
				//this->trepsRequest->setStatus(generalStatus.c_str());
				
				LOG4CXX_INFO(this->app->getLog()->getPtr(),"listBodies result : " << bodies.size() << " bodies found");
				
				t_Bodies filteredBodies;
				//get time manager instance
				TimeManagerClass *timeMgr = TimeManagerClass::getInstance();
				
				for (unsigned int i=0;i<bodies.size();i++) {
					//LOG4CXX_INFO(this->app->getLog()->getPtr(),"BodiesCDPP3DView.run(): in for : " << i);
					ns4__Body* body = bodies.at(i);
					ns4__TimeRange* coverage = body->coverage;
					if(coverage==0) continue;
					//LOG4CXX_INFO(this->app->getLog()->getPtr(),"BodiesCDPP3DView.run(): body " <<bodies.at(i)->name.c_str()<<"/"<<coverage);
					//LOG4CXX_INFO(this->app->getLog()->getPtr(),"BodiesCDPP3DView.run(): body " <<bodies.at(i)->name.c_str()<<"/"<<coverage->startTime<<"/"<<coverage->stopTime);
					time_t lBodyStart = coverage->startTime;
					time_t lBodyStop = coverage->stopTime;
					time_t lFileStart = timeMgr->to_TIMESTAMP(request->getStartTime());
					time_t lFileStop = timeMgr->to_TIMESTAMP(request->getStopTime());
					
					//LOG4CXX_INFO(this->app->getLog()->getPtr(),"BodiesCDPP3DView.run(): compare body start: " << ctime(&lBodyStart));
					//LOG4CXX_INFO(this->app->getLog()->getPtr(),"BodiesCDPP3DView.run(): compare body stop: " << ctime(&lBodyStop));
					//LOG4CXX_INFO(this->app->getLog()->getPtr(),"BodiesCDPP3DView.run(): compare filter start: " << ctime(&lFileStart));
					//LOG4CXX_INFO(this->app->getLog()->getPtr(),"BodiesCDPP3DView.run(): compare filter stop: " << ctime(&lFileStop));
					
					//check if body timerange covers file timerange
					if((lBodyStart-lFileStart)<0 && (lBodyStop-lFileStop)>0) {
						/*ns4__Body *lNewBody = soap_new_req_ns4__Body(body->soap, body->naifId, body->modelId, body->name,
							soap_new_req_ns4__TimeRange(body->soap, lBodyStart, lBodyStop),
							body->type, body->color, body->size,
							body->prefFrame, body->prefCenter, body->prefStarSubset);
						*/
						filteredBodies.push_back(body);
					}
					
				
					
					
				}
				LOG4CXX_INFO(this->app->getLog()->getPtr(),"BodiesCDPP3DView.run(): filtered bodies result : " << filteredBodies.size() << " bodies found");

				//copy result xml file in op workspace
				generalStatus = "Bodies filtered: ";
				generalStatus += filteredBodies.size();
                generalStatus += " - Running step : Save bodies result";
				//LOG4CXX_INFO(this->app->getLog()->getPtr(),"BodiesCDPP3DView.run(): set Status");
				//this->trepsRequest->setStatus(generalStatus.c_str());
				//add result
				//LOG4CXX_INFO(this->app->getLog()->getPtr(),"BodiesCDPP3DView.run(): set result");
				this->result->init(filteredBodies);
			//}

			return true;
		}


		t_Bodies BodiesCDPP3DViewClass::callListBodiesRequest(bool isSc)
		{
			bool res = false;
			t_Bodies response;

			string method = "listBodies";

				//Init 3DView listBodies request
				_ns3__listBodies listBodiesRequest;
				_ns3__listBodiesResponse listBodiesResponse;
				if(isSc) {
					listBodiesRequest.pType = ns4__BodyType__SPACECRAFT;
					LOG4CXX_INFO(this->app->getLog()->getPtr(),"BodiesCDPP3DView.callListBodiesRequest(): type : sc");
					res = (this->client->listBodies(&listBodiesRequest,&listBodiesResponse) == SOAP_OK);
				} else {
					listBodiesRequest.pType = ns4__BodyType__PLANET;
					LOG4CXX_INFO(this->app->getLog()->getPtr(),"BodiesCDPP3DView.callListBodiesRequest(): type : planet");
						//ns4__BodyType__PLANET,	///< xs:string value="PLANET"
						//ns4__BodyType__SATELLITE,	///< xs:string value="SATELLITE"
						//ns4__BodyType__COMET,	///< xs:string value="COMET"
						//ns4__BodyType__ASTEROID,	///< xs:string value="ASTEROID"
					res = (this->client->listBodies(&listBodiesRequest,&listBodiesResponse) == SOAP_OK);
					t_Bodies planets = listBodiesResponse.return_;
/*					listBodiesRequest.pType = ns4__BodyType__SATELLITE;
					res = (this->client->listBodies(&listBodiesRequest,&listBodiesResponse) == SOAP_OK);
					for(unsigned i=0;i<planets.size();i++) {
						listBodiesResponse.return_.push_back(planets.at(i));
					}
*/
				}

				//run listNewFrameOrb to perform transformation (rotation + translation)
				//_ns3__listNewFrameOrbResponse listNewFrameOrbResponse;


				//#ifndef WITH_GETORBURL
				//this->deleteTimeList(listNewFrameOrbRequest.pDateTimeInput);
				//this->deleteVectorTuple3d(listNewFrameOrbRequest.pDataInput);
				//#endif

			if (!res)
			{
				this->errorMsg = "Error during the call of ";
				this->errorMsg += method;
				this->errorMsg += " of 3DView Web Service";
				if (this->client->soap_fault_string() != NULL)
				{
					this->errorMsg += " (";
                                        this->errorMsg += this->client->soap_fault_string();
                                        this->errorMsg += ")";
				}
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				
			} else {
				response = listBodiesResponse.return_;
			}

			return response;	
		}
	}
}