Skip to content

Commit

Permalink
Server: throw exception if one or more layers are not available. Proj…
Browse files Browse the repository at this point in the history
…ects with missing layers are not cached, the layer may become available in future if e.g. the db connection has been temporarly interupted
  • Loading branch information
mhugent committed Jan 14, 2019
1 parent 3bc06e2 commit 4ea42fa
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/server/CMakeLists.txt
Expand Up @@ -40,6 +40,7 @@ SET(QGIS_SERVER_SRCS
qgsserviceregistry.cpp
qgsfeaturefilterprovidergroup.cpp
qgsfeaturefilter.cpp
qgsstorebadlayerinfo.cpp
)

SET (QGIS_SERVER_HDRS
Expand Down
8 changes: 8 additions & 0 deletions src/server/qgsconfigcache.cpp
Expand Up @@ -17,6 +17,8 @@

#include "qgsconfigcache.h"
#include "qgsmessagelog.h"
#include "qgsserverexception.h"
#include "qgsstorebadlayerinfo.h"

#include <QFile>

Expand All @@ -40,8 +42,14 @@ const QgsProject *QgsConfigCache::project( const QString &path )
if ( ! mProjectCache[ path ] )
{
std::unique_ptr<QgsProject> prj( new QgsProject() );
QgsStoreBadLayerInfo *badLayerHandler = new QgsStoreBadLayerInfo();
prj->setBadLayerHandler( badLayerHandler );
if ( prj->read( path ) )
{
if ( badLayerHandler->badLayers().size() > 0 )
{
throw QgsServerException( QStringLiteral( "Layer(s) not valid" ) );
}
mProjectCache.insert( path, prj.release() );
mFileSystemWatcher.addPath( path );
}
Expand Down
27 changes: 27 additions & 0 deletions src/server/qgsstorebadlayerinfo.cpp
@@ -0,0 +1,27 @@
#include "qgsstorebadlayerinfo.h"
#include <QDomElement>

QgsStoreBadLayerInfo::QgsStoreBadLayerInfo(): QgsProjectBadLayerHandler()
{
}

QgsStoreBadLayerInfo::~QgsStoreBadLayerInfo()
{
}

void QgsStoreBadLayerInfo::handleBadLayers( const QList<QDomNode> &layers )
{
mBadLayerIds.clear();
QList<QDomNode>::const_iterator it = layers.constBegin();
for ( ; it != layers.constEnd(); ++it )
{
if ( !it->isNull() )
{
QDomElement idElem = it->firstChildElement( "id" );
if ( !idElem.isNull() )
{
mBadLayerIds.append( idElem.text() );
}
}
}
}
32 changes: 32 additions & 0 deletions src/server/qgsstorebadlayerinfo.h
@@ -0,0 +1,32 @@
#ifndef QGSSTOREBADLAYERINFO_H
#define QGSSTOREBADLAYERINFO_H

#include "qgsprojectbadlayerhandler.h"
#include <QStringList>

/**
* \ingroup server
* Stores layer ids of bad layers
*/
class QgsStoreBadLayerInfo: public QgsProjectBadLayerHandler
{
public:
QgsStoreBadLayerInfo();
~QgsStoreBadLayerInfo();
/**
* @brief handleBadLayers
* @param layers layer nodes
*/
void handleBadLayers( const QList<QDomNode> &layers );

/**
* @brief badLayers
* @return ids of bad layers
*/
QStringList badLayers() const { return mBadLayerIds; }

private:
QStringList mBadLayerIds;
};

#endif // QGSSTOREBADLAYERINFO_H

0 comments on commit 4ea42fa

Please sign in to comment.