Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 402ef73e

Von Christian Ehringfeld vor etwa 9 Jahren hinzugefügt

  • ID 402ef73e105dc927a08dd718bc382d930b693837
  • Vorgänger 94090b83
  • Nachfolger e5a0d313

entity copy method, not fully tested

Unterschiede anzeigen:

samples/example/main.cpp
qWarning() << groupFindPtr->toString();
QSharedPointer<Entity> entityGroupFindPtr = groupFindPtr.objectCast<Entity>();
e->save(entityGroupFindPtr, true);
qWarning() << "-----------------------------";
qWarning() << "Remove Group";
qWarning() << "-----------------------------";
src/entity.cpp
return r;
}
Entity *Entity::copy() const {
return EntityHelper::copyObject(this);
}
QString Entity::slimToString() const {
QString r = "";
r.append(EntityHelper::getClassName(this));
src/entity.h
#define ENTITY_H
#include <QObject>
#include <QSharedPointer>
#include "relation.h"
#include <QStringList>
#include "entityinstancefactory.h"
#include "relation.h"
#include "validators/validatorrule.h"
#include "validators/errormsg.h"
namespace CuteEntityManager {
......
* You must not name any persisted property objectName, because its pre used by Qt and will be ignored by Entity Manager
* @brief The Entity class
*/
class ValidationRule;
class ErrorMsg;
class Entity : public QObject {
Q_OBJECT
Q_PROPERTY(qint64 id READ getId WRITE setId NOTIFY idChanged)
signals:
signals:
void idChanged();
public:
public:
virtual QString toString() const;
/**
* @brief copy
* depends on Q_PROPERTIES
* Copies them with exceptions of primary key(mostly "id") and objectName
* @return
*/
virtual Entity* copy() const;
virtual ~Entity();
virtual QList<ValidationRule> validationRules() const;
virtual QString getTablename() const;
......
QString getErrorsAsString() const;
void setErrors(const QList<ErrorMsg> &value);
protected:
protected:
explicit Entity (QObject *parent = 0);
virtual QString slimToString() const;
QList<ErrorMsg> errors;
src/entityhelper.cpp
}
const QHash<QString, Relation> EntityHelper::getNonInheritedRelations(
const Entity *entity) {
const Entity *entity) {
auto relations = entity->getRelations();
auto superObject = EntityInstanceFactory::newSuperClassInstance(entity);
if (superObject) {
......
}
const QList<const QMetaObject *> EntityHelper::superClasses(
const Entity *entity, bool
stopAtSingleTableInheritance) {
const Entity *entity, bool
stopAtSingleTableInheritance) {
QList<const QMetaObject *> classes = QList<const QMetaObject *>();
auto superMetaObject = entity->metaObject()->superClass();
if (entity->getInheritanceStrategy() == InheritanceStrategy::JOINED_TABLE) {
Entity *e = nullptr;
while (superMetaObject && QString(superMetaObject->className()) !=
QString("CuteEntityManager::Entity")) {
QString("CuteEntityManager::Entity")) {
e = EntityInstanceFactory::createInstance(superMetaObject->className());
if (e) {
classes.append(superMetaObject);
......
}
const QHash<QString, QMetaProperty> EntityHelper::getMetaProperties(
const Entity *entity) {
const Entity *entity) {
return EntityHelper::getMetaProperties(entity->metaObject());
}
const QHash<QString, QMetaProperty> EntityHelper::getSuperMetaProperties(
const Entity *entity) {
const Entity *entity) {
auto superMetaObjectPropertyMap = QHash<QString, QMetaProperty>();
auto superMeta = entity->metaObject()->superClass();
if (QString(superMeta->className()) != QString("CuteEntityManager::Entity")
......
}
const QHash<QString, QMetaProperty> EntityHelper::getMetaProperties(
const QMetaObject *object) {
const QMetaObject *object) {
auto h = QHash<QString, QMetaProperty>();
for (int var = 0; var < object->propertyCount(); ++var) {
QMetaProperty m = object->property(var);
......
}
const QHash<QString, QMetaProperty> EntityHelper::getNonInheritedMetaProperties(
const Entity *entity) {
const Entity *entity) {
auto props = EntityHelper::getMetaProperties(entity);
auto superObject = EntityInstanceFactory::newSuperClassInstance(entity);
if (superObject) {
auto superProps = EntityHelper::getMetaProperties(superObject);
for (auto iterator = superProps.constBegin(); iterator != superProps.constEnd();
++iterator) {
++iterator) {
if (props.contains(iterator.key())) {
props.remove(iterator.key());
}
......
}
const QHash<QString, QMetaProperty> EntityHelper::getInheritedMetaProperties(
const Entity *entity) {
const Entity *entity) {
auto props = EntityHelper::getMetaProperties(entity);
auto superObject = EntityInstanceFactory::newSuperClassInstance(entity);
auto wholeProperties = QHash<QString, QMetaProperty>();
......
}
const QHash<Relation, QMetaProperty> EntityHelper::getRelationProperties(
const Entity *entity) {
const Entity *entity) {
auto h = QHash<Relation, QMetaProperty>();
auto relations = entity->getRelations();
for (int var = 0; var < entity->metaObject()->propertyCount(); ++var) {
......
return h;
}
Entity *EntityHelper::copyObject(const Entity *entity)
{
auto metaObject = entity->metaObject();
auto newInstance = EntityInstanceFactory::createInstance(metaObject);
if(newInstance) {
for (int i = 0; i < metaObject->propertyCount(); ++i) {
auto property = metaObject->property(i);
QString name = property.name();
if(property.isValid() && name != QString("objectName") && name !=entity->getPrimaryKey()) {
property.write(newInstance,property.read(entity));
}
}
}
return newInstance;
}
const char *EntityHelper::getClassname(const Entity *entity) {
return entity->metaObject()->className();
}
......
}
void EntityHelper::addEntityToListProperty(const QSharedPointer<Entity>
&entity, QSharedPointer<Entity> add, const QMetaProperty &property) {
&entity, QSharedPointer<Entity> add, const QMetaProperty &property) {
QVariant var = property.read(entity.data());
if (!var.isNull() && var.canConvert<QList<QVariant>>()) {
auto list = EntityInstanceFactory::castQVariantList(var);
......
}
void EntityHelper::removeEntityFromListProperty(const QSharedPointer<Entity>
&entity, QSharedPointer<Entity> remove, const QMetaProperty &property) {
&entity, QSharedPointer<Entity> remove, const QMetaProperty &property) {
QVariant var = property.read(entity.data());
if (!var.isNull() && var.canConvert<QList<QVariant>>()) {
auto list = EntityInstanceFactory::castQVariantList(var);
......
}
QMetaProperty EntityHelper::mappedProperty(const Relation &r,
const QSharedPointer<Entity> &foreignEntity) {
const QSharedPointer<Entity> &foreignEntity) {
QMetaProperty prop;
auto props = EntityHelper::getMetaProperties(foreignEntity.data());
if (!r.getMappedBy().isEmpty() && props.contains(r.getMappedBy())) {
......
} else {
auto relations = foreignEntity->getRelations();
for (auto iterator = relations.constBegin(); iterator != relations.constEnd();
++iterator) {
++iterator) {
auto rel = iterator.value();
if (rel.getMappedBy() == r.getPropertyName()) {
prop = props.value(rel.getPropertyName());
......
}
QHash<QString, QVariant> EntityHelper::getEntityAttributes(
const QHash<QString, QMetaProperty>
&props,
const QSharedPointer<Entity> &entity) {
const QHash<QString, QMetaProperty>
&props,
const QSharedPointer<Entity> &entity) {
auto map = QHash<QString, QVariant>();
auto transientAttrs = entity->getTransientAttributes();
auto relations = entity->getRelations();
src/entityhelper.h
const Entity *entity);
static const QHash<Relation, QMetaProperty> getRelationProperties(
const Entity *entity);
static Entity* copyObject(const Entity*entity);
static const char *getClassname(const Entity *entity);
static const QString getClassName(const Entity *entity);
tests/models.h
public:
enum class Gender {MALE, FEMALE, UNKNOWNGENDER};
Q_ENUM(Gender);
enum class NameOrder {FIRST_FAMILY_NAME_ORDER, FAMILY_FIRST_NAME_ORDER};
Q_ENUM(NameOrder);
Q_INVOKABLE explicit Person(QObject *parent = 0);
Person(QString firstName, QString familyName,
Gender gender = Gender::UNKNOWNGENDER,

Auch abrufbar als: Unified diff