/// <summary>
    /// Initializes inner controls.
    /// </summary>
    /// <param name="forceReload">Indicates if form with parameters should be reloaded</param>
    private void InitializeControl(bool forceReload = false)
    {
        // Init rule selector
        uniSelector.Value = mSelectedRuleName;

        MacroRuleInfo mri = MacroRuleInfoProvider.GetMacroRuleInfo(mSelectedRuleName);

        if (mri != null)
        {
            // Show rule description
            ltlDescription.Text       = mri.MacroRuleDescription;
            txtErrorMsg.Value         = string.Empty;
            txtErrorMsg.WatermarkText = String.IsNullOrEmpty(DefaultErrorMessage) ? ResHelper.GetString("basicform.invalidinput") : DefaultErrorMessage;

            // Prepare form for rule parameters
            FormInfo fi = new FormInfo(mri.MacroRuleParameters);
            formProperties.FormInformation = fi;
            DataRow data = fi.GetDataRow();
            formProperties.DataRow = data;

            fi.LoadDefaultValues(formProperties.DataRow, FormResolveTypeEnum.AllFields);
            if ((mMacroRule != null) && (mMacroRuleTree != null) && mMacroRuleTree.RuleName.EqualsCSafe(mSelectedRuleName, true))
            {
                // Set params from rule given by Value property
                foreach (DictionaryEntry entry in mMacroRuleTree.Parameters)
                {
                    string             paramName = ValidationHelper.GetString(entry.Key, String.Empty);
                    MacroRuleParameter param     = entry.Value as MacroRuleParameter;

                    if ((param != null) && data.Table.Columns.Contains(paramName))
                    {
                        data[paramName] = param.Value;
                    }
                }

                txtErrorMsg.Value = mMacroRule.ErrorMessage;
            }

            if (forceReload)
            {
                // Reload params
                formProperties.ReloadData();
            }

            mParamsInitialized = true;
        }
    }
    /// <summary>
    /// Adds a clause according to selected item.
    /// </summary>
    private void AddClause()
    {
        MacroRuleInfo rule = MacroRuleInfoProvider.GetMacroRuleInfo(ValidationHelper.GetInteger(lstRules.SelectedValue, 0));

        if (rule != null)
        {
            List <MacroRuleTree> selected = GetSelected();
            if (selected.Count == 1)
            {
                MacroRuleTree item = selected[0];
                if ((item != null) && (item.Parent != null))
                {
                    item.Parent.AddRule(rule, item.Position + 1);
                    return;
                }
            }

            // Add the rule at the root level, when no selected item
            this.RuleTree.AddRule(rule, this.RuleTree.Children.Count);
        }
    }
    /// <summary>
    /// Creates new <see cref="FieldMacroRule"/> object based on inputs.
    /// </summary>
    private FieldMacroRule CreateMacroRule()
    {
        if (!IsValid())
        {
            return(null);
        }

        MacroRuleTree  main = null;
        FieldMacroRule fmr  = null;

        MacroRuleInfo mri = MacroRuleInfoProvider.GetMacroRuleInfo(mSelectedRuleName);

        if (mri != null)
        {
            main = new MacroRuleTree();

            MacroRuleTree childern = new MacroRuleTree()
            {
                RuleText      = mri.MacroRuleText,
                RuleName      = mri.MacroRuleName,
                RuleCondition = mri.MacroRuleCondition,
                Parent        = main
            };
            main.Children.Add(childern);

            foreach (string paramName in formProperties.Fields)
            {
                // Load value from the form control
                FormEngineUserControl ctrl = formProperties.FieldControls[paramName];
                if (ctrl != null)
                {
                    // Convert value to EN culture
                    var dataType = ctrl.FieldInfo.DataType;

                    var convertedValue = DataTypeManager.ConvertToSystemType(TypeEnum.Field, dataType, ctrl.Value);

                    string value       = ValidationHelper.GetString(convertedValue, "", CultureHelper.EnglishCulture);
                    string displayName = ctrl.ValueDisplayName;

                    if (String.IsNullOrEmpty(displayName))
                    {
                        displayName = value;
                    }

                    MacroRuleParameter param = new MacroRuleParameter
                    {
                        Value     = value,
                        Text      = displayName,
                        ValueType = dataType
                    };

                    childern.Parameters.Add(paramName, param);
                }
            }

            string macroRule = string.Format("Rule(\"{0}\", \"{1}\")", MacroElement.EscapeSpecialChars(main.GetCondition()), MacroElement.EscapeSpecialChars(main.GetXML()));

            if (!MacroSecurityProcessor.IsSimpleMacro(macroRule))
            {
                // Sign complex macros
                macroRule = MacroSecurityProcessor.AddMacroSecurityParams(macroRule, MacroIdentityOption.FromUserInfo(MembershipContext.AuthenticatedUser));
            }

            fmr              = new FieldMacroRule();
            fmr.MacroRule    = string.Format("{{%{0}%}}", macroRule);
            fmr.ErrorMessage = txtErrorMsg.Text;
        }

        return(fmr);
    }