OrderByBuilder.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / ndp / fx / src / DataWebControls / System / Data / WebControls / OrderByBuilder.cs / 1 / OrderByBuilder.cs

                            using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects; 
using System.Data.Metadata.Edm;
using System.Diagnostics; 
using System.Collections.Specialized; 
using System.Collections;
 
namespace System.Web.UI.WebControls
{
    internal class OrderByBuilder
    { 
        private readonly string _argsSortExpression;
        private readonly EntityDataSourceWrapperCollection _wrapperCollection; 
        private readonly string _orderBy; 
        private readonly bool _autoGenerateOrderByClause;
        private readonly ParameterCollection _orderByParameters; 
        private readonly EntityDataSource _owner;
        private readonly bool _generateDefaultOrderByClause;

        internal OrderByBuilder(string argsSortExpression, 
            EntityDataSourceWrapperCollection wrapperCollection,
            string orderBy, 
            bool autoGenerateOrderByClause, 
            ParameterCollection orderByParameters,
            bool generateDefaultOrderByClause, 
            EntityDataSource owner)
        {
            _argsSortExpression = argsSortExpression;
            _wrapperCollection = wrapperCollection; 
            _orderBy = orderBy;
            _autoGenerateOrderByClause = autoGenerateOrderByClause; 
            _orderByParameters = orderByParameters; 
            _owner = owner;
            _generateDefaultOrderByClause = generateDefaultOrderByClause; 
        }

        internal void Generate(TypeUsage tu, out string orderBy, out ObjectParameter[] orderByParameters)
        { 
            Debug.Assert(null != tu, "Type Usage cannot be null");
            GenerateOrderByClause(tu, out orderBy, out orderByParameters); 
        } 

        private void GenerateOrderByClause(TypeUsage tu, out string orderByClause, out ObjectParameter[] orderByObjectParameters) 
        {
            StringBuilder orderByClauseBuilder = new StringBuilder();

            // This sets the orderBy clause based on a clicked column header in the databound control. 
            AppendOrderByKey(orderByClauseBuilder, _argsSortExpression, Strings.EntityDataSourceView_ColumnHeader, tu);
 
            // AutoGenerateOrderByClause is mutually exclusive with OrderBy. 
            // Only one of the following two if statements will execute.
            if (_autoGenerateOrderByClause) 
            {
                Debug.Assert(String.IsNullOrEmpty(_orderBy), "If AutoGenerateOrderByClause is true, then OrderBy cannot be set. This should have been caught by a runtime error check");
                IOrderedDictionary paramValues = _orderByParameters.GetValues(_owner.HttpContext, _owner);
                foreach (DictionaryEntry de in paramValues) 
                {
                    // Skip AutoGenerateOrderBy on expressions that have a null value. 
                    if (!string.IsNullOrEmpty((string)(de.Value))) 
                    {
                        if (0 < orderByClauseBuilder.Length) 
                        {
                            orderByClauseBuilder.Append(", ");
                        }
                        AppendOrderByKey(orderByClauseBuilder, (string)(de.Value), Strings.EntityDataSourceView_AutoGenerateOrderByParameters, tu); 
                    }
 
                } 
            }
 
            // Append the OrderBy expression, if it's nonzero length.
            //
            if (!String.IsNullOrEmpty(_orderBy))
            { 
                orderByObjectParameters = _owner.GetOrderByParameters();
                Debug.Assert(!_autoGenerateOrderByClause, "If OrderBy is set, AutoGenerateOrderBy must be false. This should have been caught by a runtime error check"); 
                if (0 < orderByClauseBuilder.Length) 
                {
                    orderByClauseBuilder.Append(", "); 
                }
                orderByClauseBuilder.Append(_orderBy);
            }
            else 
            {
                orderByObjectParameters = new ObjectParameter[] { }; 
            } 

            if (orderByClauseBuilder.Length==0 && _generateDefaultOrderByClause) 
            {
                // This only occurs if there's no EntitySet, which means entities are not wrapped.
                orderByClauseBuilder.Append(GenerateDefaultOrderByFromTypeUsage(tu));
            } 

            orderByClause = orderByClauseBuilder.ToString(); 
        } 

        private void AppendOrderByKey(StringBuilder orderByClauseBuilder, string expression, string errorText, TypeUsage tu) 
        {
            if (!String.IsNullOrEmpty(expression))
            {
                const string ascTail = " ASC"; 
                const string descTail = " DESC";
                string tail = String.Empty; 
                string columnName = expression; 
                if (expression.EndsWith(ascTail, StringComparison.OrdinalIgnoreCase))
                { 
                    tail = ascTail;
                    columnName = expression.Substring(0, expression.Length - 4);
                }
                else if (expression.EndsWith(descTail, StringComparison.OrdinalIgnoreCase)) 
                {
                    tail = descTail; 
                    columnName = expression.Substring(0, expression.Length - 5); 
                }
 
                if (EntityDataSourceUtil.PropertyIsOnEntity(columnName, _wrapperCollection, null, tu))
                {
                    orderByClauseBuilder.Append(EntityDataSourceUtil.GetEntitySqlValueForColumnName(columnName, _wrapperCollection));
                    orderByClauseBuilder.Append(tail); 
                }
                else // pass the sort expression through verbatim. 
                { 
                    orderByClauseBuilder.Append(expression);
                } 
            }
        }

        private static string GenerateDefaultOrderByFromTypeUsage(TypeUsage tu) 
        {
            StringBuilder orderByBuilder = new StringBuilder(); 
            ReadOnlyMetadataCollection propertyCollection; 
            List keyMemberNames = null;
            EntityType entityType = tu.EdmType as EntityType; 

            if (null != entityType)
            {
                ReadOnlyMetadataCollection keyMembers; 
                keyMembers = entityType.KeyMembers;
                keyMemberNames = new List(entityType.KeyMembers.Count); 
                propertyCollection = entityType.Properties; 

                foreach (EdmMember edmMember in keyMembers) 
                {
                    keyMemberNames.Add(edmMember.Name);
                }
            } 
            else
            { 
                return String.Empty; 
            }
 
            foreach (EdmProperty property in propertyCollection)
            {
                if (keyMemberNames.Contains(property.Name) &&
                    property.TypeUsage.EdmType.BuiltInTypeKind == BuiltInTypeKind.PrimitiveType) 
                {
                    if (0 < orderByBuilder.Length) 
                    { 
                        orderByBuilder.Append(", ");
                    } 
                    orderByBuilder.Append(EntityDataSourceUtil.EntitySqlElementAlias);
                    orderByBuilder.Append(".");
                    orderByBuilder.Append(EntityDataSourceUtil.QuoteEntitySqlIdentifier(property.Name));
                } 
            }
            return orderByBuilder.ToString(); 
        } 

 

    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
´╗┐using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects; 
using System.Data.Metadata.Edm;
using System.Diagnostics; 
using System.Collections.Specialized; 
using System.Collections;
 
namespace System.Web.UI.WebControls
{
    internal class OrderByBuilder
    { 
        private readonly string _argsSortExpression;
        private readonly EntityDataSourceWrapperCollection _wrapperCollection; 
        private readonly string _orderBy; 
        private readonly bool _autoGenerateOrderByClause;
        private readonly ParameterCollection _orderByParameters; 
        private readonly EntityDataSource _owner;
        private readonly bool _generateDefaultOrderByClause;

        internal OrderByBuilder(string argsSortExpression, 
            EntityDataSourceWrapperCollection wrapperCollection,
            string orderBy, 
            bool autoGenerateOrderByClause, 
            ParameterCollection orderByParameters,
            bool generateDefaultOrderByClause, 
            EntityDataSource owner)
        {
            _argsSortExpression = argsSortExpression;
            _wrapperCollection = wrapperCollection; 
            _orderBy = orderBy;
            _autoGenerateOrderByClause = autoGenerateOrderByClause; 
            _orderByParameters = orderByParameters; 
            _owner = owner;
            _generateDefaultOrderByClause = generateDefaultOrderByClause; 
        }

        internal void Generate(TypeUsage tu, out string orderBy, out ObjectParameter[] orderByParameters)
        { 
            Debug.Assert(null != tu, "Type Usage cannot be null");
            GenerateOrderByClause(tu, out orderBy, out orderByParameters); 
        } 

        private void GenerateOrderByClause(TypeUsage tu, out string orderByClause, out ObjectParameter[] orderByObjectParameters) 
        {
            StringBuilder orderByClauseBuilder = new StringBuilder();

            // This sets the orderBy clause based on a clicked column header in the databound control. 
            AppendOrderByKey(orderByClauseBuilder, _argsSortExpression, Strings.EntityDataSourceView_ColumnHeader, tu);
 
            // AutoGenerateOrderByClause is mutually exclusive with OrderBy. 
            // Only one of the following two if statements will execute.
            if (_autoGenerateOrderByClause) 
            {
                Debug.Assert(String.IsNullOrEmpty(_orderBy), "If AutoGenerateOrderByClause is true, then OrderBy cannot be set. This should have been caught by a runtime error check");
                IOrderedDictionary paramValues = _orderByParameters.GetValues(_owner.HttpContext, _owner);
                foreach (DictionaryEntry de in paramValues) 
                {
                    // Skip AutoGenerateOrderBy on expressions that have a null value. 
                    if (!string.IsNullOrEmpty((string)(de.Value))) 
                    {
                        if (0 < orderByClauseBuilder.Length) 
                        {
                            orderByClauseBuilder.Append(", ");
                        }
                        AppendOrderByKey(orderByClauseBuilder, (string)(de.Value), Strings.EntityDataSourceView_AutoGenerateOrderByParameters, tu); 
                    }
 
                } 
            }
 
            // Append the OrderBy expression, if it's nonzero length.
            //
            if (!String.IsNullOrEmpty(_orderBy))
            { 
                orderByObjectParameters = _owner.GetOrderByParameters();
                Debug.Assert(!_autoGenerateOrderByClause, "If OrderBy is set, AutoGenerateOrderBy must be false. This should have been caught by a runtime error check"); 
                if (0 < orderByClauseBuilder.Length) 
                {
                    orderByClauseBuilder.Append(", "); 
                }
                orderByClauseBuilder.Append(_orderBy);
            }
            else 
            {
                orderByObjectParameters = new ObjectParameter[] { }; 
            } 

            if (orderByClauseBuilder.Length==0 && _generateDefaultOrderByClause) 
            {
                // This only occurs if there's no EntitySet, which means entities are not wrapped.
                orderByClauseBuilder.Append(GenerateDefaultOrderByFromTypeUsage(tu));
            } 

            orderByClause = orderByClauseBuilder.ToString(); 
        } 

        private void AppendOrderByKey(StringBuilder orderByClauseBuilder, string expression, string errorText, TypeUsage tu) 
        {
            if (!String.IsNullOrEmpty(expression))
            {
                const string ascTail = " ASC"; 
                const string descTail = " DESC";
                string tail = String.Empty; 
                string columnName = expression; 
                if (expression.EndsWith(ascTail, StringComparison.OrdinalIgnoreCase))
                { 
                    tail = ascTail;
                    columnName = expression.Substring(0, expression.Length - 4);
                }
                else if (expression.EndsWith(descTail, StringComparison.OrdinalIgnoreCase)) 
                {
                    tail = descTail; 
                    columnName = expression.Substring(0, expression.Length - 5); 
                }
 
                if (EntityDataSourceUtil.PropertyIsOnEntity(columnName, _wrapperCollection, null, tu))
                {
                    orderByClauseBuilder.Append(EntityDataSourceUtil.GetEntitySqlValueForColumnName(columnName, _wrapperCollection));
                    orderByClauseBuilder.Append(tail); 
                }
                else // pass the sort expression through verbatim. 
                { 
                    orderByClauseBuilder.Append(expression);
                } 
            }
        }

        private static string GenerateDefaultOrderByFromTypeUsage(TypeUsage tu) 
        {
            StringBuilder orderByBuilder = new StringBuilder(); 
            ReadOnlyMetadataCollection propertyCollection; 
            List keyMemberNames = null;
            EntityType entityType = tu.EdmType as EntityType; 

            if (null != entityType)
            {
                ReadOnlyMetadataCollection keyMembers; 
                keyMembers = entityType.KeyMembers;
                keyMemberNames = new List(entityType.KeyMembers.Count); 
                propertyCollection = entityType.Properties; 

                foreach (EdmMember edmMember in keyMembers) 
                {
                    keyMemberNames.Add(edmMember.Name);
                }
            } 
            else
            { 
                return String.Empty; 
            }
 
            foreach (EdmProperty property in propertyCollection)
            {
                if (keyMemberNames.Contains(property.Name) &&
                    property.TypeUsage.EdmType.BuiltInTypeKind == BuiltInTypeKind.PrimitiveType) 
                {
                    if (0 < orderByBuilder.Length) 
                    { 
                        orderByBuilder.Append(", ");
                    } 
                    orderByBuilder.Append(EntityDataSourceUtil.EntitySqlElementAlias);
                    orderByBuilder.Append(".");
                    orderByBuilder.Append(EntityDataSourceUtil.QuoteEntitySqlIdentifier(property.Name));
                } 
            }
            return orderByBuilder.ToString(); 
        } 

 

    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.

                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK