void ConvertFiles()
{
    Safir::Dob::Typesystem::BinarySerialization bin;
    boost::filesystem::path storagePath = GetStorageDirectory();

    for (boost::filesystem::directory_iterator it (storagePath);
         it != boost::filesystem::directory_iterator(); ++it)
    {
        const boost::filesystem::path path = it->path();
        const EntityIdAndHandlerId id = Filename2EntityIdAndHandlerId(*it);

        if (path.extension() == ".bin")
        {
            const size_t fileSize = static_cast<size_t>(boost::filesystem::file_size(path));
            if (fileSize == 0)
            {
                continue;
            }
            bin.resize(fileSize);

            size_t numBytesRead = 0;
            boost::filesystem::ifstream file(path, std::ios::in | std::ios::binary);
            while (file.good())
            {
                file.read(&bin[0] + numBytesRead,4096);
                numBytesRead += static_cast<size_t>(file.gcount());
            }

            if(fileSize != numBytesRead)

            {
                throw Safir::Dob::Typesystem::SoftwareViolationException(L"Stupid error in file reading, probably", __WFILE__, __LINE__);
            }
            file.close();

            const Safir::Dob::Typesystem::ObjectPtr object = Safir::Dob::Typesystem::Serialization::ToObject(bin);

            const std::wstring xml = Safir::Dob::Typesystem::Serialization::ToXml(object);
            boost::filesystem::path xmlFileName(path);
            xmlFileName.replace_extension(".xml");
            boost::filesystem::wofstream xmlFile(xmlFileName);
            xmlFile << xml;
        }
    }
}
void ConvertFiles()
{
    Safir::Dob::Typesystem::BinarySerialization bin;
    boost::filesystem::path storagePath = GetStorageDirectory();

    for (boost::filesystem::directory_iterator it (storagePath);
         it != boost::filesystem::directory_iterator(); ++it)
    {
        const boost::filesystem::path path = it->path();
        const EntityIdAndHandlerId id = Filename2EntityIdAndHandlerId(*it);

        if (path.extension() == ".bin")
        {
            const size_t fileSize = static_cast<size_t>(boost::filesystem::file_size(path));
            if (fileSize == 0)
            {
                continue;
            }
            bin.resize(fileSize);

            boost::filesystem::ifstream file(path, std::ios::in | std::ios::binary);
            file.read(&bin[0],fileSize);
            const Safir::Dob::Typesystem::ObjectPtr object = Safir::Dob::Typesystem::Serialization::ToObject(bin);

            const std::string xml = Safir::Dob::Typesystem::Utilities::ToUtf8
                (Safir::Dob::Typesystem::Serialization::ToXml(object));
            boost::filesystem::path xmlFileName(path);
            xmlFileName.replace_extension(".xml");
            boost::filesystem::ofstream xmlFile(xmlFileName);
            xmlFile << xml;
            xmlFile.close();

            //make the file world read-writeable
            using namespace boost::filesystem;
            permissions(path,
                        owner_read  | owner_write  |
                        group_read  | group_write  |
                        others_read | others_write );
        }
    }
}
//-------------------------------------------------------
void OdbcPersistor::Store(const Safir::Dob::Typesystem::EntityId& entityId,
                          const Safir::Dob::Typesystem::HandlerId& handlerId,
                          Safir::Dob::Typesystem::BinarySerialization& bin,
                          const bool update)
{
    const bool small = static_cast<int>(bin.size()) < Safir::Dob::PersistenceParameters::BinarySmallDataColumnSize();

    int retries = 0;
    bool errorReported = false;

    bool paramSet = false;
    bool done = false;
    while (!done)
    {
        try
        {
            ConnectIfNeeded(m_odbcConnection, m_isOdbcConnected, retries);

            if (!m_storeIsValid)
            {
                m_helper.AllocStatement(&m_storeStatement, m_odbcConnection);
                m_storeIsValid = true;

                m_helper.Prepare(m_storeStatement,
                                 "UPDATE PersistentEntity "
                                 "SET xmlData=NULL, binarySmallData=?, binaryData=?, handlerid=? "
                                 "WHERE typeId=? AND instance=?");

                BindParamBinary(m_storeStatement, 1, Safir::Dob::PersistenceParameters::BinarySmallDataColumnSize(), m_storeBinarySmallData.get(), &m_currentSmallDataSize);
                BindParamBinary(m_storeStatement, 2, Safir::Dob::PersistenceParameters::BinaryDataColumnSize(), m_storeBinaryLargeData.get(), &m_currentLargeDataSize);
                m_helper.BindParamInt64(m_storeStatement, 3, &m_handler);
                m_helper.BindParamInt64(m_storeStatement, 4, &m_type);
                m_helper.BindParamInt64(m_storeStatement, 5, &m_instance);

                SetStmtTimeout(m_storeStatement);

                paramSet = false;
            }

            if (!paramSet)
            {
                if (small)
                {
                    memcpy(m_storeBinarySmallData.get(),&bin[0], bin.size());
                    m_currentSmallDataSize = bin.size();
                    m_currentLargeDataSize = SQL_NULL_DATA;
                }
                else
                {
                    memcpy(m_storeBinaryLargeData.get(),&bin[0], bin.size());
                    m_currentLargeDataSize = bin.size();
                    m_currentSmallDataSize = SQL_NULL_DATA;
                }

                m_type = entityId.GetTypeId();
                m_instance = entityId.GetInstanceId().GetRawValue();
                m_handler = handlerId.GetRawValue();
                paramSet = true;
            }

            if (!update)
            {
                // After global instanceid's we can no longer assume that all instanceid's are written to the
                // the database at startup. So try to insert them here if the entity already exist
                Insert(entityId);
            }

            m_helper.Execute(m_storeStatement);

            m_debug << "Successfully stored binary entity in database. Size = "<<bin.size() << std::endl;
            done = true;

            if (errorReported)
            {
                Safir::Logging::SendSystemLog(Safir::Logging::Informational,
                                                L"Successfully connected to the database");
                errorReported = false;
                retries = 0;
            }
        }
        catch(const OdbcException& e)
        {
            const std::wstring err = Safir::Dob::Typesystem::Utilities::ToWstring(e.what());
            m_debug << "Caught a ReconnectException in Store:\n" << err << std::endl;
            if (retries > REPORT_AFTER_RECONNECTS && !errorReported)
            {
                Safir::Logging::SendSystemLog(Safir::Logging::Error,
                                              L"Store: Failed to connect to the database, will keep trying. Exception info: " +
                                              err);
                errorReported = true;
            }

            DisconnectOdbcConnection();

            boost::this_thread::sleep_for(RECONNECT_EXCEPTION_DELAY);
        }
    }
}