KItinerary

reservationconverter.cpp
1/*
2 SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "reservationconverter.h"
7#include "json/jsonldfilterengine.h"
8
9#include <QJsonObject>
10
11using namespace Qt::Literals::StringLiterals;
12using namespace KItinerary;
13
14static constexpr const JsonLdFilterEngine::TypeMapping train_to_bus_type_map[] = {
15 { "TrainReservation", "BusReservation" },
16 { "TrainStation", "BusStation" },
17 { "TrainTrip", "BusTrip" },
18};
19
20static constexpr const JsonLdFilterEngine::PropertyMapping train_to_bus_property_map[] = {
21 { "BusTrip", "arrivalStation", "arrivalBusStop" },
22 { "BusTrip", "departureStation", "departureBusStop" },
23 { "BusTrip", "trainName", "busName" },
24 { "BusTrip", "trainNumber", "busNumber" },
25};
26
28{
29 QJsonObject obj = trainRes;
30 JsonLdFilterEngine filterEngine;
31 filterEngine.setTypeMappings(train_to_bus_type_map);
32 filterEngine.setPropertyMappings(train_to_bus_property_map);
33 filterEngine.filterRecursive(obj);
34 return obj;
35}
36
37static constexpr const JsonLdFilterEngine::TypeMapping bus_to_train_type_map[] = {
38 { "BusReservation", "TrainReservation" },
39 { "BusStation", "TrainStation" },
40 { "BusStop", "TrainStation" },
41 { "BusTrip", "TrainTrip" },
42};
43
44static constexpr const JsonLdFilterEngine::PropertyMapping bus_to_train_property_map[] = {
45 { "TrainTrip", "arrivalBusStop", "arrivalStation" },
46 { "TrainTrip", "departureBusStop", "departureStation" },
47 { "TrainTrip", "busCompany", "provider" },
48 { "TrainTrip", "busName", "trainName" },
49 { "TrainTrip", "busNumber", "trainNumber" },
50};
51
53{
54 QJsonObject obj = busRes;
55 JsonLdFilterEngine filterEngine;
56 filterEngine.setTypeMappings(bus_to_train_type_map);
57 filterEngine.setPropertyMappings(bus_to_train_property_map);
58 filterEngine.filterRecursive(obj);
59 return obj;
60}
61
62static constexpr const JsonLdFilterEngine::TypeMapping flight_to_train_type_map[] = {
63 { "Airline", "Organization" },
64 { "Airport", "TrainStation" },
65 { "Flight", "TrainTrip" },
66 { "FlightReservation", "TrainReservation" },
67};
68
69static constexpr const JsonLdFilterEngine::PropertyMapping flight_to_train_property_map[] = {
70 { "Organization", "iataCode", "identifier" },
71 { "TrainStation", "iataCode", "identifier" },
72 { "TrainTrip", "airline", "provider" },
73 { "TrainTrip", "arrivalAirport", "arrivalStation" },
74 { "TrainTrip", "departureAirport", "departureStation" },
75};
76
77static void addIataIdentifierPrefix(QJsonObject &obj, QLatin1StringView key)
78{
79 auto o = obj.value(key).toObject();
80 if (const auto id = o.value("identifier"_L1).toString(); !id.isEmpty()) {
81 o["identifier"_L1] = QString("iata:"_L1 + id);
82 obj[key] = o;
83 }
84}
85
87{
88 QJsonObject obj = flightRes;
89 JsonLdFilterEngine filterEngine;
90 filterEngine.setTypeMappings(flight_to_train_type_map);
91 filterEngine.setPropertyMappings(flight_to_train_property_map);
92 filterEngine.filterRecursive(obj);
93
94 // changes that also need proeprty value adjustments
95 auto flight = obj.value("reservationFor"_L1).toObject();
96 addIataIdentifierPrefix(flight, "provider"_L1);
97 addIataIdentifierPrefix(flight, "departureStation"_L1);
98 addIataIdentifierPrefix(flight, "arrivalStation"_L1);
99 obj["reservationFor"_L1] = flight;
100
101 return obj;
102}
JSON-LD filtering for input normalization or type transforms.
void filterRecursive(QJsonObject &obj)
Recursively apply filtering rules to obj.
QJsonObject trainToBus(const QJsonObject &trainRes)
Convert a train reservation to a bus reservation.
QJsonObject flightToTrain(const QJsonObject &flightRes)
Convert a flight reservation to a train reservation.
QJsonObject busToTrain(const QJsonObject &busRes)
Convert a bus reservation to a train reservation.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
QJsonValue value(QLatin1StringView key) const const
QJsonObject toObject() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:28:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.