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);
        }