task_send_message.cpp 4.18 KB
Newer Older
1
/*
2
 * Copyright (C) 2014-2018 CZ.NIC
3 4 5 6 7 8 9 10
 *
 * 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
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 13 14 15 16 17 18 19 20 21 22 23
 * 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 <QThread>

26
#include "src/auxiliaries/attachment_helper.h"
27
#include "src/datovka_shared/log/log.h"
28
#include "src/global.h"
29
#include "src/net/db_wrapper.h"
30 31 32
#include "src/net/isds_const.h"
#include "src/worker/emitter.h"
#include "src/worker/task_send_message.h"
33
#include "src/xml/xml_send_message.h"
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
TaskSendMessage::ResultData::ResultData(void)
    : result(false),
    errInfo(),
    dbIDRecipient(),
    dmRecipient(),
    dmId(-1)
{
}

TaskSendMessage::ResultData::ResultData(bool res, const QString &eInfo,
    const QString &recId, const QString &recName, qint64 mId)
    : result(res),
    errInfo(eInfo),
    dbIDRecipient(recId),
    dmRecipient(recName),
    dmId(mId)
{
}

54
TaskSendMessage::TaskSendMessage(IsdsSession::IsdsContext &ctx,
55
    NetLayer *netLayer, const Isds::Message &message, const QString &transactId)
56
    : m_result(DL_ERR),
57
    m_dmId(0),
58 59 60
    m_lastError(),
    m_ctx(ctx),
    m_netLayer(netLayer),
61
    m_message(message),
62
    m_transactId(transactId)
63 64 65 66 67 68 69 70 71 72 73 74
{
}

void TaskSendMessage::run(void)
{
	if (Q_NULLPTR == m_netLayer) {
		Q_ASSERT(0);
		return;
	}

	/* ### Worker task begin. ### */

75 76
	logDebugLv0NL("%s", "---------------SEND MESSAGE TASK---------------");
	logDebugLv0NL("Starting in thread '%p'", QThread::currentThreadId());
77

78 79
	m_result = sendMessage(m_ctx, m_netLayer, m_message,
	    m_dmId, m_lastError);
80

81 82
	if (GlobInstcs::msgProcEmitterPtr != Q_NULLPTR) {
		emit GlobInstcs::msgProcEmitterPtr->sendMessageFinishedSignal(
83 84 85
		    m_ctx.username, m_transactId,
		    m_message.envelope().dbIDRecipient(),
		    m_message.envelope().dmRecipient(), m_dmId,
86 87
		    TaskSendMessage::DL_SUCCESS == m_result, m_lastError);
	}
88

89 90 91 92
	logDebugLv0NL("Finished in thread '%p'", QThread::currentThreadId());
	logDebugLv0NL("%s", "-----------------------------------------------");

	/* ### Worker task end. ### */
93 94 95
}

enum TaskSendMessage::Result TaskSendMessage::sendMessage(
96
    IsdsSession::IsdsContext &ctx, NetLayer *netLayer,
97
    const Isds::Message &message, qint64 &dmId, QString &lastError)
98 99 100
{
	if (ctx.username.isEmpty()) {
		lastError = QObject::tr("Internal error");
101 102
		logErrorNL("%s", "Account user name missing!");
		Q_ASSERT(0);
103 104 105
		return DL_ERR;
	}

106 107 108
	logDebugLv1NL("Sending from account '%s'",
	    ctx.account_name.toUtf8().constData());

109 110 111
	QByteArray xmlDataOut;

	/* Send SOAP request */
112
	if (!netLayer->sendSoapRequest(ctx, LOGIN_SERVICE,
113
	    Xml::xmlCreateSendMessageSoapRequest(message), xmlDataOut)) {
114
		lastError = ctx.last_isds_msg;
115
		logErrorNL("ISDS returns: '%s'", lastError.toUtf8().constData());
116 117 118 119
		return DL_ISDS_ERROR;
	}

	/* Parse SOAP response */
120 121
	dmId = Xml::parseSentMessageStatusFromXml(xmlDataOut, ctx.last_isds_msg);
	if (dmId == -1) {
122
		lastError = ctx.last_isds_msg;
123
		logErrorNL("ISDS returns: '%s'", lastError.toUtf8().constData());
124 125 126
		return DL_XML_ERROR;
	}

127
	/* Message has been sent */
128
	lastError = ctx.last_isds_msg;
129 130
	logDebugLv1NL("Message has been sent to ISDS with ID '%s'",
	    QString::number(dmId).toUtf8().constData());
131

132
	/* Store message into db */
133 134
	if (!DbWrapper::insertCompleteMessageToDb(ctx.username, dmId, message,
	    MessageDb::TYPE_SENT, ctx.last_isds_msg)) {
135 136
		logErrorNL("Cannot insert message into database: '%s'",
		    ctx.last_isds_msg.toUtf8().constData());
137 138 139
		return DL_DB_INS_ERR;
	}

140 141
	return DL_SUCCESS;
}