diff --git a/autotests/postprocessordata/train-unsorted.post.json b/autotests/postprocessordata/train-unsorted.post.json index 3293008..1a4ee86 100644 --- a/autotests/postprocessordata/train-unsorted.post.json +++ b/autotests/postprocessordata/train-unsorted.post.json @@ -1,82 +1,90 @@ [ { "@context": "http://schema.org", "@type": "TrainReservation", "reservationFor": { "@type": "TrainTrip", "arrivalStation": { "@type": "TrainStation", "geo": { "@type": "GeoCoordinates", "latitude": 43.83229064941406, "longitude": 4.365845203399658 }, "name": "Nîmes Gare" }, "arrivalTime": "2017-09-24T20:33:00+02:00", "departureStation": { "@type": "TrainStation", "geo": { "@type": "GeoCoordinates", "latitude": 45.76055908203125, "longitude": 4.8593549728393555 }, "name": "Lyon Part-Dieu" }, "departureTime": "2017-09-24T19:09:00+02:00", + "provider": { + "@type": "Organization", + "name": "SNCF" + }, "trainName": "TGV", "trainNumber": "5119" }, "reservationNumber": "XXX007", "reservedTicket": { "@type": "Ticket", "ticketToken": "aztecCode:somerandomdata DOE JOHN111110 00000", "ticketedSeat": { "@type": "Seat", "seatNumber": "71", "seatSection": "13" } }, "url": "https://www.trainline.fr/tickets" }, { "@context": "http://schema.org", "@type": "TrainReservation", "reservationFor": { "@type": "TrainTrip", "arrivalStation": { "@type": "TrainStation", "geo": { "@type": "GeoCoordinates", "latitude": 45.76055908203125, "longitude": 4.8593549728393555 }, "name": "Lyon Part-Dieu" }, "arrivalTime": "2017-09-29T19:52:00+02:00", "departureStation": { "@type": "TrainStation", "geo": { "@type": "GeoCoordinates", "latitude": 43.83229064941406, "longitude": 4.365845203399658 }, "name": "Nîmes Gare" }, "departureTime": "2017-09-29T18:26:00+02:00", + "provider": { + "@type": "Organization", + "name": "SNCF" + }, "trainName": "TGV", "trainNumber": "5186" }, "reservationNumber": "XXX007", "reservedTicket": { "@type": "Ticket", "ticketToken": "aztecCode:somerandomdata DOE JOHN111110 00000", "ticketedSeat": { "@type": "Seat", "seatNumber": "62", "seatSection": "17" } }, "url": "https://www.trainline.fr/tickets" } ] diff --git a/src/jsonldimportfilter.cpp b/src/jsonldimportfilter.cpp index 701999e..004105a 100644 --- a/src/jsonldimportfilter.cpp +++ b/src/jsonldimportfilter.cpp @@ -1,48 +1,70 @@ /* Copyright (C) 2018 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "jsonldimportfilter.h" #include using namespace KItinerary; -static QJsonObject filterReservation(QJsonObject res) +static void filterTrainTrip(QJsonObject &trip) +{ + if (trip.value(QLatin1String("@type")).toString() != QLatin1String("TrainTrip")) { + return; + } + + // move TrainTrip::trainCompany to TrainTrip::provider (as defined by schema.org) + const auto company = trip.value(QLatin1String("trainCompany")).toObject(); + if (!company.isEmpty() && !trip.contains(QLatin1String("provider"))) { + trip.insert(QLatin1String("provider"), company); + } +} + +static void filterReservation(QJsonObject &res) { // move ticketToken to Ticket (Google vs. schema.org difference) const auto token = res.value(QLatin1String("ticketToken")).toString(); if (!token.isEmpty()) { auto ticket = res.value(QLatin1String("reservedTicket")).toObject(); if (ticket.isEmpty()) { ticket.insert(QLatin1String("@type"), QLatin1String("Ticket")); } if (!ticket.contains(QLatin1String("ticketToken"))) { ticket.insert(QLatin1String("ticketToken"), token); res.insert(QLatin1String("reservedTicket"), ticket); } } - - return res; } QJsonObject JsonLdImportFilter::filterObject(const QJsonObject& obj) { - if (obj.value(QLatin1String("@type")).toString().endsWith(QLatin1String("Reservation"))) { - return filterReservation(obj); + QJsonObject res(obj); + const auto type = obj.value(QLatin1String("@type")).toString(); + if (type.endsWith(QLatin1String("Reservation"))) { + filterReservation(res); + } + + if (type == QLatin1String("TrainReservation")) { + auto train = obj.value(QLatin1String("reservationFor")).toObject(); + filterTrainTrip(train); + if (!train.isEmpty()) { + res.insert(QLatin1String("reservationFor"), train); + } } - return obj; + + return res; }