Herunterladen als
root/src/entityhelper.cpp @ 6cfbc25e
4b90a7ee | Christian Ehringfeld | /*
|
|
* Copyright (C) 2015 Christian Ehringfeld <c.ehringfeld@t-online.de>
|
|||
*
|
|||
* This program is free software; you can redistribute it and/or modify it
|
|||
* under the terms of the GNU Lesser General Public License as published by
|
|||
* the Free Software Foundation.
|
|||
*
|
|||
* This program is distributed in the hope that it will be useful, but
|
|||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|||
* for more details.
|
|||
*
|
|||
* You should have received a copy of the GNU Lesser General Public License
|
|||
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|||
*/
|
|||
e8d1537c | Christian Ehringfeld | #include "entityhelper.h"
|
|
#include "entity.h"
|
|||
#include "relation.h"
|
|||
#include "cache.h"
|
|||
using namespace CuteEntityManager;
|
|||
66704054 | Christian Ehringfeld | EntityHelper::EntityHelper() {
|
|
e8d1537c | Christian Ehringfeld | }
|
|
66704054 | Christian Ehringfeld | const QHash<QString, Relation> EntityHelper::getNonInheritedRelations(
|
|
const Entity *entity) {
|
|||
e8d1537c | Christian Ehringfeld | auto relations = entity->getRelations();
|
|
auto superObject = EntityInstanceFactory::newSuperClassInstance(entity);
|
|||
if (superObject) {
|
|||
auto superRelations = superObject->getRelations();
|
|||
auto iterator = superRelations.constBegin();
|
|||
while (iterator != relations.constEnd()) {
|
|||
if (relations.contains(iterator.key())) {
|
|||
relations.remove(iterator.key());
|
|||
}
|
|||
++iterator;
|
|||
}
|
|||
delete superObject;
|
|||
superObject = nullptr;
|
|||
}
|
|||
return relations;
|
|||
}
|
|||
66704054 | Christian Ehringfeld | const QList<const QMetaObject *> EntityHelper::superClasses(
|
|
const Entity *entity, bool
|
|||
stopAtSingleTableInheritance) {
|
|||
e8d1537c | Christian Ehringfeld | 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()) !=
|
|||
66704054 | Christian Ehringfeld | QString("CuteEntityManager::Entity")) {
|
|
e8d1537c | Christian Ehringfeld | e = EntityInstanceFactory::createInstance(superMetaObject->className());
|
|
if (e) {
|
|||
classes.append(superMetaObject);
|
|||
superMetaObject = superMetaObject->superClass();
|
|||
InheritanceStrategy s = e->getInheritanceStrategy();
|
|||
delete e;
|
|||
e = nullptr;
|
|||
if (stopAtSingleTableInheritance && s == InheritanceStrategy::PER_CLASS_TABLE) {
|
|||
break;
|
|||
}
|
|||
} else {
|
|||
break;
|
|||
}
|
|||
}
|
|||
}
|
|||
return classes;
|
|||
}
|
|||
66704054 | Christian Ehringfeld | const QHash<QString, QMetaProperty> EntityHelper::getMetaProperties(
|
|
const Entity *entity) {
|
|||
e8d1537c | Christian Ehringfeld | return EntityHelper::getMetaProperties(entity->metaObject());
|
|
}
|
|||
66704054 | Christian Ehringfeld | const QHash<QString, QMetaProperty> EntityHelper::getSuperMetaProperties(
|
|
const Entity *entity) {
|
|||
e8d1537c | Christian Ehringfeld | auto superMetaObjectPropertyMap = QHash<QString, QMetaProperty>();
|
|
auto superMeta = entity->metaObject()->superClass();
|
|||
if (QString(superMeta->className()) != QString("CuteEntityManager::Entity")
|
|||
&& entity->getInheritanceStrategy() == InheritanceStrategy::JOINED_TABLE) {
|
|||
for (int var = 0; var < superMeta->propertyCount(); ++var) {
|
|||
QMetaProperty prop = superMeta->property(var);
|
|||
if (prop.isReadable() && prop.isWritable()) {
|
|||
superMetaObjectPropertyMap.insert(QString(prop.name()), prop);
|
|||
}
|
|||
}
|
|||
}
|
|||
return superMetaObjectPropertyMap;
|
|||
}
|
|||
const QHash<QString, QMetaProperty> EntityHelper::getMetaProperties(
|
|||
66704054 | Christian Ehringfeld | const QMetaObject *object) {
|
|
e8d1537c | Christian Ehringfeld | auto h = QHash<QString, QMetaProperty>();
|
|
for (int var = 0; var < object->propertyCount(); ++var) {
|
|||
QMetaProperty m = object->property(var);
|
|||
if (m.isValid() && m.name() != QString("objectName")) {
|
|||
h.insert(m.name(), m);
|
|||
}
|
|||
}
|
|||
return h;
|
|||
}
|
|||
66704054 | Christian Ehringfeld | const QHash<QString, QMetaProperty> EntityHelper::getInheritedMetaProperties(
|
|
const Entity *entity) {
|
|||
e8d1537c | Christian Ehringfeld | auto classes = EntityHelper::superClasses(entity);
|
|
auto wholeProperties = QHash<QString, QMetaProperty>();
|
|||
for (int var = classes.size() - 1; var >= 0; --var) {
|
|||
auto metaObject = classes.at(var);
|
|||
auto properties = EntityHelper::getMetaProperties(metaObject);
|
|||
auto iterator = properties.constBegin();
|
|||
while (iterator != properties.constEnd()) {
|
|||
wholeProperties.insert(iterator.key(), iterator.value());
|
|||
++iterator;
|
|||
}
|
|||
}
|
|||
return wholeProperties;
|
|||
}
|
|||
66704054 | Christian Ehringfeld | const QHash<Relation, QMetaProperty> EntityHelper::getRelationProperties(
|
|
const Entity *entity) {
|
|||
e8d1537c | Christian Ehringfeld | auto h = QHash<Relation, QMetaProperty>();
|
|
auto relations = entity->getRelations();
|
|||
for (int var = 0; var < entity->metaObject()->propertyCount(); ++var) {
|
|||
QMetaProperty m = entity->metaObject()->property(var);
|
|||
if (m.isValid() && relations.contains(QString(m.name()))) {
|
|||
h.insert(relations.value(m.name()), m);
|
|||
}
|
|||
}
|
|||
return h;
|
|||
}
|
|||
const char *EntityHelper::getClassname(const Entity *entity) {
|
|||
return entity->metaObject()->className();
|
|||
}
|
|||
66704054 | Christian Ehringfeld | const QString EntityHelper::getClassName(const Entity *entity) {
|
|
e8d1537c | Christian Ehringfeld | return QString(entity->metaObject()->className());
|
|
}
|
|||
void EntityHelper::setListProperty(const QSharedPointer<Entity> &entity,
|
|||
66704054 | Christian Ehringfeld | QList<QSharedPointer<Entity> > &list,
|
|
const QMetaProperty &property) {
|
|||
e8d1537c | Christian Ehringfeld | QVariant var;
|
|
var.setValue<QList<QSharedPointer<Entity>>>(list);
|
|||
66704054 | Christian Ehringfeld | property.write(entity.data(), var);
|
|
e8d1537c | Christian Ehringfeld | }
|
|
void EntityHelper::addEntityToListProperty(const QSharedPointer<Entity>
|
|||
&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);
|
|||
if (!list.contains(add)) {
|
|||
list.append(add);
|
|||
EntityHelper::setListProperty(entity, list, property);
|
|||
}
|
|||
}
|
|||
}
|
|||
48245e6a | Christian Ehringfeld | void EntityHelper::removeEntityFromListProperty(const QSharedPointer<Entity>
|
|
&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);
|
|||
for (int i = 0; i < list.size(); ++i) {
|
|||
auto e = list.at(i);
|
|||
67aa0d3f | Christian Ehringfeld | if (e->getId() == remove->getId()) {
|
|
48245e6a | Christian Ehringfeld | list.removeAt(i);
|
|
EntityHelper::setListProperty(entity, list, property);
|
|||
break;
|
|||
}
|
|||
}
|
|||
}
|
|||
}
|
|||
e8d1537c | Christian Ehringfeld | void EntityHelper::setProperty(const QSharedPointer<Entity> &entity,
|
|
66704054 | Christian Ehringfeld | QSharedPointer<Entity> value,
|
|
const QMetaProperty &property) {
|
|||
e8d1537c | Christian Ehringfeld | if (value && value->getProperty(value->getPrimaryKey()).toLongLong()
|
|
> -1) {
|
|||
QVariant var;
|
|||
var.setValue<QSharedPointer<Entity>>(value);
|
|||
property.write(entity.data(), var);
|
|||
}
|
|||
}
|
|||
66704054 | Christian Ehringfeld | void EntityHelper::setProperty(const QSharedPointer<Entity> &entity,
|
|
QSharedPointer<Entity> value, const QString property) {
|
|||
e8d1537c | Christian Ehringfeld | auto props = EntityHelper::getMetaProperties(entity.data());
|
|
66704054 | Christian Ehringfeld | if (props.contains(property)) {
|
|
e8d1537c | Christian Ehringfeld | QVariant var;
|
|
var.setValue<QSharedPointer<Entity>>(value);
|
|||
66704054 | Christian Ehringfeld | entity->setProperty(property, var);
|
|
e8d1537c | Christian Ehringfeld | }
|
|
}
|
|||
QMetaProperty EntityHelper::mappedProperty(const Relation &r,
|
|||
const QSharedPointer<Entity> &foreignEntity) {
|
|||
QMetaProperty prop;
|
|||
auto props = EntityHelper::getMetaProperties(foreignEntity.data());
|
|||
if (!r.getMappedBy().isEmpty() && props.contains(r.getMappedBy())) {
|
|||
prop = props.value(r.getMappedBy());
|
|||
} else {
|
|||
auto relations = foreignEntity->getRelations();
|
|||
for (auto iterator = relations.constBegin(); iterator != relations.constEnd();
|
|||
++iterator) {
|
|||
auto rel = iterator.value();
|
|||
if (rel.getMappedBy() == r.getPropertyName()) {
|
|||
prop = props.value(rel.getPropertyName());
|
|||
break;
|
|||
}
|
|||
}
|
|||
}
|
|||
return prop;
|
|||
}
|
|||
86e5c917 | Christian Ehringfeld | ||
QHash<QString, QVariant> EntityHelper::getEntityAttributes(
|
|||
const QHash<QString, QMetaProperty>
|
|||
&props,
|
|||
const QSharedPointer<Entity> &entity) {
|
|||
auto map = QHash<QString, QVariant>();
|
|||
auto transientAttrs = entity->getTransientAttributes();
|
|||
auto relations = entity->getRelations();
|
|||
auto i = props.constBegin();
|
|||
while (i != props.constEnd()) {
|
|||
if (!transientAttrs.contains(i.key()) && !relations.contains(i.key())) {
|
|||
map.insert(i.key(), i.value().read(entity.data()));
|
|||
}
|
|||
++i;
|
|||
}
|
|||
return map;
|
|||
}
|