Revision e2ee17bf
Von Christian Ehringfeld vor etwa 10 Jahren hinzugefügt
| EntityManager.pro | ||
|---|---|---|
|
src/queryinterpreter.h \
|
||
|
src/expression.h \
|
||
|
src/orderby.h \
|
||
|
src/sqlitebackupprocessor.h
|
||
|
src/sqlitebackupprocessor.h \
|
||
|
src/validators/validator.h \
|
||
|
src/validators/param.h \
|
||
|
src/validators/errormsg.h \
|
||
|
src/validators/defaultvalidator.h \
|
||
|
src/validators/validatorfactory.h
|
||
|
|
||
|
SOURCES += \
|
||
|
src/entity.cpp \
|
||
| ... | ... | |
|
src/queryinterpreter.cpp \
|
||
|
src/expression.cpp \
|
||
|
src/orderby.cpp \
|
||
|
src/sqlitebackupprocessor.cpp
|
||
|
src/sqlitebackupprocessor.cpp \
|
||
|
src/validators/validator.cpp \
|
||
|
src/validators/param.cpp \
|
||
|
src/validators/errormsg.cpp \
|
||
|
src/validators/defaultvalidator.cpp \
|
||
|
src/validators/validatorfactory.cpp
|
||
|
|
||
|
CONFIG += c++14
|
||
|
QMAKE_CXXFLAGS += -std=c++14
|
||
| src/entityinstancefactory.cpp | ||
|---|---|---|
|
}
|
||
|
} else {
|
||
|
qWarning() << prop.name() << "on Entity" << EntityHelper::getClassname(
|
||
|
e) << "not writeable!";
|
||
|
e) << "not writeable!";
|
||
|
}
|
||
|
}
|
||
|
++iterator;
|
||
| src/entityinstancefactory.h | ||
|---|---|---|
|
//http://www.mimec.org/node/350
|
||
|
template<typename T>
|
||
|
static void registerClass() {
|
||
|
EntityInstanceFactory::instance.insert( T::staticMetaObject.className(),
|
||
|
&constructorHelper<T> );
|
||
|
QString lName = "QList<QSharedPointer<";
|
||
|
lName.append(T::staticMetaObject.className());
|
||
|
lName.append(">>");
|
||
|
/**
|
||
|
* @brief qRegisterMetaType<QList<QSharedPointer<T> > >
|
||
|
* @todo would be great if we could remove this shit
|
||
|
*/
|
||
|
qRegisterMetaType<QList<QSharedPointer<T>>>(lName.toLatin1().constData());
|
||
|
if (!EntityInstanceFactory::instance.contains(
|
||
|
T::staticMetaObject.className())) {
|
||
|
EntityInstanceFactory::instance.insert( T::staticMetaObject.className(),
|
||
|
&constructorHelper<T> );
|
||
|
QString lName = "QList<QSharedPointer<";
|
||
|
lName.append(T::staticMetaObject.className());
|
||
|
lName.append(">>");
|
||
|
/**
|
||
|
* @brief qRegisterMetaType<QList<QSharedPointer<T> > >
|
||
|
* @todo would be great if we could remove this shit
|
||
|
*/
|
||
|
qRegisterMetaType<QList<QSharedPointer<T>>>(lName.toLatin1().constData());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static Entity *createObject( const QByteArray &className) {
|
||
| src/validators/defaultvalidator.cpp | ||
|---|---|---|
|
#include "defaultvalidator.h"
|
||
|
#include "validator.h"
|
||
|
using namespace CuteEntityManager;
|
||
|
DefaultValidator::DefaultValidator() : Validator() {
|
||
|
}
|
||
|
|
||
|
ErrorMsg DefaultValidator::validateParam(QVariant value, Param param) const {
|
||
|
if (value != param.getValue()) {
|
||
|
return ErrorMsg(param.getName(), "Default value not set");
|
||
|
}
|
||
|
return ErrorMsg();
|
||
|
}
|
||
|
|
||
| src/validators/defaultvalidator.h | ||
|---|---|---|
|
#ifndef DEFAULTVALIDATOR_H
|
||
|
#define DEFAULTVALIDATOR_H
|
||
|
#include "validator.h"
|
||
|
namespace CuteEntityManager {
|
||
|
class ErrorMsg;
|
||
|
class DefaultValidator : public Validator {
|
||
|
Q_OBJECT
|
||
|
public:
|
||
|
DefaultValidator();
|
||
|
ErrorMsg validateParam(QVariant value, Param param) const final override;
|
||
|
};
|
||
|
}
|
||
|
|
||
|
#endif // DEFAULTVALIDATOR_H
|
||
| src/validators/errormsg.cpp | ||
|---|---|---|
|
#include "errormsg.h"
|
||
|
using namespace CuteEntityManager;
|
||
|
ErrorMsg::ErrorMsg() {
|
||
|
}
|
||
|
|
||
|
ErrorMsg::~ErrorMsg() {
|
||
|
}
|
||
|
|
||
|
ErrorMsg::ErrorMsg(QString param, QString errorMsg, QString propertyName) {
|
||
|
this->param = param;
|
||
|
this->errorMsg = errorMsg;
|
||
|
this->propertyName = propertyName;
|
||
|
}
|
||
|
|
||
|
QString ErrorMsg::getParam() const {
|
||
|
return param;
|
||
|
}
|
||
|
|
||
|
void ErrorMsg::setParam(const QString &value) {
|
||
|
param = value;
|
||
|
}
|
||
|
|
||
|
QString ErrorMsg::getPropertyName() const {
|
||
|
return propertyName;
|
||
|
}
|
||
|
|
||
|
void ErrorMsg::setPropertyName(const QString &value) {
|
||
|
propertyName = value;
|
||
|
}
|
||
|
|
||
|
QString ErrorMsg::getErrorMsg() const {
|
||
|
return errorMsg;
|
||
|
}
|
||
|
|
||
|
void ErrorMsg::setErrorMsg(const QString &value) {
|
||
|
errorMsg = value;
|
||
|
}
|
||
| src/validators/errormsg.h | ||
|---|---|---|
|
#ifndef ERRORMSG_H
|
||
|
#define ERRORMSG_H
|
||
|
#include <QString>
|
||
|
namespace CuteEntityManager {
|
||
|
class ErrorMsg {
|
||
|
public:
|
||
|
ErrorMsg();
|
||
|
~ErrorMsg();
|
||
|
ErrorMsg(QString param, QString errorMsg, QString propertyName = "");
|
||
|
|
||
|
QString getParam() const;
|
||
|
void setParam(const QString &value);
|
||
|
|
||
|
QString getPropertyName() const;
|
||
|
void setPropertyName(const QString &value);
|
||
|
|
||
|
QString getErrorMsg() const;
|
||
|
void setErrorMsg(const QString &value);
|
||
|
|
||
|
private:
|
||
|
QString param;
|
||
|
QString propertyName;
|
||
|
QString errorMsg;
|
||
|
};
|
||
|
}
|
||
|
#endif // ERRORMSG_H
|
||
| src/validators/param.cpp | ||
|---|---|---|
|
#include "param.h"
|
||
|
using namespace CuteEntityManager;
|
||
|
|
||
|
Param::Param() {
|
||
|
}
|
||
|
|
||
|
Param::~Param() {
|
||
|
}
|
||
|
|
||
|
Param::Param(QString name, QVariant value) {
|
||
|
this->value = value;
|
||
|
this->name = name;
|
||
|
}
|
||
|
|
||
|
QVariant Param::getValue() const {
|
||
|
return value;
|
||
|
}
|
||
|
|
||
|
void Param::setValue(QVariant value) {
|
||
|
value = value;
|
||
|
}
|
||
|
|
||
|
QString Param::getName() const {
|
||
|
return name;
|
||
|
}
|
||
|
|
||
|
void Param::setName(const QString &value) {
|
||
|
name = value;
|
||
|
}
|
||
|
|
||
|
|
||
| src/validators/param.h | ||
|---|---|---|
|
#ifndef PARAM_H
|
||
|
#define PARAM_H
|
||
|
|
||
|
#include <QString>
|
||
|
#include <QVariant>
|
||
|
namespace CuteEntityManager {
|
||
|
class Param {
|
||
|
public:
|
||
|
Param();
|
||
|
~Param();
|
||
|
Param(QString name, QVariant value = QVariant());
|
||
|
QVariant getValue() const;
|
||
|
void setValue(QVariant value);
|
||
|
|
||
|
QString getName() const;
|
||
|
void setName(const QString &value);
|
||
|
|
||
|
private:
|
||
|
QVariant value;
|
||
|
QString name;
|
||
|
};
|
||
|
}
|
||
|
|
||
|
#endif // PARAM_H
|
||
| src/validators/validator.cpp | ||
|---|---|---|
|
#include "validator.h"
|
||
|
#include "validatorfactory.h"
|
||
|
using namespace CuteEntityManager;
|
||
|
Validator::Validator() : QObject() {
|
||
|
ValidatorFactory::registerClasses();
|
||
|
}
|
||
|
|
||
|
Validator::~Validator() {
|
||
|
}
|
||
|
|
||
|
const QHash<QString, QString> Validator::builtInValidators() {
|
||
|
QHash<QString, QString> hash = QHash<QString, QString>();
|
||
|
// hash.insert("compare", "CompareValidator");
|
||
|
hash.insert("default", "DefaultValidator");
|
||
|
// hash.insert("email", "EmailValidator");
|
||
|
// hash.insert("exist", "ExistValidator");
|
||
|
// hash.insert("image", "ImageValidator");
|
||
|
// hash.insert("size", "SizeValidator");
|
||
|
// hash.insert("number", "NumberValidator");
|
||
|
// hash.insert("date", "DateValidator");
|
||
|
// hash.insert("required", "RequiredValidator");
|
||
|
// hash.insert("unique", "UniqueValidator");
|
||
|
// hash.insert("url", "UrlValidator");
|
||
|
return hash;
|
||
|
}
|
||
|
|
||
|
QList<ErrorMsg> Validator::validate(QVariant value, QList<Param> params) const {
|
||
|
QList<ErrorMsg> msgs = QList<ErrorMsg>();
|
||
|
for (int i = 0; i < params.size(); ++i) {
|
||
|
ErrorMsg msg = this->validateParam(value, params.at(i));
|
||
|
if (!msg.getErrorMsg().isEmpty()) {
|
||
|
msgs.append(msg);
|
||
|
}
|
||
|
}
|
||
|
return msgs;
|
||
|
}
|
||
|
|
||
|
QString Validator::generateErrorMsg(ErrorMsg msg) {
|
||
|
if (msg.getPropertyName().isEmpty()) {
|
||
|
//return tr(msg.getErrorMsg());
|
||
|
return msg.getErrorMsg();
|
||
|
}
|
||
|
//return tr(msg.getErrorMsg()).replace("<property>", msg.getPropertyName());
|
||
|
return msg.getErrorMsg().replace("<property>", msg.getPropertyName());
|
||
|
}
|
||
| src/validators/validator.h | ||
|---|---|---|
|
#ifndef VALIDATOR_H
|
||
|
#define VALIDATOR_H
|
||
|
|
||
|
#include <QObject>
|
||
|
#include <QHash>
|
||
|
#include "param.h"
|
||
|
#include "errormsg.h"
|
||
|
namespace CuteEntityManager {
|
||
|
class Validator : public QObject {
|
||
|
Q_OBJECT
|
||
|
public:
|
||
|
Validator();
|
||
|
virtual ~Validator();
|
||
|
static const QHash<QString, QString> builtInValidators();
|
||
|
virtual QList<ErrorMsg> validate(QVariant value, QList<Param> params) const;
|
||
|
QString generateErrorMsg(ErrorMsg msg);
|
||
|
|
||
|
protected:
|
||
|
virtual ErrorMsg validateParam(QVariant value, Param param) const = 0;
|
||
|
};
|
||
|
}
|
||
|
#endif // VALIDATOR_H
|
||
| src/validators/validatorfactory.cpp | ||
|---|---|---|
|
#include "validatorfactory.h"
|
||
|
#include "defaultvalidator.h"
|
||
|
using namespace CuteEntityManager;
|
||
|
ValidatorFactory::ValidatorFactory() {
|
||
|
}
|
||
|
|
||
|
|
||
|
QHash<QByteArray, ValidatorFactory::Constructor> ValidatorFactory::instance =
|
||
|
QHash<QByteArray, ValidatorFactory::Constructor>();
|
||
|
|
||
|
|
||
|
Validator *ValidatorFactory::createValidator(QString shortname) {
|
||
|
if (Validator::builtInValidators().contains(shortname)) {
|
||
|
return ValidatorFactory::createObject(shortname.toLatin1());
|
||
|
}
|
||
|
return nullptr;
|
||
|
}
|
||
|
|
||
|
void ValidatorFactory::registerClasses() {
|
||
|
if (ValidatorFactory::instance.isEmpty()) {
|
||
|
// Validator::registerClass<CompareValidator>();
|
||
|
ValidatorFactory::registerClass<DefaultValidator>();
|
||
|
// Validator::registerClass<EmailValidator>();
|
||
|
// Validator::registerClass<ExistValidator>();
|
||
|
// Validator::registerClass<ImageValidator>();
|
||
|
// Validator::registerClass<SizeValidator>();
|
||
|
// Validator::registerClass<NumberValidator>();
|
||
|
// Validator::registerClass<DateValidator>();
|
||
|
// Validator::registerClass<RequiredValidator>();
|
||
|
// Validator::registerClass<UniqueValidator>();
|
||
|
// Validator::registerClass<UrlValidator>();
|
||
|
}
|
||
|
}
|
||
| src/validators/validatorfactory.h | ||
|---|---|---|
|
#ifndef VALIDATORFACTORY_H
|
||
|
#define VALIDATORFACTORY_H
|
||
|
#include <QString>
|
||
|
#include <QHash>
|
||
|
namespace CuteEntityManager {
|
||
|
class Validator;
|
||
|
class ValidatorFactory {
|
||
|
public:
|
||
|
static void registerClasses();
|
||
|
static Validator *createValidator(QString shortname);
|
||
|
template<typename T>
|
||
|
static void registerClass() {
|
||
|
if (!ValidatorFactory::instance.contains(
|
||
|
T::staticMetaObject.className())) {
|
||
|
ValidatorFactory::instance.insert( T::staticMetaObject.className(),
|
||
|
&constructorHelper<T> );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static Validator *createObject( const QByteArray &className) {
|
||
|
Constructor constructor = ValidatorFactory::instance.value(className);
|
||
|
if ( constructor == nullptr ) {
|
||
|
return nullptr;
|
||
|
}
|
||
|
return (*constructor)();
|
||
|
}
|
||
|
typedef Validator *(*Constructor)();
|
||
|
template<typename T>
|
||
|
static Validator *constructorHelper() {
|
||
|
return new T();
|
||
|
}
|
||
|
static QHash<QByteArray, Constructor> instance;
|
||
|
|
||
|
protected:
|
||
|
ValidatorFactory();
|
||
|
};
|
||
|
}
|
||
|
|
||
|
#endif // VALIDATORFACTORY_H
|
||
Auch abrufbar als: Unified diff
validators