File indexing completed on 2024-05-12 05:22:27

0001 /*
0002  * SPDX-FileCopyrightText: 2021 Daniel Vrátil <dvratil@kde.org>
0003  * SPDX-FileCopyrightText: 2022 Claudio Cambra <claudio.cambra@kde.org>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-only
0006  * SPDX-License-Identifier: LGPL-3.0-only
0007  * SPDX-License-Identifier: LicenseRef-KDE-Accepted-LGPL
0008  */
0009 
0010 #include "location.h"
0011 
0012 #include "fieldmetadata.h"
0013 #include "peopleservice.h"
0014 
0015 #include <QJsonArray>
0016 #include <QJsonObject>
0017 #include <QJsonValue>
0018 #include <QSharedData>
0019 
0020 #include <algorithm>
0021 
0022 namespace KGAPI2::People
0023 {
0024 
0025 class Location::Private : public QSharedData
0026 {
0027 public:
0028     explicit Private() = default;
0029     Private(const Private &) = default;
0030     Private(Private &&) noexcept = delete;
0031     Private &operator=(const Private &) = delete;
0032     Private &operator=(Private &&) noexcept = delete;
0033     ~Private() = default;
0034 
0035     bool operator==(const Private &other) const
0036     {
0037         return deskCode == other.deskCode && floor == other.floor && buildingId == other.buildingId && type == other.type && current == other.current
0038             && value == other.value && metadata == other.metadata && floorSection == other.floorSection;
0039     }
0040 
0041     bool operator!=(const Private &other) const
0042     {
0043         return !(*this == other);
0044     }
0045 
0046     QString deskCode{};
0047     QString floor{};
0048     QString buildingId{};
0049     QString type{};
0050     bool current{};
0051     QString value{};
0052     FieldMetadata metadata{};
0053     QString floorSection{};
0054 };
0055 
0056 Location::Location()
0057     : d(new Private)
0058 {
0059 }
0060 
0061 Location::Location(const Location &) = default;
0062 Location::Location(Location &&) noexcept = default;
0063 Location &Location::operator=(const Location &) = default;
0064 Location &Location::operator=(Location &&) noexcept = default;
0065 Location::~Location() = default;
0066 
0067 bool Location::operator==(const Location &other) const
0068 {
0069     return *d == *other.d;
0070 }
0071 
0072 bool Location::operator!=(const Location &other) const
0073 {
0074     return !(*this == other);
0075 }
0076 
0077 QString Location::deskCode() const
0078 {
0079     return d->deskCode;
0080 }
0081 
0082 void Location::setDeskCode(const QString &value)
0083 {
0084     d->deskCode = value;
0085 }
0086 QString Location::floor() const
0087 {
0088     return d->floor;
0089 }
0090 
0091 void Location::setFloor(const QString &value)
0092 {
0093     d->floor = value;
0094 }
0095 QString Location::buildingId() const
0096 {
0097     return d->buildingId;
0098 }
0099 
0100 void Location::setBuildingId(const QString &value)
0101 {
0102     d->buildingId = value;
0103 }
0104 QString Location::type() const
0105 {
0106     return d->type;
0107 }
0108 
0109 void Location::setType(const QString &value)
0110 {
0111     d->type = value;
0112 }
0113 bool Location::current() const
0114 {
0115     return d->current;
0116 }
0117 
0118 void Location::setCurrent(bool value)
0119 {
0120     d->current = value;
0121 }
0122 QString Location::value() const
0123 {
0124     return d->value;
0125 }
0126 
0127 void Location::setValue(const QString &value)
0128 {
0129     d->value = value;
0130 }
0131 FieldMetadata Location::metadata() const
0132 {
0133     return d->metadata;
0134 }
0135 
0136 void Location::setMetadata(const FieldMetadata &value)
0137 {
0138     d->metadata = value;
0139 }
0140 QString Location::floorSection() const
0141 {
0142     return d->floorSection;
0143 }
0144 
0145 void Location::setFloorSection(const QString &value)
0146 {
0147     d->floorSection = value;
0148 }
0149 
0150 Location Location::fromJSON(const QJsonObject &obj)
0151 {
0152     Location location;
0153 
0154     if(!obj.isEmpty()) {
0155         const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
0156         location.setMetadata(FieldMetadata::fromJSON(metadata));
0157         location.setValue(obj.value(QStringLiteral("value")).toString());
0158         location.setType(obj.value(QStringLiteral("type")).toString());
0159         location.setCurrent(obj.value(QStringLiteral("current")).toBool());
0160         location.setBuildingId(obj.value(QStringLiteral("buildingId")).toString());
0161         location.setFloor(obj.value(QStringLiteral("floor")).toString());
0162         location.setFloorSection(obj.value(QStringLiteral("floorSection")).toString());
0163         location.setDeskCode(obj.value(QStringLiteral("deskCode")).toString());
0164     }
0165 
0166     return location;
0167 }
0168 
0169 QList<Location> Location::fromJSONArray(const QJsonArray &data)
0170 {
0171     QList<Location> locations;
0172 
0173     for(const auto &location : data) {
0174         if(location.isObject()) {
0175             const auto objectifiedLocation = location.toObject();
0176             locations.append(fromJSON(objectifiedLocation));
0177         }
0178     }
0179 
0180     return locations;
0181 }
0182 
0183 QJsonValue Location::toJSON() const
0184 {
0185     QJsonObject obj;
0186 
0187     PeopleUtils::addValueToJsonObjectIfValid(obj, "deskCode", d->deskCode);
0188     PeopleUtils::addValueToJsonObjectIfValid(obj, "floor", d->floor);
0189     PeopleUtils::addValueToJsonObjectIfValid(obj, "buildingId", d->buildingId);
0190     PeopleUtils::addValueToJsonObjectIfValid(obj, "type", d->type);
0191     PeopleUtils::addValueToJsonObjectIfValid(obj, "current", d->current);
0192     PeopleUtils::addValueToJsonObjectIfValid(obj, "value", d->value);
0193     // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata"}, d->metadata.toJSON());
0194     PeopleUtils::addValueToJsonObjectIfValid(obj, "floorSection", d->floorSection);
0195     return obj;
0196 }
0197 
0198 } // namespace KGAPI2::People