Added support for import data from file.
This commit is contained in:
+5
-2
@@ -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
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
#include "csvimporter.h"
|
||||
#include <QFile>
|
||||
#include <QVariant>
|
||||
|
||||
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<QObject> CsvImporter::nextRecord()
|
||||
{
|
||||
if (!m_parsed)
|
||||
{
|
||||
parseFile();
|
||||
}
|
||||
|
||||
QObject *entity = m_metaObject->newInstance();
|
||||
|
||||
if (entity == NULL || m_currentRec > recordCount())
|
||||
{
|
||||
++m_currentRec;
|
||||
return QSharedPointer<QObject>();
|
||||
}
|
||||
|
||||
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<QObject>();
|
||||
}
|
||||
}
|
||||
|
||||
++m_currentRec;
|
||||
|
||||
return QSharedPointer<QObject>(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;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef CSVIMPORTER_H
|
||||
#define CSVIMPORTER_H
|
||||
|
||||
#include "iimporter.h"
|
||||
#include <QStringList>
|
||||
#include <QObject>
|
||||
|
||||
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<QObject> 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
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef IIMPORTER_H
|
||||
#define IIMPORTER_H
|
||||
|
||||
#include <QMetaObject>
|
||||
#include <QObject>
|
||||
#include <QSharedPointer>
|
||||
|
||||
class IImporter
|
||||
{
|
||||
public:
|
||||
explicit IImporter(const QMetaObject *metaObject) { m_metaObject = metaObject; }
|
||||
|
||||
virtual void setImportFile(const QString &fileName) = 0;
|
||||
virtual int recordCount() = 0;
|
||||
virtual QSharedPointer<QObject> nextRecord() = 0;
|
||||
virtual bool isError() = 0;
|
||||
|
||||
protected:
|
||||
const QMetaObject *m_metaObject;
|
||||
};
|
||||
|
||||
#endif // IIMPORTER_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<QObject> qentity = importer->nextRecord();
|
||||
|
||||
if (importer->isError() || qentity.isNull()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QSharedPointer<T> entity = qentity.dynamicCast<T>();
|
||||
if (!entity.isNull()) {
|
||||
this->save(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected:
|
||||
bool checkPermission(const QString &permission) {
|
||||
if (!m_pluginId.isEmpty()) {
|
||||
|
||||
Reference in New Issue
Block a user