Skip to content
Permalink
Browse files

Update metadata validator for base class split

  • Loading branch information
nyalldawson committed Mar 22, 2018
1 parent a96d59a commit 1e05de049a09a2484483930079df3b678d823de5
@@ -41,7 +41,7 @@ Constructor for ValidationResult.

virtual ~QgsMetadataValidator();

virtual bool validate( const QgsLayerMetadata &metadata, QList< QgsMetadataValidator::ValidationResult > &results /Out/ ) const = 0;
virtual bool validate( const QgsMetadataBase *metadata, QList< QgsMetadataValidator::ValidationResult > &results /Out/ ) const = 0;
%Docstring
Validates a ``metadata`` object, and returns true if the
metadata is considered valid.
@@ -53,8 +53,32 @@ to fix the metadata.
};


class QgsNativeMetadataBaseValidator : QgsMetadataValidator
{
%Docstring
A validator for the native base QGIS metadata schema definition.

.. versionadded:: 3.20
%End

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

QgsNativeMetadataBaseValidator();
%Docstring
Constructor for QgsNativeMetadataBaseValidator.
%End

virtual bool validate( const QgsMetadataBase *metadata, QList< QgsMetadataValidator::ValidationResult > &results /Out/ ) const;


};



class QgsNativeMetadataValidator : QgsMetadataValidator
class QgsNativeMetadataValidator : QgsNativeMetadataBaseValidator
{
%Docstring
A validator for the native QGIS metadata schema definition.
@@ -72,7 +96,7 @@ class QgsNativeMetadataValidator : QgsMetadataValidator
Constructor for QgsNativeMetadataValidator.
%End

virtual bool validate( const QgsLayerMetadata &metadata, QList< QgsMetadataValidator::ValidationResult > &results /Out/ ) const;
virtual bool validate( const QgsMetadataBase *metadata, QList< QgsMetadataValidator::ValidationResult > &results /Out/ ) const;


};
@@ -18,84 +18,59 @@
#include "qgslayermetadatavalidator.h"
#include "qgslayermetadata.h"

bool QgsNativeMetadataValidator::validate( const QgsLayerMetadata &metadata, QList<ValidationResult> &results ) const

bool QgsNativeMetadataBaseValidator::validate( const QgsMetadataBase *metadata, QList<QgsMetadataValidator::ValidationResult> &results ) const
{
results.clear();
if ( !metadata )
return false;

int index = 0;
bool result = true;
if ( metadata.identifier().isEmpty() )
if ( metadata->identifier().isEmpty() )
{
result = false;
results << ValidationResult( QObject::tr( "identifier" ), QObject::tr( "Identifier element is required." ) );
}

if ( metadata.language().isEmpty() )
if ( metadata->language().isEmpty() )
{
result = false;
results << ValidationResult( QObject::tr( "language" ), QObject::tr( "Language element is required." ) );
}

if ( metadata.type().isEmpty() )
if ( metadata->type().isEmpty() )
{
result = false;
results << ValidationResult( QObject::tr( "type" ), QObject::tr( "Type element is required." ) );
}

if ( metadata.title().isEmpty() )
if ( metadata->title().isEmpty() )
{
result = false;
results << ValidationResult( QObject::tr( "title" ), QObject::tr( "Title element is required." ) );
}

if ( metadata.abstract().isEmpty() )
if ( metadata->abstract().isEmpty() )
{
result = false;
results << ValidationResult( QObject::tr( "abstract" ), QObject::tr( "Abstract element is required." ) );
}

if ( metadata.licenses().isEmpty() )
{
result = false;
results << ValidationResult( QObject::tr( "license" ), QObject::tr( "At least one license is required." ) );
}

if ( !metadata.crs().isValid() )
{
result = false;
results << ValidationResult( QObject::tr( "crs" ), QObject::tr( "A valid CRS element is required." ) );
}

int index = 0;
Q_FOREACH ( const QgsLayerMetadata::SpatialExtent &extent, metadata.extent().spatialExtents() )
{
if ( !extent.extentCrs.isValid() )
{
result = false;
results << ValidationResult( QObject::tr( "extent" ), QObject::tr( "A valid CRS element for the spatial extent is required." ), index );
}

if ( extent.bounds.width() == 0.0 || extent.bounds.height() == 0.0 )
{
result = false;
results << ValidationResult( QObject::tr( "extent" ), QObject::tr( "A valid spatial extent is required." ), index );
}
index++;
}

if ( metadata.contacts().isEmpty() )
if ( metadata->contacts().isEmpty() )
{
result = false;
results << ValidationResult( QObject::tr( "contacts" ), QObject::tr( "At least one contact is required." ) );
}

if ( metadata.links().isEmpty() )
if ( metadata->links().isEmpty() )
{
result = false;
results << ValidationResult( QObject::tr( "links" ), QObject::tr( "At least one link is required." ) );
}

// validate keywords
QgsMetadataBase::KeywordMap keywords = metadata.keywords();
QgsMetadataBase::KeywordMap keywords = metadata->keywords();
QgsMetadataBase::KeywordMap::const_iterator keywordIt = keywords.constBegin();
index = 0;
for ( ; keywordIt != keywords.constEnd(); ++keywordIt )
@@ -115,7 +90,7 @@ bool QgsNativeMetadataValidator::validate( const QgsLayerMetadata &metadata, QLi

// validate contacts
index = 0;
Q_FOREACH ( const QgsMetadataBase::Contact &contact, metadata.contacts() )
Q_FOREACH ( const QgsMetadataBase::Contact &contact, metadata->contacts() )
{
if ( contact.name.isEmpty() )
{
@@ -127,7 +102,7 @@ bool QgsNativeMetadataValidator::validate( const QgsLayerMetadata &metadata, QLi

// validate links
index = 0;
Q_FOREACH ( const QgsMetadataBase::Link &link, metadata.links() )
Q_FOREACH ( const QgsMetadataBase::Link &link, metadata->links() )
{
if ( link.name.isEmpty() )
{
@@ -149,3 +124,48 @@ bool QgsNativeMetadataValidator::validate( const QgsLayerMetadata &metadata, QLi

return result;
}

bool QgsNativeMetadataValidator::validate( const QgsMetadataBase *baseMetadata, QList<ValidationResult> &results ) const
{
results.clear();

const QgsLayerMetadata *metadata = dynamic_cast< const QgsLayerMetadata * >( baseMetadata );
if ( !metadata )
return false;

bool result = true;
if ( !QgsNativeMetadataBaseValidator::validate( metadata, results ) )
result = false;

if ( metadata->licenses().isEmpty() )
{
result = false;
results << ValidationResult( QObject::tr( "license" ), QObject::tr( "At least one license is required." ) );
}

if ( !metadata->crs().isValid() )
{
result = false;
results << ValidationResult( QObject::tr( "crs" ), QObject::tr( "A valid CRS element is required." ) );
}

int index = 0;
Q_FOREACH ( const QgsLayerMetadata::SpatialExtent &extent, metadata->extent().spatialExtents() )
{
if ( !extent.extentCrs.isValid() )
{
result = false;
results << ValidationResult( QObject::tr( "extent" ), QObject::tr( "A valid CRS element for the spatial extent is required." ), index );
}

if ( extent.bounds.width() == 0.0 || extent.bounds.height() == 0.0 )
{
result = false;
results << ValidationResult( QObject::tr( "extent" ), QObject::tr( "A valid spatial extent is required." ), index );
}
index++;
}

return result;
}

@@ -21,6 +21,7 @@
#include "qgis.h"
#include "qgis_core.h"

class QgsMetadataBase;
class QgsLayerMetadata;

/**
@@ -75,7 +76,28 @@ class CORE_EXPORT QgsMetadataValidator
* items describing why the validation failed and what needs to be rectified
* to fix the metadata.
*/
virtual bool validate( const QgsLayerMetadata &metadata, QList< QgsMetadataValidator::ValidationResult > &results SIP_OUT ) const = 0;
virtual bool validate( const QgsMetadataBase *metadata, QList< QgsMetadataValidator::ValidationResult > &results SIP_OUT ) const = 0;

};

/**
* \ingroup core
* \class QgsNativeMetadataBaseValidator
* \brief A validator for the native base QGIS metadata schema definition.
* \since QGIS 3.20
*/

class CORE_EXPORT QgsNativeMetadataBaseValidator : public QgsMetadataValidator
{

public:

/**
* Constructor for QgsNativeMetadataBaseValidator.
*/
QgsNativeMetadataBaseValidator() = default;

bool validate( const QgsMetadataBase *metadata, QList< QgsMetadataValidator::ValidationResult > &results SIP_OUT ) const override;

};

@@ -87,7 +109,7 @@ class CORE_EXPORT QgsMetadataValidator
* \since QGIS 3.0
*/

class CORE_EXPORT QgsNativeMetadataValidator : public QgsMetadataValidator
class CORE_EXPORT QgsNativeMetadataValidator : public QgsNativeMetadataBaseValidator
{

public:
@@ -97,7 +119,7 @@ class CORE_EXPORT QgsNativeMetadataValidator : public QgsMetadataValidator
*/
QgsNativeMetadataValidator() = default;

bool validate( const QgsLayerMetadata &metadata, QList< QgsMetadataValidator::ValidationResult > &results SIP_OUT ) const override;
bool validate( const QgsMetadataBase *metadata, QList< QgsMetadataValidator::ValidationResult > &results SIP_OUT ) const override;

};

@@ -667,7 +667,7 @@ bool QgsMetadataWidget::checkMetadata()
saveMetadata( metadata );
QgsNativeMetadataValidator validator;
QList<QgsMetadataValidator::ValidationResult> validationResults;
bool results = validator.validate( metadata, validationResults );
bool results = validator.validate( &metadata, validationResults );

QString errors;
if ( !results )
@@ -21,7 +21,7 @@
from qgis.core import (QgsMetadataBase,
QgsCoordinateReferenceSystem,
QgsVectorLayer,
QgsNativeMetadataValidator,
QgsNativeMetadataBaseValidator,
QgsBox3d,
QgsDateTimeRange)
from qgis.PyQt.QtCore import (QDate,
@@ -411,7 +411,7 @@ def testValidateNative(self): # spellok
Test validating metadata against QGIS native schema
"""
m = self.createTestMetadata()
v = QgsNativeMetadataValidator()
v = QgsNativeMetadataBaseValidator()

res, list = v.validate(m)
self.assertTrue(res)

0 comments on commit 1e05de0

Please sign in to comment.
You can’t perform that action at this time.