Herunterladen als
root/src/validators/uniquevalidator.cpp @ 827458ed
ec6a9500 | Christian Ehringfeld | #include "uniquevalidator.h"
|
|
10cc871a | Christian Ehringfeld | #include "entitymanager.h"
|
|
ec6a9500 | Christian Ehringfeld | using namespace CuteEntityManager;
|
|
28d2f01a | Christian Ehringfeld | UniqueValidator::UniqueValidator() : Validator() {
|
|
}
|
|||
ec6a9500 | Christian Ehringfeld | ||
28d2f01a | Christian Ehringfeld | QList<ErrorMsg> UniqueValidator::validate(QVariant value,
|
|
QList<Param> params) const {
|
|||
QList<ErrorMsg> msgs = QList<ErrorMsg>();
|
|||
if (!params.isEmpty()) {
|
|||
QString targetAttribute = "";
|
|||
QString targetClass = "";
|
|||
QString entityManagerID = "";
|
|||
for (int i = 0; i < params.size(); ++i) {
|
|||
if (params.at(i).getName() == "targetAttribute") {
|
|||
targetAttribute = params.at(i).getValue().toString();
|
|||
} else if (params.at(i).getName() == "entityManagerID") {
|
|||
entityManagerID = params.at(i).getValue().toString();
|
|||
} else if (params.at(i).getName() == "targetClass") {
|
|||
targetClass = params.at(i).getValue().toString();
|
|||
}
|
|||
}
|
|||
EntityManager *em = nullptr;
|
|||
if (entityManagerID.isEmpty()) {
|
|||
em = EntityManager::getDefaultInstance();
|
|||
} else {
|
|||
em = EntityManager::getInstance(entityManagerID);
|
|||
}
|
|||
if (!em) {
|
|||
msgs.append(ErrorMsg("", "No entitymanager."));
|
|||
} else {
|
|||
Query q = Query();
|
|||
auto e = EntityInstanceFactory::createInstance(
|
|||
targetClass.toLatin1().constData());
|
|||
q.appendFrom(e->getTablename());
|
|||
delete e;
|
|||
e = nullptr;
|
|||
QHash<QString, QVariant> params = QHash<QString, QVariant>();
|
|||
params.insert(targetAttribute, value);
|
|||
a72dc7ae | Christian Ehringfeld | q.appendWhere(em->getQueryBuilder()->where(params));
|
|
28d2f01a | Christian Ehringfeld | if (em->count(q) > 0) {
|
|
msgs.append(ErrorMsg("",
|
|||
"<property> \"" + value.toString() + "\" has already been taken."));
|
|||
}
|
|||
}
|
|||
} else {
|
|||
msgs.append(ErrorMsg("", "UniqueValidator has not enough parameters."));
|
|||
}
|
|||
return msgs;
|
|||
ec6a9500 | Christian Ehringfeld | }
|
|
ErrorMsg UniqueValidator::validateParam(QVariant value, Param param) const {
|
|||
28d2f01a | Christian Ehringfeld | Q_UNUSED(value)
|
|
Q_UNUSED(param)
|
|||
return ErrorMsg();
|
|||
ec6a9500 | Christian Ehringfeld | }
|