Files
qt5-legatus-legacy/Lts/Services/OcBaseDownload/pdf_downloader.cpp
T
2026-08-13 18:33:10 +02:00

192 lines
5.3 KiB
C++
Executable File

/**
* @RDE_HEADER@
* $Id$
* $HeadURL$
* $Author$
* Created by Hannes Fritsche, NLE, 2019-04-30
*/
#include "pdf_downloader.h"
#include "pdf_download_socket.h"
#include "main_handler.h"
#include <nglsettings/nglsettings_settings.h>
#include <ngldb/dbconnection.h>
#include <ngldb/dbtransaction.h>
#include <ngldb/db_service_handler.h>
#include <ngltools/ngltools_filesystem.h>
#include <QJsonDocument>
namespace OcBaseDownload
{
PdfDownloader::PdfDownloader(const QVector<WebSocketInterface::Messages::PdfListMessage::PdfInformation> &pdfInfoVec, MainHandler *mainHandler, const QString &hostName, int exerciseId, QObject *parent)
: QObject(parent)
, m_pdfInfoVec(pdfInfoVec)
, m_socket(new PdfDownloadSocket(hostName, this))
, m_mainHandler(mainHandler)
, m_exerciseId(exerciseId)
, m_exTblGen(new DBAdministration::ExerciseTableGenerator(getLogger("ExerciseTableGenerator"), NGL::directory(NGL::FileType::ConfigFile, QLatin1String("xml")), this))
, m_downloadCount(0)
, m_totalDownloads(pdfInfoVec.size())
, m_logger(getLogger("PdfDownloader"))
{
connect(m_socket, &PdfDownloadSocket::downloadFinished,
this, &PdfDownloader::onSingleDownloadFinished);
}
PdfDownloader::~PdfDownloader()
{
}
bool PdfDownloader::startDownload()
{
DEBUG_LOG(m_logger) << "Downloading startet";
if (!m_socket->isConnected())
{
ERROR_LOG(m_logger) << "Download could not be startet. Socket not connected.";
return false;
}
if (!openDbConnection())
{
ERROR_LOG(m_logger) << "Could not open database connection";
return false;
}
return startNextDownload();
}
void PdfDownloader::onSingleDownloadFinished(const QByteArray &data)
{
DEBUG_LOG(m_logger) << "Downloading file " << m_currentDownload.name << " finished.";
++m_downloadCount;
if (!data.size())
WARNING_LOG(m_logger) << "Got finished download without data";
QString msg = QString::fromUtf8(data);
if (msg.startsWith(QStringLiteral("ERROR")))
{
ERROR_LOG(m_logger) << msg;
startNextDownload();
return;
}
if (!m_dbConnection.isValid())
{
ERROR_LOG(m_logger) << "Database not open";
return;
}
auto flmDbHandler = std::unique_ptr<NGLDB::DbServiceHandler<FileManagement::DatabaseInterface>>(new NGLDB::DbServiceHandler<FileManagement::DatabaseInterface>());
if (!flmDbHandler->init(0, m_dbConnection))
{
ERROR_LOG(m_logger) << "Couldn't initialise flmDbHandler";
return;
}
DBTransaction trans(m_dbConnection);
FileManagement::Database::FlmFileContent dbData;
dbData.name = m_currentDownload.name;
dbData.isTemplate = m_currentDownload.isTemplate;
dbData.folderId = m_currentDownload.folderId;
dbData.fileCategoryId = m_currentDownload.fileCategory;
dbData.fileTypeId = FileManagement::FileType::Pdf;
dbData.fileStatusId = m_currentDownload.fileStatus;
if (m_currentDownload.exerciseId)
dbData.exerciseId = m_currentDownload.exerciseId;
if (m_currentDownload.phaseId)
dbData.phaseId = m_currentDownload.phaseId;
dbData.operatorId = m_currentDownload.operatorId;
dbData.content = data;
dbData.properties = QString::fromLatin1(QJsonDocument(m_currentDownload.properties.toObject()).toJson(QJsonDocument::Compact));
flmDbHandler->db()->insert(dbData);
if (!trans.commit())
ERROR_LOG(m_logger) << "Error while inserting FLM data into database.";
startNextDownload();
}
bool PdfDownloader::startNextDownload()
{
if (m_pdfInfoVec.isEmpty())
{
DEBUG_LOG(m_logger) << "No more pending downloads.";
closeDb(m_dbConnection);
emit allDownloadsFinished();
return false;
}
m_currentDownload = m_pdfInfoVec.takeFirst();
emit newState(100 / m_totalDownloads * m_downloadCount, tr("Downloading file \"%1\"").arg(m_currentDownload.name));
DEBUG_LOG(m_logger) << "Downloading file " << m_currentDownload.name;
m_socket->downloadFromHost(m_currentDownload.id, m_currentDownload.fileSize);
return true;
}
bool PdfDownloader::openDbConnection()
{
if (m_dbConnection.isValid())
{
m_exTblGen->close();
closeDb(m_dbConnection);
}
bool bOk;
m_dbConnection = openDb(&bOk);
if (!bOk)
return false;
return m_exTblGen->init(m_dbConnection, QStringLiteral("exm"));
}
QSqlDatabase PdfDownloader::openDb(bool *bOk)
{
if (bOk)
*bOk = true;
// open postgres db, read table structure, read static data
auto s = Settings::settings();
const QString dbUser(QStringLiteral("ets_flm"));
const QString dbPasswd(QStringLiteral("ets11de"));
QSqlDatabase dbConnection = DBConnection::open(m_logger,
dbUser,
dbPasswd,
s->databaseDriver(),
s->databaseHostname(),
s->databaseName());
DEBUG_LOG(m_logger) << "Trying to open database " << s->databaseHostname() << ":" << s->databaseName();
if (!dbConnection.isOpen())
{
ERROR_LOG(m_logger) << "Error opening database " << s->databaseHostname() << ":" << s->databaseName();
if (bOk)
*bOk = false;
}
return dbConnection;
}
void PdfDownloader::closeDb(QSqlDatabase &dbConn)
{
const QString connName = dbConn.connectionName();
DEBUG_LOG(m_logger) << "Closing database: " << connName;
dbConn.close();
dbConn = QSqlDatabase();
}
}