Commit b285c59c authored by Marco De Marco's avatar Marco De Marco
Browse files

Properties import seems to work, needs more debug

parent 909e7f85
Loading
Loading
Loading
Loading
+187 −52
Original line number Diff line number Diff line
#include <DataModelManager.h>

#include <soci.h>
#include <soci/rowset.h>
#include <soci/use.h>
@@ -49,6 +48,9 @@ void DataModelManager::connect() throw(soci::soci_error)
    connection << " password=" << m_password;
    connection << " dbname=" << m_schema;
    
    //DELETEME
    DEBUG_STREAM << "Connection string: " << connection.str() << endl;

    m_session_sp->open(soci::mysql, connection.str());
}

@@ -76,90 +78,223 @@ void DataModelManager::disconnect()
}

//==============================================================================
//      DataModelManager::retrieveInstrumentList()
//      DataModelManager::importInstrumentList()
//==============================================================================
Instrument::SPVector DataModelManager::retrieveInstrumentList(std::vector<std::string>&
Instrument::SPVector DataModelManager::importInstrumentList(std::vector<std::string>&
      instrumentList) throw(std::invalid_argument, soci::soci_error)
{
    DEBUG_STREAM << "DataModelManager::retrieveInstrumentList()" << endl;
    DEBUG_STREAM << "DataModelManager::importInstrumentList()" << endl;

    //TODO: if empty what?
    if(instrumentList.empty())
        throw(std::invalid_argument(""));
        throw std::invalid_argument("Instrument list is empty");
        
}
    Instrument::SPVector instrument_spvector;
    
//==============================================================================
//      DataModelManager::retrieveInstrument()
//==============================================================================
Instrument::SP DataModelManager::retrieveInstrument(std::string instrument)
    throw(std::invalid_argument, soci::soci_error)
    std::vector<std::string>::iterator it;    
    for(it=instrumentList.begin(); it!=instrumentList.end(); ++it)
    {
    DEBUG_STREAM << "DataModelManager::retrieveInstrument()" << endl;    
        if(it->empty())
            throw std::invalid_argument("Instrument list contains empty element");
                
        instrument_spvector.push_back(retrieveInstrument(*it));
    }
    
//==============================================================================
//      DataModelManager::retrieveInstrumentTuple()
//==============================================================================
Destination::SP DataModelManager::retrieveDestinations(int destinationId) 
    throw(soci::soci_error)
{
    DEBUG_STREAM << "DataModelManager::retrieveDestinations()" << endl;
    return instrument_spvector;
}

//==============================================================================
//      DataModelManager::retrieveInstrumentTuple()
//      DataModelManager::importInstrument()
//==============================================================================
Mapping::SPVector DataModelManager::retrieveMappings(int destinationId)
    throw(soci::soci_error)
Instrument::SP DataModelManager::importInstrument(std::string instrument)
    throw(std::invalid_argument, soci::soci_error)
{
    DEBUG_STREAM << "DataModelManager::retrieveMappings()" << endl;
    DEBUG_STREAM << "DataModelManager::importInstrument()" << endl;
    
    if(instrument.empty())
        throw std::invalid_argument("Instrument is empty");
    
    return retrieveInstrument(instrument);
}

//==============================================================================
//      DataModelManager::retrieveInstrumentTuple()
//      DataModelManager::()
//==============================================================================
void DataModelManager::retrieveInstrumentTuple(std::vector<std::string>& instrumentList,
     InstrumentTupleVector& instrumentTuple_vector) throw(soci::soci_error)
Instrument::SP DataModelManager::retrieveInstrument(std::string instrumentName)
        throw(soci::soci_error)
{
    DEBUG_STREAM << "DataModelManager::retrieveInstrumentTuple()" << endl;    
    /*
    DEBUG_STREAM << "DataModelManager::retrieveInstrument()" << endl;
    
    InstrumentTupleVector instrumentTuple_vector;
    
    soci::rowset< InstrumentTuple > rows = (m_session_sp->prepare <<
    "SELECT i.id, i.name, i.fits_key, i.fits_value, i.fits_date, i.dest_id, "
    "i.default_inst FROM " << m_instrumentTable << " WHERE "
    "c.name=:name", soci::use(componentName,"name"));
        "SELECT id, name, fits_key, fits_value, fits_date, dest_id "
        "FROM " << m_instrumentTable << " WHERE name=:name",
        soci::use(instrumentName, "name"));
    
    std::copy(rows.begin(), rows.end(), std::back_inserter(instrumentTuple_vector)); 
    */
    
    if(instrumentTuple_vector.empty())
            throw soci::soci_error("No tuple for instrument " + instrumentName);

    if(instrumentTuple_vector.size() > 1)
            throw soci::soci_error("Too tuples for instrument " + instrumentName);
    
    InstrumentTuple instrumentTuple = instrumentTuple_vector.at(0);
    
    if(!instrumentTuple.get<0>())
            throw soci::soci_error(" not found");
    int instId = instrumentTuple.get<0>().get();

    if(!instrumentTuple.get<1>())
            throw soci::soci_error("Name not found for id " + instId);
    std::string name = instrumentTuple.get<1>().get();

    if(!instrumentTuple.get<2>())
            throw soci::soci_error("FitsKey not found for id " + instId);
    std::string fitsKey = instrumentTuple.get<2>().get();

    if(!instrumentTuple.get<3>())
            throw soci::soci_error("FitsValue not found for id " + instId);
    std::string fitsValue = instrumentTuple.get<3>().get();

    if(!instrumentTuple.get<4>())
            throw soci::soci_error("FitsDate not found for id " + instId);
    std::string fitsDate = instrumentTuple.get<4>().get();

    if(!instrumentTuple.get<5>())
            throw soci::soci_error("DestId not found for id " + instId);
    int destinationId = instrumentTuple.get<5>().get();
    
    //DELETEME
    DEBUG_STREAM << "Instrument: " << name << " fistKey: " << fitsKey 
            << " fitsValue: " << fitsValue << " fitsDate: " << fitsDate << endl;
    
    Destination::SP destination_sp = retrieveDestination(destinationId);
    
    Instrument::SP instrument_sp = Instrument::create(name, fitsKey, fitsValue, fitsDate, destination_sp);
    
    return instrument_sp;
}        

//==============================================================================
//      DataModelManager::retrieveDestinationTuple()
//      DataModelManager::retrieveDestinations()
//==============================================================================
void DataModelManager::retrieveDestinationTuple(int destinationId, 
        DestinationTupleVector& destinationTuple_vector) throw(soci::soci_error)
Destination::SP DataModelManager::retrieveDestination(int destinationId) 
    throw(soci::soci_error)
{
    DEBUG_STREAM << "DataModelManager::retrieveDestinationTuple()" << endl;
    DEBUG_STREAM << "DataModelManager::retrieveDestinations()" << endl;
  
    DestinationTupleVector destinationTuple_vector;
    
    soci::rowset< DestinationTuple > rows = (m_session_sp->prepare <<
        "SELECT id, host, port, user, password, schema_name, table_name, "
    "storage_path, id FROM " << m_destinationTable << " WHERE id=:id",
        "storage_path FROM " << m_destinationTable << " WHERE id=:id",
        soci::use(destinationId,"id"));

    std::copy(rows.begin(), rows.end(), std::back_inserter(destinationTuple_vector)); 

    if(destinationTuple_vector.empty())
            throw soci::soci_error("No tuple for destination id " + destinationId);

    if(destinationTuple_vector.size() > 1)
            throw soci::soci_error("Too tuples for destination id " + destinationId);

    DestinationTuple destinationTuple = destinationTuple_vector.at(0);
    
    if(!destinationTuple.get<1>())
            throw soci::soci_error("Host not found for id " + destinationId);
    std::string host = destinationTuple.get<1>().get();

    if(!destinationTuple.get<2>())
            throw soci::soci_error("Port not found for id " + destinationId);
    int port = destinationTuple.get<2>().get();

    if(!destinationTuple.get<3>())
            throw soci::soci_error("User not found for id " + destinationId);
    std::string user = destinationTuple.get<3>().get();

    if(!destinationTuple.get<4>())
            throw soci::soci_error("Password not found for id " + destinationId);
    std::string password = destinationTuple.get<4>().get();

    if(!destinationTuple.get<5>())
            throw soci::soci_error("Schema not found for id " + destinationId);
    std::string schema = destinationTuple.get<5>().get();

    if(!destinationTuple.get<6>())
            throw soci::soci_error("Table not found for id " + destinationId);
    std::string table = destinationTuple.get<6>().get();

    if(!destinationTuple.get<7>())
            throw soci::soci_error("Storage not found for id " + destinationId);
    std::string storage = destinationTuple.get<7>().get();
    
    //DELETEME
    DEBUG_STREAM << "Host: " << host << " port: " << port << " user: " << user 
            << " password: " << password << " schema: " << schema 
            << " table: " << table << " storage: " << storage << endl;
            
    Mapping::SPVector mapping_spvector = retrieveMapping(destinationId);    

    Destination::SP destination_sp( Destination::create(host, port, user, password,
            schema, table, storage, mapping_spvector) );

    return destination_sp;    
}

//==============================================================================
//      DataModelManager::retrieveMappingTuple()
//      DataModelManager::retrieveInstrumentTuple()
//==============================================================================
void DataModelManager::retrieveMappingTuple(int destinationId, 
        MappingTupleVector& mappingTuple_vector) throw(soci::soci_error)
Mapping::SPVector DataModelManager::retrieveMapping(int destinationId)
    throw(soci::soci_error)
{
    DEBUG_STREAM << "DataModelManager::retrieveMappingTuple()" << endl;
    DEBUG_STREAM << "DataModelManager::retrieveMappings()" << endl;

    MappingTupleVector mappingTupleList;
    
    soci::rowset< MappingTuple > rows = (m_session_sp->prepare <<
    "SELECT id, column_name, column_type, fits_key_pri, fits_key_sec, mandatory "
    "FROM " << m_mappingTable << " WHERE dest_id=:id", soci::use(destinationId,"id"));

    std::copy(rows.begin(), rows.end(), std::back_inserter(mappingTuple_vector));
    std::copy(rows.begin(), rows.end(), std::back_inserter(mappingTupleList));

    Mapping::SPVector mapping_spvector;

    MappingTupleVector::iterator it; 
    for(it=mappingTupleList.begin(); it!=mappingTupleList.end(); it++)
    {   
        if(!it->get<0>())
                throw soci::soci_error("Id not found");
        int mapId = it->get<0>().get();

        if(!it->get<1>())
                throw soci::soci_error("ColumnName not found for id " + mapId);
        std::string columnName = it->get<1>().get();

        if(!it->get<2>())
                throw soci::soci_error("ColumnType not found for id " + mapId);
        std::string columnType = it->get<2>().get();

        if(!it->get<3>())
                throw soci::soci_error("FitsPrimary not found for id " + mapId);
        std::string fitsPrimary = it->get<3>().get();

        if(!it->get<4>())
                throw soci::soci_error("FitsSecondary not found for id " + mapId);
        std::string fitsSecondary = it->get<4>().get();

        if(!it->get<5>())
                throw soci::soci_error("Mandatory not found for id " + mapId);
        int mandatory = it->get<5>().get();

        //DELETEME
        DEBUG_STREAM << "Column name: " << columnName << " columnType: " << columnType
                << " fitsPrimary: " << fitsPrimary << " fitsSecondary: " << fitsSecondary
                << " mandatory: " << mandatory << endl;
        
        mapping_spvector.push_back( Mapping::create(columnName, columnType,
                 fitsPrimary, fitsSecondary, mandatory) );    
    }   
    
    return mapping_spvector;     
}
+26 −22
Original line number Diff line number Diff line
@@ -32,7 +32,7 @@ public:
    virtual ~DataModelManager();

//------------------------------------------------------------------------------
//	Public methods
//	Users methods
//------------------------------------------------------------------------------    
    virtual void connect() throw(soci::soci_error);
    
@@ -40,29 +40,37 @@ public:
    
    virtual void disconnect();
        
    virtual Instrument::SPVector retrieveInstrumentList(std::vector<std::string>&)
    virtual Instrument::SPVector importInstrumentList(std::vector<std::string>&)
        throw(std::invalid_argument, soci::soci_error);
    
    virtual Instrument::SP retrieveInstrument(std::string)
    virtual Instrument::SP importInstrument(std::string)
        throw(std::invalid_argument, soci::soci_error);
  
protected:
//------------------------------------------------------------------------------
//  Protected methods
//  Utilities methods
//------------------------------------------------------------------------------
    virtual Destination::SP retrieveDestinations(int) throw(soci::soci_error);        
    virtual Instrument::SP retrieveInstrument(std::string)
        throw(soci::soci_error);
    
    virtual Mapping::SPVector retrieveMappings(int) throw(soci::soci_error);        
    virtual Destination::SP retrieveDestination(int)
        throw(soci::soci_error);        
    
    virtual Mapping::SPVector retrieveMapping(int)
        throw(soci::soci_error);        

//------------------------------------------------------------------------------
//  Instrument tuple typedef
//------------------------------------------------------------------------------
    typedef boost::tuple< boost::optional<int>, boost::optional<std::string>,
            boost::optional<std::string>, boost::optional<std::string>,
            boost::optional<std::string>, boost::optional<int> > InstrumentTuple;

    typedef std::vector< InstrumentTuple > InstrumentTupleVector;

    virtual void retrieveInstrumentTuple(std::vector<std::string>&,
        InstrumentTupleVector&) throw(soci::soci_error);

//------------------------------------------------------------------------------
//  Destination tuple typedef
//------------------------------------------------------------------------------
    typedef boost::tuple< boost::optional<int>, boost::optional<std::string>,
            boost::optional<int>, boost::optional<std::string>,
            boost::optional<std::string>, boost::optional<std::string>,
@@ -70,21 +78,17 @@ protected:

    typedef std::vector< DestinationTuple > DestinationTupleVector;

    virtual void retrieveDestinationTuple(int, DestinationTupleVector&)
             throw(soci::soci_error);

//------------------------------------------------------------------------------
//  Mapping tuple typedef
//------------------------------------------------------------------------------    
    typedef boost::tuple< boost::optional<int>, boost::optional<std::string>,
            boost::optional<std::string>, boost::optional<std::string>,
            boost::optional<std::string>, boost::optional<int>,
            boost::optional<int> > MappingTuple;
            boost::optional<std::string>, boost::optional<int>  > MappingTuple;

    typedef std::vector< MappingTuple > MappingTupleVector;
    
    virtual void retrieveMappingTuple(int, MappingTupleVector&)
             throw(soci::soci_error);
    
//------------------------------------------------------------------------------
//	Protected variables
//	Class variables
//------------------------------------------------------------------------------
    //Data model database host
    std::string m_host;
+45 −20
Original line number Diff line number Diff line
@@ -165,21 +165,28 @@ void FitsImporter::init_device()
    {
        try
        {
            //Create i-notify mask from event list property
            const uint32_t inotify_mask = create_inotify_mask(event_list);
            
            //Initialize configuration container class
            m_configuration_sp = Configuration::create(watchPath, workerNumber,
                    waitTime, connectionNumber, inotify_mask);
            
            //Initialize data model manager using tango properties as parameters
            DataModelManager::SP data_model_manager_sp( new DataModelManager(this,
                     dMDBHost, dMDBPort, dMDBUser, dMDBPassword, dMDBSchema,
                        dMDBInstrumentTable, dMDBDestinationTable, dMDBMappingTable));
            
            //Connect to data model database
            data_model_manager_sp->connect();
            
            m_instrumentList_spvector = data_model_manager_sp->retrieveInstrumentList(instrumentList);
            //Initialize instrument vector from data model database parameters
            m_instrumentList_spvector = data_model_manager_sp->importInstrumentList(instrumentList);
            
            m_defaultInstrument_sp = data_model_manager_sp->retrieveInstrument(defaultInstrument);
            //Initialize default instrument from data model database parameters
            m_defaultInstrument_sp = data_model_manager_sp->importInstrument(defaultInstrument);
            
            //Disconnect
            data_model_manager_sp->disconnect();
        }
        catch(std::exception& ex)
@@ -417,55 +424,73 @@ void FitsImporter::get_device_property()
    try
    {                
        if(watchPath.empty())
            throw(std::invalid_argument("WatchPath property is empty or not defined"));
            throw(invalid_argument("WatchPath property is empty or not defined"));

        if(workerNumber<MIN_WORKER_NUMBER || workerNumber>MAX_WORKER_NUMBER)
            throw(std::invalid_argument("WorkerNumber property out of range or not defined"));
            throw(invalid_argument("WorkerNumber property out of range or not defined"));

        if(connectionNumber<MIN_CONNECTION_NUMBER || connectionNumber>MAX_CONNECTION_NUMBER)
            throw(std::invalid_argument("ConnectionNumber property out of range or not defined"));
            throw(invalid_argument("ConnectionNumber property out of range or not defined"));

        if(event_list.empty())
            throw(std::invalid_argument("EventList property is empty or not defined"));
            throw(invalid_argument("EventList property is empty or not defined"));
        
        for(vector<string>::size_type e_ind=0; e_ind<event_list.size(); ++e_ind)
            if(event_list.at(e_ind).empty())
            {
                stringstream event_list_error;
                event_list_error << "EventList property has an empty element at \""
                        << e_ind << "\" position" << endl;
                throw(invalid_argument(event_list_error.str()));
            }
            
        if(waitTime<MIN_WAIT_TIME || waitTime>MAX_WAIT_TIME)
            throw(std::invalid_argument("WaitTime property is out of range or not defined"));
            throw(invalid_argument("WaitTime property is out of range or not defined"));

        if(instrumentList.empty()) 
            throw(std::invalid_argument("EventList property is empty or not defined"));
            throw(invalid_argument("InstrumentList property is empty or not defined"));

        for(vector<string>::size_type i_ind=0; i_ind<instrumentList.size(); ++i_ind)
            if(instrumentList.at(i_ind).empty())
            {
                stringstream event_list_error;
                event_list_error << "InstrumentList property has an empty element at \""
                        << i_ind << "\" position" << endl;
                throw(invalid_argument(event_list_error.str()));
            }
        
        if(defaultInstrument.empty())
            throw(std::invalid_argument("DefaultInstrument property is empty or not defined"));
            throw(invalid_argument("DefaultInstrument property is empty or not defined"));

        if(dMDBHost.empty())
            throw(std::invalid_argument("DMDBHost property is empty or not defined"));
            throw(invalid_argument("DMDBHost property is empty or not defined"));

        if(dMDBPort<MIN_DMDB_PORT || dMDBPort>MAX_DMDB_PORT)
            throw(std::invalid_argument("DMDB property out of range or not defined"));
            throw(invalid_argument("DMDB property out of range or not defined"));

        if(dMDBUser.empty())
            throw(std::invalid_argument("DMDBUser property is empty or not defined"));
            throw(invalid_argument("DMDBUser property is empty or not defined"));

        if(dMDBPassword.empty())
            throw(std::invalid_argument("DMDBPassword property is empty or not defined"));
            throw(invalid_argument("DMDBPassword property is empty or not defined"));

        if(dMDBSchema.empty())
            throw(std::invalid_argument("DMDBSchema property is empty or not defined"));
            throw(invalid_argument("DMDBSchema property is empty or not defined"));

        if(dMDBInstrumentTable.empty())
            throw(std::invalid_argument("DMDBInstrumentTable property is empty or not defined"));
            throw(invalid_argument("DMDBInstrumentTable property is empty or not defined"));

        if(dMDBDestinationTable.empty())
            throw(std::invalid_argument("DMDBDestinationTable property is empty or not defined"));
            throw(invalid_argument("DMDBDestinationTable property is empty or not defined"));

        if(dMDBMappingTable.empty())
            throw(std::invalid_argument("DMDBMappingTable property is empty or not defined"));
            throw(invalid_argument("DMDBMappingTable property is empty or not defined"));
    }
    catch(std::invalid_argument& ex)
    catch(invalid_argument& ex)
    {
        set_state(Tango::FAULT);
        std::stringstream error_stream;
        error_stream << "FitsImporter::get_device_property(): " << ex.what() << std::endl;
        stringstream error_stream;
        error_stream << "FitsImporter::get_device_property(): " << ex.what() << endl;
        set_status(error_stream.str());
    }  
	
+4 −11
Original line number Diff line number Diff line
@@ -20,11 +20,9 @@ private:
//	Private constructor destructor and deleter
//------------------------------------------------------------------------------
	Instrument(std::string name, std::string fitsKey, std::string fitsValue,
		std::string fitsDate, bool defaultInstrument,
		boost::shared_ptr<Destination> destination_sp) :
		std::string fitsDate, boost::shared_ptr<Destination> destination_sp) :		
		m_name(name), m_fitsKey(fitsKey), m_fitsValue(fitsValue), 
		m_fitsDate(fitsDate), m_defaultInstrument(defaultInstrument),
		m_destination_sp(destination_sp) {}
		m_fitsDate(fitsDate), m_destination_sp(destination_sp) {}		
	~Instrument() {}

	class deleter;
@@ -40,11 +38,10 @@ public:
//	Public methods
//------------------------------------------------------------------------------
	static Instrument::SP create(std::string name, std::string fitsKey,
		std::string fitsValue, std::string fitsDate, bool defaultInstrument,
		Destination::SP destination_sp)
		std::string fitsValue, std::string fitsDate, Destination::SP destination_sp)		
	{
		Instrument::SP d_sp( new Instrument(name, fitsKey, fitsValue, fitsDate,
			 defaultInstrument, destination_sp), Instrument::deleter() );
			 destination_sp), Instrument::deleter() );
		return d_sp;
	}

@@ -52,7 +49,6 @@ public:
	std::string getFitsKey() const { return m_fitsKey; }
	std::string getFitsValue() const { return m_fitsValue; }
	std::string getFitsDate() const { return m_fitsDate; }
	bool isDefault() const { return m_defaultInstrument; }
	boost::shared_ptr<Destination> getDestination() { return m_destination_sp; }

private:
@@ -71,9 +67,6 @@ private:
	//Date fits key
	const std::string m_fitsDate;
	
	//Default instrument flag
	const bool m_defaultInstrument;

	//Destination of instrument metadata
	Destination::SP m_destination_sp;
};
+1 −1

File changed.

Contains only whitespace changes.