#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.Schema; using System.Globalization; using GeoAPI.Geometries; namespace OSGeo.MapGuide.MaestroAPI.Feature { /// /// Base class of all MapGuide property values. Functions as a nullable box type /// around an underlying data type /// public abstract class PropertyValue { /// /// Initializes a new instance of the class. /// protected PropertyValue() { this.IsNull = true; } /// /// Gets or sets a value indicating whether this instance is null. /// /// /// true if this instance is null; otherwise, false. /// public virtual bool IsNull { get; protected set; } /// /// Sets the value to null. /// public virtual void SetNull() { this.IsNull = true; } /// /// Gets the type. /// public abstract PropertyValueType Type { get; } /// /// Gets the suggsted property definition type for this value /// /// /// The suggsted property definition type. /// public abstract PropertyDefinitionType PropertyDefType { get; } /// /// Gets the value as a string /// /// public abstract string ValueAsString(); } /// /// Base class of all nullable value type property values. /// /// public abstract class ValueTypePropertyValue : PropertyValue where T : struct { /// /// Initializes a new instance of the class. /// protected ValueTypePropertyValue() : base() { } /// /// Initializes a new instance of the class. /// /// The value. protected ValueTypePropertyValue(T value) : base() { _value = value; } private Nullable _value; /// /// Gets or sets a value indicating whether this instance is null. /// /// /// true if this instance is null; otherwise, false. /// public override bool IsNull { get { return !_value.HasValue; } } /// /// Gets or sets the value. /// /// /// The value. /// public T Value { get { if (IsNull) throw new Exception(Strings.ErrorNullValue); //LOCALIZEME return _value.Value; } set { _value = value; } } /// /// Sets the value to null. /// public override void SetNull() { _value = null; } /// /// Gets the suggsted property definition type for this value /// /// /// The suggsted property definition type. /// public override PropertyDefinitionType PropertyDefType { get { return PropertyDefinitionType.Data; } } } /// /// Base class of all reference type property values /// /// public abstract class ReferenceTypePropertyValue : PropertyValue where T : class { /// /// Initializes a new instance of the class. /// protected ReferenceTypePropertyValue() : base() { } /// /// Initializes a new instance of the class. /// /// The value. protected ReferenceTypePropertyValue(T value) : base() { this.Value = value; } private T _value; /// /// Gets or sets the value. /// /// /// The value. /// public T Value { get { if (IsNull) throw new Exception(Strings.ErrorNullValue); return _value; } set { _value = value; this.IsNull = (value == null); } } } /// /// Stores byte data /// public class ByteValue : ValueTypePropertyValue { /// /// Initializes this instance /// public ByteValue() : base() { } /// /// Initializes this instance /// public ByteValue(byte value) : base() { } /// /// Gets the type. /// public override PropertyValueType Type { get { return PropertyValueType.Byte; } } /// /// Gets the value as a string /// /// public override string ValueAsString() { if (IsNull) throw new Exception(Strings.ErrorNullValue); return this.Value.ToString(CultureInfo.InvariantCulture); } } /// /// Stores boolean data /// public class BooleanValue : ValueTypePropertyValue { /// /// Initializes a new instance /// public BooleanValue() : base() { } /// /// Initializes a new instance /// public BooleanValue(bool value) : base(value) { } /// /// Gets the type. /// public override PropertyValueType Type { get { return PropertyValueType.Boolean; } } /// /// Gets the value as a string /// /// public override string ValueAsString() { if (IsNull) throw new Exception(Strings.ErrorNullValue); return this.Value.ToString(); } } /// /// Stores blob data /// public class BlobValue : ReferenceTypePropertyValue { /// /// Initializes a new instance /// public BlobValue() : base() { } /// /// Initializes a new instance /// public BlobValue(byte[] value) : base(value) { } /// /// Gets the type. /// public override PropertyValueType Type { get { return PropertyValueType.Blob; } } /// /// Gets the suggsted property definition type for this value /// /// /// The suggsted property definition type. /// public override PropertyDefinitionType PropertyDefType { get { return PropertyDefinitionType.Data; } } /// /// Gets the value as a string /// /// public override string ValueAsString() { throw new NotSupportedException(); } } /// /// Stores clob data /// public class ClobValue : ReferenceTypePropertyValue { /// /// Initializes a new instance /// public ClobValue() : base() { } /// /// Initializes a new instance /// public ClobValue(char[] value) : base(value) { } /// /// Gets the type. /// public override PropertyValueType Type { get { return PropertyValueType.Clob; } } /// /// Gets the suggsted property definition type for this value /// /// /// The suggsted property definition type. /// public override PropertyDefinitionType PropertyDefType { get { return PropertyDefinitionType.Data; } } /// /// Gets the value as a string /// /// public override string ValueAsString() { throw new NotSupportedException(); } } /// /// Stores datetime data /// public class DateTimeValue : ValueTypePropertyValue { /// /// Initializes a new instance /// public DateTimeValue() : base() { } /// /// Initializes a new instance /// public DateTimeValue(DateTime value) : base(value) { } /// /// Gets the type. /// public override PropertyValueType Type { get { return PropertyValueType.DateTime; } } static string PadLeft(string str, char ch, int totalChars) { var value = str; while (value.Length < totalChars) value = ch + value; return value; } /// /// Gets the value as a string /// /// public override string ValueAsString() { if (IsNull) throw new Exception(Strings.ErrorNullValue); var dt = this.Value; return string.Format("TIMESTAMP '{0}-{1}-{2} {3}:{4}:{5}'", //NOXLATE PadLeft(dt.Year.ToString(), '0', 4), //NOXLATE PadLeft(dt.Month.ToString(), '0', 2), //NOXLATE PadLeft(dt.Year.ToString(), '0', 2), //NOXLATE PadLeft(dt.Hour.ToString(), '0', 2), //NOXLATE PadLeft(dt.Minute.ToString(), '0', 2), //NOXLATE PadLeft(dt.Second.ToString(), '0', 2)); //NOXLATE } } /// /// Stores double data /// public class DoubleValue : ValueTypePropertyValue { /// /// Initializes a new instance /// public DoubleValue() : base() { } /// /// Initializes a new instance /// public DoubleValue(double value) : base(value) { } /// /// Gets the type. /// public override PropertyValueType Type { get { return PropertyValueType.Double; } } /// /// Gets the value as a string /// /// public override string ValueAsString() { if (IsNull) throw new Exception(Strings.ErrorNullValue); return this.Value.ToString(CultureInfo.InvariantCulture); } } /// /// Stores feature data /// public class FeatureValue : ReferenceTypePropertyValue { /// /// Initializes a new instance /// public FeatureValue() : base() { } /// /// Initializes a new instance /// public FeatureValue(IFeature[] values) : base(values) { } /// /// Gets the type. /// public override PropertyValueType Type { get { return PropertyValueType.Feature; } } /// /// Gets the suggsted property definition type for this value /// /// /// The suggsted property definition type. /// public override PropertyDefinitionType PropertyDefType { get { return PropertyDefinitionType.Object; } } /// /// Gets the value as a string /// /// public override string ValueAsString() { if (IsNull) throw new Exception(Strings.ErrorNullValue); throw new NotSupportedException(); } } /// /// Stores geometry data /// public class GeometryValue : ReferenceTypePropertyValue { /// /// Initializes a new instance /// public GeometryValue() : base() { } /// /// Initializes a new instance /// public GeometryValue(IGeometry value) : base(value) { } /// /// Gets the type. /// public override PropertyValueType Type { get { return PropertyValueType.Geometry; } } /// /// Gets the suggsted property definition type for this value /// /// /// The suggsted property definition type. /// public override PropertyDefinitionType PropertyDefType { get { return PropertyDefinitionType.Geometry; } } /// /// Gets the value as a string /// /// public override string ValueAsString() { if (IsNull) throw new Exception(Strings.ErrorNullValue); return this.Value.AsText(); } } /// /// Stores int16 data /// public class Int16Value : ValueTypePropertyValue { /// /// Initializes a new instance /// public Int16Value() : base() { } /// /// Initializes a new instance /// public Int16Value(short value) : base(value) { } /// /// Gets the type. /// public override PropertyValueType Type { get { return PropertyValueType.Int16; } } /// /// Gets the value as a string /// /// public override string ValueAsString() { if (IsNull) throw new Exception(Strings.ErrorNullValue); return this.Value.ToString(CultureInfo.InvariantCulture); } } /// /// Stores int32 data /// public class Int32Value : ValueTypePropertyValue { /// /// Initializes a new instance /// public Int32Value() : base() { } /// /// Initializes a new instance /// public Int32Value(int value) : base(value) { } /// /// Gets the type. /// public override PropertyValueType Type { get { return PropertyValueType.Int32; } } /// /// Gets the value as a string /// /// public override string ValueAsString() { if (IsNull) throw new Exception(Strings.ErrorNullValue); return this.Value.ToString(CultureInfo.InvariantCulture); } } /// /// Stores int64 data /// public class Int64Value : ValueTypePropertyValue { /// /// Initializes a new instance /// public Int64Value() : base() { } /// /// Initializes a new instance /// public Int64Value(long value) : base(value) { } /// /// Gets the type. /// public override PropertyValueType Type { get { return PropertyValueType.Int64; } } /// /// Gets the value as a string /// /// public override string ValueAsString() { if (IsNull) throw new Exception(Strings.ErrorNullValue); return this.Value.ToString(CultureInfo.InvariantCulture); } } /// /// Stores raster data /// public class RasterValue : ReferenceTypePropertyValue { /// /// Initializes a new instance /// public RasterValue() : base() { } /// /// Initializes a new instance /// public RasterValue(byte[] value) : base(value) { } /// /// Gets the type. /// public override PropertyValueType Type { get { return PropertyValueType.Raster; } } /// /// Gets the suggsted property definition type for this value /// /// /// The suggsted property definition type. /// public override PropertyDefinitionType PropertyDefType { get { return PropertyDefinitionType.Raster; } } /// /// Gets the value as a string /// /// public override string ValueAsString() { throw new NotSupportedException(); } } /// /// Stores float data /// public class SingleValue : ValueTypePropertyValue { /// /// Initializes a new instance /// public SingleValue() : base() { } /// /// Initializes a new instance /// public SingleValue(float value) : base(value) { } /// /// Gets the type. /// public override PropertyValueType Type { get { return PropertyValueType.Single; } } /// /// Gets the value as a string /// /// public override string ValueAsString() { if (IsNull) throw new Exception(Strings.ErrorNullValue); return this.Value.ToString(CultureInfo.InvariantCulture); } } /// /// Stores string data /// public class StringValue : ReferenceTypePropertyValue { /// /// Initializes a new instance /// public StringValue() : base() { } /// /// Initializes a new instance /// public StringValue(string value) : base(value) { } /// /// Gets the type. /// public override PropertyValueType Type { get { return PropertyValueType.String; } } /// /// Gets the suggsted property definition type for this value /// /// /// The suggsted property definition type. /// public override PropertyDefinitionType PropertyDefType { get { return PropertyDefinitionType.Data; } } /// /// Gets the value as a string /// /// public override string ValueAsString() { if (IsNull) throw new Exception(Strings.ErrorNullValue); return this.Value; } } }