Herunterladen als
root/src/entity.cpp @ 92f2130d
81c23b56 | Christian Ehringfeld | /*
|
|
4d58ef6a | 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/>.
|
|||
*/
|
|||
81c23b56 | Christian Ehringfeld | ||
#include "entity.h"
|
|||
e8d1537c | Christian Ehringfeld | #include "entityhelper.h"
|
|
82442988 | Christian Ehringfeld | #include "entityinstancefactory.h"
|
|
9cf4747e | Christian Ehringfeld | using namespace CuteEntityManager;
|
|
81c23b56 | Christian Ehringfeld | ||
9d05e414 | Christian Ehringfeld | Entity::Entity(QObject *parent) : QObject(parent) {
|
|
2c49732b | Christian Ehringfeld | this->id = -1;
|
|
81c23b56 | Christian Ehringfeld | }
|
|
2e43da3f | Christian Ehringfeld | QString Entity::toString() const {
|
|
b5d490c7 | Christian Ehringfeld | QString r = "";
|
|
e8d1537c | Christian Ehringfeld | r.append(EntityHelper::getClassName(this));
|
|
b5d490c7 | Christian Ehringfeld | r.append(": {");
|
|
e8d1537c | Christian Ehringfeld | auto properties = EntityHelper::getMetaProperties(this);
|
|
b5d490c7 | Christian Ehringfeld | for (auto var = properties.constBegin(); var != properties.constEnd(); ++var) {
|
|
6c58eb3f | Christian Ehringfeld | QString val = "";
|
|
auto value = var.value().read(this);
|
|||
if (var.value().isEnumType()) {
|
|||
val = var.value().enumerator().valueToKey(var.value().read(this).toInt());
|
|||
} else if (value.canConvert<QList<QVariant>>()) {
|
|||
val.append("[");
|
|||
82442988 | Christian Ehringfeld | auto list = EntityInstanceFactory::castQVariantList(value);
|
|
int size = list.size();
|
|||
val.append(QString::number(size));
|
|||
val.append(" ");
|
|||
val.append(size == 1 ? "element" : "elements");
|
|||
6c58eb3f | Christian Ehringfeld | val.append("]");
|
|
} else {
|
|||
val = value.toString();
|
|||
}
|
|||
r.append(var.key() + ": " + val + ", ");
|
|||
b5d490c7 | Christian Ehringfeld | }
|
|
r.append("}");
|
|||
return r;
|
|||
9d05e414 | Christian Ehringfeld | }
|
|
612083d3 | Christian Ehringfeld | Entity* Entity::copy() const {
|
|
402ef73e | Christian Ehringfeld | return EntityHelper::copyObject(this);
|
|
}
|
|||
6c58eb3f | Christian Ehringfeld | QString Entity::slimToString() const {
|
|
QString r = "";
|
|||
e8d1537c | Christian Ehringfeld | r.append(EntityHelper::getClassName(this));
|
|
6c58eb3f | Christian Ehringfeld | r.append(": {");
|
|
r.append("id: ") + this->getId() + "}";
|
|||
return r;
|
|||
}
|
|||
fc14f551 | Christian Ehringfeld | QList<ErrorMsg> Entity::getErrors() const {
|
|
return errors;
|
|||
}
|
|||
e80feccc | Christian Ehringfeld | QString Entity::getErrorsAsString() const {
|
|
QList<ErrorMsg> errors = this->getErrors();
|
|||
bool first = true;
|
|||
QString r = "";
|
|||
for (int i = 0; i < errors.size(); ++i) {
|
|||
ErrorMsg msg = errors.at(i);
|
|||
if (first) {
|
|||
first = false;
|
|||
} else {
|
|||
r += ";";
|
|||
}
|
|||
r += msg.getErrorMsg();
|
|||
}
|
|||
return r;
|
|||
}
|
|||
fc14f551 | Christian Ehringfeld | void Entity::setErrors(const QList<ErrorMsg> &value) {
|
|
e80feccc | Christian Ehringfeld | this->errors = value;
|
|
fc14f551 | Christian Ehringfeld | }
|
|
6c58eb3f | Christian Ehringfeld | ||
81c23b56 | Christian Ehringfeld | Entity::~Entity() {
|
|
ba800d6d | Christian Ehringfeld | ||
81c23b56 | Christian Ehringfeld | }
|
|
fc14f551 | Christian Ehringfeld | QList<ValidationRule> Entity::validationRules() const {
|
|
return QList<ValidationRule>();
|
|||
}
|
|||
2e43da3f | Christian Ehringfeld | QString Entity::getTablename() const {
|
|
2075db87 | Christian Ehringfeld | return QString(this->metaObject()->className()).toLower();
|
|
caea9141 | Christian Ehringfeld | }
|
|
7e233492 | Christian Ehringfeld | ||
a06633f7 | Christian Ehringfeld | const QHash<QString, Relation> Entity::getRelations() const {
|
|
24425325 | Christian Ehringfeld | return QHash<QString, Relation>();
|
|
24c5480c | Christian Ehringfeld | }
|
|
a06633f7 | Christian Ehringfeld | const QStringList Entity::getTransientAttributes() const {
|
|
7e233492 | Christian Ehringfeld | return QStringList();
|
|
}
|
|||
24c5480c | Christian Ehringfeld | ||
a06633f7 | Christian Ehringfeld | const QStringList Entity::getBLOBColumns() const {
|
|
24c5480c | Christian Ehringfeld | return QStringList();
|
|
}
|
|||
2e43da3f | Christian Ehringfeld | InheritanceStrategy Entity::getInheritanceStrategy() const {
|
|
da565582 | Christian Ehringfeld | return InheritanceStrategy::JOINED_TABLE;
|
|
1a3b37ba | Christian Ehringfeld | }
|
|
da3ce9cf | Christian Ehringfeld | bool Entity::isInheritanceCascaded() const {
|
|
return true;
|
|||
}
|
|||
2e43da3f | Christian Ehringfeld | QString Entity::getPrimaryKey() const {
|
|
24c5480c | Christian Ehringfeld | return "id";
|
|
}
|
|||
813205af | Christian Ehringfeld | ||
b5d490c7 | Christian Ehringfeld | QVariant Entity::getProperty(const QString &name) const {
|
|
abb9e8c5 | Christian Ehringfeld | if (!name.isEmpty()) {
|
|
return QObject::property(name.toLatin1().constData());
|
|||
}
|
|||
return QVariant();
|
|||
da3ce9cf | Christian Ehringfeld | }
|
|
b5d490c7 | Christian Ehringfeld | bool Entity::setProperty(const QString &name, const QVariant &value) {
|
|
return QObject::setProperty(name.toLatin1().constData(), value);
|
|||
}
|
|||
813205af | Christian Ehringfeld | qint64 Entity::getId() const {
|
|
586bb527 | Christian Ehringfeld | return id;
|
|
}
|
|||
813205af | Christian Ehringfeld | void Entity::setId(const qint64 &value) {
|
|
a06633f7 | Christian Ehringfeld | if (value != this->id) {
|
|
a205e8a9 | Christian Ehringfeld | id = value;
|
|
emit idChanged();
|
|||
}
|
|||
586bb527 | Christian Ehringfeld | }
|
|
fc14f551 | Christian Ehringfeld | ||
bool Entity::hasErrors() const {
|
|||
return !this->errors.isEmpty();
|
|||
}
|