#region Disclaimer / License // Copyright (C) 2010, Jackie Ng // http://trac.osgeo.org/mapguide/wiki/maestro, jumpinjackie@gmail.com // // 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 using System; using System.Collections.Generic; using System.Text; using OSGeo.MapGuide.MaestroAPI.Resource; using OSGeo.MapGuide.ObjectModels.Common; using OSGeo.MapGuide.MaestroAPI; using System.ComponentModel; using System.IO; using OSGeo.MapGuide.MaestroAPI.Schema; using System.Collections.Specialized; namespace OSGeo.MapGuide.ObjectModels.FeatureSource { /// /// Represents an FDO feature source /// public interface IFeatureSource : IResource { /// /// Removes all specified connection properties /// void ClearConnectionProperties(); /// /// Gets an array of names of the currently specified connection properties /// string[] ConnectionPropertyNames { get; } /// /// Gets or sets the FDO provider. /// /// The FDO provider. string Provider { get; set; } /// /// Gets the connection property. /// /// The name. /// string GetConnectionProperty(string name); /// /// Sets the connection property. /// /// The name. /// The value. If null, the matching parameter is removed from the feature source void SetConnectionProperty(string name, string value); /// /// Gets the connection string. /// /// The connection string. string ConnectionString { get; } /// /// Gets the name of the embedded data resource. Can only be called if returns true. /// /// /// If is false string GetEmbeddedDataName(); /// /// Gets the name of the alias. Can only be called if returns true /// /// /// If is false string GetAliasName(); /// /// Gets the name of the aliased file. Can only be called if returns true. An /// empty string is returned if it is a directory (ie. no file name was found) /// /// /// If is false string GetAliasedFileName(); /// /// Gets a value indicating whether [uses embedded data files]. /// /// /// true if [uses embedded data files]; otherwise, false. /// bool UsesEmbeddedDataFiles{ get; } /// /// Gets a value indicating whether [uses aliased data files]. /// /// /// true if [uses aliased data files]; otherwise, false. /// bool UsesAliasedDataFiles { get; } /// /// Gets the supplemental spatial context info (coordinate system overrides). /// /// The supplemental spatial context info. IEnumerable SupplementalSpatialContextInfo { get; } /// /// Adds the spatial context override. /// /// The sc. void AddSpatialContextOverride(ISpatialContextInfo sc); /// /// Removes the spatial context override. /// /// The sc. void RemoveSpatialContextOverride(ISpatialContextInfo sc); /// /// Gets the extensions for this feature source. /// /// The extensions. IEnumerable Extension { get; } /// /// Adds the extension. /// /// The ext. void AddExtension(IFeatureSourceExtension ext); /// /// Removes the extension. /// /// The ext. void RemoveExtension(IFeatureSourceExtension ext); /// /// Gets or sets the name of the configuration document. /// /// The name of the configuration document. string ConfigurationDocument { get; set; } } /// /// Represents a spatial context override /// public interface ISpatialContextInfo { /// /// Gets or sets the name. /// /// The name. string Name { get; set; } /// /// Gets or sets the coordinate system. /// /// The coordinate system. string CoordinateSystem { get; set; } } /// /// Represents an extended feature class /// public interface IFeatureSourceExtension : INotifyPropertyChanged { /// /// Gets or sets the name. /// /// The name. string Name { get; set; } /// /// Gets or sets the feature class to extend /// /// The feature class. string FeatureClass { get; set; } /// /// Gets the calculated properties. /// /// The calculated properties. IEnumerable CalculatedProperty { get; } /// /// Adds the calculated property. /// /// The prop. void AddCalculatedProperty(ICalculatedProperty prop); /// /// Removes the calculated property. /// /// The prop. void RemoveCalculatedProperty(ICalculatedProperty prop); /// /// Gets the attribute joins /// /// The attribute joins. IEnumerable AttributeRelate { get; } /// /// Adds the relation. /// /// The relate. void AddRelation(IAttributeRelation relate); /// /// Removes the relation. /// /// The relate. void RemoveRelation(IAttributeRelation relate); } /// /// Represents a FDO calculated property /// public interface ICalculatedProperty : INotifyPropertyChanged { /// /// Gets or sets the name. /// /// The name. string Name { get; set; } /// /// Gets or sets the FDO expression. /// /// The FDO expression. string Expression { get; set; } } /// /// Defines the type of joins /// [System.SerializableAttribute()] public enum RelateTypeEnum { /// LeftOuter, /// RightOuter, /// Inner, /// Association, } /// /// Represents an attribute join /// public interface IAttributeRelation : INotifyPropertyChanged { /// /// Gets or sets whether to force 1:1 cardinality /// bool ForceOneToOne { get; set; } /// /// Gets the type of join /// RelateTypeEnum RelateType { get; set; } /// /// Gets or sets the feature source id containing the feature class to extend /// string ResourceId { get; set; } /// /// Gets or sets the name of the feature class to extend /// string AttributeClass { get; set; } /// /// Gets or sets the name of the join /// string Name { get; set; } /// /// Gets or sets the prefix that prevents a naming collision on both sides of the join /// string AttributeNameDelimiter { get; set; } /// /// Gets the property pairs involved in this join /// /// The property pairs. IEnumerable RelateProperty { get; } /// /// Creates the property join. /// /// The primary property. /// The secondary property. /// IRelateProperty CreatePropertyJoin(string primaryProperty, string secondaryProperty); /// /// Adds the relate property. /// /// The prop. void AddRelateProperty(IRelateProperty prop); /// /// Removes the relate property. /// /// The prop. void RemoveRelateProperty(IRelateProperty prop); /// /// Removes all relate properties. /// void RemoveAllRelateProperties(); } /// /// Represents a property pair in an attribute join /// public interface IRelateProperty { /// /// Gets or sets the feature class property. /// /// The feature class property. string FeatureClassProperty { get; set; } /// /// Gets or sets the attribute class property. /// /// The attribute class property. string AttributeClassProperty { get; set; } } /// /// Extension method class /// public static class FeatureSourceExtensions { /// /// Gets a collection of connection properties /// /// /// public static NameValueCollection GetConnectionProperties(this IFeatureSource fs) { Check.NotNull(fs, "fs"); var values = new NameValueCollection(); foreach (string name in fs.ConnectionPropertyNames) { values[name] = fs.GetConnectionProperty(name); } return values; } /// /// Gets the names of all the schemas in this feature source /// /// /// public static string[] GetSchemaNames(this IFeatureSource fs) { Check.NotNull(fs, "fs"); return fs.CurrentConnection.FeatureService.GetSchemas(fs.ResourceID); } /// /// Gets the names of all the feature classes in the specified schema of this feature source /// /// /// /// public static string[] GetClassNames(this IFeatureSource fs, string schemaName) { Check.NotNull(fs, "fs"); Check.NotEmpty(schemaName, "schemaName"); return fs.CurrentConnection.FeatureService.GetClassNames(fs.ResourceID, schemaName); } /// /// Sets the connection properties of the feature source /// /// /// public static void ApplyConnectionProperties(this IFeatureSource fs, NameValueCollection values) { Check.NotNull(fs, "fs"); Check.NotNull(values, "values"); fs.ClearConnectionProperties(); foreach (string name in values.Keys) { string value = values[name]; fs.SetConnectionProperty(name, value); } } /// /// Gets the configuration document content /// /// /// public static string GetConfigurationContent(this IFeatureSource fs) { Check.NotNull(fs, "fs"); if (string.IsNullOrEmpty(fs.ConfigurationDocument)) return string.Empty; var content = fs.GetResourceData(fs.ConfigurationDocument); if (content != null) { using (var sr = new StreamReader(content)) { return sr.ReadToEnd(); } } return string.Empty; } /// /// Sets the configuration document content /// /// /// public static void SetConfigurationContent(this IFeatureSource fs, string xmlContent) { Check.NotNull(fs, "fs"); if (string.IsNullOrEmpty(fs.ConfigurationDocument)) fs.ConfigurationDocument = "config.xml"; if (string.IsNullOrEmpty(xmlContent)) { bool hasResourceData = false; var resDataList = fs.EnumerateResourceData(); foreach (var resData in resDataList) { if (resData.Name == fs.ConfigurationDocument) { hasResourceData = true; break; } } if (hasResourceData) fs.DeleteResourceData(fs.ConfigurationDocument); } else { using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(xmlContent))) { fs.SetResourceData(fs.ConfigurationDocument, ResourceDataType.Stream, ms); } } } /// /// Convenience method to return the description of this feature source /// /// /// If you only need to list schemas and class names, use the respective and /// methods. Using this API will have a noticeable performance impact on /// really large datastores (whose size is in the 100s of classes). /// /// public static FeatureSourceDescription Describe(this IFeatureSource fs) { Check.NotNull(fs, "fs"); return fs.CurrentConnection.FeatureService.DescribeFeatureSource(fs.ResourceID); } /// /// Convenience method to retrieve the spatial context information of this feature source /// /// public static OSGeo.MapGuide.ObjectModels.Common.FdoSpatialContextList GetSpatialInfo(this IFeatureSource fs, bool activeOnly) { Check.NotNull(fs, "fs"); return fs.CurrentConnection.FeatureService.GetSpatialContextInfo(fs.ResourceID, activeOnly); } /// /// Convenience methods to get the identity properties of a given feature class (name) /// /// The fs. /// Name of the class. /// public static string[] GetIdentityProperties(this IFeatureSource fs, string className) { Check.NotNull(fs, "fs"); try { return fs.CurrentConnection.FeatureService.GetIdentityProperties(fs.ResourceID, className); } catch (Exception ex) { //MgClassNotFoundException is thrown for classes w/ no identity properties //when the correct server response should be an empty array if (ex.Message.IndexOf("MgClassNotFoundException") >= 0) { return new string[0]; } else { throw; } } } /// /// Convenience method to get the spatial extents of a given feature class /// /// The fs. /// Name of the class. /// The geom property. /// public static IEnvelope GetSpatialExtent(this IFeatureSource fs, string className, string geomProperty) { Check.NotNull(fs, "fs"); return fs.CurrentConnection.FeatureService.GetSpatialExtent(fs.ResourceID, className, geomProperty); } /// /// Convenience method to get the feature class definition /// /// The fs. /// Name of the qualified. /// public static ClassDefinition GetClass(this IFeatureSource fs, string qualifiedName) { Check.NotNull(fs, "fs"); return fs.CurrentConnection.FeatureService.GetClassDefinition(fs.ResourceID, qualifiedName); } /// /// Adds a spatial context override /// /// /// /// public static void AddSpatialContextOverride(this IFeatureSource fs, string name, string coordSys) { Check.NotNull(fs, "fs"); fs.AddSpatialContextOverride(new OSGeo.MapGuide.ObjectModels.FeatureSource_1_0_0.SpatialContextType() { Name = name, CoordinateSystem = coordSys }); } /// /// Tests the connection settings in this feature source /// /// /// public static string TestConnection(this IFeatureSource fs) { Check.NotNull(fs, "fs"); return fs.CurrentConnection.FeatureService.TestConnection(fs.ResourceID); } /// /// Adds the specified property pair to this join /// /// /// /// public static void AddRelateProperty(this IAttributeRelation rel, string primary, string secondary) { Check.NotNull(rel, "rel"); rel.AddRelateProperty(new OSGeo.MapGuide.ObjectModels.FeatureSource_1_0_0.RelatePropertyType() { FeatureClassProperty = primary, AttributeClassProperty = secondary }); } } }