Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prototype storing project file to database #11403

Merged
merged 11 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion ApplicationLibCode/Application/RiaPreferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ RiaPreferences::RiaPreferences()
CAF_PDM_InitField( &m_loggerFlushInterval, "loggerFlushInterval", 500, "Logging Flush Interval [ms]" );
CAF_PDM_InitField( &m_loggerTrapSignalAndFlush, "loggerTrapSignalAndFlush", false, "Trap SIGNAL and Flush File Logs" );

CAF_PDM_InitField( &m_storeBackupOfProjectFile, "storeBackupOfProjectFile", true, "Store Backup of Project Files" );

CAF_PDM_InitField( &ssihubAddress, "ssihubAddress", QString( "http://" ), "SSIHUB Address" );
ssihubAddress.uiCapability()->setUiLabelPosition( caf::PdmUiItemInfo::TOP );

Expand Down Expand Up @@ -477,7 +479,8 @@ void RiaPreferences::defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering&
otherGroup->add( &m_gridCalculationExpressionFolder );
otherGroup->add( &m_summaryCalculationExpressionFolder );

caf::PdmUiGroup* loggingGroup = uiOrdering.addNewGroup( "Logging" );
caf::PdmUiGroup* loggingGroup = uiOrdering.addNewGroup( "Logging and Backup" );
loggingGroup->add( &m_storeBackupOfProjectFile );
loggingGroup->add( &m_loggerFilename );
loggingGroup->add( &m_loggerFlushInterval );
loggingGroup->add( &m_loggerTrapSignalAndFlush );
Expand Down Expand Up @@ -983,6 +986,14 @@ bool RiaPreferences::loggerTrapSignalAndFlush() const
return m_loggerTrapSignalAndFlush();
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaPreferences::storeBackupOfProjectFiles() const
{
return m_storeBackupOfProjectFile();
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions ApplicationLibCode/Application/RiaPreferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class RiaPreferences : public caf::PdmObject
QString loggerFilename() const;
int loggerFlushInterval() const;
bool loggerTrapSignalAndFlush() const;
bool storeBackupOfProjectFiles() const;

RiaPreferencesGeoMech* geoMechPreferences() const;
RiaPreferencesSummary* summaryPreferences() const;
Expand Down Expand Up @@ -212,6 +213,8 @@ class RiaPreferences : public caf::PdmObject
caf::PdmField<int> m_loggerFlushInterval;
caf::PdmField<bool> m_loggerTrapSignalAndFlush;

caf::PdmField<bool> m_storeBackupOfProjectFile;

// Surface Import
caf::PdmField<double> m_surfaceImportResamplingDistance;

Expand Down
2 changes: 2 additions & 0 deletions ApplicationLibCode/Application/Tools/CMakeLists_files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ set(SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RiaNumericalTools.h
${CMAKE_CURRENT_LIST_DIR}/RiaRegressionTextTools.h
${CMAKE_CURRENT_LIST_DIR}/RiaFileLogger.h
${CMAKE_CURRENT_LIST_DIR}/RiaProjectBackupTools.h
)

set(SOURCE_GROUP_SOURCE_FILES
Expand Down Expand Up @@ -105,6 +106,7 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RiaNumericalTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaRegressionTextTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaFileLogger.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaProjectBackupTools.cpp
)

list(APPEND CODE_SOURCE_FILES ${SOURCE_GROUP_SOURCE_FILES})
Expand Down
90 changes: 90 additions & 0 deletions ApplicationLibCode/Application/Tools/RiaProjectBackupTools.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////

#include "RiaProjectBackupTools.h"
#include "RiaLogging.h"

#include <QDateTime>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QVariant>

namespace RiaProjectBackupTools
{

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool createTableIfNeeded()
{
QSqlQuery query;
if ( !query.exec( "CREATE TABLE IF NOT EXISTS file_versions ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"timestamp DATETIME,"
"content TEXT)" ) )
{
QString txt = "Error creating table:" + query.lastError().text();
RiaLogging::error( txt );
return false;
}

return true;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool insertContent( const QString& content )
{
QSqlQuery query;
query.prepare( "INSERT INTO file_versions (timestamp, content) "
"VALUES (:timestamp, :content)" );
query.bindValue( ":timestamp", QDateTime::currentDateTime() );
query.bindValue( ":content", content );
if ( !query.exec() )
{
QString txt = "Error saving file content to database:" + query.lastError().text();
RiaLogging::error( txt );
return false;
}

return true;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool appendTextToDatabase( const QString& databaseFilePath, const QString& content )
{
QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
db.setDatabaseName( databaseFilePath );
if ( !db.open() )
{
QString txt = "Error opening database:" + db.lastError().text();
RiaLogging::error( txt );
return false;
}

if ( !createTableIfNeeded() ) return false;
if ( !insertContent( content ) ) return false;

return true;
}

} // namespace RiaProjectBackupTools
29 changes: 29 additions & 0 deletions ApplicationLibCode/Application/Tools/RiaProjectBackupTools.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////

#pragma once

#include <QString>

//==================================================================================================
//
//==================================================================================================
namespace RiaProjectBackupTools
{
bool appendTextToDatabase( const QString& databaseFilePath, const QString& content );
}
2 changes: 2 additions & 0 deletions ApplicationLibCode/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ if(Qt5Core_FOUND)
Concurrent
PrintSupport
Svg
Sql
OPTIONAL_COMPONENTS Charts
)
set(QT_LIBRARIES
Expand All @@ -49,6 +50,7 @@ if(Qt5Core_FOUND)
Qt5::Concurrent
Qt5::PrintSupport
Qt5::Svg
Qt5::Sql
magnesj marked this conversation as resolved.
Show resolved Hide resolved
)
if(Qt5Charts_FOUND)
list(APPEND QT_LIBRARIES Qt5::Charts)
Expand Down
18 changes: 16 additions & 2 deletions ApplicationLibCode/ProjectDataModel/RimProject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "RiaFieldHandleTools.h"
#include "RiaFilePathTools.h"
#include "RiaGuiApplication.h"
#include "RiaPreferences.h"
#include "RiaProjectBackupTools.h"
#include "RiaProjectFileVersionTools.h"
#include "RiaTextStringTools.h"
#include "RiaVersionInfo.h"
Expand Down Expand Up @@ -405,10 +407,22 @@ RimMainPlotCollection* RimProject::mainPlotCollection() const
bool RimProject::writeProjectFile()
{
transferPathsToGlobalPathList();
bool couldOpenFile = writeFile();

QFile xmlFile( fileName );
if ( !xmlFile.open( QIODevice::WriteOnly | QIODevice::Text ) ) return false;

QString content = documentAsString();
xmlFile.write( content.toUtf8() );

if ( RiaPreferences::current()->storeBackupOfProjectFiles() )
{
QString backupFilename = fileName + "db";
RiaProjectBackupTools::appendTextToDatabase( backupFilename, content );
}

distributePathsFromGlobalPathList();

return couldOpenFile;
return true;
}

//--------------------------------------------------------------------------------------------------
Expand Down
38 changes: 31 additions & 7 deletions Fwk/AppFwk/cafProjectDataModel/cafPdmDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,46 @@ bool PdmDocument::writeFile()
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmDocument::writeFile( QIODevice* xmlFile )
auto writeDocumentToXmlStream = []( QXmlStreamWriter& xmlStream, const PdmDocument& document )
magnesj marked this conversation as resolved.
Show resolved Hide resolved
{
// Ask all objects to make them ready to write themselves to file
setupBeforeSaveRecursively();

QXmlStreamWriter xmlStream( xmlFile );
xmlStream.setAutoFormatting( true );

xmlStream.writeStartDocument();
QString className = classKeyword();
QString className = document.classKeyword();

xmlStream.writeStartElement( "", className );
writeFields( xmlStream );
document.writeFields( xmlStream );
xmlStream.writeEndElement();

xmlStream.writeEndDocument();
};

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmDocument::writeFile( QIODevice* xmlFile )
{
// Ask all objects to make them ready to write themselves to file
setupBeforeSaveRecursively();

QXmlStreamWriter xmlStream( xmlFile );
writeDocumentToXmlStream( xmlStream, *this );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString PdmDocument::documentAsString()
{
// Ask all objects to make them ready to write themselves to file
setupBeforeSaveRecursively();

QString content;

QXmlStreamWriter xmlStream( &content );
writeDocumentToXmlStream( xmlStream, *this );

return content;
}

//--------------------------------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions Fwk/AppFwk/cafProjectDataModel/cafPdmDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class PdmDocument : public PdmObject
void readFile();
bool writeFile();

QString documentAsString();

void readFile( QIODevice* device );
void writeFile( QIODevice* device );

Expand Down