Herunterladen als
root/src/entityhelper.cpp @ 7f1090e5
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(
|
|
612083d3 | Christian Ehringfeld | const Entity *entity) {
|
|
e8d1537c | Christian Ehringfeld | auto relations = entity->getRelations();
|
|
auto superObject = EntityInstanceFactory::newSuperClassInstance(entity);
|
|||
if (superObject) {
|
|||
auto superRelations = superObject->getRelations();
|
|||
f8b8663b | Christian Ehringfeld | for (auto i = superRelations.constBegin(); i != superRelations.constEnd(); ++i) {
|
|
if (relations.contains(i.key())) {
|
|||
relations.remove(i.key());
|
|||
e8d1537c | Christian Ehringfeld | }
|
|
}
|
|||
delete superObject;
|
|||
superObject = nullptr;
|
|||
}
|
|||
return relations;
|
|||
}
|
|||
66704054 | Christian Ehringfeld | const QList<const QMetaObject *> EntityHelper::superClasses(
|
|
612083d3 | Christian Ehringfeld | 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()) !=
|
|||
612083d3 | 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(
|
|
612083d3 | Christian Ehringfeld | const Entity *entity) {
|
|
e8d1537c | Christian Ehringfeld | return EntityHelper::getMetaProperties(entity->metaObject());
|
|
}
|
|||
66704054 | Christian Ehringfeld | const QHash<QString, QMetaProperty> EntityHelper::getSuperMetaProperties(
|
|
612083d3 | Christian Ehringfeld | 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(
|
|||
612083d3 | 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;
|
|||
}
|
|||
87739ae0 | Christian Ehringfeld | const QHash<QString, QMetaProperty> EntityHelper::getNonInheritedMetaProperties(
|
|
612083d3 | Christian Ehringfeld | const Entity *entity) {
|
|
87739ae0 | Christian Ehringfeld | 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();
|
|||
612083d3 | Christian Ehringfeld | ++iterator) {
|
|
87739ae0 | Christian Ehringfeld | if (props.contains(iterator.key())) {
|
|
props.remove(iterator.key());
|
|||
}
|
|||
}
|
|||
delete superObject;
|
|||
superObject = nullptr;
|
|||
}
|
|||
return props;
|
|||
}
|
|||
66704054 | Christian Ehringfeld | const QHash<QString, QMetaProperty> EntityHelper::getInheritedMetaProperties(
|
|
612083d3 | Christian Ehringfeld | const Entity *entity) {
|
|
87739ae0 | Christian Ehringfeld | auto props = EntityHelper::getMetaProperties(entity);
|
|
auto superObject = EntityInstanceFactory::newSuperClassInstance(entity);
|
|||
e8d1537c | Christian Ehringfeld | auto wholeProperties = QHash<QString, QMetaProperty>();
|
|
87739ae0 | Christian Ehringfeld | if (superObject) {
|
|
auto superProps = EntityHelper::getMetaProperties(superObject);
|
|||
auto iterator = superProps.constBegin();
|
|||
for (auto i = props.constBegin(); i != props.constEnd(); ++i) {
|
|||
if (!superProps.contains(iterator.key())) {
|
|||
wholeProperties.insert(iterator.key(), iterator.value());
|
|||
}
|
|||
e8d1537c | Christian Ehringfeld | }
|
|
87739ae0 | Christian Ehringfeld | delete superObject;
|
|
superObject = nullptr;
|
|||
e8d1537c | Christian Ehringfeld | }
|
|
return wholeProperties;
|
|||
}
|
|||
66704054 | Christian Ehringfeld | const QHash<Relation, QMetaProperty> EntityHelper::getRelationProperties(
|
|
612083d3 | Christian Ehringfeld | const Entity *entity) {
|
|
e8d1537c | Christian Ehringfeld | auto h = QHash<Relation, QMetaProperty>();
|
|
3b82c8c0 | Christian Ehringfeld | if(entity) {
|
|
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);
|
|||
}
|
|||
e8d1537c | Christian Ehringfeld | }
|
|
}
|
|||
return h;
|
|||
}
|
|||
612083d3 | Christian Ehringfeld | Entity* EntityHelper::copyObject(const Entity *entity) {
|
|
402ef73e | Christian Ehringfeld | auto metaObject = entity->metaObject();
|
|
auto newInstance = EntityInstanceFactory::createInstance(metaObject);
|
|||
if(newInstance) {
|
|||
for (int i = 0; i < metaObject->propertyCount(); ++i) {
|
|||
auto property = metaObject->property(i);
|
|||
8925defa | Christian Ehringfeld | if(property.isValid() && property.name() != entity->getPrimaryKey()) {
|
|
6be60ddf | Christian Ehringfeld | property.write(newInstance, property.read(entity));
|
|
402ef73e | Christian Ehringfeld | }
|
|
}
|
|||
}
|
|||
return newInstance;
|
|||
}
|
|||
4e75d5a1 | Christian Ehringfeld | Entity *EntityHelper::getBaseClassObject(const QSharedPointer<Entity> &entity,
|
|
QString attributeName) {
|
|||
auto superObject = EntityInstanceFactory::createInstance(entity->metaObject());
|
|||
auto objectBefore = superObject;
|
|||
bool first = true;
|
|||
while(superObject) {
|
|||
auto props = EntityHelper::getMetaProperties(superObject);
|
|||
if(!props.contains(attributeName) ||
|
|||
superObject->getInheritanceStrategy() == InheritanceStrategy::PER_CLASS_TABLE) {
|
|||
break;
|
|||
95d9cf46 | Christian Ehringfeld | } else if(!first) {
|
|
4e75d5a1 | Christian Ehringfeld | delete objectBefore;
|
|
objectBefore = nullptr;
|
|||
} else {
|
|||
first = false;
|
|||
}
|
|||
objectBefore = superObject;
|
|||
superObject = EntityInstanceFactory::newSuperClassInstance(superObject);
|
|||
}
|
|||
return objectBefore;
|
|||
}
|
|||
e8d1537c | Christian Ehringfeld | 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::addEntityToListProperty(const QSharedPointer<Entity>
|
|||
612083d3 | Christian Ehringfeld | &entity, QSharedPointer<Entity> add, const QMetaProperty &property) {
|
|
e8d1537c | Christian Ehringfeld | QVariant var = property.read(entity.data());
|
|
70e3d0c6 | Christian Ehringfeld | QList<QSharedPointer<Entity>> list = (!var.isNull() && var.data() &&
|
|
f8b8663b | Christian Ehringfeld | var.canConvert<QList<QVariant>>() ? EntityInstanceFactory::castQVariantList(
|
|
var) : QList<QSharedPointer<Entity>>());
|
|||
70e3d0c6 | Christian Ehringfeld | if (!list.contains(add)) {
|
|
list.append(add);
|
|||
51f88a34 | Christian Ehringfeld | EntityHelper::setListProperty(entity, list, property);
|
|
e8d1537c | Christian Ehringfeld | }
|
|
}
|
|||
48245e6a | Christian Ehringfeld | void EntityHelper::removeEntityFromListProperty(const QSharedPointer<Entity>
|
|
612083d3 | Christian Ehringfeld | &entity, QSharedPointer<Entity> remove, const QMetaProperty &property) {
|
|
48245e6a | Christian Ehringfeld | 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);
|
|
break;
|
|||
}
|
|||
}
|
|||
51f88a34 | Christian Ehringfeld | EntityHelper::setListProperty(entity, list, property);
|
|
23337ecc | Christian Ehringfeld | }
|
|
}
|
|||
void EntityHelper::clearEntityListProperty(const QSharedPointer<Entity> &entity,
|
|||
const QMetaProperty &property) {
|
|||
QVariant var = property.read(entity.data());
|
|||
if (var.canConvert<QList<QVariant>>()) {
|
|||
auto list = EntityInstanceFactory::castQVariantList(var);
|
|||
list.clear();
|
|||
51f88a34 | Christian Ehringfeld | EntityHelper::setListProperty(entity, list, property);
|
|
48245e6a | Christian Ehringfeld | }
|
|
}
|
|||
51f88a34 | Christian Ehringfeld | ||
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) {
|
|||
51f88a34 | Christian Ehringfeld | auto i = EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(property.typeName()));
|
|
if(i) {
|
|||
i->setProperty(entity, value, property);
|
|||
delete i;
|
|||
}
|
|||
e8d1537c | Christian Ehringfeld | }
|
|
}
|
|||
51f88a34 | Christian Ehringfeld | void EntityHelper::setListProperty(const QSharedPointer<Entity> &entity,
|
|
QList<QSharedPointer<Entity>> &value, const QMetaProperty &property) {
|
|||
auto i = EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(property.typeName()));
|
|||
if(i) {
|
|||
i->setListProperty(entity, value, property);
|
|||
delete i;
|
|||
e8d1537c | Christian Ehringfeld | }
|
|
}
|
|||
51f88a34 | Christian Ehringfeld | ||
e8d1537c | Christian Ehringfeld | QMetaProperty EntityHelper::mappedProperty(const Relation &r,
|
|
612083d3 | Christian Ehringfeld | const QSharedPointer<Entity> &foreignEntity) {
|
|
e8d1537c | Christian Ehringfeld | 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();
|
|||
612083d3 | Christian Ehringfeld | ++iterator) {
|
|
e8d1537c | Christian Ehringfeld | 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(
|
|||
612083d3 | Christian Ehringfeld | const QHash<QString, QMetaProperty>
|
|
&props,
|
|||
const QSharedPointer<Entity> &entity) {
|
|||
86e5c917 | Christian Ehringfeld | 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())) {
|
|||
95d9cf46 | Christian Ehringfeld | if(i.value().isEnumType()) {
|
|
map.insert(i.key(), i.value().read(entity.data()).toInt());
|
|||
} else {
|
|||
map.insert(i.key(), i.value().read(entity.data()));
|
|||
}
|
|||
86e5c917 | Christian Ehringfeld | }
|
|
++i;
|
|||
}
|
|||
return map;
|
|||
}
|