/// <summary>
 /// Initializes a new instance of the <see cref="UPCRMAlternateExpandChecker"/> class.
 /// </summary>
 /// <param name="recordIdentification">
 /// The record identification.
 /// </param>
 /// <param name="rootExpand">
 /// The root expand.
 /// </param>
 /// <param name="theDelegate">
 /// The the delegate.
 /// </param>
 public UPCRMAlternateExpandChecker(
     string recordIdentification,
     UPConfigExpand rootExpand,
     IAlternateExpandCheckerDelegate theDelegate)
 {
     this.recordIdentification = recordIdentification;
     this.rootExpand           = rootExpand;
     this.theDelegate          = theDelegate;
 }
        /// <summary>
        /// The search operation did finish with result.
        /// </summary>
        /// <param name="operation">
        /// The operation.
        /// </param>
        /// <param name="result">
        /// The result.
        /// </param>
        public void SearchOperationDidFinishWithResult(Operation operation, UPCRMResult result)
        {
            if (result?.RowCount > 0)
            {
                UPCRMResultRow row = result.ResultRowAtIndex(0) as UPCRMResultRow;
                this.targetExpand = this.rootExpand.ExpandForResultRow(row);
            }
            else
            {
                this.targetExpand = this.rootExpand;
            }

            this.theDelegate?.AlternateExpandCheckerDidFinishWithResult(this, this.targetExpand);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="UPConfigExpand"/> class.
        /// </summary>
        /// <param name="expand">The expand.</param>
        /// <param name="crmQuery">The CRM query.</param>
        public UPConfigExpand(UPConfigExpand expand, UPContainerMetaInfo crmQuery)
        {
            this.InfoAreaId       = expand.InfoAreaId;
            this.FieldGroupName   = expand.FieldGroupName;
            this.HeaderGroupName  = expand.HeaderGroupName;
            this.MenuLabel        = expand.MenuLabel;
            this.TableCaptionName = expand.TableCaptionName;
            this.ColorKey         = expand.ColorKey;
            this.ImageName        = expand.ImageName;

            this.AlternateExpands = expand.AlternateExpands?
                                    .Select(alternate => new UPConfigExpandAlternate(alternate, crmQuery, this, new List <string> {
                expand.UnitName
            }))
                                    .ToList();
        }
        /// <summary>
        /// Expands for result row.
        /// </summary>
        /// <param name="row">The row.</param>
        /// <returns>expand configuration instance</returns>
        public UPConfigExpand ExpandForResultRow(UPCRMResultRow row)
        {
            UPConfigExpand alternateExpand = null;

            if (this.AlternateExpands != null)
            {
                foreach (var alternate in this.AlternateExpands)
                {
                    alternateExpand = alternate.ResultForRow(row);
                    if (alternateExpand != null)
                    {
                        break;
                    }
                }
            }

            return(alternateExpand ?? this);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="UPConfigExpandAlternate"/> class.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="crmQuery">The CRM query.</param>
        /// <param name="parentExpand">The parent expand.</param>
        /// <param name="parentList">The parent list.</param>
        public UPConfigExpandAlternate(UPConfigExpandAlternate source, UPContainerMetaInfo crmQuery, UPConfigExpand parentExpand, List <string> parentList)
        {
            this.ParentExpand  = parentExpand;
            this.AlternateName = source.AlternateName;
            var conditionArray = new List <UPConfigExpandCondition>(source.Conditions.Count);

            foreach (var condition in source.Conditions)
            {
                conditionArray.Add(new UPConfigExpandConditionForQuery(condition, crmQuery, this));
            }

            this.Conditions    = conditionArray;
            this.CurrentExpand = ConfigurationUnitStore.DefaultStore.ExpandByName(this.AlternateName);
            if (this.CurrentExpand != null && this.CurrentExpand.AlternateExpands?.Count > 0)
            {
                var subSet = new List <string>(parentList)
                {
                    this.AlternateName
                };
                List <UPConfigExpandAlternate> _subAlternates = null;
                foreach (var subSource in this.CurrentExpand.AlternateExpands)
                {
                    if (subSet.Contains(subSource.AlternateName))
                    {
                        continue;
                    }

                    var subAlternate = new UPConfigExpandAlternate(subSource, crmQuery, this.CurrentExpand, subSet);
                    if (_subAlternates == null)
                    {
                        _subAlternates = new List <UPConfigExpandAlternate> {
                            subAlternate
                        };
                    }
                    else
                    {
                        _subAlternates.Add(subAlternate);
                    }
                }

                this.AlternateExpands = _subAlternates;
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="UPConfigExpandAlternate"/> class.
        /// </summary>
        /// <param name="alternateName">Name of the alternate.</param>
        /// <param name="conditions">The conditions.</param>
        /// <param name="parentExpand">The parent expand.</param>
        public UPConfigExpandAlternate(string alternateName, List <object> conditions, UPConfigExpand parentExpand)
        {
            this.ParentExpand  = parentExpand;
            this.AlternateName = alternateName;
            if (conditions == null)
            {
                return;
            }

            var conditionArray = new List <UPConfigExpandCondition>(conditions.Count);

            conditionArray.AddRange(from List <object> conditionDef in conditions select new UPConfigExpandCondition(conditionDef, this));

            this.Conditions = conditionArray;
        }