#region Disclaimer / License
// Copyright (C) 2009, Kenneth Skovhede
// http://www.hexad.dk, opensource@hexad.dk
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion Disclaimer / License
using OSGeo.MapGuide.MaestroAPI.Mapping;
using OSGeo.MapGuide.MaestroAPI.Services;
using System;
using System.Drawing;
using ObjCommon = OSGeo.MapGuide.ObjectModels.Common;
namespace OSGeo.MapGuide.MaestroAPI
{
///
/// Base class of all MapGuide connection classes. Covers functionality encompassed by
/// the MapGuide Geospatial Platform API and the MapGuide-specific services (Site, Rendering,
/// Mapping, Tile, Drawing)
///
public abstract class MgServerConnectionBase : PlatformConnectionBase
{
///
/// A flag that indicates if a session will be automatically restarted
///
protected bool m_autoRestartSession = false;
///
/// The username used to open this connection, if any
///
protected string m_username;
///
/// The password used to open this connection, if any
///
protected string m_password;
///
/// cached user list
///
protected ObjCommon.UserList m_cachedUserList = null;
///
/// cached group list
///
protected ObjCommon.GroupList m_cachedGroupList = null;
///
/// Initializes a new instance of the class.
///
protected MgServerConnectionBase()
: base()
{
m_username = null;
m_password = null;
_canAutoRestartSession = true;
}
#region Session Management
///
/// Gets or sets a value indicating if the session should automatically be restarted if it expires
///
public virtual bool AutoRestartSession
{
get { return m_autoRestartSession; }
set
{
if (value && !_canAutoRestartSession)
throw new InvalidOperationException(Strings.ErrorConnectionCannotAutoRestartSession);
m_autoRestartSession = value;
}
}
///
/// Determines whether session auto-recover is possible
///
protected bool _canAutoRestartSession;
///
/// Indicates this connection cannot use session recovery, normally due to the fact the connection was initialized
/// with just a session id.
///
protected void DisableAutoSessionRecovery()
{
this.AutoRestartSession = false;
_canAutoRestartSession = false;
}
///
/// Determines if an exception is a "Session Expired" exception.
///
/// The exception to evaluate
/// True if the exception is a session expired exception
public abstract bool IsSessionExpiredException(Exception ex);
///
/// Restarts the server session, and creates a new session ID
///
public void RestartSession() => RestartSession(true);
///
/// Raised when the associated session id has changed. This would happen if the connection detected an expired session
/// and built a new session
///
public event EventHandler SessionIDChanged;
///
/// Restarts the server session, and creates a new session ID
///
/// If set to true, the call throws an exception if the call failed
/// True if the creation succeed, false otherwise
public bool RestartSession(bool throwException)
{
var oldSessionId = this.SessionID;
var ret = RestartSessionInternal(throwException);
var newSessionId = this.SessionID;
CheckAndRaiseSessionChanged(oldSessionId, newSessionId);
return ret;
}
///
/// Raises the event if the
/// old and new session ids do not match
///
///
///
protected void CheckAndRaiseSessionChanged(string oldSessionId, string newSessionId)
{
if (!string.IsNullOrEmpty(oldSessionId))
{
if (oldSessionId != newSessionId)
{
this.SessionIDChanged?.Invoke(this, EventArgs.Empty);
}
}
}
///
/// Attempts to create a new session
///
///
///
protected abstract bool RestartSessionInternal(bool throwException);
#endregion Session Management
#region Site
///
/// Gets the site info.
///
///
public abstract ObjCommon.SiteInformation GetSiteInfo();
///
/// Gets a list of all users on the server
///
/// The list of users
public virtual ObjCommon.UserList EnumerateUsers() => this.EnumerateUsers(null);
///
/// Gets a list of users in a group
///
/// The group to retrieve the users from
/// The list of users
public abstract ObjCommon.UserList EnumerateUsers(string group);
///
/// Gets a list of all groups on the server
///
/// The list of groups
public abstract ObjCommon.GroupList EnumerateGroups();
#endregion Site
#region Rendering
///
/// Identifies those features that meet the specified spatial selection criteria. This operation is used to implement server-side selection. In addition to a selection set, this operation returns attribute information in case only one feature is selected.
///
/// The runtime map to identify features
/// The maximum number of features to return
/// The WKT of the filter geometry
/// If true will update the selection set for the given map
/// The type of spatial operator to use for the spatial query
/// Extra querying options
///
public abstract string QueryMapFeatures(RuntimeMap rtMap, int maxFeatures, string wkt, bool persist, string selectionVariant, QueryMapOptions extraOptions);
///
/// Renders a minature bitmap of the layers style
///
/// The scale for the bitmap to match
/// The layer the image should represent
/// If the layer is themed, this gives the theme index, otherwise set to 0
/// The geometry type, 1 for point, 2 for line, 3 for area, 4 for composite
/// The minature bitmap
public virtual Image GetLegendImage(double scale, string layerdefinition, int themeIndex, int type)
=> GetLegendImage(scale, layerdefinition, themeIndex, type, 16, 16, "PNG");
///
/// Gets the legend image.
///
/// The scale.
/// The layerdefinition.
/// Index of the theme.
/// The type.
/// The width.
/// The height.
/// The format.
///
public abstract Image GetLegendImage(double scale, string layerdefinition, int themeIndex, int type, int width, int height, string format);
///
/// Renders the runtime map.
///
/// The runtime map instance.
/// The x.
/// The y.
/// The scale.
/// The width.
/// The height.
/// The dpi.
///
public virtual System.IO.Stream RenderRuntimeMap(RuntimeMap map, double x, double y, double scale, int width, int height, int dpi)
=> this.RenderRuntimeMap(map, x, y, scale, width, height, dpi, "PNG", false);
///
/// Renders the runtime map.
///
/// The runtime map instance.
/// The x1.
/// The y1.
/// The x2.
/// The y2.
/// The width.
/// The height.
/// The dpi.
///
public virtual System.IO.Stream RenderRuntimeMap(RuntimeMap map, double x1, double y1, double x2, double y2, int width, int height, int dpi)
=> this.RenderRuntimeMap(map, x1, y1, x2, y2, width, height, dpi, "PNG", false);
///
/// Renders the runtime map.
///
/// The runtime map instance.
/// The x.
/// The y.
/// The scale.
/// The width.
/// The height.
/// The dpi.
/// The format.
///
public virtual System.IO.Stream RenderRuntimeMap(RuntimeMap map, double x, double y, double scale, int width, int height, int dpi, string format)
=> this.RenderRuntimeMap(map, x, y, scale, width, height, dpi, format, false);
///
/// Renders the runtime map.
///
/// The runtime map instance.
/// The x1.
/// The y1.
/// The x2.
/// The y2.
/// The width.
/// The height.
/// The dpi.
/// The format.
///
public virtual System.IO.Stream RenderRuntimeMap(RuntimeMap map, double x1, double y1, double x2, double y2, int width, int height, int dpi, string format)
=> this.RenderRuntimeMap(map, x1, y1, x2, y2, width, height, dpi, format, false);
///
/// Renders the runtime map.
///
/// The runtime map instance.
/// The x.
/// The y.
/// The scale.
/// The width.
/// The height.
/// The dpi.
/// The format.
/// if set to true [clip].
///
public abstract System.IO.Stream RenderRuntimeMap(RuntimeMap map, double x, double y, double scale, int width, int height, int dpi, string format, bool clip);
///
/// Renders the runtime map.
///
/// The runtime map instance.
/// The x1.
/// The y1.
/// The x2.
/// The y2.
/// The width.
/// The height.
/// The dpi.
/// The format.
/// if set to true [clip].
///
public abstract System.IO.Stream RenderRuntimeMap(RuntimeMap map, double x1, double y1, double x2, double y2, int width, int height, int dpi, string format, bool clip);
///
/// Renders a dynamic overlay image of the map
///
///
///
///
///
public System.IO.Stream RenderDynamicOverlay(RuntimeMap map, MapSelection selection, string format)
{
return RenderDynamicOverlay(map, selection, format, true);
}
///
/// Renders a dynamic overlay image of the map
///
///
///
///
///
///
public abstract System.IO.Stream RenderDynamicOverlay(RuntimeMap map, MapSelection selection, string format, bool keepSelection);
///
/// Renders a dynamic overlay image of the map
///
///
///
///
///
///
///
public abstract System.IO.Stream RenderDynamicOverlay(RuntimeMap map, MapSelection selection, string format, Color selectionColor, int behaviour);
#endregion Rendering
#region Tile
///
/// Gets the tile.
///
/// The mapdefinition.
/// The baselayergroup.
/// The col.
/// The row.
/// The scaleindex.
/// The format.
///
public abstract System.IO.Stream GetTile(string mapdefinition, string baselayergroup, int col, int row, int scaleindex, string format);
#endregion Tile
}
}