protected Boolean ProcessIntersectionSet(String forStatement, Boolean outputStep)
        {
            Boolean success = true;

            String procedureStatement = String.Empty;

            Int32 currentPass = 0;

            Int64 currentRowIndex = 0;

            System.Data.DataTable memberServiceTable = new System.Data.DataTable();

            System.Diagnostics.Debug.WriteLine("\r\n\r\n ----- " + Name + " -----");

            try {
                do
                {
                    currentPass = currentPass + 1;


                    if (String.IsNullOrEmpty(forStatement))
                    {
                        procedureStatement = String.Empty;

                        procedureStatement = procedureStatement + IntersectionSetMemberServiceSql();

                        procedureStatement = procedureStatement + IntersectionSetMemberServiceSortedSql();

                        procedureStatement = procedureStatement + IntersectionSetMemberServiceFilteredSql();
                    }

                    else
                    {
                        procedureStatement = forStatement;
                    }


                    System.Diagnostics.Debug.WriteLine(procedureStatement);

                    if (outputStep)
                    {
                        base.ProcessStep_StartStep("Service Set", Name + ": " + currentPass, procedureStatement);
                    }

                    memberServiceTable = base.application.EnvironmentDatabase.SelectDataTable(procedureStatement, 86400);

                    if (base.application.EnvironmentDatabase.LastException != null)
                    {
                        throw base.application.EnvironmentDatabase.LastException;
                    }

                    currentRowIndex = 0;

                    foreach (System.Data.DataRow currentRow in memberServiceTable.Rows)
                    {
                        currentRowIndex = currentRowIndex + 1;

                        MemberService memberService = new MemberService(application);

                        memberService.MemberId = (Int64)currentRow["MemberId"];

                        memberService.ServiceId = Id;

                        memberService.EventDate = (DateTime)currentRow["EventDate"];

                        memberService.AddedManually = false;

                        success = memberService.Save(base.application);

                        if (success)
                        {
                            for (Int32 currentDefinition = 1; currentDefinition <= ActiveDefinitions.Count; currentDefinition++)
                            {
                                String fieldPrefix = "Service" + currentDefinition.ToString() + "_";


                                // THE BELOW SHOULD BE COLUMN "SetDefinitionId" VERSUS "ServiceSetDefinitionId" FROM DYNAMIC SQL

                                MemberServiceDetailSet memberServiceDetail = new MemberServiceDetailSet(memberService.Id, (Int64)currentRow[fieldPrefix + "SetDefinitionId"]);

                                memberServiceDetail.MemberId = memberService.MemberId;

                                memberServiceDetail.DetailMemberServiceId = (Int64)currentRow[fieldPrefix + "MemberServiceId"];

                                memberServiceDetail.EventDate = (DateTime)currentRow[fieldPrefix + "EventDate"];

                                memberServiceDetail.ParentServiceId = Id;

                                memberServiceDetail.ServiceId = (Int64)currentRow[fieldPrefix + "ServiceId"];

                                memberServiceDetail.ServiceName = (String)currentRow[fieldPrefix + "ServiceName"];

                                memberServiceDetail.ServiceType = (Mercury.Server.Core.MedicalServices.Enumerations.MedicalServiceType)(Int32) currentRow[fieldPrefix + "ServiceType"];

                                success = memberServiceDetail.Save(base.application);

                                if (!success)
                                {
                                    break;
                                }
                            }
                        }
                    }

                    if (outputStep)
                    {
                        base.ProcessStep_StopStep((success) ? "Success: " + memberServiceTable.Rows.Count.ToString() : "Failure", "");
                    }

                    if (!success)
                    {
                        break;
                    }
                } while (memberServiceTable.Rows.Count != 0);
            }

            catch (Exception executionException) {
                success = false;

                base.application.SetLastException(executionException);

                if (outputStep)
                {
                    base.ProcessStep_StopStep((success) ? "Success: " + memberServiceTable.Rows.Count.ToString() : "Failure", executionException.Message);
                }
            }

            return(success);
        }
        public Boolean Process()
        {
            Boolean success = true;

            String selectStatement = String.Empty;

            System.Data.DataTable resultsTable;


            Int64 memberServiceId;

            MemberService memberService;

            MemberServiceDetailSet memberServiceDetail;


            if (!Enabled)
            {
                return(false);
            }


            base.ProcessLog_StartProcess();

            switch (SetType)
            {
            case Mercury.Server.Core.MedicalServices.Enumerations.ServiceSetType.Union:

                #region Union Set

                foreach (MedicalServices.Definitions.ServiceSetDefinition currentDefinition in ActiveDefinitions)
                {
                    if (currentDefinition.Enabled)
                    {
                        selectStatement = currentDefinition.SqlStatement;

                        if (!String.IsNullOrEmpty(selectStatement))
                        {
                            resultsTable = base.application.EnvironmentDatabase.SelectDataTable(selectStatement, 0);

                            foreach (System.Data.DataRow currentRow in resultsTable.Rows)
                            {
                                memberServiceId = base.application.MemberServiceGetId((Int64)currentRow["MemberId"], Id, (DateTime)currentRow["EventDate"]);

                                if (memberServiceId == 0)
                                {
                                    memberService = new MemberService(application);

                                    memberService.MemberId = (Int64)currentRow["MemberId"];

                                    memberService.ServiceId = Id;

                                    memberService.EventDate = (DateTime)currentRow["EventDate"];

                                    memberService.AddedManually = false;

                                    success = memberService.Save(base.application);

                                    memberServiceId = memberService.Id;
                                }

                                if ((success) && (memberServiceId != 0))
                                {
                                    memberServiceDetail = new MemberServiceDetailSet(memberServiceId, currentDefinition.Id);

                                    memberServiceDetail.MapDataFields(currentRow);

                                    success = memberServiceDetail.Save(base.application);
                                }

                                if (!success)
                                {
                                    break;
                                }
                            }
                        }
                    }

                    if (!success)
                    {
                        break;
                    }
                }


                #endregion

                break;

            case Mercury.Server.Core.MedicalServices.Enumerations.ServiceSetType.Intersection:

                if (definitions.Count >= 4)
                {
                    success = ProcessIntersectionSetLarge();
                }

                else
                {
                    success = ProcessIntersectionSet(String.Empty, true);
                }

                break;
            }

            base.ProcessLog_StopProcess((success) ? "Success" : "Failure", "");

            return(success);
        }
        public List <MemberServiceDetailSet> Preview(Application application)
        {
            List <MemberServiceDetailSet> previewResults = new List <MemberServiceDetailSet> ();

            MemberServiceDetailSet detailResult;

            System.Data.DataTable resultsTable;

            String sqlStatement = String.Empty;


            String identifiedMemberServices = String.Empty;


            switch (SetType)
            {
            case Mercury.Server.Core.MedicalServices.Enumerations.ServiceSetType.Union:

                #region Union Set

                foreach (MedicalServices.Definitions.ServiceSetDefinition currentDefinition in ActiveDefinitions)
                {
                    if (currentDefinition.Enabled)
                    {
                        if (sqlStatement != String.Empty)
                        {
                            sqlStatement = sqlStatement + "\r\n UNION ";
                        }

                        sqlStatement = sqlStatement + currentDefinition.SqlStatement;
                    }
                }

                if (!String.IsNullOrEmpty(sqlStatement))
                {
                    sqlStatement = sqlStatement.Replace("SELECT", "SELECT TOP 10");

                    sqlStatement = sqlStatement.Replace("LEFT JOIN MemberServiceDetailSet ON MemberService.MemberServiceId = MemberServiceDetailSet.MemberServiceId", "");

                    sqlStatement = sqlStatement.Replace("AND MemberServiceDetailSet.ParentServiceId = " + Id.ToString(), "");

                    sqlStatement = sqlStatement.Replace("AND MemberServiceDetailSet.MemberServiceId IS NULL", "");

                    resultsTable = base.application.EnvironmentDatabase.SelectDataTable(sqlStatement, 0);

                    foreach (System.Data.DataRow currentRow in resultsTable.Rows)
                    {
                        detailResult = new MemberServiceDetailSet(0, (Int64)currentRow ["ServiceSetDefinitionId"]);

                        detailResult.MapDataFields(currentRow);

                        previewResults.Add(detailResult);
                    }
                }

                #endregion

                break;

            case Mercury.Server.Core.MedicalServices.Enumerations.ServiceSetType.Intersection:

                #region Intersection Set

                String virtualJoinTables = String.Empty;


                sqlStatement = IntersectionSqlStatement(out virtualJoinTables);

                sqlStatement = "SELECT TOP 10 " + sqlStatement.Substring(6, sqlStatement.Length - 6);

                resultsTable = base.application.EnvironmentDatabase.SelectDataTable(sqlStatement, 0);

                foreach (System.Data.DataRow currentRow in resultsTable.Rows)
                {
                    Int64 memberId = (Int64)currentRow ["MemberServiceMemberId"];

                    DateTime maxEventDate = new DateTime(1900, 1, 1);

                    String uniqueServiceKey;

                    foreach (String currentTable in virtualJoinTables.Split(';'))
                    {
                        if (maxEventDate < ((DateTime)currentRow [currentTable + "EventDate"]))
                        {
                            maxEventDate = (DateTime)currentRow [currentTable + "EventDate"];
                        }
                    }

                    uniqueServiceKey = memberId.ToString() + ":" + maxEventDate.ToString("MM/dd/yyyy");

                    if (!identifiedMemberServices.Contains(uniqueServiceKey))
                    {
                        identifiedMemberServices = identifiedMemberServices + uniqueServiceKey + "|";

                        foreach (String currentTable in virtualJoinTables.Split(';'))
                        {
                            detailResult = new MemberServiceDetailSet(memberId, (Int64)currentRow[currentTable + "ServiceSetDefinitionId"]);

                            detailResult.MemberId = memberId;

                            detailResult.DetailMemberServiceId = (Int64)currentRow[currentTable + "DetailMemberServiceId"];

                            detailResult.EventDate = (DateTime)currentRow[currentTable + "EventDate"];

                            detailResult.ServiceId = (Int64)currentRow[currentTable + "ServiceId"];

                            detailResult.ServiceName = (String)currentRow[currentTable + "ServiceName"];

                            detailResult.ServiceType = (Mercury.Server.Core.MedicalServices.Enumerations.MedicalServiceType)(Int32) currentRow[currentTable + "ServiceType"];

                            previewResults.Add(detailResult);
                        }
                    }
                }

                #endregion

                break;
            }

            return(previewResults);
        }