Commit ef77d56b authored by Karel Slaný's avatar Karel Slaný Committed by Martin Straka

Performing basic checks for user input before sending e-gov request.

parent 30b7d6ee
......@@ -54,6 +54,11 @@ Item {
userMandatoryText.visible = govFormModel.containsMandatoryUser()
fromDataBoxText.visible = govFormModel.containsBoxOwnerData()
}
onValidityNotification: {
validityNotification.text = message
actionButton.enabled = govFormModel.haveAllMandatory()
}
}
/* Makes date selection easier. */
......@@ -82,7 +87,9 @@ Item {
image.source: "qrc:/ui/send-msg.svg"
accessibleName: qsTr("Send request")
onClicked: {
isds.doIsdsAction("sendGovMessage", userName)
if (govFormModel.haveAllValid()) {
isds.doIsdsAction("sendGovMessage", userName)
}
}
}
}
......@@ -117,6 +124,15 @@ Item {
font.bold: false
text: " "
}
AccessibleText {
id: validityNotification
font.bold: false
color: "#ffff0000" // Red
width: parent.width
wrapMode: Text.Wrap
text: ""
visible: validityNotification.text !== ""
}
AccessibleText {
id: userMandatoryText
font.bold: false
......
......@@ -26,6 +26,14 @@
#include "src/datovka_shared/gov_services/helper.h"
QString Gov::Helper::withoutWhiteSpaces(const QString &str)
{
const static QRegularExpression wsRe("\\s+");
QString retStr(str);
return retStr.replace(wsRe, QString());
}
#define MAX_IC_LEN 8 /* IC has 8 digits. */
/*!
......
......@@ -28,15 +28,24 @@
namespace Gov {
/*!
* @brief Contains various conversion and checking sunctions.
* @brief Contains various conversion and checking functions.
*/
namespace Helper {
/*!
* @brief Check whether strin gcontains a valid IC.
* @brief Remove white-space characters.
*
* @param[in,out] str Input string.
*
* @return Sting with all white-space characters removed.
*/
QString withoutWhiteSpaces(const QString &str);
/*!
* @brief Check whether string contains a valid IC.
*
* @param[in] str String containing an IC number.
* @return True if supplied string contains a valuid IC only.
* @return True if supplied string contains a valid IC only.
*/
bool isValidIcStr(const QString &str);
......
......@@ -62,8 +62,6 @@ static const char xml_template[] =
#define SURNAME_KEY "surname"
#define BIRTH_KEY "birthDate"
#define DATE_FORMAT "yyyy-MM-dd"
class MvCrrVbhData {
Q_DECLARE_TR_FUNCTIONS(MvCrrVbhData)
......@@ -120,7 +118,7 @@ QList<Gov::FormField> MvCrrVbhData::allFields(void) const
{
Gov::FormField ff;
ff.setKey(BIRTH_KEY);
ff.setVal(m_boxBirthDate.isNull() ? QString() : m_boxBirthDate.toString(DATE_FORMAT));
ff.setVal(m_boxBirthDate.isNull() ? QString() : m_boxBirthDate.toString(GOV_DATE_FORMAT));
ff.setDescr(tr("Birth date"));
//ff.setPlaceholder(QString());
ff.setProperties(Gov::FormFieldType::PROP_MANDATORY |
......@@ -202,7 +200,7 @@ bool Gov::SrvcMvCrrVbh::canSend(enum Isds::Type::DbType dbType) const
bool Gov::SrvcMvCrrVbh::setFieldVal(const QString &key, const QString &val)
{
if (key == BIRTH_KEY) {
if (QDate::fromString(val, DATE_FORMAT).isNull()) {
if (QDate::fromString(val, GOV_DATE_FORMAT).isNull()) {
return false;
}
}
......@@ -232,7 +230,7 @@ bool Gov::SrvcMvCrrVbh::setOwnerInfoFields(const Isds::DbOwnerInfo &dbOwnerInfo)
Q_ASSERT(0);
return false;
}
if (Q_UNLIKELY(!Service::setFieldVal(BIRTH_KEY, boxBirthDate.toString(DATE_FORMAT)))) {
if (Q_UNLIKELY(!Service::setFieldVal(BIRTH_KEY, boxBirthDate.toString(GOV_DATE_FORMAT)))) {
Q_ASSERT(0);
return false;
}
......@@ -240,6 +238,33 @@ bool Gov::SrvcMvCrrVbh::setOwnerInfoFields(const Isds::DbOwnerInfo &dbOwnerInfo)
return true;
}
bool Gov::SrvcMvCrrVbh::haveAllValidFields(QString *errDescr)
{
bool ret = false;
ret = Service::checkStrRemoveWhiteSpace(DRILIC_KEY, errDescr);
if (!ret) {
return ret;
}
ret = Service::checkStrTrimmed(NAME_KEY, errDescr);
if (!ret) {
return ret;
}
ret = Service::checkStrTrimmed(SURNAME_KEY, errDescr);
if (!ret) {
return ret;
}
ret = Service::checkDate(BIRTH_KEY, errDescr);
if (!ret) {
return ret;
}
if (errDescr != Q_NULLPTR) {
errDescr->clear();
}
return true;
}
QByteArray Gov::SrvcMvCrrVbh::binaryXmlContent(void) const
{
QString xml(xml_template);
......
......@@ -80,6 +80,9 @@ namespace Gov {
virtual
bool setOwnerInfoFields(const Isds::DbOwnerInfo &dbOwnerInfo) Q_DECL_OVERRIDE;
virtual
bool haveAllValidFields(QString *errDescr = Q_NULLPTR) Q_DECL_OVERRIDE;
virtual
QByteArray binaryXmlContent(void) const Q_DECL_OVERRIDE;
};
......
......@@ -614,6 +614,11 @@ bool Gov::SrvcMvIrVp::setOwnerInfoFields(const Isds::DbOwnerInfo &dbOwnerInfo)
return true;
}
bool Gov::SrvcMvIrVp::haveAllValidFields(QString *errDescr)
{
return Service::checkIc(IC_KEY, errDescr);
}
QByteArray Gov::SrvcMvIrVp::binaryXmlContent(void) const
{
QString xml(xml_template);
......
......@@ -79,6 +79,9 @@ namespace Gov {
virtual
bool setOwnerInfoFields(const Isds::DbOwnerInfo &dbOwnerInfo) Q_DECL_OVERRIDE;
virtual
bool haveAllValidFields(QString *errDescr = Q_NULLPTR) Q_DECL_OVERRIDE;
virtual
QByteArray binaryXmlContent(void) const Q_DECL_OVERRIDE;
};
......
......@@ -76,8 +76,6 @@ static const char xml_template[] =
#define REGION_KEY "region"
#define CITY_KEY "city"
#define DATE_FORMAT "yyyy-MM-dd"
class MvRtVtData {
Q_DECLARE_TR_FUNCTIONS(MvRtVtData)
......@@ -124,7 +122,7 @@ QList<Gov::FormField> MvRtVtData::allFields(void) const
{
Gov::FormField ff;
ff.setKey(BIRTH_KEY);
ff.setVal(m_boxBirthDate.isNull() ? QString() : m_boxBirthDate.toString(DATE_FORMAT));
ff.setVal(m_boxBirthDate.isNull() ? QString() : m_boxBirthDate.toString(GOV_DATE_FORMAT));
ff.setDescr(tr("Birth date"));
//ff.setPlaceholder(QString());
ff.setProperties(Gov::FormFieldType::PROP_MANDATORY |
......@@ -225,7 +223,7 @@ bool Gov::SrvcMvRtVt::canSend(enum Isds::Type::DbType dbType) const
bool Gov::SrvcMvRtVt::setFieldVal(const QString &key, const QString &val)
{
if (key == BIRTH_KEY) {
if (QDate::fromString(val, DATE_FORMAT).isNull()) {
if (QDate::fromString(val, GOV_DATE_FORMAT).isNull()) {
return false;
}
}
......@@ -259,7 +257,7 @@ bool Gov::SrvcMvRtVt::setOwnerInfoFields(const Isds::DbOwnerInfo &dbOwnerInfo)
Q_ASSERT(0);
return false;
}
if (Q_UNLIKELY(!Service::setFieldVal(BIRTH_KEY, boxBirthDate.toString(DATE_FORMAT)))) {
if (Q_UNLIKELY(!Service::setFieldVal(BIRTH_KEY, boxBirthDate.toString(GOV_DATE_FORMAT)))) {
Q_ASSERT(0);
return false;
}
......@@ -275,10 +273,41 @@ bool Gov::SrvcMvRtVt::setOwnerInfoFields(const Isds::DbOwnerInfo &dbOwnerInfo)
return true;
}
bool Gov::SrvcMvRtVt::haveAllValidFields(QString *errDescr)
{
bool ret = false;
ret = Service::checkStrTrimmed(NAME_KEY, errDescr);
if (!ret) {
return ret;
}
ret = Service::checkStrTrimmed(SURNAME_KEY, errDescr);
if (!ret) {
return ret;
}
ret = Service::checkDate(BIRTH_KEY, errDescr);
if (!ret) {
return ret;
}
ret = Service::checkStrTrimmed(REGION_KEY, errDescr);
if (!ret) {
return ret;
}
ret = Service::checkStrTrimmed(CITY_KEY, errDescr);
if (!ret) {
return ret;
}
if (errDescr != Q_NULLPTR) {
errDescr->clear();
}
return true;
}
QByteArray Gov::SrvcMvRtVt::binaryXmlContent(void) const
{
const QDate boxBirthDate(
QDate::fromString(fieldVal(BIRTH_KEY), DATE_FORMAT));
QDate::fromString(fieldVal(BIRTH_KEY), GOV_DATE_FORMAT));
QString xml(xml_template);
xml.replace("__repl_NAME__", fieldVal(NAME_KEY));
......
......@@ -79,6 +79,9 @@ namespace Gov {
virtual
bool setOwnerInfoFields(const Isds::DbOwnerInfo &dbOwnerInfo) Q_DECL_OVERRIDE;
virtual
bool haveAllValidFields(QString *errDescr = Q_NULLPTR) Q_DECL_OVERRIDE;
virtual
QByteArray binaryXmlContent(void) const Q_DECL_OVERRIDE;
};
......
......@@ -619,6 +619,11 @@ bool Gov::SrvcMvRtpoVt::setOwnerInfoFields(const Isds::DbOwnerInfo &dbOwnerInfo)
return true;
}
bool Gov::SrvcMvRtpoVt::haveAllValidFields(QString *errDescr)
{
return Service::checkIc(IC_KEY, errDescr);
}
QByteArray Gov::SrvcMvRtpoVt::binaryXmlContent(void) const
{
QString xml(xml_template);
......
......@@ -79,6 +79,9 @@ namespace Gov {
virtual
bool setOwnerInfoFields(const Isds::DbOwnerInfo &dbOwnerInfo) Q_DECL_OVERRIDE;
virtual
bool haveAllValidFields(QString *errDescr = Q_NULLPTR) Q_DECL_OVERRIDE;
virtual
QByteArray binaryXmlContent(void) const Q_DECL_OVERRIDE;
};
......
......@@ -615,6 +615,11 @@ bool Gov::SrvcMvSkdVp::setOwnerInfoFields(const Isds::DbOwnerInfo &dbOwnerInfo)
return true;
}
bool Gov::SrvcMvSkdVp::haveAllValidFields(QString *errDescr)
{
return Service::checkIc(IC_KEY, errDescr);
}
QByteArray Gov::SrvcMvSkdVp::binaryXmlContent(void) const
{
QString xml(xml_template);
......
......@@ -79,6 +79,9 @@ namespace Gov {
virtual
bool setOwnerInfoFields(const Isds::DbOwnerInfo &dbOwnerInfo) Q_DECL_OVERRIDE;
virtual
bool haveAllValidFields(QString *errDescr = Q_NULLPTR) Q_DECL_OVERRIDE;
virtual
QByteArray binaryXmlContent(void) const Q_DECL_OVERRIDE;
};
......
......@@ -648,6 +648,11 @@ bool Gov::SrvcMvVrVp::setOwnerInfoFields(const Isds::DbOwnerInfo &dbOwnerInfo)
return true;
}
bool Gov::SrvcMvVrVp::haveAllValidFields(QString *errDescr)
{
return Service::checkIc(IC_KEY, errDescr);
}
QByteArray Gov::SrvcMvVrVp::binaryXmlContent(void) const
{
QString xml(xml_template);
......
......@@ -79,6 +79,9 @@ namespace Gov {
virtual
bool setOwnerInfoFields(const Isds::DbOwnerInfo &dbOwnerInfo) Q_DECL_OVERRIDE;
virtual
bool haveAllValidFields(QString *errDescr = Q_NULLPTR) Q_DECL_OVERRIDE;
virtual
QByteArray binaryXmlContent(void) const Q_DECL_OVERRIDE;
};
......
......@@ -614,6 +614,11 @@ bool Gov::SrvcMvZrVp::setOwnerInfoFields(const Isds::DbOwnerInfo &dbOwnerInfo)
return true;
}
bool Gov::SrvcMvZrVp::haveAllValidFields(QString *errDescr)
{
return Service::checkIc(IC_KEY, errDescr);
}
QByteArray Gov::SrvcMvZrVp::binaryXmlContent(void) const
{
QString xml(xml_template);
......
......@@ -79,6 +79,9 @@ namespace Gov {
virtual
bool setOwnerInfoFields(const Isds::DbOwnerInfo &dbOwnerInfo) Q_DECL_OVERRIDE;
virtual
bool haveAllValidFields(QString *errDescr = Q_NULLPTR) Q_DECL_OVERRIDE;
virtual
QByteArray binaryXmlContent(void) const Q_DECL_OVERRIDE;
};
......
......@@ -21,6 +21,9 @@
* the two.
*/
#include <QDate>
#include "src/datovka_shared/gov_services/helper.h"
#include "src/datovka_shared/gov_services/service/gov_service.h"
Gov::Service::Service(void)
......@@ -139,3 +142,129 @@ Isds::Message Gov::Service::dataMessage(void) const
return message;
}
int Gov::Service::fieldIndex(const QString &key) const
{
for (int i = 0; i < m_formFields.size(); ++i) {
const FormField &ff(m_formFields[i]);
if (key == ff.key()) {
return i;
}
}
return -1;
}
bool Gov::Service::checkDate(const QString &key, QString *errDescr)
{
int idx = -1;
idx = fieldIndex(key);
if (Q_UNLIKELY(idx < 0)) {
Q_ASSERT(0);
if (errDescr != Q_NULLPTR) {
*errDescr = tr("Cannot access IC field.");
}
return false;
}
FormField &ff(m_formFields[idx]);
ff.setVal(Helper::withoutWhiteSpaces(ff.val()));
if (QDate::fromString(ff.val(), GOV_DATE_FORMAT).isNull()) {
if (errDescr != Q_NULLPTR) {
*errDescr =
tr("The field '%1' contains an invalid date '%2'.")
.arg(ff.descr()).arg(ff.val());
}
return false;
}
if (errDescr != Q_NULLPTR) {
errDescr->clear();
}
return true;
}
bool Gov::Service::checkIc(const QString &key, QString *errDescr)
{
int idx = -1;
idx = fieldIndex(key);
if (Q_UNLIKELY(idx < 0)) {
Q_ASSERT(0);
if (errDescr != Q_NULLPTR) {
*errDescr = tr("Cannot access IC field.");
}
return false;
}
FormField &ff(m_formFields[idx]);
ff.setVal(Helper::withoutWhiteSpaces(ff.val()));
if (!Helper::isValidIcStr(ff.val())) {
if (errDescr != Q_NULLPTR) {
*errDescr =
tr("The field '%1' contains an invalid value '%2'.")
.arg(ff.descr()).arg(ff.val());
}
return false;
}
if (errDescr != Q_NULLPTR) {
errDescr->clear();
}
return true;
}
bool Gov::Service::checkStrRemoveWhiteSpace(const QString &key, QString *errDescr)
{
int idx = -1;
idx = fieldIndex(key);
if (Q_UNLIKELY(idx < 0)) {
Q_ASSERT(0);
if (errDescr != Q_NULLPTR) {
*errDescr = tr("Cannot access IC field.");
}
return false;
}
FormField &ff(m_formFields[idx]);
ff.setVal(Helper::withoutWhiteSpaces(ff.val()));
if (ff.val().isEmpty()) {
if (errDescr != Q_NULLPTR) {
*errDescr = tr("The field '%1' contains no value.")
.arg(ff.descr());
}
return false;
}
if (errDescr != Q_NULLPTR) {
errDescr->clear();
}
return true;
}
bool Gov::Service::checkStrTrimmed(const QString &key, QString *errDescr)
{
int idx = -1;
idx = fieldIndex(key);
if (Q_UNLIKELY(idx < 0)) {
Q_ASSERT(0);
if (errDescr != Q_NULLPTR) {
*errDescr = tr("Cannot access IC field.");
}
return false;
}
FormField &ff(m_formFields[idx]);
ff.setVal(ff.val().trimmed());
if (ff.val().isEmpty()) {
if (errDescr != Q_NULLPTR) {
*errDescr = tr("The field '%1' contains no value.")
.arg(ff.descr());
}
return false;
}
if (errDescr != Q_NULLPTR) {
errDescr->clear();
}
return true;
}
......@@ -24,6 +24,7 @@
#pragma once
#include <QByteArray>
#include <QCoreApplication> /* Q_DECLARE_TR_FUNCTIONS */
#include <QList>
#include <QString>
......@@ -41,12 +42,16 @@
#define GOV_MV_DB_ID "xhzkdqv"
#define GOV_MV_XML_FILE_NAME "czpathome.xml"
#define GOV_DATE_FORMAT "yyyy-MM-dd"
namespace Gov {
/*!
* @brief Encapsulates the general e-gov service request.
*/
class Service {
Q_DECLARE_TR_FUNCTIONS(Service)
public:
/*!
* @brief Constructor.
......@@ -200,6 +205,16 @@ namespace Gov {
virtual
bool containsBoxOwnerDataFields(void) const;
/*!
* @brief Check whether fields contain valid data.
*
* @param[out] errDescr Non-empty string with error description,
* empty string if no error found.
* @return True on success.
*/
virtual
bool haveAllValidFields(QString *errDescr = Q_NULLPTR) = 0;
/*!
* @brief Create ISDS message containing the request.
*
......@@ -217,6 +232,57 @@ namespace Gov {
QByteArray binaryXmlContent(void) const = 0;
protected:
/*!
* @brief Get index of the entry with given key.
*
* @param[in] key Key identifying the data.
* Return non-negative index, if key found, -1 else.
*/
int fieldIndex(const QString &key) const;
/*!
* @brief Removes white spaces. Checks whether contains a valid
* date in GOV_DATE_FORMAT.
*
* @param[in] key Key identifying the data.
* @param[out] errDescr Error description if value invalid.
* @return True on success.
*/
bool checkDate(const QString &key,
QString *errDescr = Q_NULLPTR);
/*!
* @brief removes white spaces. Checks whether sting contains
* a valid IC.
*
* @param[in] key Key identifying the data.
* @param[out] errDescr Error description if value invalid.
* @return True on success.
*/
bool checkIc(const QString &key, QString *errDescr = Q_NULLPTR);
/*!
* @brief removes all white spaces. Checks whether remaining
* sting is non-empty.
*
* @param[in] key Key identifying the data.
* @param[out] errDescr Error description if value invalid.
* @return True on success.
*/
bool checkStrRemoveWhiteSpace(const QString &key,
QString *errDescr = Q_NULLPTR);
/*!
* @brief removes leading and trailing white spaces. Checks
* whether remaining sting is non-empty.
*
* @param[in] key Key identifying the data.
* @param[out] errDescr Error description if value invalid.
* @return True on success.
*/
bool checkStrTrimmed(const QString &key,
QString *errDescr = Q_NULLPTR);
QList<FormField> m_formFields; /*!< Data within the form. */
};
......
......@@ -153,6 +153,14 @@ bool Gov::SrvcSzrRobVu::setOwnerInfoFields(const Isds::DbOwnerInfo &dbOwnerInfo)
return true;
}
bool Gov::SrvcSzrRobVu::haveAllValidFields(QString *errDescr)
{
if (errDescr != Q_NULLPTR) {
errDescr->clear();
}
return true;
}
QByteArray Gov::SrvcSzrRobVu::binaryXmlContent(void) const
{
return QString(xml_template).toUtf8();
......
......@@ -79,6 +79,9 @@ namespace Gov {
virtual
bool setOwnerInfoFields(const Isds::DbOwnerInfo &dbOwnerInfo) Q_DECL_OVERRIDE;
virtual
bool haveAllValidFields(QString *errDescr = Q_NULLPTR) Q_DECL_OVERRIDE;
virtual
QByteArray binaryXmlContent(void) const Q_DECL_OVERRIDE;
};
......
......@@ -67,8 +67,6 @@ static const char xml_template[] =
#define FROM_KEY "fromDate"
#define TO_KEY "toDate"
#define DATE_FORMAT "yyyy-MM-dd"
class SzrRobVvuData {
Q_DECLARE_TR_FUNCTIONS(SzrRobVvuData)
......@@ -91,7 +89,7 @@ QList<Gov::FormField> SzrRobVvuData::allFields(void) const
{
Gov::FormField ff;
ff.setKey(FROM_KEY);
ff.setVal(m_userFromDate.isNull() ? QString() : m_userFromDate.toString(DATE_FORMAT));
ff.setVal(m_userFromDate.isNull() ? QString() : m_userFromDate.toString(GOV_DATE_FORMAT));
ff.setDescr(tr("From"));
ff.setPlaceholder(tr("Enter date (YYYY-MM-DD)"));
ff.setProperties(Gov::FormFieldType::PROP_MANDATORY |
......@@ -102,7 +100,7 @@ QList<Gov::FormField> SzrRobVvuData::allFields(void) const
{
Gov::FormField ff;
ff.setKey(TO_KEY);
ff.setVal(m_userToDate.isNull() ? QString() : m_userToDate.toString(DATE_FORMAT));
ff.setVal(m_userToDate.isNull() ? QString() : m_userToDate.toString(GOV_DATE_FORMAT));
ff.setDescr(tr("To"));
ff.setPlaceholder(tr("Enter date (YYYY-MM-DD)"));
ff.setProperties(Gov::FormFieldType::PROP_MANDATORY |
......@@ -184,7 +182,7 @@ bool Gov::SrvcSzrRobVvu::canSend(enum Isds::Type::DbType dbType) const
bool Gov::SrvcSzrRobVvu::setFieldVal(const QString &key, const QString &val)
{
if ((key == FROM_KEY) || (key == TO_KEY)) {
if (QDate::fromString(val, DATE_FORMAT).isNull()) {
if (QDate::fromString(val, GOV_DATE_FORMAT).isNull()) {
return false;
}
}
......@@ -201,6 +199,40 @@ bool Gov::SrvcSzrRobVvu::setOwnerInfoFields(const Isds::DbOwnerInfo &dbOwnerInfo
return true;
}
bool Gov::SrvcSzrRobVvu::haveAllValidFields(QString *errDescr)
{
bool ret = true;
ret = Service::checkDate(FROM_KEY, errDescr);
if (!ret) {
return ret;
}
ret = Service::checkDate(TO_KEY, errDescr);
if (!ret) {
return ret;
}
const QDate fromDate(QDate::fromString(fieldVal(FROM_KEY), GOV_DATE_FORMAT));
const QDate toDate(QDate::fromString(fieldVal(TO_KEY), GOV_DATE_FORMAT));
if (Q_UNLIKELY(fromDate.isNull() || toDate.isNull())) {
/* This branch shouldn't be taken as the checks before passed. */
Q_ASSERT(0);
return false;
}
if (fromDate > toDate) {
if (errDescr != Q_NULLPTR) {
*errDescr = tr("The date of start cannot be later than the date of end.");
}
return false;
}
if (errDescr != Q_NULLPTR) {
errDescr->clear();
}
return true;
}
QByteArray Gov::SrvcSzrRobVvu::