gov_wrapper.cpp 8.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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 <http://www.gnu.org/licenses/>.
 *
 * 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.
 */

24 25
#include <QScopedPointer>

26
#include "src/datovka_shared/gov_services/model/gov_form_list_model.h"
27
#include "src/datovka_shared/gov_services/model/gov_service_list_model.h"
28
#include "src/datovka_shared/gov_services/service/gov_mv_crr_vbh.h"
Karel Slaný's avatar
Karel Slaný committed
29
#include "src/datovka_shared/gov_services/service/gov_mv_ir_vp.h"
30
#include "src/datovka_shared/gov_services/service/gov_mv_rt_vt.h"
31
#include "src/datovka_shared/gov_services/service/gov_mv_rtpo_vt.h"
Karel Slaný's avatar
Karel Slaný committed
32
#include "src/datovka_shared/gov_services/service/gov_mv_skd_vp.h"
Karel Slaný's avatar
Karel Slaný committed
33
#include "src/datovka_shared/gov_services/service/gov_mv_vr_vp.h"
Karel Slaný's avatar
Karel Slaný committed
34
#include "src/datovka_shared/gov_services/service/gov_mv_zr_vp.h"
35
#include "src/datovka_shared/gov_services/service/gov_service.h"
36
#include "src/datovka_shared/gov_services/service/gov_szr_rob_vu.h"
37
#include "src/datovka_shared/gov_services/service/gov_szr_rob_vvu.h"
38
#include "src/datovka_shared/gov_services/service/gov_szr_ros_vv.h"
39 40 41 42 43
#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"
44
#include "src/gov_wrapper.h"
45
#include "src/models/accountmodel.h"
46
#include "src/net/isds_wrapper.h"
47 48
#include "src/sqlite/account_db.h"

49
GovWrapper::GovWrapper(IsdsWrapper *isds, QObject *parent)
50 51 52 53
    : QObject(parent),
    m_govServices(),
    m_isds(isds)
{
54
	initGovServices();
55 56
}

Karel Slaný's avatar
Karel Slaný committed
57 58
GovWrapper::~GovWrapper(void)
{
59
	clearGovServices();
60 61
}

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
/*!
 * @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<QString, const Gov::Service *> &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;
	}
}

88
void GovWrapper::initGovServices(void)
89 90 91
{
	debugFuncCall();

92
	clearGovServices();
93

Karel Slaný's avatar
Karel Slaný committed
94
	/* Výpis bodového hodnocení z Centrálního registru řidičů */
95
	insertService(m_govServices, new (std::nothrow) Gov::SrvcMvCrrVbh);
96

Karel Slaný's avatar
Karel Slaný committed
97
	/* Výpis z insolvenčního rejstříku */
98
	insertService(m_govServices, new (std::nothrow) Gov::SrvcMvIrVp);
Karel Slaný's avatar
Karel Slaný committed
99

Karel Slaný's avatar
Karel Slaný committed
100
	/* Výpis z Rejstříku trestů */
101
	insertService(m_govServices, new (std::nothrow) Gov::SrvcMvRtVt);
102

103
	/* Výpis z Rejstříku trestů právnických osob */
104
	insertService(m_govServices, new (std::nothrow) Gov::SrvcMvRtpoVt);
105

Karel Slaný's avatar
Karel Slaný committed
106
	/* Výpis ze seznamu kvalifikovaných dodavatelů */
107
	insertService(m_govServices, new (std::nothrow) Gov::SrvcMvSkdVp);
Karel Slaný's avatar
Karel Slaný committed
108

Karel Slaný's avatar
Karel Slaný committed
109
	/* Výpis z veřejného rejstříku */
110
	insertService(m_govServices, new (std::nothrow) Gov::SrvcMvVrVp);
Karel Slaný's avatar
Karel Slaný committed
111

Karel Slaný's avatar
Karel Slaný committed
112
	/* Výpis z živnostenského rejstříku */
113
	insertService(m_govServices, new (std::nothrow) Gov::SrvcMvZrVp);
Karel Slaný's avatar
Karel Slaný committed
114

Karel Slaný's avatar
Karel Slaný committed
115
	/* Výpis z Registru obyvatel */
116
	insertService(m_govServices, new (std::nothrow) Gov::SrvcSzrRobVu);
117

Karel Slaný's avatar
Karel Slaný committed
118
	/* Výpis o využití údajů z registru obyvatel */
119
	insertService(m_govServices, new (std::nothrow) Gov::SrvcSzrRobVvu);
120

121
	/* Veřejný výpis z registru osob */
122
	insertService(m_govServices, new (std::nothrow) Gov::SrvcSzrRosVv);
123 124
}

125 126 127 128
void GovWrapper::clearGovServices(void)
{
	debugFuncCall();

129 130
	foreach (const Gov::Service *gs, m_govServices.values()) {
		delete const_cast<Gov::Service *>(gs);
131 132 133 134
	}
	m_govServices.clear();
}

135 136
void GovWrapper::loadServicesToModel(const QString &userName,
    const QVariant &srvcModelVariant) const
137 138 139
{
	debugFuncCall();

140 141 142
	GovServiceListModel *srvcModel =
	    GovServiceListModel::fromVariant(srvcModelVariant);
	if (Q_UNLIKELY(srvcModel == Q_NULLPTR)) {
143
		logErrorNL("%s", "Cannot access gov services model.");
144
		Q_ASSERT(0);
145 146 147
		return;
	}

148 149 150
	const Isds::DbOwnerInfo dbOwnerInfo(
	    GlobInstcs::accountDbPtr->getOwnerInfo(userName));

151
	srvcModel->clearAll();
152

153 154 155 156 157 158 159 160 161 162
	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;
	}

163
	foreach (const QString &key, m_govServices.keys()) {
164 165
		const Gov::Service *cgs = m_govServices.value(key);
		if (Q_UNLIKELY(cgs == Q_NULLPTR)) {
166 167 168 169
			Q_ASSERT(0);
			continue;
		}
		/* Enlist only services which can be used. */
170 171
		if (cgs->canSend(dbOwnerInfo.dbType())) {
			srvcModel->appendService(cgs);
172 173 174
		} else {
			logInfo("User '%s' cannot use the e-gov service '%s'.",
			    userName.toUtf8().constData(),
175
			    cgs->internalId().toUtf8().constData());
176
		}
177 178 179
	}
}

180
void GovWrapper::loadFormToModel(const QString &userName,
181 182 183 184
    const QString &serviceInternalId, const QVariant &formModelVariant) const
{
	debugFuncCall();

185 186
	const Gov::Service *cgs = m_govServices.value(serviceInternalId, Q_NULLPTR);
	if (Q_UNLIKELY(cgs == Q_NULLPTR)) {
187 188 189 190 191
		logErrorNL("Cannot access gov service '%s'.",
		    serviceInternalId.toUtf8().constData());
		Q_ASSERT(0);
		return;
	}
192 193 194 195 196
	QScopedPointer<Gov::Service> gs(cgs->createNew());
	if (Q_UNLIKELY(gs.isNull())) {
		Q_ASSERT(0);
		return;
	}
197

198 199 200 201 202 203 204 205
	GovFormListModel *formModel =
	    GovFormListModel::fromVariant(formModelVariant);
	if (Q_UNLIKELY(formModel == Q_NULLPTR)) {
		logErrorNL("%s", "Cannot access gov form model.");
		Q_ASSERT(0);
		return;
	}

206 207 208 209 210 211
	/* Set content from data box. */
	const Isds::DbOwnerInfo dbOwnerInfo(
	    GlobInstcs::accountDbPtr->getOwnerInfo(userName));
	gs->setOwnerInfoFields(dbOwnerInfo);

	formModel->setEntries(gs->requiredFields());
212 213
}

214
bool GovWrapper::sendGovRequest(const QString &userName,
215
    const QString &serviceInternalId, const QVariant &formModelVariant) const
216 217 218
{
	debugFuncCall();

219 220
	const Gov::Service *cgs = m_govServices.value(serviceInternalId, Q_NULLPTR);
	if (cgs == Q_NULLPTR) {
221 222 223
		logErrorNL("Cannot access gov service '%s'.",
		    serviceInternalId.toUtf8().constData());
		Q_ASSERT(0);
224 225
		return false;
	}
226 227 228 229 230
	QScopedPointer<Gov::Service> gs(cgs->createNew());
	if (Q_UNLIKELY(gs.isNull())) {
		Q_ASSERT(0);
		return false;
	}
231

232 233 234 235 236
	const GovFormListModel *formModel =
	    GovFormListModel::fromVariant(formModelVariant);
	if (Q_UNLIKELY(formModel == Q_NULLPTR)) {
		logErrorNL("%s", "Cannot access gov form model.");
		Q_ASSERT(0);
237 238 239
		return false;
	}

240
	{
241
		QString service = tr("Request: %1").arg(gs->fullName());
242
		service.append("\n");
243
		service.append(tr("Recipient: %1").arg(gs->instituteName()));
244 245

		int msgResponse = Dialogues::message(Dialogues::QUESTION,
246 247 248
		    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);
249 250 251 252 253
		if (msgResponse == Dialogues::NO) {
			return false;
		}
	}

254 255 256 257
	if (GlobInstcs::accountDbPtr == Q_NULLPTR) {
		return false;
	}

258
	/* Set message content according to model data. */
259 260
	foreach (const Gov::FormField &ff, formModel->allEntries()) {
		if (!gs->setRequiredField(ff.key(), ff.val())) {
261
			logWarningNL("The gov service '%s' does not accept the key '%s'.",
262
			    gs->internalId().toUtf8().constData(),
263
			    ff.key().toUtf8().constData());
264 265
		}
	}
266
	if (!gs->haveAllMandatoryFields()) {
267
		logErrorNL("The gov service '%s' is missing some mandatory data.",
268
		    gs->internalId().toUtf8().constData());
269 270
		return false;
	}
271

272
	const Isds::Message msg(gs->dataMessage());
273 274 275 276
	if (Q_UNLIKELY(msg.isNull())) {
		Q_ASSERT(0);
		return false;
	}
277 278 279 280 281 282 283

	if (Q_NULLPTR != m_isds) {
		return m_isds->sendGovRequest(userName, msg);
	}

	return false;
}