/*
* Copyright (C) 2014-2018 CZ.NIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 General Public License
* along with this program. If not, see .
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*/
#include
#include "src/datovka_shared/gov_services/model/gov_form_list_model.h"
#include "src/datovka_shared/gov_services/model/gov_service_list_model.h"
#include "src/datovka_shared/gov_services/service/gov_mv_crr_vbh.h"
#include "src/datovka_shared/gov_services/service/gov_mv_ir_vp.h"
#include "src/datovka_shared/gov_services/service/gov_mv_rt_vt.h"
#include "src/datovka_shared/gov_services/service/gov_mv_rtpo_vt.h"
#include "src/datovka_shared/gov_services/service/gov_mv_skd_vp.h"
#include "src/datovka_shared/gov_services/service/gov_mv_vr_vp.h"
#include "src/datovka_shared/gov_services/service/gov_mv_zr_vp.h"
#include "src/datovka_shared/gov_services/service/gov_service.h"
#include "src/datovka_shared/gov_services/service/gov_szr_rob_vu.h"
#include "src/datovka_shared/gov_services/service/gov_szr_rob_vvu.h"
#include "src/datovka_shared/gov_services/service/gov_szr_ros_vv.h"
#include "src/datovka_shared/isds/message_interface.h"
#include "src/datovka_shared/log/log.h"
#include "src/datovka_shared/log/memory_log.h"
#include "src/dialogues/dialogues.h"
#include "src/global.h"
#include "src/gov_wrapper.h"
#include "src/models/accountmodel.h"
#include "src/net/isds_wrapper.h"
#include "src/sqlite/account_db.h"
GovWrapper::GovWrapper(IsdsWrapper *isds, QObject *parent)
: QObject(parent),
m_govServices(),
m_isds(isds)
{
initGovServices();
}
GovWrapper::~GovWrapper(void)
{
clearGovServices();
}
/*!
* @brief Add a newly allocated service into the service container.
*
* @note The service object is freed when it cannot be added into the container.
*
* @param[in] map Service container.
* @param[in] gs Newly allocated service object.
*/
static
void insertService(QMap &map,
Gov::Service *gs)
{
if (Q_UNLIKELY(gs == Q_NULLPTR)) {
return;
}
const QString &key(gs->internalId());
if (!map.contains(key)) {
map.insert(key, gs);
} else {
logError("Key '%s' already exists in gov service container.",
key.toUtf8().constData());
delete gs;
}
}
void GovWrapper::initGovServices(void)
{
debugFuncCall();
clearGovServices();
/* Výpis bodového hodnocení z Centrálního registru řidičů */
insertService(m_govServices, new (std::nothrow) Gov::SrvcMvCrrVbh);
/* Výpis z insolvenčního rejstříku */
insertService(m_govServices, new (std::nothrow) Gov::SrvcMvIrVp);
/* Výpis z Rejstříku trestů */
insertService(m_govServices, new (std::nothrow) Gov::SrvcMvRtVt);
/* Výpis z Rejstříku trestů právnických osob */
insertService(m_govServices, new (std::nothrow) Gov::SrvcMvRtpoVt);
/* Výpis ze seznamu kvalifikovaných dodavatelů */
insertService(m_govServices, new (std::nothrow) Gov::SrvcMvSkdVp);
/* Výpis z veřejného rejstříku */
insertService(m_govServices, new (std::nothrow) Gov::SrvcMvVrVp);
/* Výpis z živnostenského rejstříku */
insertService(m_govServices, new (std::nothrow) Gov::SrvcMvZrVp);
/* Výpis z Registru obyvatel */
insertService(m_govServices, new (std::nothrow) Gov::SrvcSzrRobVu);
/* Výpis o využití údajů z registru obyvatel */
insertService(m_govServices, new (std::nothrow) Gov::SrvcSzrRobVvu);
/* Veřejný výpis z registru osob */
insertService(m_govServices, new (std::nothrow) Gov::SrvcSzrRosVv);
}
void GovWrapper::clearGovServices(void)
{
debugFuncCall();
foreach (const Gov::Service *gs, m_govServices.values()) {
delete const_cast(gs);
}
m_govServices.clear();
}
void GovWrapper::loadServicesToModel(const QString &userName,
const QVariant &srvcModelVariant) const
{
debugFuncCall();
GovServiceListModel *srvcModel =
GovServiceListModel::fromVariant(srvcModelVariant);
if (Q_UNLIKELY(srvcModel == Q_NULLPTR)) {
logErrorNL("%s", "Cannot access gov services model.");
Q_ASSERT(0);
return;
}
const Isds::DbOwnerInfo dbOwnerInfo(
GlobInstcs::accountDbPtr->getOwnerInfo(userName));
srvcModel->clearAll();
if ((*GlobInstcs::acntMapPtr)[userName].isTestAccount()) {
/*
* The e-gov services aren't available in the testing
* environment.
*/
logWarningNL("%s",
"Cannot access e-gov services from an testing account.");
return;
}
foreach (const QString &key, m_govServices.keys()) {
const Gov::Service *cgs = m_govServices.value(key);
if (Q_UNLIKELY(cgs == Q_NULLPTR)) {
Q_ASSERT(0);
continue;
}
/* Enlist only services which can be used. */
if (cgs->canSend(dbOwnerInfo.dbType())) {
srvcModel->appendService(cgs);
} else {
logInfo("User '%s' cannot use the e-gov service '%s'.",
userName.toUtf8().constData(),
cgs->internalId().toUtf8().constData());
}
}
}
void GovWrapper::loadFormToModel(const QString &userName,
const QString &serviceInternalId, const QVariant &formModelVariant) const
{
debugFuncCall();
const Gov::Service *cgs = m_govServices.value(serviceInternalId, Q_NULLPTR);
if (Q_UNLIKELY(cgs == Q_NULLPTR)) {
logErrorNL("Cannot access gov service '%s'.",
serviceInternalId.toUtf8().constData());
Q_ASSERT(0);
return;
}
QScopedPointer gs(cgs->createNew());
if (Q_UNLIKELY(gs.isNull())) {
Q_ASSERT(0);
return;
}
GovFormListModel *formModel =
GovFormListModel::fromVariant(formModelVariant);
if (Q_UNLIKELY(formModel == Q_NULLPTR)) {
logErrorNL("%s", "Cannot access gov form model.");
Q_ASSERT(0);
return;
}
/* Set content from data box. */
const Isds::DbOwnerInfo dbOwnerInfo(
GlobInstcs::accountDbPtr->getOwnerInfo(userName));
gs->setOwnerInfoFields(dbOwnerInfo);
formModel->setEntries(gs->requiredFields());
}
bool GovWrapper::sendGovRequest(const QString &userName,
const QString &serviceInternalId, const QVariant &formModelVariant) const
{
debugFuncCall();
const Gov::Service *cgs = m_govServices.value(serviceInternalId, Q_NULLPTR);
if (cgs == Q_NULLPTR) {
logErrorNL("Cannot access gov service '%s'.",
serviceInternalId.toUtf8().constData());
Q_ASSERT(0);
return false;
}
QScopedPointer gs(cgs->createNew());
if (Q_UNLIKELY(gs.isNull())) {
Q_ASSERT(0);
return false;
}
const GovFormListModel *formModel =
GovFormListModel::fromVariant(formModelVariant);
if (Q_UNLIKELY(formModel == Q_NULLPTR)) {
logErrorNL("%s", "Cannot access gov form model.");
Q_ASSERT(0);
return false;
}
{
QString service = tr("Request: %1").arg(gs->fullName());
service.append("\n");
service.append(tr("Recipient: %1").arg(gs->instituteName()));
int msgResponse = Dialogues::message(Dialogues::QUESTION,
tr("Send e-gov request"),
tr("Do you want to send the e-gov request to data box '%1'?").arg(gs->boxId()),
service, Dialogues::NO | Dialogues::YES, Dialogues::NO);
if (msgResponse == Dialogues::NO) {
return false;
}
}
if (GlobInstcs::accountDbPtr == Q_NULLPTR) {
return false;
}
/* Set message content according to model data. */
foreach (const Gov::FormField &ff, formModel->allEntries()) {
if (!gs->setRequiredField(ff.key(), ff.val())) {
logWarningNL("The gov service '%s' does not accept the key '%s'.",
gs->internalId().toUtf8().constData(),
ff.key().toUtf8().constData());
}
}
if (!gs->haveAllMandatoryFields()) {
logErrorNL("The gov service '%s' is missing some mandatory data.",
gs->internalId().toUtf8().constData());
return false;
}
const Isds::Message msg(gs->dataMessage());
if (Q_UNLIKELY(msg.isNull())) {
Q_ASSERT(0);
return false;
}
if (Q_NULLPTR != m_isds) {
return m_isds->sendGovRequest(userName, msg);
}
return false;
}