diff --git a/core/core.pro b/core/core.pro index fe4434a..c0a6c46 100644 --- a/core/core.pro +++ b/core/core.pro @@ -62,7 +62,8 @@ SOURCES += \ settings/seasonnamedialog.cpp \ reporting/report.cpp \ reporting/reportviewer.cpp \ - reporting/reportdialog.cpp + reporting/reportdialog.cpp \ + csvimporter.cpp HEADERS += core.h\ core_global.h \ @@ -122,7 +123,9 @@ HEADERS += core.h\ settings/seasonnamedialog.h \ reporting/report.h \ reporting/reportviewer.h \ - reporting/reportdialog.h + reporting/reportdialog.h \ + iimporter.h \ + csvimporter.h unix { target.path = /usr/lib diff --git a/core/csvimporter.cpp b/core/csvimporter.cpp new file mode 100644 index 0000000..7c8f136 --- /dev/null +++ b/core/csvimporter.cpp @@ -0,0 +1,93 @@ +#include "csvimporter.h" +#include +#include + +CsvImporter::CsvImporter(const QMetaObject *metaObject, QObject *parent) + :QObject(parent), + IImporter(metaObject) +{ + m_parsed = false; + m_currentRec = 1; + m_error = false; +} + +void CsvImporter::setImportFile(const QString &fileName) +{ + m_fileName = fileName; +} + +int CsvImporter::recordCount() +{ + if (!m_parsed) + { + parseFile(); + } + + return m_lines.count() - 1; +} + +QSharedPointer CsvImporter::nextRecord() +{ + if (!m_parsed) + { + parseFile(); + } + + QObject *entity = m_metaObject->newInstance(); + + if (entity == NULL || m_currentRec > recordCount()) + { + ++m_currentRec; + return QSharedPointer(); + } + + QStringList props = m_header.split(m_separator); + QString line = m_lines[m_currentRec]; + QStringList values = line.split(m_separator); + + for (int i = 0; i < props.size(); i++) { + QString property = props[i]; + QString value = values[i]; + if (!entity->setProperty(property.toStdString().c_str(), QVariant(value))) + { + m_error = true; + emit noSuchProperty(property); + + ++m_currentRec; + return QSharedPointer(); + } + } + + ++m_currentRec; + + return QSharedPointer(entity); +} + +bool CsvImporter::isError() +{ + return m_error; +} + +void CsvImporter::parseFile() +{ + QFile file(m_fileName); + + if (!file.open(QFile::ReadOnly | QFile::Text)) + { + m_error = true; + emit parseError(); + return; + } + + QByteArray data = file.readAll(); + QString strData(data); + + m_lines = strData.split("\n"); + m_header = m_lines[0]; + m_parsed = true; +} + +void CsvImporter::setSeparator(const QString &separator) +{ + m_separator = separator; +} diff --git a/core/csvimporter.h b/core/csvimporter.h new file mode 100644 index 0000000..b395fd9 --- /dev/null +++ b/core/csvimporter.h @@ -0,0 +1,40 @@ +#ifndef CSVIMPORTER_H +#define CSVIMPORTER_H + +#include "iimporter.h" +#include +#include + +class CsvImporter : public QObject, public IImporter +{ + Q_OBJECT + +public: + explicit CsvImporter(const QMetaObject *metaObject, QObject *parent = NULL); + + // IImporter interface +public: + void setImportFile(const QString &fileName); + int recordCount(); + QSharedPointer nextRecord(); + bool isError(); + + void setSeparator(const QString &separator); + +signals: + void parseError(); + void noSuchProperty(QString propName); + +private: + void parseFile(); + + QString m_header; + QString m_separator; + QString m_fileName; + QStringList m_lines; + bool m_parsed; + bool m_error; + int m_currentRec; +}; + +#endif // CSVIMPORTER_H diff --git a/core/iimporter.h b/core/iimporter.h new file mode 100644 index 0000000..19d62a7 --- /dev/null +++ b/core/iimporter.h @@ -0,0 +1,22 @@ +#ifndef IIMPORTER_H +#define IIMPORTER_H + +#include +#include +#include + +class IImporter +{ +public: + explicit IImporter(const QMetaObject *metaObject) { m_metaObject = metaObject; } + + virtual void setImportFile(const QString &fileName) = 0; + virtual int recordCount() = 0; + virtual QSharedPointer nextRecord() = 0; + virtual bool isError() = 0; + +protected: + const QMetaObject *m_metaObject; +}; + +#endif // IIMPORTER_H diff --git a/core/service.h b/core/service.h index e3267db..60b9fa0 100644 --- a/core/service.h +++ b/core/service.h @@ -14,6 +14,7 @@ #include "context.h" #include "iservice.h" #include "permissionevaluator.h" +#include "iimporter.h" #include "transaction.h" @@ -178,6 +179,33 @@ public: } } + bool importData(IImporter *importer) { + int count = importer->recordCount(); + + if (importer->isError()) { + return false; + } + + for (int i = 0; i < count - 1; i++) { + QSharedPointer qentity = importer->nextRecord(); + + if (importer->isError() || qentity.isNull()) { + return false; + } + + QSharedPointer entity = qentity.dynamicCast(); + if (!entity.isNull()) { + this->save(entity); + } + else + { + return false; + } + } + + return true; + } + protected: bool checkPermission(const QString &permission) { if (!m_pluginId.isEmpty()) {