/// <summary>
 /// Construct expression by a series of parameters.
 /// </summary>
 /// <param name="fieldName">Query expression field name</param>
 /// <param name="op">Query expression operator</param>
 /// <param name="value">Query expression value</param>
 /// <param name="configuration">Query field configuration</param>
 public QueryParameterExpression(string fieldName, QueryFieldOperators op, object value, QueryFieldConfiguration configuration)
 {
     this.FieldName = fieldName;
     this.Operator = op;
     this.Value = value;
     this.Configuration = configuration;
 }
        object IControlValueResolver.Resolve(QueryFieldConfiguration queryFieldConfiguration, string httpPostValue)
        {
            if (queryFieldConfiguration.FieldValueType != null)
                return Kit.ConvertType(httpPostValue, queryFieldConfiguration.FieldValueType);

            return httpPostValue;
        }
        object IControlValueResolver.Resolve(QueryFieldConfiguration queryFieldConfiguration, string httpPostValue)
        {
            DateTime returnedValue;
            if (!DateTime.TryParse(httpPostValue, out returnedValue))
                throw new ControlValueResolveException(string.Format(CultureInfo.InvariantCulture, Resources.DP_ControlValueConvertFailedMessage, httpPostValue, "DateTime"));

            return returnedValue;
        }
        /// <summary>
        /// Resolve server side control value from http post value for specified query field configuration.
        /// </summary>
        /// <param name="queryFieldConfiguration"></param>
        /// <param name="httpPostValue"></param>
        /// <returns></returns>
        public object Resolve(QueryFieldConfiguration queryFieldConfiguration, string httpPostValue)
        {
            string resolverName = null;
            if (queryFieldConfiguration.Control.ControlType == ControlConfigurationTypes.Custom)
                resolverName = (queryFieldConfiguration.Control as CustomConfiguration).ResolverName;
            else
                resolverName = string.Format(CultureInfo.InvariantCulture, "{0}ControlValueResolver", queryFieldConfiguration.Control.ControlType);

            if (!this.Resolvers.ContainsKey(resolverName))
                throw new ConfigurationErrorsException(string.Format(CultureInfo.InvariantCulture, Resources.DP_ControlValueResolverUnavailable, resolverName));

            IControlValueResolver resolver = this.Resolvers[resolverName];
            return resolver.Resolve(queryFieldConfiguration, httpPostValue);
        }
        object IControlValueResolver.Resolve(QueryFieldConfiguration queryFieldConfiguration, string httpPostValue)
        {
            int selectedIndex;
            if (int.TryParse(httpPostValue, out selectedIndex))
            {
                RadioGroupConfiguration radioGroupConfiguration = queryFieldConfiguration.Control as RadioGroupConfiguration;
                if(selectedIndex>= radioGroupConfiguration.Items.Count)
                    throw new IndexOutOfRangeException(string.Format(CultureInfo.InvariantCulture, "The http post value for the query field \"{0}\" mapping to RadioGroup is out of radio group items.", httpPostValue));

                var fieldValue = radioGroupConfiguration.Items[selectedIndex].Value;
                if (queryFieldConfiguration.FieldValueType != null)
                    return Kit.ConvertType(fieldValue, queryFieldConfiguration.FieldValueType);

                return fieldValue;
            }

            string errorMsg = string.Format(CultureInfo.InvariantCulture, "The http post value for the query field \"{0}\" mapping to RadioGroup is invalid.", httpPostValue);
            throw new ValidationException(errorMsg);
        }
        object IControlValueResolver.Resolve(QueryFieldConfiguration queryFieldConfiguration, string httpPostValue)
        {
            if(string.IsNullOrEmpty(httpPostValue))
                return new ArrayList();

            List<HierarchyItem> hierarchyItems = serializer.Deserialize<List<HierarchyItem>>(httpPostValue);
            IEnumerable<string> postedValueItems = hierarchyItems.Select(item => item.Id.ToString());

            Type fieldValueType = queryFieldConfiguration.FieldValueType;
            ArrayList parameters = new ArrayList();
            foreach (string postedValueItem in postedValueItems)
            {
                object parameterValue = postedValueItem;
                if (fieldValueType != null)
                    parameterValue = Kit.ConvertType(postedValueItem.Trim(), fieldValueType);

                parameters.Add(parameterValue);
            }

            return parameters;
        }
        object IControlValueResolver.Resolve(QueryFieldConfiguration queryFieldConfiguration, string httpPostValue)
        {
            CheckBoxGroupConfiguration checkBoxGroupConfiguration = queryFieldConfiguration.Control as CheckBoxGroupConfiguration;
            string[] postedValueItems = httpPostValue.Split(new[] { WebUtility.ARRAY_ITEM_SEPARATOR }, StringSplitOptions.None);

            Type fieldValueType = queryFieldConfiguration.FieldValueType;

            ArrayList parameters = new ArrayList();
            foreach (string postedValueItem in postedValueItems)
            {
                int checkBoxItemIndex;
                if (int.TryParse(postedValueItem, out checkBoxItemIndex) && checkBoxItemIndex < checkBoxGroupConfiguration.Items.Count)
                {
                    string checkBoxValue = checkBoxGroupConfiguration.Items[checkBoxItemIndex].Value;
                    object parameterValue = checkBoxValue;
                    if (fieldValueType != null)
                        parameterValue = Kit.ConvertType(checkBoxValue, fieldValueType);

                    parameters.Add(parameterValue);
                }
            }

            return parameters;
        }
        private HtmlTableCell CreateControlCells(Panel layoutPanel, HtmlTableRow row, QueryFieldConfiguration queryFieldConfiguration, IEnumerable<IQueryFieldControl> controls, int queryControlOccupiedCells)
        {
            HtmlTableCell labelCell = new HtmlTableCell("td");
            labelCell.Attributes["class"] = "c1";
            labelCell.NoWrap = true;
            labelCell.InnerText = queryFieldConfiguration.Control.Label;
            row.Cells.Add(labelCell);

            HtmlTableCell controlCell = new HtmlTableCell("td");
            HtmlTableCell lastControlCell = controlCell;
            controlCell.Attributes["class"] = "c2";
            controlCell.NoWrap = true;
            if (queryControlOccupiedCells > 1)
                controlCell.ColSpan = queryControlOccupiedCells - 1;

            row.Cells.Add(controlCell);

            if (controls.Count() == 1)
            {
                controlCell.Controls.Add(controls.FirstOrDefault() as Control);
            }
            else
            {
                HtmlTable controlContainerTable = new HtmlTable { CellPadding = 0, CellSpacing = 0 }; ;
                controlCell.Controls.Add(controlContainerTable);

                HtmlTableRow controlContainerRow = new HtmlTableRow();
                controlContainerTable.Rows.Add(controlContainerRow);

                bool addFirstQueryFieldControl = true;
                foreach (IQueryFieldControl queryFieldControl in controls)
                {
                    if (!addFirstQueryFieldControl)
                    {
                        HtmlTableCell controlSeparatorCell = new HtmlTableCell { InnerHtml = string.Format(CultureInfo.InvariantCulture, " {0} ", Resources.DPCtrl_QueryFieldControl_Between_Separator), Width = "16px", Align = "center" };
                        controlContainerRow.Cells.Add(controlSeparatorCell);
                    }

                    HtmlTableCell controlContainerCell = new HtmlTableCell();
                    controlContainerRow.Cells.Add(controlContainerCell);
                    controlContainerCell.Controls.Add(queryFieldControl as Control);
                    addFirstQueryFieldControl = false;
                }
            }

            return lastControlCell;
        }
        private static IQueryFieldControl CreateQueryFieldControl(QueryFieldConfiguration queryFieldConfiguration)
        {
            switch (queryFieldConfiguration.Control.ControlType)
            {
                case ControlConfigurationTypes.CheckBox:
                    return CreateCheckBox(queryFieldConfiguration.Control as CheckBoxConfiguration);

                case ControlConfigurationTypes.CheckBoxGroup:
                    return CreateCheckBoxGroup(queryFieldConfiguration.Control as CheckBoxGroupConfiguration);

                case ControlConfigurationTypes.ComboBox:
                    return CreateComboBox(queryFieldConfiguration.Control as ComboBoxConfiguration);

                case ControlConfigurationTypes.Date:
                    return CreateDatePicker(queryFieldConfiguration.Control as DateConfiguration);

                case ControlConfigurationTypes.DateTime:
                    return CreateDateTimePicker(queryFieldConfiguration.Control as DateTimeConfiguration);

                case ControlConfigurationTypes.Decimal:
                    return CreateDecimalTextBox(queryFieldConfiguration.Control as DecimalConfiguration);

                case ControlConfigurationTypes.Integer:
                    return CreateIntegerTextBox(queryFieldConfiguration.Control as IntegerConfiguration);

                case ControlConfigurationTypes.RadioGroup:
                    return CreateRadioGroup(queryFieldConfiguration.Control as RadioGroupConfiguration);

                case ControlConfigurationTypes.TextBox:
                    return CreateTextBox(queryFieldConfiguration.Control as TextBoxConfiguration);

                case ControlConfigurationTypes.HierarchySelector:
                    return CreateHierarchySelector(queryFieldConfiguration.Control as HierarchySelectorConfiguration);

                case ControlConfigurationTypes.Custom:
                    return CreateCustomControl(queryFieldConfiguration.Control as CustomConfiguration);
            }

            throw new NotSupportedException(Resources.DPCtrl_QueryFieldControlNotSupport);
        }
 private static int CalculateQueryFieldControlCount(QueryFieldConfiguration queryFieldConfiguration)
 {
     switch (queryFieldConfiguration.Operator)
     {
         case QueryFieldOperators.Between:
             return 2;
         case QueryFieldOperators.Auto:
             if (TypesUseDefaultOperatorBetween.Contains(queryFieldConfiguration.Control.ControlType)) return 2;
             else return 1;
         default:
             return 1;
     }
 }
        /// <summary>
        /// Create Query controls from configuration.
        /// </summary>
        /// <param name="queryFieldConfiguration"></param>
        /// <returns></returns>
        public static IEnumerable<IQueryFieldControl> CreateQueryControls(QueryFieldConfiguration queryFieldConfiguration)
        {
            bool useBetween = false;
            if (queryFieldConfiguration.Operator == QueryFieldOperators.Between)
                useBetween = true;

            if (queryFieldConfiguration.Operator == QueryFieldOperators.Auto && TypesUseDefaultOperatorBetween.Contains(queryFieldConfiguration.Control.ControlType))
                useBetween = true;

            IQueryFieldControl[] queryFieldControls;
            if (!useBetween)
                queryFieldControls = new[] { CreateQueryFieldControl(queryFieldConfiguration) };
            else
                queryFieldControls = new[] { CreateQueryFieldControl(queryFieldConfiguration), CreateQueryFieldControl(queryFieldConfiguration) };

            return queryFieldControls;
        }
        /// <summary>
        /// Calculate how many cells the query field control occupied in rendering UI.
        /// </summary>
        /// <param name="queryFieldConfiguration"></param>
        /// <returns></returns>
        public static int CalculateQueryControlOccupation(QueryFieldConfiguration queryFieldConfiguration)
        {
            if (queryFieldConfiguration.Occupation > 0)
                return queryFieldConfiguration.Occupation;

            switch (queryFieldConfiguration.Operator)
            {
                case QueryFieldOperators.Between:
                    return 2;
                case QueryFieldOperators.In:
                case QueryFieldOperators.NotIn:
                    return int.MaxValue;
                case QueryFieldOperators.Auto:
                    if (TypesUseDefaultOperatorBetween.Contains(queryFieldConfiguration.Control.ControlType)) return 2;
                    else if (TypesOccupiedWholeLine.Contains(queryFieldConfiguration.Control.ControlType)) return 999;
                    else return 1;
                default:
                    return 1;
            }
        }
 object IControlValueResolver.Resolve(QueryFieldConfiguration queryFieldConfiguration, string httpPostValue)
 {
     return true;
 }