swift
datastoreutility.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QJsonArray>
7 #include <QJsonDocument>
8 #include <QJsonObject>
9 #include <QJsonValue>
10 #include <QTimeZone>
11 
12 #include "misc/logcategories.h"
14 #include "misc/statusmessage.h"
15 #include "misc/statusmessagelist.h"
16 #include "misc/stringutils.h"
17 
18 using namespace swift::misc;
19 using namespace swift::misc::network;
20 
21 namespace swift::misc::db
22 {
23  const QStringList &CDatastoreUtility::getLogCategories()
24  {
25  static const QStringList cats({ CLogCategories::swiftDbWebservice() });
26  return cats;
27  }
28 
29  bool CDatastoreUtility::dbBoolStringToBool(const QString &dbBool) { return swift::misc::stringToBool(dbBool); }
30 
31  const QString &CDatastoreUtility::boolToDbYN(bool v)
32  {
33  static const QString y("Y");
34  static const QString n("N");
35  return v ? y : n;
36  }
37 
38  int CDatastoreUtility::extractIntegerKey(const QString &stringWithKey)
39  {
40  QString ks(stringWithKey.trimmed());
41  if (ks.isEmpty()) { return -1; }
42  bool ok = false;
43  int key = ks.toInt(&ok);
44  if (ok) { return key; } // only a number
45 
46  // key in string with ()
47  int i1 = ks.lastIndexOf('(');
48  if (i1 < 0) { return -1; }
49  int i2 = ks.lastIndexOf(')');
50  if (i2 <= i1 + 1) { return -1; }
51  const QString n(ks.mid(i1 + 1, i2 - i1 - 1));
52  ok = false;
53  key = n.toInt(&ok);
54  return ok ? key : -1;
55  }
56 
57  QString CDatastoreUtility::stripKeyInParentheses(const QString &valueWithKey)
58  {
59  const int i = valueWithKey.indexOf('(');
60  if (i < 0) { return valueWithKey.trimmed(); }
61  if (i < 1) { return {}; }
62  return valueWithKey.left(i - 1).trimmed();
63  }
64 
65  QDateTime CDatastoreUtility::parseTimestamp(const QString &timestamp)
66  {
67  if (timestamp.isEmpty()) { return QDateTime(); }
69  }
70 
71  bool CDatastoreUtility::parseAutoPublishResponse(const QString &jsonResponse, CStatusMessageList &messages)
72  {
73  if (jsonResponse.isEmpty())
74  {
76  u"Empty JSON data for published models"));
77  return false;
78  }
79 
80  const QJsonDocument jsonDoc(QJsonDocument::fromJson(jsonResponse.toUtf8()));
81 
82  // array of messages only
83  if (jsonDoc.isArray())
84  {
85  const CStatusMessageList msgs(CStatusMessageList::fromDatabaseJson(jsonDoc.array()));
86  messages.push_back(msgs);
87  return true;
88  }
89 
90  // no object -> most likely some fucked up HTML string with the PHP error
91  if (!jsonDoc.isObject())
92  {
93  const QString phpError(CNetworkUtils::removeHtmlPartsFromPhpErrorMessage(jsonResponse));
94  messages.push_back(
95  CStatusMessage(static_cast<CDatastoreUtility *>(nullptr), CStatusMessage::SeverityError, phpError));
96  return false;
97  }
98 
99  QJsonObject json(jsonDoc.object());
100  if (json.contains("msgs"))
101  {
102  const QJsonValue msgJson(json.take("msgs"));
103  const CStatusMessageList msgs(CStatusMessageList::fromDatabaseJson(msgJson.toArray()));
104  if (!msgs.isEmpty())
105  {
106  messages.push_back(msgs);
107  return !messages.hasErrorMessages();
108  }
109  }
110  return false;
111  }
112 } // namespace swift::misc::db
static const QString & swiftDbWebservice()
Webservice with swift DB.
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition: sequence.h:305
bool isEmpty() const
Synonym for empty.
Definition: sequence.h:285
Streamable status message, e.g.
constexpr static auto SeverityError
Status severities.
Status messages, e.g. from Core -> GUI.
bool hasErrorMessages() const
Error messages.
static CStatusMessageList fromDatabaseJson(const QJsonArray &array)
From our database JSON format.
Class with datastore related utilities.
static QString removeHtmlPartsFromPhpErrorMessage(const QString &errorMessage)
Remove the HTML formatting from a PHP error message.
Free functions in swift::misc.
SWIFT_MISC_EXPORT QString removeDateTimeSeparators(const QString &s)
Remove the typical separators such as "-", " ".
SWIFT_MISC_EXPORT bool stringToBool(const QString &boolString)
Convert string to bool.
SWIFT_MISC_EXPORT QDateTime parseDateTimeStringOptimized(const QString &dateTimeString)
Parse yyyyMMddHHmmsszzz strings optimized.