Commit b7934e86 authored by Volker Krause's avatar Volker Krause
Browse files

Use the new extractor result validation API

This now makes it explicit which types we can display, and it's the first
step towards merging minimal cancellation elements with existing data in
the calendar.
parent 2d0e8419
......@@ -87,7 +87,7 @@ set(AKONADICONTACT_LIB_VERSION "5.14.40")
set(IMPORTWIZARD_LIB_VERSION "5.14.40")
set(MAILIMPORTER_LIB_VERSION "5.14.40")
set(KPIMPKPASS_LIB_VERSION "5.14.40")
set(KPIMITINERARY_LIB_VERSION "5.14.40")
set(KPIMITINERARY_LIB_VERSION "5.14.41")
set(KONTACTINTERFACE_LIB_VERSION "5.14.40")
set(CALENDARSUPPORT_LIB_VERSION_LIB "5.14.40")
find_package(KF5CalendarCore ${KF5_MIN_VERSION} CONFIG REQUIRED)
......
......@@ -22,6 +22,7 @@
#include <CalendarSupport/CalendarSingleton>
#include <KItinerary/CalendarHandler>
#include <KItinerary/ExtractorValidator>
#include <KItinerary/Flight>
#include <KItinerary/JsonLdDocument>
#include <KItinerary/MergeUtil>
......@@ -34,6 +35,11 @@
using namespace KItinerary;
ItineraryMemento::ItineraryMemento()
{
m_postProc.setValidationEnabled(false);
}
void ItineraryMemento::detach()
{
}
......@@ -71,11 +77,29 @@ bool ItineraryMemento::hasData() const
QVector<ItineraryMemento::TripData> ItineraryMemento::data()
{
if (m_data.isEmpty() && !m_postProc.result().isEmpty()) {
// filter out types we can't handle, but keep incomplete elements to see if we can complete them from the calendar
ExtractorValidator validator;
validator.setAcceptedTypes<
BusReservation,
EventReservation,
FlightReservation,
FoodEstablishmentReservation,
LodgingReservation,
RentalCarReservation,
TaxiReservation,
TrainReservation
>();
validator.setAcceptOnlyCompleteElements(false);
auto postProcResult = m_postProc.result();
postProcResult.erase(std::remove_if(postProcResult.begin(), postProcResult.end(), [&validator](const auto &elem) {
return !validator.isValidElement(elem);
}), postProcResult.end());
// perform calendar lookup and merge results
std::vector<std::pair<QVariant, KCalendarCore::Event::Ptr> > resolvedEvents;
resolvedEvents.reserve(m_postProc.result().size());
resolvedEvents.reserve(postProcResult.size());
const auto calendar = CalendarSupport::calendarSingleton(!qEnvironmentVariableIsSet("BPF_ITINERARY_TESTMODE"));
for (const auto &r : m_postProc.result()) {
for (const auto &r : qAsConst(postProcResult)) {
auto e = std::make_pair(r, CalendarHandler::findEvent(calendar, r));
if (e.second) {
const auto existingRes = CalendarHandler::reservationsForEvent(e.second);
......@@ -89,6 +113,12 @@ QVector<ItineraryMemento::TripData> ItineraryMemento::data()
resolvedEvents.push_back(e);
}
// discard elemnents we couldn't complete from the calendar
validator.setAcceptOnlyCompleteElements(true);
resolvedEvents.erase(std::remove_if(resolvedEvents.begin(), resolvedEvents.end(), [&validator](const auto &p) {
return !validator.isValidElement(p.first);
}), resolvedEvents.end());
// merge multi-traveler elements
for (auto it = resolvedEvents.begin(); it != resolvedEvents.end();) {
TripData data;
......
......@@ -45,6 +45,7 @@ class QDateTime;
class ItineraryMemento : public MimeTreeParser::Interface::BodyPartMemento
{
public:
ItineraryMemento();
~ItineraryMemento() override = default;
void detach() override;
......
......@@ -129,19 +129,12 @@ MimeTreeParser::MessagePart::Ptr ItineraryProcessor::process(MimeTreeParser::Int
//qCDebug(ITINERARY_LOG).noquote() << QJsonDocument(data).toJson();
auto decodedData = JsonLdDocument::fromJson(data);
for (auto it = decodedData.begin(); it != decodedData.end();) {
for (auto it = decodedData.begin(); it != decodedData.end(); ++it) {
if (JsonLd::isA<Event>(*it)) { // promote Event to EventReservation
EventReservation res;
res.setReservationFor(*it);
*it = res;
}
// filter out non-Reservation objects we can't display
if (!JsonLd::canConvert<Reservation>(*it)) {
it = decodedData.erase(it);
continue;
}
++it;
}
if (!decodedData.isEmpty()) {
......
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