gov_wrapper.cpp 7.34 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 27 28 29 30 31 32
#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_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"
34 35 36 37 38 39 40 41
#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"
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 71 72 73 74 75 76 77 78 79 80 81 82 83
{
	debugFuncCall();

	m_govServices.clear();

	/* Výpis z Registru obyvatel */
	GovSzrRobVu *gs1 = new GovSzrRobVu();
	m_govServices.insert(gs1->serviceId(), gs1);

	/* 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);

	/* Výpis bodového hodnocení z Centrálního registru řidičů */
84
	Gov::Service *gs4 = new (std::nothrow) Gov::SrvcMvCrrVbh;
85
	if (gs4 != Q_NULLPTR) {
86 87 88 89 90 91 92 93
		const QString &key(gs4->internalId());
		if (!m_govServices.contains(key)) {
			m_govServices.insert(key, gs4);
		} else {
			logError(
			    "Key '%s' already exists in gov services container.",
			    key.toUtf8().constData());
		}
94
	}
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

	/* Výpis z Rejstříku trestů právnických osob */
	GovMvRtpoVt *gs5 = new GovMvRtpoVt();
	m_govServices.insert(gs5->serviceId(), gs5);

	/* Výpis z Rejstříku trestů */
	GovMvRtVt *gs6 = new GovMvRtVt();
	m_govServices.insert(gs6->serviceId(), gs6);

	/* Výpis z živnostenského rejstříku */
	GovMvZrVp *gs7 = new GovMvZrVp();
	m_govServices.insert(gs7->serviceId(), gs7);

	/* Výpis z veřejného rejstříku */
	GovMvVrVp *gs8 = new GovMvVrVp();
	m_govServices.insert(gs8->serviceId(), gs8);

	/* Výpis z insolvenčního rejstříku */
	GovMvIrVp *gs9 = new GovMvIrVp();
	m_govServices.insert(gs9->serviceId(), gs9);

	/* Výpis ze seznamu kvalifikovaných dodavatelů */
	GovMvSkdVp *gs10 = new GovMvSkdVp();
	m_govServices.insert(gs10->serviceId(), gs10);
}

121
void GovWrapper::loadServicesToModel(const QVariant &srvcModelVariant) const
122 123 124
{
	debugFuncCall();

125 126 127
	GovServiceListModel *srvcModel =
	    GovServiceListModel::fromVariant(srvcModelVariant);
	if (Q_UNLIKELY(srvcModel == Q_NULLPTR)) {
128
		logErrorNL("%s", "Cannot access gov services model.");
129
		Q_ASSERT(0);
130 131 132
		return;
	}

133
	srvcModel->clearAll();
134

135
	foreach (const QString &key, m_govServices.keys()) {
136
		srvcModel->appendEntry(GovServiceListModel::Entry(
137 138 139 140
		    m_govServices.value(key)->internalId(),
		    m_govServices.value(key)->fullName(),
		    m_govServices.value(key)->instituteName(),
		    m_govServices.value(key)->boxId()
141 142 143 144
		));
	}
}

145
void GovWrapper::loadFormToModel(const QString &userName,
146 147 148 149 150 151
    const QString &serviceInternalId, const QVariant &formModelVariant) const
{
	debugFuncCall();

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

152 153
	Gov::Service *govSrvc = m_govServices.value(serviceInternalId, Q_NULLPTR);
	if (Q_UNLIKELY(govSrvc == Q_NULLPTR)) {
154 155 156 157 158 159
		logErrorNL("Cannot access gov service '%s'.",
		    serviceInternalId.toUtf8().constData());
		Q_ASSERT(0);
		return;
	}

160 161 162 163 164 165 166 167
	GovFormListModel *formModel =
	    GovFormListModel::fromVariant(formModelVariant);
	if (Q_UNLIKELY(formModel == Q_NULLPTR)) {
		logErrorNL("%s", "Cannot access gov form model.");
		Q_ASSERT(0);
		return;
	}

168
	formModel->setEntries(govSrvc->requiredFields());
169 170
}

171
bool GovWrapper::sendGovRequest(const QString &userName,
172
    const QString &serviceInternalId, const QVariant &formModelVariant) const
173 174 175
{
	debugFuncCall();

176 177
	Gov::Service *govSrvc = m_govServices.value(serviceInternalId, Q_NULLPTR);
	if (govSrvc == Q_NULLPTR) {
178 179 180
		logErrorNL("Cannot access gov service '%s'.",
		    serviceInternalId.toUtf8().constData());
		Q_ASSERT(0);
181 182 183
		return false;
	}

184 185 186 187 188
	const GovFormListModel *formModel =
	    GovFormListModel::fromVariant(formModelVariant);
	if (Q_UNLIKELY(formModel == Q_NULLPTR)) {
		logErrorNL("%s", "Cannot access gov form model.");
		Q_ASSERT(0);
189 190 191
		return false;
	}

192
	{
193
		QString service = tr("Request: %1").arg(govSrvc->fullName());
194
		service.append("\n");
195
		service.append(tr("Recipient: %1").arg(govSrvc->instituteName()));
196 197 198 199

		int msgResponse = Dialogues::message(Dialogues::QUESTION,
		    tr("Send GOV request"),
		    tr("Do you want to send the GOV request to data box '%1'?").
200
		        arg(govSrvc->boxId()), service,
201 202 203 204 205 206
		    Dialogues::NO | Dialogues::YES, Dialogues::NO);
		if (msgResponse == Dialogues::NO) {
			return false;
		}
	}

207 208 209 210
	if (GlobInstcs::accountDbPtr == Q_NULLPTR) {
		return false;
	}

211
	/* Set message content according to model data. */
212 213
	foreach (const Gov::RequiredInput &ri, formModel->allEntries()) {
		if (!govSrvc->setRequiredField(ri.key, ri.val)) {
214
			logWarningNL("The gov service '%s' does not accept the key '%s'.",
215
			    govSrvc->internalId().toUtf8().constData(),
216 217 218
			    ri.key.toUtf8().constData());
		}
	}
219
	if (!govSrvc->haveAllMandatoryFields()) {
220
		logErrorNL("The gov service '%s' is missing some mandatory data.",
221
		    govSrvc->internalId().toUtf8().constData());
222 223
		return false;
	}
224

225 226
	const Isds::DbOwnerInfo dbOwnerInfo(
	    GlobInstcs::accountDbPtr->getOwnerInfo(userName));
227

228
	const Isds::Message msg(govSrvc->dataMessage(dbOwnerInfo));
229 230 231 232
	if (Q_UNLIKELY(msg.isNull())) {
		Q_ASSERT(0);
		return false;
	}
233 234 235 236 237 238 239

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

	return false;
}