mirror of
https://gitlab.com/spaceti-app/integrations/acs/colnod-connector.git
synced 2026-07-12 11:50:37 +02:00
add dry mode, compare '>' for tokens doesnt work correctly
This commit is contained in:
parent
e0e0d5be7e
commit
921fc2cb77
@ -18,6 +18,7 @@ Acces Management System of Colsys: [Product info](http://produkty.colsys.cz/coln
|
||||
- To DB
|
||||
- Number of subjects added/deleted/updated
|
||||
- Number of tokens added/deleted/updated
|
||||
|
||||
## Requirements
|
||||
|
||||
- QPSQL driver must be installed (PostgreSQL)
|
||||
|
||||
@ -2,11 +2,19 @@
|
||||
|
||||
#include "datamanager.h"
|
||||
#include "colnodapi.h"
|
||||
#include <QCommandLineParser>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication a(argc, argv);
|
||||
auto *api = new ColnodAPI();
|
||||
QCoreApplication app(argc, argv);
|
||||
QCoreApplication::setApplicationName("colnod-connector");
|
||||
QCoreApplication::setApplicationVersion("1.0");
|
||||
QCommandLineParser parser;
|
||||
parser.setApplicationDescription("Dry mode");
|
||||
QCommandLineOption dryModeOption("dryMode", "ColnodAPI", "Read-only mode, prints changes which normally be doing");
|
||||
parser.addOption(dryModeOption);
|
||||
parser.process(app);
|
||||
auto *api = new ColnodAPI(true); // parser.isSet(dryModeOption));
|
||||
api->loadDataFromDB();
|
||||
api->loadDataFromColnod();
|
||||
QObject::connect(api, &ColnodAPI::dataDownloaded, [=](){
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
#include "colnodapi.h"
|
||||
|
||||
ColnodAPI::ColnodAPI(bool debug)
|
||||
ColnodAPI::ColnodAPI(bool dryMode)
|
||||
{
|
||||
reqMan = new RequestManager();
|
||||
dataMan = new DataManager();
|
||||
debugMode = debug;
|
||||
dataMan = new DataManager(dryMode);
|
||||
this->dryMode = dryMode;
|
||||
}
|
||||
|
||||
void ColnodAPI::loadDataFromColnod()
|
||||
@ -67,12 +67,11 @@ void ColnodAPI::loadDeletedDataFromColnod()
|
||||
// fill structures in data manager (tokens to updata/to delte)
|
||||
void ColnodAPI::loadDataFromDB()
|
||||
{
|
||||
dataMan->fillTokensToUpdateDB();
|
||||
dataMan->fillTokensToChangeDB();
|
||||
}
|
||||
|
||||
void ColnodAPI::syncDataFromColnodToDB()
|
||||
{
|
||||
dataMan->filterTokensColnod();
|
||||
syncUpdatedDataFromColnodToDB();
|
||||
if (dataMan->getLastSync() != 0)
|
||||
syncDeletedDataFromColnodToDB();
|
||||
@ -95,13 +94,17 @@ void ColnodAPI::syncDeletedDataFromColnodToDB()
|
||||
// push changes form DB to Colnod
|
||||
void ColnodAPI::syncDataFromDBToColnod()
|
||||
{
|
||||
dataMan->filterTokensDB();
|
||||
|
||||
if (!dataMan->getTokensToUpdateDB().contains("empty")){
|
||||
syncDeletedDataFromDBToColnod();
|
||||
}
|
||||
else
|
||||
deleteFinished = true;
|
||||
if (!dataMan->getTokensToDeleteDB().contains("empty")){
|
||||
syncUpdatedDataFromDBToColnod();
|
||||
}
|
||||
else
|
||||
updateFinished = true;
|
||||
connect(this, &ColnodAPI::dataUpdatedToColnod, dataMan, &DataManager::clear);
|
||||
connect(this, &ColnodAPI::dataUpdatedToColnod, dataMan, &DataManager::setLastSync);
|
||||
connect(this, &ColnodAPI::dataUpdatedToColnod, [=](){
|
||||
@ -121,17 +124,25 @@ void ColnodAPI::syncDataFromDBToColnod()
|
||||
void ColnodAPI::syncUpdatedDataFromDBToColnod()
|
||||
{
|
||||
auto tokensToUpdate = dataMan->getTokensToUpdateDB();
|
||||
Response* resp0 = reqMan->setTokens(tokensToUpdate);
|
||||
connect(resp0, &Response::ready, [=](){
|
||||
updateFinished = true;
|
||||
areDataUpdatedToColnod();
|
||||
resp0->deleteLater();
|
||||
});
|
||||
if (dryMode){
|
||||
for (const auto &token : tokensToUpdate)
|
||||
qInfo() << "Updating token: " << token->idToStringWithoutBraces() << "to Colnod";
|
||||
}
|
||||
else {
|
||||
Response* resp0 = reqMan->setTokens(tokensToUpdate);
|
||||
connect(resp0, &Response::ready, [=](){
|
||||
updateFinished = true;
|
||||
areDataUpdatedToColnod();
|
||||
resp0->deleteLater();
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// delete tokens in Colnod
|
||||
void ColnodAPI::syncDeletedDataFromDBToColnod()
|
||||
{
|
||||
dataMan->filterTokensDB();
|
||||
auto tokensToDelete = dataMan->getTokensToDeleteDB();
|
||||
Response* resp1 = reqMan->deleteTokens(tokensToDelete);
|
||||
connect(resp1, &Response::ready, [=](){
|
||||
|
||||
@ -9,7 +9,7 @@ class ColnodAPI : public QObject
|
||||
|
||||
public:
|
||||
|
||||
ColnodAPI(bool debugMode = false);
|
||||
ColnodAPI(bool dryMode = false);
|
||||
|
||||
void syncDataFromColnodToDB();
|
||||
void syncDataFromDBToColnod();
|
||||
@ -47,7 +47,7 @@ private:
|
||||
private:
|
||||
RequestManager* reqMan = nullptr;
|
||||
DataManager* dataMan = nullptr;
|
||||
bool debugMode = false;
|
||||
bool dryMode = false;
|
||||
bool updateFinished = false;
|
||||
bool deleteFinished = false;
|
||||
};
|
||||
|
||||
@ -1,27 +1,22 @@
|
||||
#include "databasemanager.h"
|
||||
|
||||
DatabaseManager::DatabaseManager(const QString& host, const QString& dbName, const QString& user, const QString& password)
|
||||
DatabaseManager::DatabaseManager(const QString& host, const QString& dbName, const QString& user, const QString& password, const bool &dryMode)
|
||||
{
|
||||
|
||||
db = QSqlDatabase::addDatabase("QPSQL");
|
||||
|
||||
db.setDatabaseName(dbName);
|
||||
db.setHostName(host);
|
||||
db.setUserName(user);
|
||||
// home-office stuff
|
||||
//db.setPort(80);
|
||||
//
|
||||
db.setUserName(user); // home-office stuff -->> db.setPort(80);
|
||||
db.setPassword(password);
|
||||
|
||||
if (!db.open())
|
||||
{
|
||||
qDebug() << "Opening error" + db.lastError().text();
|
||||
}
|
||||
query = QSqlQuery("query", db);
|
||||
this->dryMode = dryMode;
|
||||
}
|
||||
|
||||
// add or update subject to db
|
||||
void DatabaseManager::saveSubject(Subject* subject, const QString& table)
|
||||
int DatabaseManager::saveSubject(Subject* subject, const QString& table)
|
||||
{
|
||||
QString id = subject->idToStringWithoutBraces();
|
||||
if (checkIfExists(id, table))
|
||||
@ -34,6 +29,7 @@ void DatabaseManager::saveSubject(Subject* subject, const QString& table)
|
||||
query.bindValue(":last_update", QDateTime::fromMSecsSinceEpoch(subject->getLastUpdate()).toString("yyyy-MM-dd hh:mm:ss.zzz"));
|
||||
query.bindValue(":pin", subject->getPin());
|
||||
executeQuery(query);
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -45,11 +41,12 @@ void DatabaseManager::saveSubject(Subject* subject, const QString& table)
|
||||
query.bindValue(":name", subject->getName());
|
||||
query.bindValue(":pin", subject->getPin());
|
||||
executeQuery(query);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// add or update token to DB
|
||||
void DatabaseManager::saveToken(Token* token, const QString& table)
|
||||
int DatabaseManager::saveToken(Token* token, const QString& table)
|
||||
{
|
||||
QString id = token->idToStringWithoutBraces();
|
||||
if (checkIfExists(id, table))
|
||||
@ -70,6 +67,7 @@ void DatabaseManager::saveToken(Token* token, const QString& table)
|
||||
query.bindValue(":last_modified", QDateTime::fromMSecsSinceEpoch(token->getLastUpdate()).toString("yyyy-MM-dd hh:mm:ss.zzz"));
|
||||
query.bindValue(":deleted", token->getDeleted());
|
||||
executeQuery(query);
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -90,6 +88,7 @@ void DatabaseManager::saveToken(Token* token, const QString& table)
|
||||
query.bindValue(":last_modified", QDateTime::fromMSecsSinceEpoch(token->getLastUpdate()).toString("yyyy-MM-dd hh:mm:ss.zzz"));
|
||||
query.bindValue(":deleted", token->getDeleted());
|
||||
executeQuery(query);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
@ -140,29 +139,60 @@ bool DatabaseManager::checkIfExists(const QString& id, const QString& table)
|
||||
return query.value(0).toBool();
|
||||
}
|
||||
|
||||
QString DatabaseManager::getLastQuery(const QSqlQuery& query) const
|
||||
{
|
||||
QString str = query.lastQuery();
|
||||
QMapIterator<QString, QVariant> it(query.boundValues());
|
||||
while (it.hasNext())
|
||||
{
|
||||
it.next();
|
||||
str.replace(it.key(),it.value().toString());
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
void DatabaseManager::executeNonPreperedQuery(const QString &statement, QSqlQuery &query) const
|
||||
{
|
||||
if (dryMode){
|
||||
if (statement.contains("SELECT"))
|
||||
query.exec(statement);
|
||||
}
|
||||
else{
|
||||
query.exec(statement);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool DatabaseManager::executeQuery(QSqlQuery &query) const
|
||||
{
|
||||
bool res = query.exec();
|
||||
return res;
|
||||
|
||||
if (dryMode){
|
||||
QString sqlStatement = getLastQuery(query);
|
||||
qDebug() << sqlStatement;
|
||||
if (sqlStatement.contains("SELECT"))
|
||||
query.exec();
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
bool res = query.exec();
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
void DatabaseManager::dropTables()
|
||||
{
|
||||
query.exec("DROP TABLE IF EXISTS colnod_token, colnod_subject, colnod_subject_attribute");
|
||||
executeNonPreperedQuery("DROP TABLE IF EXISTS colnod_token, colnod_subject, colnod_subject_attribute", query);
|
||||
}
|
||||
|
||||
// create table after deleting, it is used for full sync
|
||||
void DatabaseManager::createTables()
|
||||
{
|
||||
query.exec("CREATE TABLE colnod_subject ("
|
||||
executeNonPreperedQuery("CREATE TABLE colnod_subject ("
|
||||
"id character(36) primary key NOT NULL,"
|
||||
"description character varying(255),"
|
||||
"last_update timestamp NOT NULL,"
|
||||
"name character varying(255),"
|
||||
"pin character varying(255))");
|
||||
|
||||
query.exec("CREATE TABLE colnod_token ("
|
||||
"pin character varying(255))", query);
|
||||
executeNonPreperedQuery("CREATE TABLE colnod_token ("
|
||||
"id CHAR(36) primary key NOT NULL,"
|
||||
"auth_errors INTEGER NOT NULL,"
|
||||
"description VARCHAR(255),"
|
||||
@ -176,19 +206,18 @@ void DatabaseManager::createTables()
|
||||
"token_type VARCHAR(255),"
|
||||
"subject_id CHAR(36),"
|
||||
"last_modified timestamp,"
|
||||
"deleted BOOLEAN)");
|
||||
|
||||
query.exec("CREATE TABLE colnod_subject_attribute ("
|
||||
"deleted BOOLEAN)", query);
|
||||
executeNonPreperedQuery("CREATE TABLE colnod_subject_attribute ("
|
||||
"id VARCHAR(255) primary key,"
|
||||
"attribute_key VARCHAR(255),"
|
||||
"attribute_value VARCHAR(255),"
|
||||
"subject_id CHAR(36))");
|
||||
"subject_id CHAR(36))", query);
|
||||
}
|
||||
|
||||
// return last_sync time
|
||||
quint64 DatabaseManager::getLastSync()
|
||||
{
|
||||
query.exec("SELECT last_sync FROM config");
|
||||
executeNonPreperedQuery("SELECT last_sync FROM config", query);
|
||||
query.next();
|
||||
quint64 temp = query.value(0).toULongLong();
|
||||
return temp;
|
||||
@ -207,18 +236,18 @@ void DatabaseManager::updateLastSync(quint64 lastSync)
|
||||
QSqlQuery* DatabaseManager::getUpdatedTokens()
|
||||
{
|
||||
QSqlQuery* qry = new QSqlQuery;
|
||||
qry->exec("SELECT id, auth_errors, description, enabled, last_update, name, no_validity, time_to_live, token_bits, token_data, token_type, subject_id, last_modified, deleted "
|
||||
"FROM colnod_token WHERE EXTRACT(EPOCH FROM last_modified) * 1000 > EXTRACT(EPOCH FROM last_update) * 1000");
|
||||
executeNonPreperedQuery("SELECT id, auth_errors, description, enabled, last_update, name, no_validity, time_to_live, token_bits, token_data, token_type, subject_id, last_modified, deleted "
|
||||
"FROM colnod_token WHERE EXTRACT(EPOCH FROM last_modified) * 1000 > EXTRACT(EPOCH FROM last_update) * 1000", *qry);
|
||||
return qry;
|
||||
}
|
||||
|
||||
QSqlQuery* DatabaseManager::getToken(const QString& id)
|
||||
{
|
||||
QSqlQuery* qry = new QSqlQuery;
|
||||
qry->prepare("SELECT SELECT id, auth_errors, description, enabled, last_update, name, no_validity, time_to_live, token_bits, token_data, token_type, subject_id, last_modified, deleted "
|
||||
qry->prepare("SELECT id, auth_errors, description, enabled, last_update, name, no_validity, time_to_live, token_bits, token_data, token_type, subject_id, last_modified, deleted "
|
||||
"FROM colnod_token WHERE id = :id");
|
||||
qry->bindValue(":id", id);
|
||||
qry->exec();
|
||||
executeQuery(*qry);
|
||||
if (qry->next()){
|
||||
return qry;
|
||||
}
|
||||
@ -227,7 +256,7 @@ QSqlQuery* DatabaseManager::getToken(const QString& id)
|
||||
|
||||
quint64 DatabaseManager::getNow()
|
||||
{
|
||||
query.exec("SELECT EXTRACT(EPOCH FROM NOW())*1000");
|
||||
executeNonPreperedQuery("SELECT EXTRACT(EPOCH FROM NOW())*1000", query);
|
||||
query.next();
|
||||
return query.value(0).toULongLong();
|
||||
}
|
||||
|
||||
@ -16,10 +16,10 @@ class DatabaseManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
DatabaseManager(const QString& host, const QString& dbName, const QString& user, const QString& password);
|
||||
DatabaseManager(const QString& host, const QString& dbName, const QString& user, const QString& password, const bool& dryMode = false);
|
||||
|
||||
void saveSubject(Subject* subject, const QString& table);
|
||||
void saveToken(Token* token, const QString& table);
|
||||
int saveSubject(Subject* subject, const QString& table);
|
||||
int saveToken(Token* token, const QString& table);
|
||||
void saveSubjectAttributes(Subject* subject, const QString& table);
|
||||
void setDeleted(const QString& id, const QString& table);
|
||||
|
||||
@ -38,8 +38,11 @@ public:
|
||||
private:
|
||||
bool checkIfExists(const QString& id, const QString& table);
|
||||
bool executeQuery(QSqlQuery &query) const;
|
||||
QString getLastQuery(const QSqlQuery &query) const;
|
||||
void executeNonPreperedQuery(const QString &statement, QSqlQuery& query) const;
|
||||
|
||||
private:
|
||||
QSqlDatabase db;
|
||||
QSqlQuery query;
|
||||
bool dryMode = false;
|
||||
};
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
#include "datamanager.h"
|
||||
|
||||
DataManager::DataManager()
|
||||
DataManager::DataManager(const bool &dryMode)
|
||||
{
|
||||
db = new DatabaseManager("192.168.1.94", "deloitte", "postgres", "34rjkciea12");
|
||||
db = new DatabaseManager("192.168.1.94", "deloitte", "postgres", "34rjkciea12", dryMode);
|
||||
nextLastSync = db->getNow();
|
||||
lastSync = db->getLastSync();
|
||||
qDebug() << lastSync;
|
||||
this->dryMode = dryMode;
|
||||
//db = new DatabaseManager("localhost", "deloitte", "postgres", "34rjkciea12");
|
||||
}
|
||||
|
||||
@ -25,6 +25,7 @@ void DataManager::fillFullTokensColnod(const QString& response)
|
||||
}
|
||||
else
|
||||
{
|
||||
qCritical() << "Colnod is Empty";
|
||||
auto token = new Token("empty");
|
||||
allTokensFromColnod.insert(token->idToStringWithoutBraces(), token);
|
||||
}
|
||||
@ -44,11 +45,13 @@ void DataManager::fillTokensToUpdateColnod(const QString& response)
|
||||
token->setDeleted(false);
|
||||
tokensToUpdateFromColnodToDB.insert(token->idToStringWithoutBraces(), token);
|
||||
}
|
||||
qInfo() << tokensToUpdateFromColnodToDB.size() << " tokens to update from Colnod";
|
||||
}
|
||||
else
|
||||
{
|
||||
qInfo() << "0 tokens to update from Colnod";
|
||||
auto token = new Token("empty");
|
||||
tokensToUpdateFromColnodToDB.insert(token->idToStringWithoutBraces(), token);
|
||||
tokensToUpdateFromColnodToDB.insert(token->getName(), token);
|
||||
}
|
||||
}
|
||||
|
||||
@ -65,16 +68,18 @@ void DataManager::fillSubjectsToUpdateColnod(const QString& response)
|
||||
subject->fromJSON(item.toObject());
|
||||
subjectsToUpdateFromColnodToDB.insert(subject->idToStringWithoutBraces(), subject);
|
||||
}
|
||||
qInfo() << subjectsToUpdateFromColnodToDB.size() << " subjects to update from Colnod";
|
||||
}
|
||||
else
|
||||
{
|
||||
qInfo() << "0 subjects to update from Colnod";
|
||||
Subject* subject = new Subject("empty");
|
||||
subjectsToUpdateFromColnodToDB.insert(subject->idToStringWithoutBraces(), subject);
|
||||
subjectsToUpdateFromColnodToDB.insert(subject->getName(), subject);
|
||||
}
|
||||
}
|
||||
|
||||
// load working sets with updated/deleted tokens from DB
|
||||
void DataManager::fillTokensToUpdateDB()
|
||||
void DataManager::fillTokensToChangeDB()
|
||||
{
|
||||
QSqlQuery* query = db->getUpdatedTokens();
|
||||
while (query->next())
|
||||
@ -88,8 +93,8 @@ void DataManager::fillTokensToUpdateDB()
|
||||
tokensToUpdateFromDBToColnod.insert(token->idToStringWithoutBraces(), token);
|
||||
}
|
||||
}
|
||||
qDebug() << "Number of tokens to update from DB: "<< tokensToUpdateFromDBToColnod.size();
|
||||
qDebug() << "Number of tokens to delete from DB: "<< tokensFromDBToDeleteFromColnod.size();
|
||||
qDebug() << tokensToUpdateFromDBToColnod.size() << " tokens to update from DB";
|
||||
qDebug() << tokensFromDBToDeleteFromColnod.size() << " tokens to delete from DB";
|
||||
}
|
||||
|
||||
void DataManager::fillSubjectsToUpdateDB()
|
||||
@ -107,13 +112,15 @@ void DataManager::fillSubjectsToDeleteColnod(const QString& json)
|
||||
for (QJsonValueRef item : items) {
|
||||
subjectsFromColnodToDeleteFromDB.append(item.toString());
|
||||
}
|
||||
qInfo() << subjectsFromColnodToDeleteFromDB.size() << " subjects deleted in Colnod";
|
||||
}
|
||||
else {
|
||||
qInfo() << "0 subjects deleted in Colnod";
|
||||
subjectsFromColnodToDeleteFromDB.append("empty");
|
||||
}
|
||||
}
|
||||
|
||||
// load working set with tokenst to delete from colnod
|
||||
// load working set with tokens to delete from colnod
|
||||
void DataManager::fillTokensToDeleteColnod(const QString& json)
|
||||
{
|
||||
QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8());
|
||||
@ -123,8 +130,10 @@ void DataManager::fillTokensToDeleteColnod(const QString& json)
|
||||
for (QJsonValueRef item : items) {
|
||||
tokensFromColnodToDeleteFromDB.append(item.toString());
|
||||
}
|
||||
qInfo() << tokensFromColnodToDeleteFromDB.size() << " tokens deleted in Colnod";
|
||||
}
|
||||
else {
|
||||
qInfo() << "0 tokens deleted in Colnod";
|
||||
tokensFromColnodToDeleteFromDB.append("empty");
|
||||
}
|
||||
}
|
||||
@ -138,52 +147,54 @@ void DataManager::fillSubjectsToDeleteDB()
|
||||
void DataManager::pushSubjectsToDB()
|
||||
{
|
||||
if (subjectsToUpdateFromColnodToDB.contains("empty")) {
|
||||
qDebug() << "All subjects are up to date";
|
||||
return;
|
||||
}
|
||||
//QSqlDatabase::database().transaction();
|
||||
qDebug() << "Subjects in progress...";
|
||||
int updated = 0, added = 0;
|
||||
qInfo() << "Subjects in progress...";
|
||||
for (const auto& subject : subjectsToUpdateFromColnodToDB)
|
||||
{
|
||||
db->saveSubject(subject, "colnod_subject");
|
||||
if (db->saveSubject(subject, "colnod_subject") > 0)
|
||||
added++;
|
||||
else
|
||||
updated++;
|
||||
db->saveSubjectAttributes(subject, "colnod_subject_attribute");
|
||||
}
|
||||
//QSqlDatabase::database().commit();
|
||||
qDebug() << subjectsToUpdateFromColnodToDB.size() << " subjects has been updated";
|
||||
|
||||
qInfo() << added << " subjects added to DB";
|
||||
qInfo() << updated << " subjects updated to DB";
|
||||
}
|
||||
|
||||
// update tokens from colnod to DB
|
||||
void DataManager::pushTokensToDB()
|
||||
{
|
||||
if (tokensToUpdateFromColnodToDB.contains("empty"))
|
||||
{
|
||||
qDebug() << "All tokens are up to date";
|
||||
if (tokensToUpdateFromColnodToDB.contains("empty")){
|
||||
return;
|
||||
}
|
||||
//QSqlDatabase::database().transaction();
|
||||
qDebug() << "Tokens in progress...";
|
||||
filterTokensColnod();
|
||||
int updated = 0, added = 0;
|
||||
qInfo() << "Tokens in progress...";
|
||||
for (const auto& token : tokensToUpdateFromColnodToDB)
|
||||
{
|
||||
db->saveToken(token, "colnod_token");
|
||||
if (db->saveToken(token, "colnod_token") > 0)
|
||||
added++;
|
||||
else
|
||||
updated++;
|
||||
}
|
||||
//QSqlDatabase::database().commit();
|
||||
qDebug() << tokensToUpdateFromColnodToDB.size() << " tokens has been updated";
|
||||
qInfo() << added << " tokens added to DB";
|
||||
qInfo() << updated << " tokens updated to DB";
|
||||
}
|
||||
|
||||
void DataManager::deleteSubjectsFromDB()
|
||||
{
|
||||
if (subjectsFromColnodToDeleteFromDB[0] == "empty")
|
||||
{
|
||||
qDebug() << "No subjects to be deleted";
|
||||
return;
|
||||
}
|
||||
qDebug() << "Deleted subjects in progress...";
|
||||
qInfo() << "Deleting subjects in progress...";
|
||||
for (const auto& subjectId : subjectsFromColnodToDeleteFromDB)
|
||||
{
|
||||
db->setDeleted(subjectId, "colnod_subject");
|
||||
}
|
||||
qDebug() << subjectsFromColnodToDeleteFromDB.size() << "subject has beed deleted";
|
||||
qInfo() << subjectsFromColnodToDeleteFromDB.size() << " subject deleted from DB";
|
||||
}
|
||||
|
||||
// "delete" (set deleted true) tokens in DB depends on Colnod
|
||||
@ -191,7 +202,7 @@ void DataManager::setDeletedToTokensFromDB()
|
||||
{
|
||||
if (tokensFromColnodToDeleteFromDB[0] == "empty")
|
||||
{
|
||||
qDebug() << "No tokens to be deleted";
|
||||
qDebug() << "0 tokens to be deleted";
|
||||
return;
|
||||
}
|
||||
qDebug() << "Deleted tokens in progress...";
|
||||
@ -199,7 +210,7 @@ void DataManager::setDeletedToTokensFromDB()
|
||||
{
|
||||
db->setDeleted(tokenId, "colnod_token");
|
||||
}
|
||||
qDebug() << tokensFromColnodToDeleteFromDB.size() << "tokens has beed deleted";
|
||||
qDebug() << tokensFromColnodToDeleteFromDB.size() << "tokens deleted from DB";
|
||||
}
|
||||
|
||||
// do I have loaded all data
|
||||
@ -262,6 +273,7 @@ void DataManager::filterTokensColnod()
|
||||
auto tokenDB = new Token;
|
||||
tokenDB->fromSQLRecord(record);
|
||||
Q_ASSERT(tokenDB != token);
|
||||
qDebug() << tokenDB->getLastModified() << " > " << token->getLastUpdate();
|
||||
if (tokenDB > token)
|
||||
tokensToUpdateFromColnodToDB.remove(id);
|
||||
}
|
||||
|
||||
@ -14,7 +14,7 @@ class DataManager : public QObject
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
DataManager();
|
||||
DataManager(const bool& dryMode = false);
|
||||
|
||||
void fillFullTokensColnod(const QString& jsonResponse);
|
||||
|
||||
@ -23,7 +23,7 @@ public:
|
||||
void fillSubjectsToDeleteColnod(const QString& jsonResponse);
|
||||
void fillTokensToDeleteColnod(const QString& jsonResponse);
|
||||
|
||||
void fillTokensToUpdateDB();
|
||||
void fillTokensToChangeDB();
|
||||
void fillSubjectsToUpdateDB();
|
||||
void fillSubjectsToDeleteDB();
|
||||
|
||||
@ -71,4 +71,6 @@ private:
|
||||
QList<QString> subjectsToDeleteDB;
|
||||
QList<QString> tokensFromDBToDeleteFromColnod;
|
||||
|
||||
bool dryMode = false;
|
||||
|
||||
};
|
||||
|
||||
@ -60,7 +60,7 @@ QNetworkReply* RequestManager::postRequest(const QString& endpoint, const QByteA
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
qDebug() << "Authorized successfully, response ready.";
|
||||
//qDebug() << "Authorized successfully, response ready.";
|
||||
emit responseReady();
|
||||
reply->deleteLater();
|
||||
});
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
|
||||
#include <QJsonArray>
|
||||
|
||||
Subject::Subject(const QString& name)
|
||||
Subject::Subject(const QString &name)
|
||||
{
|
||||
setName(name);
|
||||
}
|
||||
|
||||
@ -17,7 +17,7 @@ Token::Token(const QString& name, QString tokenData, const QString& decription)
|
||||
|
||||
}
|
||||
|
||||
Token::Token(const QString& name)
|
||||
Token::Token(const QString &name)
|
||||
{
|
||||
this->setName(name);
|
||||
}
|
||||
@ -67,14 +67,16 @@ void Token::fromSQLRecord(QSqlQuery *query)
|
||||
if (query->value(3).toBool())
|
||||
flags.insert("Enabled");
|
||||
this->flags = flags;
|
||||
setLastUpdate(query->value(4).toUInt());
|
||||
QDateTime date = query->value(4).toDateTime();
|
||||
setLastUpdate(date.toMSecsSinceEpoch());
|
||||
setName(query->value(5).toString());
|
||||
timeToLive = query->value(7).toInt();
|
||||
tokenBits = query->value(8).toInt();
|
||||
tokenData = query->value(9).toString();
|
||||
tokenType = query->value(10).toString();
|
||||
subjectId = query->value(11).toUuid();
|
||||
lastModified = query->value(12).toUInt();
|
||||
date = query->value(12).toDateTime();
|
||||
lastModified = date.toMSecsSinceEpoch();
|
||||
deleted = query->value(13).toBool();
|
||||
}
|
||||
|
||||
|
||||
@ -4,6 +4,8 @@
|
||||
#include <QSet>
|
||||
#include <qsqlquery.h>
|
||||
#include <QVariant>
|
||||
#include <QDebug>
|
||||
#include <QDateTime>
|
||||
|
||||
class Token : public Entity {
|
||||
public:
|
||||
@ -13,10 +15,10 @@ public:
|
||||
Token(const QString& name, QString tokenData, const QString& decription = "mobile");
|
||||
Token(const QString& name);
|
||||
|
||||
friend bool operator<(const Token &rToken, const Token &lToken){
|
||||
return rToken.getLastModified() < lToken.getLastUpdate();
|
||||
friend bool operator<(const Token &lToken, const Token &rToken){
|
||||
return lToken.getLastModified() < rToken.getLastUpdate();
|
||||
}
|
||||
friend bool operator>(const Token &rToken, const Token &lToken){
|
||||
friend bool operator>(const Token &lToken, const Token &rToken){
|
||||
return rToken.getLastModified() > lToken.getLastUpdate();
|
||||
}
|
||||
|
||||
|
||||
@ -12,7 +12,7 @@ void ComplexTest::initTestCase()
|
||||
deleteTokensRespSpy.wait(timeout);
|
||||
QTest::qWait(2500);
|
||||
start = QDateTime::currentMSecsSinceEpoch()-2000;
|
||||
api = new ColnodAPI;
|
||||
api = new ColnodAPI(true);
|
||||
qDebug() << start;
|
||||
}
|
||||
|
||||
@ -117,8 +117,13 @@ void ComplexTest::fullSync()
|
||||
api->loadDataFromColnod();
|
||||
QSignalSpy spy(api, &ColnodAPI::dataDownloaded);
|
||||
spy.wait(timeout);
|
||||
api->loadDataFromDB();
|
||||
api->syncDataFromColnodToDB();
|
||||
// api->syncDataToColnod();
|
||||
api->syncDataFromDBToColnod();
|
||||
QSignalSpy processSpy(api, &ColnodAPI::dataUpdatedToColnod);
|
||||
processSpy.wait(timeout);
|
||||
QSignalSpy finish(api, &ColnodAPI::syncFinished);
|
||||
finish.wait(timeout);
|
||||
}
|
||||
|
||||
void ComplexTest::syncLastUpdated()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user