ConditionalTagHandler.cs :  » Persistence-Frameworks » iBATIS.NET » IBatisNet » DataMapper » Configuration » Sql » Dynamic » Handlers » 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 » iBATIS.NET 
iBATIS.NET » IBatisNet » DataMapper » Configuration » Sql » Dynamic » Handlers » ConditionalTagHandler.cs

#region Apache Notice
/*****************************************************************************
 * $Revision: 405046 $
 * $LastChangedDate: 2006-05-08 15:21:44 +0200 (lun., 08 mai 2006) $
 * $LastChangedBy: gbayon $
 * 
 * iBATIS.NET Data Mapper
 * Copyright (C) 2006/2005 - The Apache Software Foundation
 *  
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 ********************************************************************************/
#endregion

#region Imports
using System;
using System.Text;
using IBatisNet.Common.Utilities.Objects.Members;
using IBatisNet.DataMapper.Exceptions;
using IBatisNet.DataMapper.Configuration.Sql.Dynamic.Elements;
using IBatisNet.Common.Utilities.Objects;

#endregion

namespace IBatisNet.DataMapper.Configuration.Sql.Dynamic.Handlers{
  /// <summary>
  /// Description rsume de ConditionalTagHandler.
  /// </summary>
  public abstract class ConditionalTagHandler : BaseTagHandler 
  {

    #region Const
    /// <summary>
    /// 
    /// </summary>
    public const long NOT_COMPARABLE = long.MinValue;
    #endregion


        /// <summary>
        /// Initializes a new instance of the <see cref="ConditionalTagHandler"/> class.
        /// </summary>
        /// <param name="accessorFactory">The accessor factory.</param>
        public ConditionalTagHandler(AccessorFactory accessorFactory)
            : base(accessorFactory)
    {
    }

    #region Methods
    /// <summary>
    /// 
    /// </summary>
    /// <param name="ctx"></param>
    /// <param name="tag"></param>
    /// <param name="parameterObject"></param>
    /// <returns></returns>
    public abstract bool IsCondition(SqlTagContext ctx, SqlTag tag, object parameterObject);

    /// <summary>
    /// 
    /// </summary>
    /// <param name="ctx"></param>
    /// <param name="tag"></param>
    /// <param name="parameterObject"></param>
    /// <returns></returns>
    public override int DoStartFragment(SqlTagContext ctx, SqlTag tag, Object parameterObject) 
    {
      if (IsCondition(ctx, tag, parameterObject)) 
      {
        return BaseTagHandler.INCLUDE_BODY;
      } 
      else 
      {
        return BaseTagHandler.SKIP_BODY;
      }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="ctx"></param>
    /// <param name="tag"></param>
    /// <param name="parameterObject"></param>
    /// <param name="bodyContent"></param>
    /// <returns></returns>
    public override int DoEndFragment(SqlTagContext ctx, SqlTag tag, Object parameterObject, StringBuilder bodyContent) 
    {
      return BaseTagHandler.INCLUDE_BODY;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="ctx"></param>
    /// <param name="sqlTag"></param>
    /// <param name="parameterObject"></param>
    /// <returns></returns>
    protected long Compare(SqlTagContext ctx, SqlTag sqlTag, object parameterObject)
    {
      Conditional tag = (Conditional)sqlTag;
      string propertyName = tag.Property;
      string comparePropertyName = tag.CompareProperty;
      string compareValue = tag.CompareValue;

      object value1 = null;
      Type type = null;
      if (propertyName != null && propertyName.Length > 0) 
      {
        value1 = ObjectProbe.GetMemberValue(parameterObject, propertyName, this.AccessorFactory);
        type = value1.GetType();
      } 
      else 
      {
        value1 = parameterObject;
        if (value1 != null) 
        {
          type = parameterObject.GetType();
        } 
        else 
        {
          type = typeof(object);
        }
      }
      if (comparePropertyName != null && comparePropertyName.Length > 0) 
      {
                object value2 = ObjectProbe.GetMemberValue(parameterObject, comparePropertyName, this.AccessorFactory);
        return CompareValues(type, value1, value2);
      } 
      else if (compareValue != null && compareValue != "") 
      {
        return CompareValues(type, value1, compareValue);
      } 
      else 
      {
        throw new DataMapperException("Error comparing in conditional fragment.  Uknown 'compare to' values.");
      }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="type"></param>
    /// <param name="value1"></param>
    /// <param name="value2"></param>
    /// <returns></returns>
    protected long CompareValues(Type type, object value1, object value2) 
    {
      long result = NOT_COMPARABLE;

      if (value1 == null || value2 == null) 
      {
        result = value1 == value2 ? 0 : NOT_COMPARABLE;
      } 
      else 
      {
        if (value2.GetType() != type) 
        {
          value2 = ConvertValue(type, value2.ToString());
        }
        if (value2 is string && type != typeof(string)) 
        {
          value1 = value1.ToString();
        }
        if (!(value1 is IComparable && value2 is IComparable)) 
        {
          value1 = value1.ToString();
          value2 = value2.ToString();
        }
        result = ((IComparable) value1).CompareTo(value2);
      }

      return result;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="type"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    protected object ConvertValue(Type type, string value) 
    {

      if (type == typeof(String)) 
      {
        return value;
      } 
      else if (type == typeof(bool)) 
      {
        return System.Convert.ToBoolean(value);
      } 
      else if (type == typeof(Byte)) 
      {
        return System.Convert.ToByte(value);
      } 
      else if (type == typeof(Char)) 
      {
        return System.Convert.ToChar(value.Substring(0,1));//new Character(value.charAt(0));
      }      
      else if (type == typeof(DateTime)) 
      {
        try 
        {
          return System.Convert.ToDateTime(value);
        } 
        catch (Exception e) 
        {
          throw new DataMapperException("Error parsing date. Cause: " + e.Message, e);
        }
      } 
      else if (type == typeof(Decimal)) 
      {
        return System.Convert.ToDecimal(value);
      } 
      else if (type == typeof(Double))  
      {
        return System.Convert.ToDouble(value);
      } 
      else if (type == typeof(Int16)) 
      {
        return System.Convert.ToInt16(value);
      } 
      else if (type == typeof(Int32)) 
      {
        return System.Convert.ToInt32(value);
      } 
      else if (type == typeof(Int64)) 
      {
        return System.Convert.ToInt64(value);
      } 
      else if (type == typeof(Single)) 
      {
        return System.Convert.ToSingle(value);
      } 
      else 
      {
        return value;
      }

    }
    #endregion

  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.