Property.cs :  » Persistence-Frameworks » Data-Holder » DataHolder » Containers » Property » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Persistence Frameworks » Data Holder 
Data Holder » DataHolder » Containers » Property » Property.cs
/*
 * Namespace Summary
 * Copyright (C) 2005+ Bogdan Damian Constantin
 * E-Mail: damianbcpetro@gmail.com
 * WEB: http://www.sourceforge.net/projects/dataholder
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License 2.1 or later, as
 * published by the Free Software Foundation. See the included License.txt
 * or http://www.gnu.org/copyleft/lesser.html for details.
 *
 */

using System;
using System.Data;
using System.Collections;
using System.Xml;
using System.ComponentModel;

namespace DataHolder.Containers.Property{
  /// <summary>
  /// Keeps the definition of a table column
  /// </summary>
  public sealed class Property : GenericDataProperty, Interfaces.IDBGenericProperty, IComparer
  {
    private string _PropertyName;
    private Type _PropertyType;
    private object _PropertyDefaultValue;
    private bool _Mandatory;
    private delegate int ComparerDelagate(object Value1, object Value2);
    private ComparerDelagate Comparer = null;

    public Property(string Name, Type type)
    {      
      _PropertyName = Name;
      _PropertyType = type;
      _Mandatory = false;
      SetComparerDelegate();
    }

    public Property(string Name, Type type, object DefaultValue):this(Name, type)
    {  
      _PropertyDefaultValue = DefaultValue;
    }

    public Property(string Name, Type type, object DefaultValue, bool pMandatory):this(Name, type)
    {  
      _PropertyDefaultValue = DefaultValue;
      _Mandatory = pMandatory;
    }

    public Property(string Name, Type type, object DefaultValue, bool pMandatory, IPropAttribute [] pAttributes):this(Name, type, DefaultValue, pMandatory)
    {
      locAttributes = pAttributes;
    }

    private void SetComparerDelegate()
    {
      if(Comparer != null)
        return;
      if(_PropertyType == typeof(bool))
        Comparer = new ComparerDelagate(CompareBool);
      else if(_PropertyType == typeof(SByte))
        Comparer = new ComparerDelagate(CompareInt16);
      else if(_PropertyType == typeof(Int16))
        Comparer = new ComparerDelagate(CompareInt16);
      else if(_PropertyType == typeof(Int32))
        Comparer = new ComparerDelagate(CompareInt32);
      else if(_PropertyType == typeof(Int64))
        Comparer = new ComparerDelagate(CompareInt64);
      else if(_PropertyType == typeof(byte))
        Comparer = new ComparerDelagate(CompareUInt);
      else if(_PropertyType == typeof(UInt16))
        Comparer = new ComparerDelagate(CompareUInt);
      else if(_PropertyType == typeof(UInt32))
        Comparer = new ComparerDelagate(CompareUInt);
      else if(_PropertyType == typeof(UInt64))
        Comparer = new ComparerDelagate(CompareUInt);
      else if(_PropertyType == typeof(Decimal))
        Comparer = new ComparerDelagate(CompareDecimal);
      else if(_PropertyType == typeof(Double))
        Comparer = new ComparerDelagate(CompareDecimal);
      else if(_PropertyType == typeof(float))
        Comparer = new ComparerDelagate(CompareDecimal);
      else if(_PropertyType == typeof(DateTime))
        Comparer = new ComparerDelagate(CompareDateTime);
      else 
        Comparer = new ComparerDelagate(CompareOther);
    }

    public override string PropertyName
    {
      get{return _PropertyName;}
      set{_PropertyName = value;}
    }

    public override Type PropertyType
    {
      get{return _PropertyType;}
      set{_PropertyType = value;}
    }

    public override void PreSerializatonAction(object data, SerializationHelper srinfo)
    {
      //nothing
    }

    public object PropertyDefaultValue
    {
      get{return _PropertyDefaultValue;}
      set{_PropertyDefaultValue = value;}
    }

    public bool Mandatory
    {
      get{return _Mandatory;}
      set{_Mandatory = value;}
    } 

    private void Validate()
    {
      if(_PropertyName == null || _PropertyName.Trim().Length <= 0)
        throw new Exceptions.DataHolderPropertyException("Wrong Name");
      if(_PropertyType == null)
        throw new Exceptions.DataHolderPropertyException("Wrong Type");
    }
  
    public override void WriteXml(XmlWriter writer, GenericData ge, int PropertyIndex, 
      bool SerializeOriginalValues, bool SerializeIntermediarValues)
    {
      object val = ge[PropertyIndex];
      writer.WriteStartElement(this.PropertyName);
      if(val == null || val == DBNull.Value)
        writer.WriteAttributeString("Null", "1");
      else
        writer.WriteString(val.ToString());
      writer.WriteEndElement();

      
      if(SerializeOriginalValues)
      {
        writer.WriteStartElement(GenericData.OriginalValuesSerializationIdentifier + this.PropertyName);
        val = ge[PropertyIndex, GenericDataVersion.Original];
        if(val == null || val == DBNull.Value)
          writer.WriteAttributeString("Null", "1");
        else
          writer.WriteString(val.ToString());
        writer.WriteEndElement();
      }

      if(SerializeIntermediarValues)
      {
        writer.WriteStartElement(GenericData.IntermediarValuesSerializationIdentifier + this.PropertyName);
        val = ge[PropertyIndex, GenericDataVersion.Intermediar];
        if(val == null || val == DBNull.Value)
          writer.WriteAttributeString("Null", "1");
        else
          writer.WriteString(val.ToString());
        writer.WriteEndElement();
      }
    }

    public override void ReadXml(XmlReader reader, GenericData ge, 
      bool DeSerializeOriginalValues, bool DeSerializeIntermediarValues)
    {
      object val = DBNull.Value;
      string InnerText = reader.ReadString();
      if(InnerText == null || InnerText.Length == 0)
        if(Mandatory)
          val = this.PropertyDefaultValue;
        else
          val = DBNull.Value;
      else
        val = Convert.ChangeType(InnerText, this.PropertyType);
      
      int Index = ge.Properties.IndexOf(this.PropertyName);
      ge[Index] = val;
      if(DeSerializeOriginalValues)
      {
        reader.Read();
        if(reader.IsStartElement(GenericData.OriginalValuesSerializationIdentifier + this.PropertyName))
        {
          InnerText = reader.ReadString();
          if(InnerText == null || InnerText.Length == 0)
            if(Mandatory)
              val = this.PropertyDefaultValue;
            else
              val = DBNull.Value;
          else
            val = Convert.ChangeType(InnerText, this.PropertyType);
        }
        ge[Index, GenericDataVersion.Original] = val;
      }
      if(DeSerializeIntermediarValues)
      {
        reader.Read();
        if(reader.IsStartElement(GenericData.IntermediarValuesSerializationIdentifier + this.PropertyName))
        {
          InnerText = reader.ReadString();
          if(InnerText == null || InnerText.Length == 0)
            if(Mandatory)
              val = this.PropertyDefaultValue;
            else
              val = DBNull.Value;
          else
            val = Convert.ChangeType(InnerText, this.PropertyType);
        }
        ge[Index, GenericDataVersion.Intermediar] = val;
      }
    }

    public int Compare(object Value1, object Value2)
    {
      if(Value1 == DBNull.Value || Value1 == null)
        if(Value2 == DBNull.Value || Value2 == null)
          return 0;
        else
          return -1;
      else if(Value2 == DBNull.Value || Value2 == null)
        return 1;
      else 
        return Comparer(Value1, Value2);
    }

    private int CompareBool(object Value1, object Value2)
    {
      return Convert.ToInt32(Value1) - Convert.ToInt32(Value2);
    }

    private int CompareInt16(object Value1, object Value2)
    {
      return Convert.ToInt32(Convert.ToInt16(Value1) - Convert.ToInt16(Value2));
    }

    private int CompareInt32(object Value1, object Value2)
    {
      return Convert.ToInt32(Convert.ToInt32(Value1) - Convert.ToInt32(Value2));
    }

    private int CompareInt64(object Value1, object Value2)
    {
      return Convert.ToInt32(Convert.ToInt64(Value1) - Convert.ToInt64(Value2));
    }

    private int CompareUInt(object Value1, object Value2)
    {
      return Convert.ToInt32((ulong)Value1 - (ulong)Value2);
    }

    private int CompareDecimal(object Value1, object Value2)
    {
      return Convert.ToInt32(Convert.ToDecimal(Value1) - Convert.ToDecimal(Value2));
    }

    private int CompareDateTime(object Value1, object Value2)
    {
      if((DateTime)Value1 == (DateTime)Value2)
        return 0;
      else if((DateTime)Value1 > (DateTime)Value2)
        return 1;
      else
        return -1;
    }

    private int CompareOther(object Value1, object Value2)
    {
      string V1 = Value1.ToString();
      string V2 = Value2.ToString();
      int Res = V1.Length - V2.Length;
      int Min = V1.Length >  V2.Length ? V2.Length : V1.Length;
      if(Min == 0)
        return Res;
      for(int i = 0; i < Min; i++)
        if(V1[i] > V2[i])
          return 1;
        else if(V1[i] < V2[i])
          return -1;
      return Res;
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.