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

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

55
void GovWrapper::deleteGovServices(void)
56 57 58 59 60 61 62 63 64
{
	debugFuncCall();

	QStringList keys = m_govServices.keys();
	foreach (const QString &key, keys) {
		delete m_govServices.take(key);
	}
}

65
void GovWrapper::initGovServices(void)
66 67 68 69 70
{
	debugFuncCall();

	m_govServices.clear();

Karel Slaný's avatar
Karel Slaný committed
71
	Gov::Service *gs;
72

Karel Slaný's avatar
Karel Slaný committed
73 74 75
//	/* Výpis z Registru obyvatel */
//	GovSzrRobVu *gs1 = new GovSzrRobVu();
//	m_govServices.insert(gs1->serviceId(), gs1);
76

Karel Slaný's avatar
Karel Slaný committed
77 78 79 80 81 82 83
//	/* Výpis o využití údajů z registru obyvatel */
//	GovSzrRobVvu *gs2 = new GovSzrRobVvu();
//	m_govServices.insert(gs2->serviceId(), gs2);

//	/* Veřejný výpis z registru osob */
//	GovSzrRosVv *gs3 = new GovSzrRosVv();
//	m_govServices.insert(gs3->serviceId(), gs3);
84 85

	/* Výpis bodového hodnocení z Centrálního registru řidičů */
Karel Slaný's avatar
Karel Slaný committed
86 87 88
	gs = new (std::nothrow) Gov::SrvcMvCrrVbh;
	if (gs != Q_NULLPTR) {
		const QString &key(gs->internalId());
89
		if (!m_govServices.contains(key)) {
Karel Slaný's avatar
Karel Slaný committed
90
			m_govServices.insert(key, gs);
91 92 93 94 95
		} else {
			logError(
			    "Key '%s' already exists in gov services container.",
			    key.toUtf8().constData());
		}
96
	}
97

Karel Slaný's avatar
Karel Slaný committed
98 99 100
//	/* Výpis z Rejstříku trestů právnických osob */
//	GovMvRtpoVt *gs5 = new GovMvRtpoVt();
//	m_govServices.insert(gs5->serviceId(), gs5);
101 102

	/* Výpis z Rejstříku trestů */
Karel Slaný's avatar
Karel Slaný committed
103 104 105 106 107 108 109 110 111 112 113
	gs = new (std::nothrow) Gov::SrvcMvRtVt;
	if (gs != Q_NULLPTR) {
		const QString &key(gs->internalId());
		if (!m_govServices.contains(key)) {
			m_govServices.insert(key, gs);
		} else {
			logError(
			    "Key '%s' already exists in gov services container.",
			    key.toUtf8().constData());
		}
	}
114

Karel Slaný's avatar
Karel Slaný committed
115 116 117
//	/* Výpis z živnostenského rejstříku */
//	GovMvZrVp *gs7 = new GovMvZrVp();
//	m_govServices.insert(gs7->serviceId(), gs7);
118

Karel Slaný's avatar
Karel Slaný committed
119 120 121
//	/* Výpis z veřejného rejstříku */
//	GovMvVrVp *gs8 = new GovMvVrVp();
//	m_govServices.insert(gs8->serviceId(), gs8);
122

Karel Slaný's avatar
Karel Slaný committed
123 124 125
//	/* Výpis z insolvenčního rejstříku */
//	GovMvIrVp *gs9 = new GovMvIrVp();
//	m_govServices.insert(gs9->serviceId(), gs9);
126

Karel Slaný's avatar
Karel Slaný committed
127 128 129
//	/* Výpis ze seznamu kvalifikovaných dodavatelů */
//	GovMvSkdVp *gs10 = new GovMvSkdVp();
//	m_govServices.insert(gs10->serviceId(), gs10);
130 131
}

132
void GovWrapper::loadServicesToModel(const QVariant &srvcModelVariant) const
133 134 135
{
	debugFuncCall();

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

144
	srvcModel->clearAll();
145

146
	foreach (const QString &key, m_govServices.keys()) {
147
		srvcModel->appendEntry(GovServiceListModel::Entry(
148 149 150 151
		    m_govServices.value(key)->internalId(),
		    m_govServices.value(key)->fullName(),
		    m_govServices.value(key)->instituteName(),
		    m_govServices.value(key)->boxId()
152 153 154 155
		));
	}
}

156
void GovWrapper::loadFormToModel(const QString &userName,
157 158 159 160 161 162
    const QString &serviceInternalId, const QVariant &formModelVariant) const
{
	debugFuncCall();

	Q_UNUSED(userName); /* Currently unused. */

163 164
	Gov::Service *govSrvc = m_govServices.value(serviceInternalId, Q_NULLPTR);
	if (Q_UNLIKELY(govSrvc == Q_NULLPTR)) {
165 166 167 168 169 170
		logErrorNL("Cannot access gov service '%s'.",
		    serviceInternalId.toUtf8().constData());
		Q_ASSERT(0);
		return;
	}

171 172 173 174 175 176 177 178
	GovFormListModel *formModel =
	    GovFormListModel::fromVariant(formModelVariant);
	if (Q_UNLIKELY(formModel == Q_NULLPTR)) {
		logErrorNL("%s", "Cannot access gov form model.");
		Q_ASSERT(0);
		return;
	}

179
	formModel->setEntries(govSrvc->requiredFields());
180 181
}

182
bool GovWrapper::sendGovRequest(const QString &userName,
183
    const QString &serviceInternalId, const QVariant &formModelVariant) const
184 185 186
{
	debugFuncCall();

187 188
	Gov::Service *govSrvc = m_govServices.value(serviceInternalId, Q_NULLPTR);
	if (govSrvc == Q_NULLPTR) {
189 190 191
		logErrorNL("Cannot access gov service '%s'.",
		    serviceInternalId.toUtf8().constData());
		Q_ASSERT(0);
192 193 194
		return false;
	}

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

203
	{
204
		QString service = tr("Request: %1").arg(govSrvc->fullName());
205
		service.append("\n");
206
		service.append(tr("Recipient: %1").arg(govSrvc->instituteName()));
207 208 209 210

		int msgResponse = Dialogues::message(Dialogues::QUESTION,
		    tr("Send GOV request"),
		    tr("Do you want to send the GOV request to data box '%1'?").
211
		        arg(govSrvc->boxId()), service,
212 213 214 215 216 217
		    Dialogues::NO | Dialogues::YES, Dialogues::NO);
		if (msgResponse == Dialogues::NO) {
			return false;
		}
	}

218 219 220 221
	if (GlobInstcs::accountDbPtr == Q_NULLPTR) {
		return false;
	}

222
	/* Set message content according to model data. */
223 224
	foreach (const Gov::RequiredInput &ri, formModel->allEntries()) {
		if (!govSrvc->setRequiredField(ri.key, ri.val)) {
225
			logWarningNL("The gov service '%s' does not accept the key '%s'.",
226
			    govSrvc->internalId().toUtf8().constData(),
227 228 229
			    ri.key.toUtf8().constData());
		}
	}
230
	if (!govSrvc->haveAllMandatoryFields()) {
231
		logErrorNL("The gov service '%s' is missing some mandatory data.",
232
		    govSrvc->internalId().toUtf8().constData());
233 234
		return false;
	}
235

236 237
	const Isds::DbOwnerInfo dbOwnerInfo(
	    GlobInstcs::accountDbPtr->getOwnerInfo(userName));
238

239
	const Isds::Message msg(govSrvc->dataMessage(dbOwnerInfo));
240 241 242 243
	if (Q_UNLIKELY(msg.isNull())) {
		Q_ASSERT(0);
		return false;
	}
244 245 246 247 248 249 250

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

	return false;
}