Skip to content

Commit

Permalink
use the QgsAbstractValidator for validating the project in the server…
Browse files Browse the repository at this point in the history
… context
  • Loading branch information
Gustry committed Mar 30, 2020
1 parent 23715aa commit f2cbe04
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@




class QgsAbstractMetadataBaseValidator
class QgsAbstractBaseValidator
{
%Docstring
Abstract base class for metadata validators.
Abstract base class for validators.

.. versionadded:: 3.0
.. versionadded:: 3.14
%End

%TypeHeaderCode
#include "qgslayermetadatavalidator.h"
%End
public:


virtual ~QgsAbstractBaseValidator();

struct ValidationResult
{

Expand All @@ -38,10 +40,26 @@ Constructor for ValidationResult.

QString note;
};
};



class QgsAbstractMetadataBaseValidator : QgsAbstractBaseValidator
{
%Docstring
Abstract base class for metadata validators.

.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgslayermetadatavalidator.h"
%End
public:

virtual ~QgsAbstractMetadataBaseValidator();

virtual bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractMetadataBaseValidator::ValidationResult > &results /Out/ ) const = 0;
virtual bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractBaseValidator::ValidationResult > &results /Out/ ) const = 0;
%Docstring
Validates a ``metadata`` object, and returns ``True`` if the
metadata is considered valid.
Expand Down Expand Up @@ -71,7 +89,7 @@ A validator for the native base QGIS metadata schema definition.
Constructor for QgsNativeMetadataBaseValidator.
%End

virtual bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractMetadataBaseValidator::ValidationResult > &results /Out/ ) const;
virtual bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractBaseValidator::ValidationResult > &results /Out/ ) const;


};
Expand All @@ -96,7 +114,7 @@ A validator for the native QGIS layer metadata schema definition.
Constructor for QgsNativeMetadataValidator.
%End

virtual bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractMetadataBaseValidator::ValidationResult > &results /Out/ ) const;
virtual bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractBaseValidator::ValidationResult > &results /Out/ ) const;


};
Expand All @@ -120,7 +138,7 @@ A validator for the native QGIS project metadata schema definition.
Constructor for QgsNativeProjectMetadataValidator.
%End

virtual bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractMetadataBaseValidator::ValidationResult > &results /Out/ ) const;
virtual bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractBaseValidator::ValidationResult > &results /Out/ ) const;


};
Expand Down
19 changes: 7 additions & 12 deletions python/core/auto_generated/qgsprojectservervalidator.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
************************************************************************/


class QgsProjectServerValidator



class QgsProjectServerValidator : QgsAbstractBaseValidator
{
%Docstring
Project server validator.
Expand All @@ -20,20 +23,12 @@ Project server validator.
%End
public:

static void checkOWS( QgsLayerTreeGroup *treeGroup, QStringList &owsNames, QStringList &encodingMessages );
QgsProjectServerValidator();
%Docstring
Recursive function to check a layer tree group.
The function will collect OWS names and encoding for each layers.

.. versionadded:: 3.14
Constructor for :py:class:`QgsNativeMetadataBaseValidator`.
%End

static QString projectStatusHtml( QgsLayerTree *layerTree );
%Docstring
Check a layer tree.

.. versionadded:: 3.14
%End
bool validate( QgsLayerTree *layerTree, QList< QgsAbstractBaseValidator::ValidationResult > &results /Out/ ) const;

};

Expand Down
25 changes: 24 additions & 1 deletion src/app/qgsprojectproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1906,10 +1906,33 @@ void QgsProjectProperties::pbnWCSLayersDeselectAll_clicked()

void QgsProjectProperties::pbnLaunchOWSChecker_clicked()
{
QList<QgsAbstractBaseValidator::ValidationResult> validationResults;
QgsProjectServerValidator validator;
bool results = validator.validate( QgisApp::instance()->layerTreeView()->layerTreeModel()->rootGroup(), validationResults );

QString errors;
if ( !results )
{
for ( const QgsAbstractBaseValidator::ValidationResult &result : qgis::as_const( validationResults ) )
{
errors += QLatin1String( "<b>" ) % result.section % QLatin1String( " :</b> " );
if ( ! result.identifier.isNull() )
{
errors += QLatin1String( " " ) % result.identifier.toString();
}
errors += QLatin1String( " : " ) % result.note % QLatin1String( "<br />" );
}
}
else
{
errors = QString( tr( "Ok, it seems valid." ) );
}

QString myStyle = QgsApplication::reportStyleSheet();
myStyle.append( QStringLiteral( "body { margin: 10px; }\n " ) );
teOWSChecker->clear();
teOWSChecker->document()->setDefaultStyleSheet( myStyle );
teOWSChecker->setHtml( QgsProjectServerValidator::projectStatusHtml( QgisApp::instance()->layerTreeView()->layerTreeModel()->rootGroup() ) );
teOWSChecker->setHtml( errors );
}

void QgsProjectProperties::pbnAddScale_clicked()
Expand Down
4 changes: 2 additions & 2 deletions src/core/metadata/qgslayermetadatavalidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// QgsNativeMetadataBaseValidator
//

bool QgsNativeMetadataBaseValidator::validate( const QgsAbstractMetadataBase *metadata, QList<QgsAbstractMetadataBaseValidator::ValidationResult> &results ) const
bool QgsNativeMetadataBaseValidator::validate( const QgsAbstractMetadataBase *metadata, QList<QgsAbstractBaseValidator::ValidationResult> &results ) const
{
results.clear();
if ( !metadata )
Expand Down Expand Up @@ -185,7 +185,7 @@ bool QgsNativeMetadataValidator::validate( const QgsAbstractMetadataBase *baseMe
// QgsNativeProjectMetadataValidator
//

bool QgsNativeProjectMetadataValidator::validate( const QgsAbstractMetadataBase *baseMetadata, QList<QgsAbstractMetadataBaseValidator::ValidationResult> &results ) const
bool QgsNativeProjectMetadataValidator::validate( const QgsAbstractMetadataBase *baseMetadata, QList<QgsAbstractBaseValidator::ValidationResult> &results ) const
{
results.clear();

Expand Down
35 changes: 25 additions & 10 deletions src/core/metadata/qgslayermetadatavalidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ class QgsLayerMetadata;

/**
* \ingroup core
* \class QgsAbstractMetadataBaseValidator
* \brief Abstract base class for metadata validators.
* \since QGIS 3.0
* \class QgsAbstractBaseValidator
* \brief Abstract base class for validators.
* \since QGIS 3.14
*/

class CORE_EXPORT QgsAbstractMetadataBaseValidator
class CORE_EXPORT QgsAbstractBaseValidator
{

public:


virtual ~QgsAbstractBaseValidator() = default;

/**
* Contains the parameters describing a metadata validation
* failure.
Expand Down Expand Up @@ -68,6 +69,20 @@ class CORE_EXPORT QgsAbstractMetadataBaseValidator
//! The reason behind the validation failure.
QString note;
};
};


/**
* \ingroup core
* \class QgsAbstractMetadataBaseValidator
* \brief Abstract base class for metadata validators.
* \since QGIS 3.0
*/

class CORE_EXPORT QgsAbstractMetadataBaseValidator : public QgsAbstractBaseValidator
{

public:

virtual ~QgsAbstractMetadataBaseValidator() = default;

Expand All @@ -78,7 +93,7 @@ class CORE_EXPORT QgsAbstractMetadataBaseValidator
* items describing why the validation failed and what needs to be rectified
* to fix the metadata.
*/
virtual bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractMetadataBaseValidator::ValidationResult > &results SIP_OUT ) const = 0;
virtual bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractBaseValidator::ValidationResult > &results SIP_OUT ) const = 0;

};

Expand All @@ -99,7 +114,7 @@ class CORE_EXPORT QgsNativeMetadataBaseValidator : public QgsAbstractMetadataBas
*/
QgsNativeMetadataBaseValidator() = default;

bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractMetadataBaseValidator::ValidationResult > &results SIP_OUT ) const override;
bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractBaseValidator::ValidationResult > &results SIP_OUT ) const override;

};

Expand All @@ -121,7 +136,7 @@ class CORE_EXPORT QgsNativeMetadataValidator : public QgsNativeMetadataBaseValid
*/
QgsNativeMetadataValidator() = default;

bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractMetadataBaseValidator::ValidationResult > &results SIP_OUT ) const override;
bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractBaseValidator::ValidationResult > &results SIP_OUT ) const override;

};

Expand All @@ -142,7 +157,7 @@ class CORE_EXPORT QgsNativeProjectMetadataValidator : public QgsNativeMetadataBa
*/
QgsNativeProjectMetadataValidator() = default;

bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractMetadataBaseValidator::ValidationResult > &results SIP_OUT ) const override;
bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractBaseValidator::ValidationResult > &results SIP_OUT ) const override;

};

Expand Down
73 changes: 43 additions & 30 deletions src/core/qgsprojectservervalidator.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
/***************************************************************************
qgsprojectservervalidator.cpp
---------------------------
begin : March 2020
copyright : (C) 2020 by Etienne Trimaille
email : etienne dot trimaille at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/


#include "qgsapplication.h"
#include "qgslayertreelayer.h"
#include "qgsprojectservervalidator.h"
#include "qgsvectorlayer.h"


void QgsProjectServerValidator::checkOWS( QgsLayerTreeGroup *treeGroup, QStringList &owsNames, QStringList &encodingMessages )
void QgsProjectServerValidator::browseLayerTree( QgsLayerTreeGroup *treeGroup, QStringList &owsNames, QStringList &encodingMessages )
{
QList< QgsLayerTreeNode * > treeGroupChildren = treeGroup->children();
for ( int i = 0; i < treeGroupChildren.size(); ++i )
Expand All @@ -18,7 +36,7 @@ void QgsProjectServerValidator::checkOWS( QgsLayerTreeGroup *treeGroup, QStringL
owsNames << treeGroupChild->name();
else
owsNames << shortName;
checkOWS( treeGroupChild, owsNames, encodingMessages );
browseLayerTree( treeGroupChild, owsNames, encodingMessages );
}
else
{
Expand All @@ -35,68 +53,63 @@ void QgsProjectServerValidator::checkOWS( QgsLayerTreeGroup *treeGroup, QStringL
{
QgsVectorLayer *vl = static_cast<QgsVectorLayer *>( l );
if ( vl->dataProvider()->encoding() == QLatin1String( "System" ) )
encodingMessages << QObject::tr( "Update layer \"%1\" encoding" ).arg( l->name() );
encodingMessages << l->name();
}
}
}
}
}


QString QgsProjectServerValidator::projectStatusHtml( QgsLayerTree *layerTree )
bool QgsProjectServerValidator::validate( QgsLayerTree *layerTree, QList<QgsAbstractBaseValidator::ValidationResult> &results ) const
{
results.clear();
bool result = true;

QString html = QStringLiteral( "<h1>" ) + QObject::tr( "Start checking QGIS Server" ) + QStringLiteral( "</h1>" );
if ( !layerTree )
return false;

QStringList owsNames, encodingMessages;
checkOWS( layerTree, owsNames, encodingMessages );
browseLayerTree( layerTree, owsNames, encodingMessages );

QStringList duplicateNames, regExpMessages;
QRegExp snRegExp = QgsApplication::shortNameRegExp();
const auto constOwsNames = owsNames;
for ( const QString &name : constOwsNames )
{
if ( !snRegExp.exactMatch( name ) )
regExpMessages << QObject::tr( "Use short name for \"%1\"" ).arg( name );
{
regExpMessages << name;
}

if ( duplicateNames.contains( name ) )
{
continue;
}

if ( owsNames.count( name ) > 1 )
{
duplicateNames << name;
}
}

if ( !duplicateNames.empty() )
{
QString nameMessage = QStringLiteral( "<h1>" ) + QObject::tr( "Some layers and groups have the same name or short name" ) + QStringLiteral( "</h1>" );
nameMessage += "<h2>" + QObject::tr( "Duplicate names:" ) + "</h2>";
nameMessage += duplicateNames.join( QStringLiteral( "</li><li>" ) ) + QStringLiteral( "</li></ul>" );
html += nameMessage;
}
else
{
html += QStringLiteral( "<h1>" ) + QObject::tr( "All names and short names of layer and group are unique" ) + QStringLiteral( "</h1>" );
result = false;
results << ValidationResult( QObject::tr( "Duplicated names" ), QObject::tr( "Layers/groups have the same name or short name." ), duplicateNames.join( QStringLiteral( ", " ) ) );
}

if ( !regExpMessages.empty() )
{
html += QStringLiteral( "<h1>" ) + QObject::tr( "Some layer short names have to be updated:" ) + QStringLiteral( "</h1><ul><li>" );
html += regExpMessages.join( QStringLiteral( "</li><li>" ) );
html += QStringLiteral( "</li></ul>" );
}
else
{
html += QStringLiteral( "<h1>" ) + QObject::tr( "All layer short names are well formed" ) + QStringLiteral( "</h1>" );
result = false;
results << ValidationResult( QObject::tr( "Short names" ), QObject::tr( "Layers short names have to be updated." ), regExpMessages.join( QStringLiteral( ", " ) ) );
}

if ( !encodingMessages.empty() )
{
html += QStringLiteral( "<h1>" ) + QObject::tr( "Some layer encodings are not set:" ) + QStringLiteral( "</h1><ul><li>" );
html += encodingMessages.join( QStringLiteral( "</li><li>" ) );
html += QStringLiteral( "</li></ul>" );
}
else
{
html += QStringLiteral( "<h1>" ) + QObject::tr( "All layer encodings are set" ) + QStringLiteral( "</h1>" );
result = false;
results << ValidationResult( QObject::tr( "Encoding" ), QObject::tr( "Encoding is not set." ), encodingMessages.join( QStringLiteral( ", " ) ) );
}

return html;
return result;
}
Loading

0 comments on commit f2cbe04

Please sign in to comment.