Commit b497294a authored by Volker Krause's avatar Volker Krause

Port trip grouping to the new multi-traveler batching

parent 98256b1c
......@@ -248,6 +248,8 @@ private Q_SLOTS:
QCOMPARE(mgr.reservationsForBatch(batchId).size(), 2);
QCOMPARE(mgr.batchForReservation(batchId), batchId);
QCOMPARE(mgr.batchForReservation(secId), batchId);
QCOMPARE(mgr.hasBatch(batchId), true);
QCOMPARE(mgr.hasBatch(secId), false);
// changing primary does update batch
auto res2 = mgr.reservation(batchId).value<FlightReservation>();
......
......@@ -35,9 +35,11 @@ class TripGroupProxyTest : public QObject
private:
void clearReservations(ReservationManager *mgr)
{
for (const auto &id : mgr->reservations()) {
mgr->removeReservation(id);
const auto batches = mgr->batches(); // copy, as this is getting modified in the process
for (const auto &id : batches) {
mgr->removeBatch(id);
}
QCOMPARE(mgr->batches().size(), 0);
}
QByteArray readFile(const QString &fn)
......
......@@ -29,9 +29,11 @@ class TripGroupTest : public QObject
private:
void clearReservations(ReservationManager *mgr)
{
for (const auto &id : mgr->reservations()) {
mgr->removeReservation(id);
const auto batches = mgr->batches(); // copy, as this is getting modified in the process
for (const auto &id : batches) {
mgr->removeBatch(id);
}
QCOMPARE(mgr->batches().size(), 0);
}
QByteArray readFile(const QString &fn)
......@@ -65,7 +67,7 @@ private Q_SLOTS:
mgr.setReservationManager(&resMgr);
QCOMPARE(addSpy.size(), 1);
auto g = mgr.tripGroup(addSpy.at(0).at(0).toString());
QCOMPARE(g.elements().size(), resMgr.reservations().size());
QCOMPARE(g.elements().size(), resMgr.batches().size());
}
TripGroupManager::clear();
......@@ -78,7 +80,7 @@ private Q_SLOTS:
mgr.setReservationManager(&resMgr);
QCOMPARE(addSpy.size(), 1);
auto g = mgr.tripGroup(addSpy.at(0).at(0).toString());
QCOMPARE(g.elements().size(), resMgr.reservations().size());
QCOMPARE(g.elements().size(), resMgr.batches().size());
}
TripGroupManager::clear();
......@@ -91,7 +93,7 @@ private Q_SLOTS:
mgr.setReservationManager(&resMgr);
QCOMPARE(addSpy.size(), 1);
auto g = mgr.tripGroup(addSpy.at(0).at(0).toString());
QCOMPARE(g.elements().size(), resMgr.reservations().size());
QCOMPARE(g.elements().size(), resMgr.batches().size());
}
TripGroupManager::clear();
......@@ -103,8 +105,8 @@ private Q_SLOTS:
QVERIFY(addSpy.isValid());
mgr.setReservationManager (&resMgr);
QCOMPARE(addSpy.size(), 3);
QCOMPARE(mgr.tripGroup(addSpy.at(0).at(0).toString()).elements().size(), 4);
QCOMPARE(mgr.tripGroup(addSpy.at(1).at(0).toString()).elements().size(), 4);
QCOMPARE(mgr.tripGroup(addSpy.at(0).at(0).toString()).elements().size(), 2);
QCOMPARE(mgr.tripGroup(addSpy.at(1).at(0).toString()).elements().size(), 2);
QCOMPARE(mgr.tripGroup(addSpy.at(2).at(0).toString()).elements().size(), 12);
}
}
......@@ -123,13 +125,13 @@ private Q_SLOTS:
resMgr.importReservation(readFile(QLatin1String(SOURCE_DIR "/data/google-multi-passenger-flight.json")));
QCOMPARE(addSpy.size(), 1);
auto g = mgr.tripGroup(addSpy.at(0).at(0).toString());
QCOMPARE(g.elements().size(), resMgr.reservations().size());
QCOMPARE(changeSpy.size(), 1);
QCOMPARE(g.elements().size(), resMgr.batches().size());
QCOMPARE(changeSpy.size(), 0);
changeSpy.clear();
clearReservations(&resMgr);
QCOMPARE(rmSpy.size(), 1);
QCOMPARE(changeSpy.size(), 2);
QCOMPARE(changeSpy.size(), 0);
}
void testGroupName_data()
......@@ -159,7 +161,7 @@ private Q_SLOTS:
mgr.setReservationManager(&resMgr);
QCOMPARE(addSpy.size(), 1);
auto g = mgr.tripGroup(addSpy.at(0).at(0).toString());
QCOMPARE(g.elements().size(), resMgr.reservations().size());
QCOMPARE(g.elements().size(), resMgr.batches().size());
QCOMPARE(g.name(), expectedName);
}
......@@ -173,7 +175,7 @@ private Q_SLOTS:
mgr.setReservationManager(&resMgr);
QCOMPARE(addSpy.size(), 1);
auto g = mgr.tripGroup(addSpy.at(0).at(0).toString());
QCOMPARE(g.elements().size(), resMgr.reservations().size() - 1);
QCOMPARE(g.elements().size(), resMgr.batches().size() - 1);
QCOMPARE(g.name(), QStringLiteral("Oslo Airport (June 2000)"));
}
};
......
......@@ -68,13 +68,9 @@ void ReservationManager::setPkPassManager(PkPassManager* mgr)
connect(mgr, &PkPassManager::passRemoved, this, &ReservationManager::passRemoved);
}
bool ReservationManager::hasReservation(const QString &id) const
bool ReservationManager::hasBatch(const QString &batchId) const
{
if (m_reservations.contains(id)) {
return true;
}
return QFile::exists(reservationsBasePath() + id + QLatin1String(".jsonld"));
return m_batchToResMap.contains(batchId);
}
QVector<QString> ReservationManager::reservations() const
......
......@@ -47,7 +47,6 @@ public:
void setPkPassManager(PkPassManager *mgr);
bool hasReservation(const QString &id) const;
QVector<QString> reservations() const;
Q_INVOKABLE QVariant reservation(const QString &id) const;
......@@ -60,6 +59,7 @@ public:
void importReservations(const QVector<QVariant> &resData);
const std::vector<QString>& batches() const;
bool hasBatch(const QString &batchId) const;
QString batchForReservation(const QString &resId) const;
Q_INVOKABLE QStringList reservationsForBatch(const QString &batchId) const;
Q_INVOKABLE void removeBatch(const QString &batchId);
......
......@@ -21,7 +21,6 @@
#include "reservationmanager.h"
#include <KItinerary/LocationUtil>
#include <KItinerary/MergeUtil>
#include <KItinerary/Organization>
#include <KItinerary/Reservation>
#include <KItinerary/SortUtil>
......@@ -53,11 +52,12 @@ TripGroupManager::~TripGroupManager() = default;
void TripGroupManager::setReservationManager(ReservationManager *resMgr)
{
m_resMgr = resMgr;
connect(m_resMgr, &ReservationManager::reservationAdded, this, &TripGroupManager::reservationAdded);
connect(m_resMgr, &ReservationManager::reservationUpdated, this, &TripGroupManager::reservationChanged);
connect(m_resMgr, &ReservationManager::reservationRemoved, this, &TripGroupManager::reservationRemoved);
connect(m_resMgr, &ReservationManager::batchAdded, this, &TripGroupManager::batchAdded);
connect(m_resMgr, &ReservationManager::batchContentChanged, this, &TripGroupManager::batchContentChanged);
connect(m_resMgr, &ReservationManager::batchRemoved, this, &TripGroupManager::batchRemoved);
connect(m_resMgr, &ReservationManager::batchRenamed, this, &TripGroupManager::batchRenamed);
const auto allReservations = m_resMgr->reservations();
const auto allReservations = m_resMgr->batches();
m_reservations.clear();
m_reservations.reserve(allReservations.size());
std::copy(allReservations.begin(), allReservations.end(), std::back_inserter(m_reservations));
......@@ -121,6 +121,10 @@ void TripGroupManager::load()
void TripGroupManager::removeTripGroup(const QString &groupId)
{
const auto groupIt = m_tripGroups.constFind(groupId);
if (groupIt == m_tripGroups.constEnd()) {
return;
}
for (const auto &elem : groupIt.value().elements()) {
m_reservationToGroupMap.remove(elem);
}
......@@ -138,7 +142,7 @@ void TripGroupManager::clear()
d.removeRecursively();
}
void TripGroupManager::reservationAdded(const QString &resId)
void TripGroupManager::batchAdded(const QString &resId)
{
auto it = std::lower_bound(m_reservations.begin(), m_reservations.end(), resId, [this](const auto &lhs, const auto &rhs) {
return SortUtil::isBefore(m_resMgr->reservation(lhs), m_resMgr->reservation(rhs));
......@@ -148,14 +152,21 @@ void TripGroupManager::reservationAdded(const QString &resId)
scanAll();
}
void TripGroupManager::reservationChanged(const QString &resId)
void TripGroupManager::batchContentChanged(const QString &resId)
{
// ### we can probably make this more efficient
reservationRemoved(resId);
reservationAdded(resId);
batchRemoved(resId);
batchAdded(resId);
}
void TripGroupManager::batchRenamed(const QString &oldBatchId, const QString &newBatchId)
{
// ### this can be done more efficiently
batchRemoved(oldBatchId);
batchAdded(newBatchId);
}
void TripGroupManager::reservationRemoved(const QString &resId)
void TripGroupManager::batchRemoved(const QString &resId)
{
// check if resId is part of a group
const auto mapIt = m_reservationToGroupMap.constFind(resId);
......@@ -242,22 +253,6 @@ void TripGroupManager::scanOne(std::vector<QString>::const_iterator beginIt)
}
res = curRes;
// is this a multi-traveler element? -> continue searching
Q_ASSERT(JsonLd::canConvert<Reservation>(prevRes));
Q_ASSERT(JsonLd::canConvert<Reservation>(res));
const auto prevTrip = JsonLd::convert<Reservation>(prevRes).reservationFor();
const auto curTrip = JsonLd::convert<Reservation>(res).reservationFor();
if (MergeUtil::isSame(prevTrip, curTrip)) {
// if the result iterators are on it -1 we should advanced them here too
if (connectedIt == it - 1) {
++connectedIt;
}
if (resNumIt == it - 1) {
++resNumIt;
}
continue;
}
// all search strategies think they are done
if (resNumSearchDone && connectedSearchDone) {
break;
......@@ -410,7 +405,7 @@ void TripGroupManager::checkConsistency()
// look for dangling reservation references
for (auto it = m_reservationToGroupMap.constBegin(); it != m_reservationToGroupMap.constEnd(); ++it) {
if (!m_resMgr->hasReservation(it.key())) {
if (!m_resMgr->hasBatch(it.key())) {
tgIds.push_back(it.value());
}
}
......
......@@ -25,7 +25,9 @@ class ReservationManager;
class TripGroup;
class TripGroupTest;
/** Trip group computation and persistence. */
/** Trip group computation and persistence.
* This operates on multi-traveller batches as provided by ReservationManager.
*/
class TripGroupManager : public QObject
{
Q_OBJECT
......@@ -53,9 +55,10 @@ private:
void load();
void removeTripGroup(const QString &groupId);
void reservationAdded(const QString &resId);
void reservationChanged(const QString &resId);
void reservationRemoved(const QString &resId);
void batchAdded(const QString &resId);
void batchContentChanged(const QString &resId);
void batchRenamed(const QString &oldBatchId, const QString &newBatchId);
void batchRemoved(const QString &resId);
void scanAll();
void scanOne(std::vector<QString>::const_iterator beginIt);
......
Markdown is supported
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