Commit 84b4aff7 authored by Volker Krause's avatar Volker Krause
Browse files

Deduplicate/merge passes on import

Makes the behavior match that of importing reservation elements.
parent 957b3fa1
Pipeline #192471 passed with stage
in 3 minutes and 6 seconds
......@@ -41,6 +41,8 @@ private Q_SLOTS:
// test import
QVERIFY(mgr.import(JsonLdDocument::fromJsonSingular(QJsonDocument::fromJson(Test::readFile(QStringLiteral(SOURCE_DIR "/data/bahncard.json"))).object())));
QVERIFY(mgr.import(JsonLdDocument::fromJsonSingular(QJsonDocument::fromJson(Test::readFile(QStringLiteral(SOURCE_DIR "/data/9euroticket.json"))).object())));
// duplicates get merged
QVERIFY(mgr.import(JsonLdDocument::fromJsonSingular(QJsonDocument::fromJson(Test::readFile(QStringLiteral(SOURCE_DIR "/data/9euroticket.json"))).object())));
QCOMPARE(mgr.rowCount(), 2);
// retrieval
......@@ -69,9 +71,12 @@ private Q_SLOTS:
QCOMPARE(idx.data(PassManager::ValidUntilRole).toDateTime(), QDateTime({2022, 5, 31}, {23, 59, 59}));
QVERIFY(!idx.data(PassManager::SectionRole).toString().isEmpty());
QVERIFY(!mgr.pass(passId2).isNull());
mgr.remove(passId2);
QCOMPARE(mgr.rowCount(), 1);
{
// test persistence
PassManager mgr2;
......
......@@ -9,6 +9,7 @@
#include <KItinerary/ExtractorPostprocessor>
#include <KItinerary/JsonLdDocument>
#include <KItinerary/MergeUtil>
#include <KItinerary/ProgramMembership>
#include <KItinerary/Ticket>
......@@ -86,21 +87,24 @@ int PassManager::rowCount(const QModelIndex &parent) const
bool PassManager::import(const QVariant &pass, const QString &id)
{
// check if this is an element we already have
for (auto it = m_entries.begin(); it != m_entries.end(); ++it) {
if ((!id.isEmpty() && (*it).id == id) || MergeUtil::isSame((*it).data, pass)) {
(*it).data = MergeUtil::merge((*it).data, pass);
write((*it).data, (*it).id);
const auto idx = index(std::distance(m_entries.begin(), it), 0);
Q_EMIT dataChanged(idx, idx);
return true;
}
}
if (JsonLd::isA<KItinerary::ProgramMembership>(pass) || JsonLd::isA<GenericPkPass>(pass) || JsonLd::isA<KItinerary::Ticket>(pass)) {
Entry entry;
entry.id = id.isEmpty() ? QUuid::createUuid().toString() : id;
entry.data = pass;
auto path = basePath();
QDir().mkpath(path);
path += entry.id;
QFile f(path);
if (!f.open(QFile::WriteOnly)) {
qCWarning(Log) << "Failed to open file:" << f.fileName() << f.errorString();
if (!write(entry.data, entry.id)) {
return false;
}
f.write(QJsonDocument(JsonLdDocument::toJson(entry.data)).toJson());
f.close();
const auto it = std::lower_bound(m_entries.begin(), m_entries.end(), entry, PassComparator(m_baseTime));
if (it != m_entries.end() && (*it).id == entry.id) {
......@@ -238,6 +242,20 @@ void PassManager::load()
std::sort(m_entries.begin(), m_entries.end(), PassComparator(m_baseTime));
}
bool PassManager::write(const QVariant &data, const QString &id) const
{
auto path = basePath();
QDir().mkpath(path);
path += id;
QFile f(path);
if (!f.open(QFile::WriteOnly)) {
qCWarning(Log) << "Failed to open file:" << f.fileName() << f.errorString();
return false;
}
f.write(QJsonDocument(JsonLdDocument::toJson(data)).toJson());
return true;
}
QByteArray PassManager::rawData(const Entry &entry) const
{
QFile f(basePath() + entry.id);
......
......@@ -75,6 +75,7 @@ private:
};
void load();
bool write(const QVariant &data, const QString &id) const;
QByteArray rawData(const Entry &entry) const;
static QString basePath();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment