QString FormTemplateWizardPage::changeUiClassName(const QString &uiXml, const QString &newUiClassName)
{
    // Prepare I/O: Sheet
    const QString xsltSheet = QString::fromLatin1(classNameChangingSheetFormatC).arg(newUiClassName);
    QByteArray xsltSheetBA = xsltSheet.toUtf8();
    QBuffer xsltSheetBuffer(&xsltSheetBA);
    xsltSheetBuffer.open(QIODevice::ReadOnly);
    // Prepare I/O: Xml
    QByteArray xmlBA = uiXml.toUtf8();
    QBuffer  xmlBuffer(&xmlBA);
    xmlBuffer.open(QIODevice::ReadOnly);
    // Prepare I/O: output
    QBuffer outputBuffer;
    outputBuffer.open(QIODevice::WriteOnly);

    // Run query
    QXmlQuery query(QXmlQuery::XSLT20);
    query.setFocus(&xmlBuffer);
    query.setQuery(&xsltSheetBuffer);
    if (!query.evaluateTo(&outputBuffer)) {
        qWarning("Unable to change the ui class name in a form template.\n%s\nUsing:\n%s\n",
                 xmlBA.constData(), xsltSheetBA.constData());
        return uiXml;
    }
    outputBuffer.close();
    return QString::fromUtf8(outputBuffer.data());
}
Пример #2
0
bool ofXml::load(const string & path){
	ofFile file(path, ofFile::ReadOnly);
	if(!file.exists()) {
		ofLogError("ofXml") << "couldn't load, \"" << file.getFileName() << "\" not found";
		return false;
	}
	ofBuffer xmlBuffer(file);
	return loadFromBuffer(xmlBuffer);
}
Пример #3
0
//-------------------------------------------------------
void OdbcPersistor::RestoreAll()
{
    int connectionAttempts = 0;
    bool errorReported = false;
    EntityIdSet restoredObjects;

    const int binaryLargeSize = Safir::Dob::PersistenceParameters::BinaryDataColumnSize();
    const int binarySmallSize = Safir::Dob::PersistenceParameters::BinarySmallDataColumnSize();
    const int xmlSize = Safir::Dob::PersistenceParameters::XmlDataColumnSize();

    SQLHDBC                                     getAllConnection = SQL_NULL_HANDLE;
    bool                                        isConnected = false;
    bool                                        getAllIsValid = false;
    SQLHSTMT                                    getAllStatement = SQL_NULL_HANDLE;
    Safir::Dob::Typesystem::Int64               typeId = 0;
    Safir::Dob::Typesystem::Int64               instance = 0;
    Safir::Dob::Typesystem::Int64               handlerId = 0;
    boost::scoped_array<unsigned char>          storeBinarySmallData(new unsigned char[binarySmallSize]);
    SQLLEN                                      currentSmallDataSize = 0;
    boost::scoped_array<unsigned char>          storeBinaryLargeData(new unsigned char[binaryLargeSize]);
    SQLLEN                                      currentLargeDataSize = 0;
    boost::scoped_array<char>                   xmlBuffer(new char[xmlSize]);
    boost::scoped_array<wchar_t>                xmlBufferW(new wchar_t[xmlSize / sizeof(wchar_t)]);
    SQLLEN                                      currentXmlSize = 0;

    const boost::chrono::steady_clock::time_point startTime = boost::chrono::steady_clock::now();

    const SQLRETURN ret = ::SQLAllocHandle(SQL_HANDLE_DBC, m_environment, &getAllConnection);
    if (!SQL_SUCCEEDED(ret))
    {
        OdbcHelper::ThrowException(SQL_HANDLE_ENV, m_environment);
    }

    bool done = false;
    while (!done)
    {
        try
        {
            ConnectIfNeeded(getAllConnection, isConnected, connectionAttempts);

            for (;;)
            {
                //getAll statement execution (will loop here until we successfully execute)
                if (!getAllIsValid)
                {
                    m_helper.AllocStatement(&getAllStatement, getAllConnection);
                    m_helper.Prepare(
                        getAllStatement,
                        "SELECT typeId, instance, handlerid, xmlData, binaryData, binarySmallData "
                        "FROM PersistentEntity");
                    m_helper.BindColumnInt64(getAllStatement, 1, &typeId);
                    m_helper.BindColumnInt64(getAllStatement, 2, &instance);
                    m_helper.BindColumnInt64(getAllStatement, 3, &handlerId);

                    if (Safir::Dob::PersistenceParameters::TextColumnsAreUtf8())
                    {
                        BindColumnString(getAllStatement, 4, xmlSize, xmlBuffer.get(), &currentXmlSize);
                    }
                    else
                    {
                        BindColumnStringW(getAllStatement,
                                          4,
                                          xmlSize / sizeof(wchar_t),
                                          xmlBufferW.get(),
                                          &currentXmlSize);
                    }

                    m_helper.BindColumnBinary(getAllStatement, 5, binaryLargeSize, storeBinaryLargeData.get(), &currentLargeDataSize);
                    m_helper.BindColumnBinary(getAllStatement, 6, binarySmallSize, storeBinarySmallData.get(), &currentSmallDataSize);
                }

                try
                {
                    m_helper.Execute(getAllStatement);
                    break;
                }
                catch(const OdbcException& e)
                {
                    const std::wstring err = Safir::Dob::Typesystem::Utilities::ToWstring(e.what());
                    m_debug << "Caught a RetryException in GetAll:\n" << err << std::endl;
                    boost::this_thread::sleep_for(RETRY_EXCEPTION_DELAY);
                }
            }

            for (;;)
            {
                if (!m_helper.Fetch(getAllStatement))
                {//we've got all rows!
                    done = true;
                    break;
                }

                const Safir::Dob::Typesystem::EntityId entityId
                    (typeId,
                     Safir::Dob::Typesystem::InstanceId(instance));

                const Safir::Dob::Typesystem::HandlerId handler(handlerId);

                auto findIt = GetPersistentTypes().find(entityId.GetTypeId());
                if (findIt == GetPersistentTypes().end())
                { //not to be persisted!
                    Remove(entityId);
                    continue;
                }

                if (restoredObjects.find(entityId) != restoredObjects.end())
                { //already restored this object
                    continue;
                }

                try
                {
                    Safir::Dob::ConnectionAspectInjector injector(m_dobConnection);

                    if (currentXmlSize != SQL_NULL_DATA)
                    { //some xml persistent data set
                        std::wstring xml;

                        if (Safir::Dob::PersistenceParameters::TextColumnsAreUtf8())
                        {
                           xml = Safir::Dob::Typesystem::Utilities::ToWstring(xmlBuffer.get());
                        }
                        else
                        {
                            xml = xmlBufferW.get();
                        }
                        m_debug
                            << "Restoring from xml"                 << entityId
                            << ", size = "                          << xml.size()
                            << ". First 100 chars of the data: "    << xml.substr(0,100)
                            << std::endl;

                        const Safir::Dob::Typesystem::ObjectPtr object =
                            Safir::Dob::Typesystem::Serialization::ToObject(xml);
                        const Safir::Dob::EntityPtr entity =
                            boost::dynamic_pointer_cast<Safir::Dob::Entity>(object);
                        m_debug << "Successfully deserialized" <<std::endl;

                        injector.InitialSet(entity, entityId.GetInstanceId(), handler);
                        m_debug << "InitialSet successful"<<std::endl;

                        Safir::Dob::Typesystem::BinarySerialization bin;
                        Safir::Dob::Typesystem::Serialization::ToBinary(entity, bin);
                        Store(entityId, handler, bin, true);
                        m_debug << "Stored it as binary" << std::endl;
                    }
                    else if (currentSmallDataSize != SQL_NULL_DATA)
                    {
                        const char * const data = reinterpret_cast<const char * const>(storeBinarySmallData.get());
                        m_debug << "Restoring " << entityId << " from binary " <<std::endl;

                        Safir::Dob::EntityPtr entity =
                            boost::dynamic_pointer_cast<Safir::Dob::Entity>
                            (Safir::Dob::Typesystem::ObjectFactory::Instance().CreateObject(data));
                        m_debug << "Successfully deserialized" <<std::endl;

                        injector.InitialSet(entity, entityId.GetInstanceId(), handler);
                        m_debug << "InitialSet successful"<<std::endl;
                    }
                    else
                    {
                        if (currentLargeDataSize != SQL_NULL_DATA)
                        { //some binarypersistent data set
                            const char * const data = reinterpret_cast<const char * const>(storeBinaryLargeData.get());
                            m_debug << "Restoring " << entityId << " from binary " <<std::endl;

                            Safir::Dob::EntityPtr entity =
                                boost::dynamic_pointer_cast<Safir::Dob::Entity>
                                (Safir::Dob::Typesystem::ObjectFactory::Instance().CreateObject(data));
                            m_debug << "Successfully deserialized" <<std::endl;

                            injector.InitialSet(entity, entityId.GetInstanceId(), handler);
                            m_debug << "InitialSet successful"<<std::endl;
                        }
                        else
                        {
                            m_debug << "No data set for " << entityId <<std::endl;
                        }
                    }
                    //add the objectid to the list of objectids that we've done, so that we can resume from where
                    //we were in case the db goes down during restore
                    restoredObjects.insert(entityId);
                }
                catch(const Safir::Dob::Typesystem::IllegalValueException &)
                {
                    m_debug << "Could not restore "
                            << entityId.ToString()
                            << ", removing it" << std::endl;

                    Safir::Logging::SendSystemLog(Safir::Logging::Error,
                                                    L"Failed to restore entity" +
                                                    entityId.ToString() +
                                                    L", will remove persisted data.");

                    //we don't want to try it again if the connection fails later.
                    restoredObjects.insert(entityId);
                    //remove the row from the db
                    Remove(entityId);
                    //since we did not remove the objectid from persistentObjects an empty row will be inserted below.
                }
            }
        }
        catch(const OdbcException& e)
        {
            const std::wstring err = Safir::Dob::Typesystem::Utilities::ToWstring(e.what());
            m_debug << "Caught a ReconnectException in RestoreAll:\n" << err << std::endl;
            if (connectionAttempts > REPORT_AFTER_RECONNECTS && !errorReported)
            {
                Safir::Logging::SendSystemLog(Safir::Logging::Error,
                                              L"RestoreAll: Failed to connect to the database, will keep trying. Exception info: " +
                                              err);
                errorReported = true;
            }

            if (isConnected)
            {
                Disconnect(getAllConnection);
                isConnected = false;
            }

            getAllIsValid = false;

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

    if (errorReported)
    {
        Safir::Logging::SendSystemLog(Safir::Logging::Informational,
                                      L"Successfully connected to the database");
        errorReported = false;
        connectionAttempts = 0;
    }

    try
    {
        if (isConnected)
        {
            Disconnect(getAllConnection);
        }
        FreeConnection(getAllConnection);
    }
    catch(const OdbcException& e)
    {
        const std::wstring err = Safir::Dob::Typesystem::Utilities::ToWstring(e.what());
        Safir::Logging::SendSystemLog(Safir::Logging::Error,
                                        L"Whoops. Error while disconnecting the getAllConnection. Ignoring this and moving on. Exception info: " +
                                        err);
    }

    m_debug << "RestoreAll completed: "
            << restoredObjects.size()
            << " objects restored in time "
            << boost::chrono::steady_clock::now() - startTime << std::endl;
}