gov_wrapper.cpp 5.67 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/service/gov_service.h"
27
#include "src/datovka_shared/gov_services/service/gov_services_all.h"
28 29 30 31 32
#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"
33 34
#include "src/gov_services/models/gov_form_list_model.h"
#include "src/gov_services/models/gov_service_list_model.h"
35
#include "src/gov_wrapper.h"
36
#include "src/models/accountmodel.h"
37
#include "src/net/isds_wrapper.h"
38 39
#include "src/sqlite/account_db.h"

40
GovWrapper::GovWrapper(IsdsWrapper *isds, QObject *parent)
41 42 43 44
    : QObject(parent),
    m_govServices(),
    m_isds(isds)
{
45
	m_govServices = Gov::allServiceMap();
46 47
}

Karel Slaný's avatar
Karel Slaný committed
48 49
GovWrapper::~GovWrapper(void)
{
50
	Gov::clearServiceMap(m_govServices);
51 52
}

53 54
void GovWrapper::loadServicesToModel(const QString &userName,
    const QVariant &srvcModelVariant) const
55 56 57
{
	debugFuncCall();

58 59 60
	GovServiceListModel *srvcModel =
	    GovServiceListModel::fromVariant(srvcModelVariant);
	if (Q_UNLIKELY(srvcModel == Q_NULLPTR)) {
61
		logErrorNL("%s", "Cannot access gov services model.");
62
		Q_ASSERT(0);
63 64 65
		return;
	}

66 67 68
	const Isds::DbOwnerInfo dbOwnerInfo(
	    GlobInstcs::accountDbPtr->getOwnerInfo(userName));

69
	srvcModel->clearAll();
70

71 72 73 74 75 76 77 78 79 80
	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;
	}

81
	foreach (const QString &key, m_govServices.keys()) {
82 83
		const Gov::Service *cgs = m_govServices.value(key);
		if (Q_UNLIKELY(cgs == Q_NULLPTR)) {
84 85 86 87
			Q_ASSERT(0);
			continue;
		}
		/* Enlist only services which can be used. */
88 89
		if (cgs->canSend(dbOwnerInfo.dbType())) {
			srvcModel->appendService(cgs);
90 91 92
		} else {
			logInfo("User '%s' cannot use the e-gov service '%s'.",
			    userName.toUtf8().constData(),
93
			    cgs->internalId().toUtf8().constData());
94
		}
95 96 97
	}
}

98
void GovWrapper::loadFormToModel(const QString &userName,
99 100 101 102
    const QString &serviceInternalId, const QVariant &formModelVariant) const
{
	debugFuncCall();

103 104
	const Gov::Service *cgs = m_govServices.value(serviceInternalId, Q_NULLPTR);
	if (Q_UNLIKELY(cgs == Q_NULLPTR)) {
105 106 107 108 109
		logErrorNL("Cannot access gov service '%s'.",
		    serviceInternalId.toUtf8().constData());
		Q_ASSERT(0);
		return;
	}
110 111 112 113 114
	QScopedPointer<Gov::Service> gs(cgs->createNew());
	if (Q_UNLIKELY(gs.isNull())) {
		Q_ASSERT(0);
		return;
	}
115

116 117 118 119 120 121 122 123
	GovFormListModel *formModel =
	    GovFormListModel::fromVariant(formModelVariant);
	if (Q_UNLIKELY(formModel == Q_NULLPTR)) {
		logErrorNL("%s", "Cannot access gov form model.");
		Q_ASSERT(0);
		return;
	}

124 125 126 127 128
	/* Set content from data box. */
	const Isds::DbOwnerInfo dbOwnerInfo(
	    GlobInstcs::accountDbPtr->getOwnerInfo(userName));
	gs->setOwnerInfoFields(dbOwnerInfo);

129 130 131 132
	Gov::Service *oldService = formModel->setService(gs.take());
	if (oldService != Q_NULLPTR) {
		delete oldService; oldService = Q_NULLPTR;
	}
133 134
}

135
bool GovWrapper::sendGovRequest(const QString &userName,
136
    const QString &serviceInternalId, const QVariant &formModelVariant) const
137 138 139
{
	debugFuncCall();

140 141
	const Gov::Service *cgs = m_govServices.value(serviceInternalId, Q_NULLPTR);
	if (cgs == Q_NULLPTR) {
142
		logErrorNL("Cannot access e-gov service '%s'.",
143 144
		    serviceInternalId.toUtf8().constData());
		Q_ASSERT(0);
145 146 147
		return false;
	}

148 149 150
	const GovFormListModel *formModel =
	    GovFormListModel::fromVariant(formModelVariant);
	if (Q_UNLIKELY(formModel == Q_NULLPTR)) {
151 152 153 154 155 156 157 158
		logErrorNL("%s", "Cannot access e-gov form model.");
		Q_ASSERT(0);
		return false;
	}

	const Gov::Service *gs = formModel->service();
	if (Q_UNLIKELY(gs == Q_NULLPTR)) {
		logErrorNL("%s", "Cannot access e-gov service from form model.");
159
		Q_ASSERT(0);
160 161 162
		return false;
	}

163
	{
164
		QString service = tr("Request: %1").arg(gs->fullName());
165
		service.append("\n");
166
		service.append(tr("Recipient: %1").arg(gs->instituteName()));
167 168

		int msgResponse = Dialogues::message(Dialogues::QUESTION,
169 170 171
		    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);
172 173 174 175 176
		if (msgResponse == Dialogues::NO) {
			return false;
		}
	}

177 178 179 180
	if (GlobInstcs::accountDbPtr == Q_NULLPTR) {
		return false;
	}

181
	/* Set message content according to model data. */
182
	if (!gs->haveAllMandatoryFields()) {
183
		logErrorNL("The e-gov service '%s' is missing some mandatory data.",
184
		    gs->internalId().toUtf8().constData());
185 186
		return false;
	}
187

188
	const Isds::Message msg(gs->dataMessage());
189 190 191 192
	if (Q_UNLIKELY(msg.isNull())) {
		Q_ASSERT(0);
		return false;
	}
193 194 195 196 197 198 199

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

	return false;
}