Renamed core subproject into BeetRoundCore.
This commit is contained in:
158
libs/BeetRoundCore/formats/csvparser.cpp
Normal file
158
libs/BeetRoundCore/formats/csvparser.cpp
Normal file
@ -0,0 +1,158 @@
|
||||
#include "csvparser.h"
|
||||
|
||||
#include <QHash>
|
||||
#include <QLocale>
|
||||
#include <QVariant>
|
||||
|
||||
#include "../../3rdParty/rapidcsv/src/rapidcsv.h"
|
||||
#include "../model/metadata.h"
|
||||
|
||||
using namespace rapidcsv;
|
||||
|
||||
QList<ModelItemValues> CsvParser::getItemsFromCSVFile(const QString& fileName) {
|
||||
Document doc(fileName.toStdString());
|
||||
|
||||
const bool isCompatible = isCsvCompatible(doc);
|
||||
if (isCompatible) {
|
||||
const QList<ModelItemValues> result = createListItemsFromCsvEntries(doc);
|
||||
return result;
|
||||
} else {
|
||||
return QList<ModelItemValues>();
|
||||
}
|
||||
}
|
||||
|
||||
bool CsvParser::exportToCSVFile(const QList<QStringList>& rows, const QString& filePath) {
|
||||
Document doc(std::string(), LabelParams(0, -1));
|
||||
const QList<QString> headerNames = GET_HEADER_NAMES();
|
||||
for (int column = 0; column < headerNames.size(); ++column) {
|
||||
doc.SetColumnName(column, headerNames.at(column).toStdString());
|
||||
}
|
||||
for (int row = 0; row < rows.size(); ++row) {
|
||||
QStringList rowValues = rows.at(row);
|
||||
std::vector<std::string> rowValueStrings;
|
||||
for (int column = 0; column < rowValues.size(); ++column) {
|
||||
rowValueStrings.push_back(rowValues.at(column).toStdString());
|
||||
}
|
||||
doc.InsertRow(row, rowValueStrings);
|
||||
}
|
||||
doc.Save(filePath.toStdString());
|
||||
return true;
|
||||
}
|
||||
|
||||
CsvParser::CsvParser() {}
|
||||
|
||||
/** A CSV file is compatible if the following is true:
|
||||
* - there is a CSV column for every column in the table model
|
||||
* (except there are optional columns defined in the model)
|
||||
* @brief CsvParser::isCsvCompatible
|
||||
* @param doc
|
||||
* @return
|
||||
*/
|
||||
bool CsvParser::isCsvCompatible(const rapidcsv::Document& doc) {
|
||||
qInfo() << "Checking CSV document for compatiblity...";
|
||||
const std::vector<std::string> columnNames = doc.GetColumnNames();
|
||||
for (const QString& headerName : GET_HEADER_NAMES()) {
|
||||
bool isHeaderNameFound = false;
|
||||
if (std::find(columnNames.begin(), columnNames.end(), headerName) != columnNames.end()) {
|
||||
qDebug() << QString("Header found in column names: %1").arg(headerName);
|
||||
} else {
|
||||
const QString errorString =
|
||||
QString("Couldn't find header name '%1' in CSV file. Aborting...").arg(headerName);
|
||||
qWarning() << errorString;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
QList<ModelItemValues> CsvParser::createListItemsFromCsvEntries(const rapidcsv::Document& doc) {
|
||||
QList<ModelItemValues> result;
|
||||
const int rowCount = doc.GetRowCount();
|
||||
const QList<QString> headerNames = GET_HEADER_NAMES();
|
||||
|
||||
/// get the values for all columns
|
||||
QHash<QString, std::vector<std::string>> columnValueMap = extractColumnValues(headerNames, doc);
|
||||
|
||||
/// get item values for each row
|
||||
for (int row = 0; row < rowCount; ++row) {
|
||||
const ModelItemValues itemValues = getItemValuesForRow(headerNames, columnValueMap, row);
|
||||
result.append(itemValues);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
QHash<QString, std::vector<std::string>> CsvParser::extractColumnValues(
|
||||
const QList<QString> headerNames,
|
||||
const rapidcsv::Document& doc) {
|
||||
QHash<QString, std::vector<std::string>> columnValueMap;
|
||||
for (const QString& columnName : headerNames) {
|
||||
// TODO add support for optional columns
|
||||
// if (optionalCsvHeaderNames.contains(columnName)) {
|
||||
// const std::vector<std::string> columnNames = doc.GetColumnNames();
|
||||
// int columnIdx = doc.GetColumnIdx(columnName.toStdString());
|
||||
// if (columnIdx == -1) {
|
||||
// continue;
|
||||
// }
|
||||
// }
|
||||
const std::vector<std::string> columnValues =
|
||||
doc.GetColumn<std::string>(columnName.toStdString());
|
||||
columnValueMap.insert(columnName, columnValues);
|
||||
}
|
||||
|
||||
return columnValueMap;
|
||||
}
|
||||
|
||||
ModelItemValues CsvParser::getItemValuesForRow(
|
||||
const QList<QString>& headerNames,
|
||||
const QHash<QString, std::vector<std::string>>& columnValueMap,
|
||||
const int row) {
|
||||
ModelItemValues result;
|
||||
for (const QString& columnName : headerNames) {
|
||||
if (!columnValueMap.contains(columnName)) {
|
||||
continue;
|
||||
}
|
||||
int role = ROLE_NAMES.key(columnName.toLatin1());
|
||||
std::string valueString = columnValueMap.value(columnName).at(row);
|
||||
|
||||
QVariant value = parseItemValue(role, valueString);
|
||||
if (value.isValid()) {
|
||||
result[role] = value;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
QVariant CsvParser::parseItemValue(const int role, const std::string& valueString) {
|
||||
QVariant result;
|
||||
if (STRING_ROLES.contains(role)) {
|
||||
/// string values
|
||||
result = QString::fromStdString(valueString);
|
||||
} else if (INT_ROLES.contains(role)) {
|
||||
/// int values
|
||||
|
||||
/// GetColumn<int> crashed (probably because of the empty values)
|
||||
/// so the strings will be processed later
|
||||
const QString intAsString = QString::fromStdString(valueString);
|
||||
result = intAsString.toInt();
|
||||
} else if (DOUBLE_ROLES.contains(role)) {
|
||||
/// double values
|
||||
const QString doubleAsString = QString::fromStdString(valueString);
|
||||
double doubleValue;
|
||||
if (doubleAsString.contains(',')) {
|
||||
QLocale german(QLocale::German);
|
||||
doubleValue = german.toDouble(doubleAsString);
|
||||
} else {
|
||||
doubleValue = doubleAsString.toDouble();
|
||||
}
|
||||
result = doubleValue;
|
||||
|
||||
// } else if (typeColumns.contains(columnName)) {
|
||||
// // TODO validate string is allowed
|
||||
// values[role] = QString::fromStdString(columnValueMap.value(columnName).at(row));
|
||||
} else {
|
||||
/// no type recognized for column
|
||||
QString errorString = QString("Couldn't find value type for role '%1'").arg(role);
|
||||
qCritical() << errorString;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
32
libs/BeetRoundCore/formats/csvparser.h
Normal file
32
libs/BeetRoundCore/formats/csvparser.h
Normal file
@ -0,0 +1,32 @@
|
||||
#ifndef CSVPARSER_H
|
||||
#define CSVPARSER_H
|
||||
|
||||
#include <QString>
|
||||
|
||||
typedef QMap<int, QVariant> ModelItemValues;
|
||||
|
||||
namespace rapidcsv {
|
||||
class Document;
|
||||
}
|
||||
|
||||
class CsvParser {
|
||||
public:
|
||||
static QList<ModelItemValues> getItemsFromCSVFile(const QString& fileName);
|
||||
static bool exportToCSVFile(const QList<QStringList>& rows, const QString& filePath);
|
||||
|
||||
private:
|
||||
explicit CsvParser();
|
||||
|
||||
static bool isCsvCompatible(const rapidcsv::Document& doc);
|
||||
static QList<ModelItemValues> createListItemsFromCsvEntries(const rapidcsv::Document& doc);
|
||||
static QHash<QString, std::vector<std::string>> extractColumnValues(
|
||||
const QList<QString> headerNames,
|
||||
const rapidcsv::Document& doc);
|
||||
static ModelItemValues getItemValuesForRow(
|
||||
const QList<QString>& headerNames,
|
||||
const QHash<QString, std::vector<std::string>>& columnValueMap,
|
||||
const int row);
|
||||
static QVariant parseItemValue(const int role, const std::string& valueString);
|
||||
};
|
||||
|
||||
#endif // CSVPARSER_H
|
||||
119
libs/BeetRoundCore/formats/jsonparser.cpp
Normal file
119
libs/BeetRoundCore/formats/jsonparser.cpp
Normal file
@ -0,0 +1,119 @@
|
||||
#include "jsonparser.h"
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
|
||||
#include "../model/metadata.h"
|
||||
|
||||
QList<ModelItemValues> JsonParser::toItemValuesList(const QByteArray& jsonData,
|
||||
const QString& rootValueName) {
|
||||
QList<ModelItemValues> result;
|
||||
|
||||
if (jsonData.isEmpty()) {
|
||||
return result;
|
||||
}
|
||||
// TODO tidy up the following code and encapsulate into functions;
|
||||
|
||||
QJsonDocument doc = QJsonDocument::fromJson(jsonData);
|
||||
|
||||
/// case one: json value name in plural -> should contain an array of items
|
||||
if (rootValueName == ITEMS_KEY_STRING) {
|
||||
QJsonArray itemArray = extractItemArray(doc, rootValueName);
|
||||
|
||||
foreach (QJsonValue value, itemArray) {
|
||||
QJsonObject itemJsonObject = value.toObject();
|
||||
ModelItemValues values = jsonObjectToItemValues(itemJsonObject);
|
||||
result.append(values);
|
||||
}
|
||||
}
|
||||
/// case two: json value name in singular -> should contain an object of one item
|
||||
if (rootValueName == ITEM_KEY_STRING) {
|
||||
QJsonObject rootObject = doc.object();
|
||||
QJsonObject itemJsonObject = rootObject.value(rootValueName).toObject();
|
||||
ModelItemValues values = jsonObjectToItemValues(itemJsonObject);
|
||||
result.append(values);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
QByteArray JsonParser::itemValuesListToJson(const QList<ModelItemValues>& itemValuesList,
|
||||
const QString& objectName) {
|
||||
QJsonDocument jsonDoc;
|
||||
QJsonObject rootObject;
|
||||
QJsonArray itemArray;
|
||||
for (const ModelItemValues& itemValues : itemValuesList) {
|
||||
QJsonObject itemObject;
|
||||
|
||||
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
||||
while (i.hasNext()) {
|
||||
const UserRoles role = i.next();
|
||||
const QString roleName = ROLE_NAMES.value(role);
|
||||
const QVariant value = itemValues.value(role);
|
||||
if (STRING_ROLES.contains(role)) {
|
||||
itemObject.insert(roleName, value.toString());
|
||||
} else if (INT_ROLES.contains(role)) {
|
||||
itemObject.insert(roleName, value.toInt());
|
||||
} else if (DOUBLE_ROLES.contains(role)) {
|
||||
itemObject.insert(roleName, value.toDouble());
|
||||
} else {
|
||||
qCritical() << QString("Can't find data type for role %1!!!").arg(role);
|
||||
}
|
||||
}
|
||||
|
||||
itemArray.append(itemObject);
|
||||
}
|
||||
|
||||
rootObject.insert(objectName, itemArray);
|
||||
jsonDoc.setObject(rootObject);
|
||||
|
||||
return jsonDoc.toJson(QJsonDocument::Compact);
|
||||
}
|
||||
|
||||
JsonParser::JsonParser() {}
|
||||
|
||||
QJsonArray JsonParser::extractItemArray(const QJsonDocument& doc, const QString& objectName) {
|
||||
QJsonArray itemArray;
|
||||
if (objectName.isEmpty()) {
|
||||
itemArray = doc.array();
|
||||
} else {
|
||||
QJsonObject rootObject = doc.object();
|
||||
itemArray = rootObject.value(objectName).toArray();
|
||||
}
|
||||
return itemArray;
|
||||
}
|
||||
|
||||
ModelItemValues JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonObject) {
|
||||
ModelItemValues values;
|
||||
|
||||
const UserRoles idRole = IdRole;
|
||||
const QString idRoleName = ROLE_NAMES.value(idRole);
|
||||
// QVariant idValue = data(idRole);
|
||||
if (itemJsonObject.contains(idRoleName)) {
|
||||
std::pair<int, QVariant> keyValuePair = getKeyValuePair(itemJsonObject, idRole);
|
||||
values.insert(keyValuePair.first, keyValuePair.second);
|
||||
}
|
||||
|
||||
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
||||
while (i.hasNext()) {
|
||||
const UserRoles role = i.next();
|
||||
std::pair<int, QVariant> keyValuePair = getKeyValuePair(itemJsonObject, role);
|
||||
values.insert(keyValuePair.first, keyValuePair.second);
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
pair<int, QVariant> JsonParser::getKeyValuePair(const QJsonObject& itemJsonObject, const int role) {
|
||||
QVariant result;
|
||||
const QJsonValue jsonValue = itemJsonObject[ROLE_NAMES.value(role)];
|
||||
if (STRING_ROLES.contains(role)) {
|
||||
result = jsonValue.toString();
|
||||
} else if (INT_ROLES.contains(role)) {
|
||||
result = jsonValue.toInt();
|
||||
} else if (DOUBLE_ROLES.contains(role)) {
|
||||
result = jsonValue.toDouble();
|
||||
} else {
|
||||
qCritical() << QString("Cant find data type of role %1!!!").arg(role);
|
||||
}
|
||||
return pair<int, QVariant>(role, result);
|
||||
}
|
||||
31
libs/BeetRoundCore/formats/jsonparser.h
Normal file
31
libs/BeetRoundCore/formats/jsonparser.h
Normal file
@ -0,0 +1,31 @@
|
||||
#ifndef JSONPARSER_H
|
||||
#define JSONPARSER_H
|
||||
|
||||
#include <QVariant>
|
||||
|
||||
class QJsonObject;
|
||||
class QString;
|
||||
class QByteArray;
|
||||
class QJsonArray;
|
||||
|
||||
typedef QMap<int, QVariant> ModelItemValues;
|
||||
|
||||
using namespace std;
|
||||
|
||||
class JsonParser {
|
||||
public:
|
||||
static QList<ModelItemValues> toItemValuesList(const QByteArray& jsonData,
|
||||
const QString& rootValueName = "");
|
||||
static QByteArray itemValuesListToJson(const QList<ModelItemValues>& itemValuesList,
|
||||
const QString& objectName = "");
|
||||
|
||||
private:
|
||||
explicit JsonParser();
|
||||
|
||||
static QJsonArray extractItemArray(const QJsonDocument& doc, const QString& objectName);
|
||||
static ModelItemValues jsonObjectToItemValues(const QJsonObject& itemJsonObject);
|
||||
|
||||
static pair<int, QVariant> getKeyValuePair(const QJsonObject& itemJsonObject, const int role);
|
||||
};
|
||||
|
||||
#endif // JSONPARSER_H
|
||||
Reference in New Issue
Block a user