#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 System.Globalization;
using System.Xml;
using OSGeo.MapGuide.ObjectModels.Common;
namespace OSGeo.MapGuide.MaestroAPI.Schema
{
///
/// Derives from and represents simple values or
/// collections of simple values. This can take on any of the data types listed in the
/// enumeration.
///
public class DataPropertyDefinition : PropertyDefinition
{
private DataPropertyDefinition() { this.DataType = DataPropertyType.String; }
///
/// Initializes a new instance of the class.
///
/// The name.
/// The description.
public DataPropertyDefinition(string name, string description)
: this()
{
this.Name = name;
this.Description = description;
}
///
/// Gets the data type of this property
///
public DataPropertyType DataType { get; set; }
///
/// Gets or sets the default value. Applies only to string data types
///
public string DefaultValue { get; set; }
///
/// Gets or sets the length of this property. Applies only to string and blob/clob data types
///
public int Length { get; set; }
///
/// Gets or sets whether this property accepts null values
///
public bool IsNullable { get; set; }
///
/// Gets or sets the precision of this property. Applies only to decimal data types
///
public int Precision { get; set; }
///
/// Gets or sets whether this property is read-only
///
public bool IsReadOnly { get; set; }
///
/// Gets or sets the scale of this property. Applies only to decimal data types.
///
public int Scale { get; set; }
///
/// Gets or sets whether this property automatically generates a value on insert.
///
public bool IsAutoGenerated { get; set; }
///
/// Gets the type of Property Definition
///
public override PropertyDefinitionType Type
{
get { return PropertyDefinitionType.Data; }
}
private static string GetXmlType(DataPropertyType dataPropertyType)
{
switch (dataPropertyType)
{
case DataPropertyType.Blob:
return "xs:hexBinary";
case DataPropertyType.Boolean:
return "xs:boolean";
case DataPropertyType.Byte:
return "xs:unsignedByte";
case DataPropertyType.DateTime:
return "xs:dateTime";
case DataPropertyType.Double:
return "xs:double";
case DataPropertyType.Int16:
return "xs:short";
case DataPropertyType.Int32:
return "xs:int";
case DataPropertyType.Int64:
return "xs:long";
case DataPropertyType.Single:
return "xs:float";
case DataPropertyType.String:
return "xs:string";
case DataPropertyType.Clob:
return "fdo:clob";
default:
throw new ArgumentException();
}
}
///
/// Gets the type of the data.
///
/// Type of the XML.
///
public static DataPropertyType GetDataType(string xmlType)
{
switch (xmlType.ToLower())
{
case "xs:hexbinary": //NOXLATE
case "xs:base64binary": //NOXLATE
return DataPropertyType.Blob;
case "xs:boolean": //NOXLATE
return DataPropertyType.Boolean;
case "fdo:byte": //NOXLATE
case "xs:byte": //NOXLATE
case "xs:unsignedbyte": //NOXLATE
return DataPropertyType.Byte;
case "xs:date": //NOXLATE
case "xs:datetime": //NOXLATE
return DataPropertyType.DateTime;
case "fdo:double": //NOXLATE
case "fdo:decimal": //NOXLATE
case "xs:decimal": //NOXLATE
case "xs:double": //NOXLATE
return DataPropertyType.Double;
case "fdo:int16": //NOXLATE
case "xs:int16": //NOXLATE
case "xs:short": //NOXLATE
case "xs:unsignedshort": //NOXLATE
return DataPropertyType.Int16;
case "fdo:int32": //NOXLATE
case "xs:int32": //NOXLATE
case "xs:integer": //NOXLATE
case "xs:negativeinteger": //NOXLATE
case "xs:nonnegativeinteger": //NOXLATE
case "xs:nonpositiveinteger": //NOXLATE
case "xs:positiveinteger": //NOXLATE
case "xs:unsignedint": //NOXLATE
case "xs:int": //NOXLATE
return DataPropertyType.Int32;
case "fdo:int64": //NOXLATE
case "xs:int64": //NOXLATE
case "xs:long": //NOXLATE
case "xs:unsignedlong": //NOXLATE
return DataPropertyType.Int64;
case "xs:float": //NOXLATE
case "xs:single": //NOXLATE
case "fdo:single": //NOXLATE
return DataPropertyType.Single;
case "xs:string": //NOXLATE
return DataPropertyType.String;
case "fdo:clob": //NOXLATE
return DataPropertyType.Clob;
default:
throw new ArgumentException();
}
}
///
/// Writes the current element's content
///
///
///
public override void WriteXml(System.Xml.XmlDocument doc, System.Xml.XmlNode currentNode)
{
var en = Utility.EncodeFDOName(this.Name);
var prop = doc.CreateElement("xs", "element", XmlNamespaces.XS);
prop.SetAttribute("name", en);
prop.SetAttribute("minOccurs", this.IsNullable ? "0" : "1");
if (this.IsReadOnly)
prop.SetAttribute("readOnly", XmlNamespaces.FDO, this.IsReadOnly.ToString().ToLower());
if (this.IsAutoGenerated)
prop.SetAttribute("autogenerated", XmlNamespaces.FDO, this.IsAutoGenerated.ToString().ToLower());
//Write description node
var anno = doc.CreateElement("xs", "annotation", XmlNamespaces.XS); //NOXLATE
var docN = doc.CreateElement("xs", "documentation", XmlNamespaces.XS); //NOXLATE
docN.InnerText = this.Description;
prop.AppendChild(anno);
anno.AppendChild(docN);
var simp = doc.CreateElement("xs", "simpleType", XmlNamespaces.XS); //NOXLATE
prop.AppendChild(simp);
var rest = doc.CreateElement("xs", "restriction", XmlNamespaces.XS);
simp.AppendChild(rest);
rest.SetAttribute("base", GetXmlType(this.DataType));
if (this.DataType == DataPropertyType.String)
{
var max = doc.CreateElement("xs", "maxLength", XmlNamespaces.XS);
max.SetAttribute("value", this.Length.ToString(CultureInfo.InvariantCulture));
rest.AppendChild(max);
}
currentNode.AppendChild(prop);
}
///
/// Set the current element's content from the current XML node
///
///
///
public override void ReadXml(System.Xml.XmlNode node, System.Xml.XmlNamespaceManager mgr)
{
var ro = Utility.GetFdoAttribute(node, "readOnly");
var autogen = Utility.GetFdoAttribute(node, "autogenerated");
this.DataType = GetDataType(node["xs:simpleType"]["xs:restriction"].Attributes["base"].Value.ToLower());
this.IsNullable = (node.Attributes["minOccurs"] != null && node.Attributes["minOccurs"].Value == "0");
this.IsReadOnly = (ro != null && ro.Value == "true");
this.IsAutoGenerated = (autogen != null && autogen.Value == "true");
this.DefaultValue = (node.Attributes["default"] != null ? node.Attributes["default"].Value : string.Empty);
}
///
/// Convenience method to get whether this data property is numeric
///
///
public bool IsNumericType()
{
return this.DataType == DataPropertyType.Byte ||
this.DataType == DataPropertyType.Double ||
this.DataType == DataPropertyType.Int16 ||
this.DataType == DataPropertyType.Int32 ||
this.DataType == DataPropertyType.Int64 ||
this.DataType == DataPropertyType.Single;
}
static ExpressionDataType GetExpressionType(DataPropertyType dt)
{
switch (dt)
{
case DataPropertyType.Blob:
return ExpressionDataType.Data_Blob;
case DataPropertyType.Boolean:
return ExpressionDataType.Data_Boolean;
case DataPropertyType.Byte:
return ExpressionDataType.Data_Byte;
case DataPropertyType.Clob:
return ExpressionDataType.Data_Clob;
case DataPropertyType.DateTime:
return ExpressionDataType.Data_DateTime;
case DataPropertyType.Double:
return ExpressionDataType.Data_Double;
case DataPropertyType.Int16:
return ExpressionDataType.Data_Int16;
case DataPropertyType.Int32:
return ExpressionDataType.Data_Int32;
case DataPropertyType.Int64:
return ExpressionDataType.Data_Int64;
case DataPropertyType.Single:
return ExpressionDataType.Data_Single;
case DataPropertyType.String:
return ExpressionDataType.Data_String;
}
throw new ArgumentException();
}
///
/// Gets the expression data type
///
public override ExpressionDataType ExpressionType
{
get { return GetExpressionType(this.DataType); }
}
}
}