public override System.Xml.XmlDocument XmlSerialize()
        {
            System.Xml.XmlDocument document = base.XmlSerialize();

            System.Xml.XmlNode propertiesNode = document.ChildNodes[1].ChildNodes[0];


            #region Properties

            if (Service != null)
            {
                CommonFunctions.XmlDocumentAppendPropertyNode(document, propertiesNode, "ServiceId", Service.Id.ToString());

                CommonFunctions.XmlDocumentAppendPropertyNode(document, propertiesNode, "ServiceName", Service.Name);
            }

            if (ExclusionService != null)
            {
                CommonFunctions.XmlDocumentAppendPropertyNode(document, propertiesNode, "ExclusionServiceId", ExclusionService.Id.ToString());

                CommonFunctions.XmlDocumentAppendPropertyNode(document, propertiesNode, "ExclusionServiceName", ExclusionService.Name);
            }



            CommonFunctions.XmlDocumentAppendPropertyNode(document, propertiesNode, "AnchorDateInt32", ((Int32)AnchorDate).ToString());

            CommonFunctions.XmlDocumentAppendPropertyNode(document, propertiesNode, "AnchorDate", AnchorDate.ToString());

            CommonFunctions.XmlDocumentAppendPropertyNode(document, propertiesNode, "AnchorDateValue", AnchorDateValue.ToString());

            CommonFunctions.XmlDocumentAppendPropertyNode(document, propertiesNode, "ScheduleDateValue", ScheduleDateValue.ToString());

            CommonFunctions.XmlDocumentAppendPropertyNode(document, propertiesNode, "ScheduleDateQualifierInt32", ((Int32)ScheduleDateQualifier).ToString());

            CommonFunctions.XmlDocumentAppendPropertyNode(document, propertiesNode, "ScheduleDateQualifier", ScheduleDateQualifier.ToString());

            CommonFunctions.XmlDocumentAppendPropertyNode(document, propertiesNode, "Reoccuring", Reoccurring.ToString());

            #endregion


            #region Thresholds

            System.Xml.XmlNode thresholdsNode = document.CreateElement("Thresholds");

            document.LastChild.AppendChild(thresholdsNode);

            foreach (PopulationServiceEventThreshold currentThreshold in Thresholds)
            {
                thresholdsNode.AppendChild(document.ImportNode(currentThreshold.XmlSerialize().LastChild, true));
            }

            #endregion


            #region Object Nodes

            System.Xml.XmlNode dependenciesNode = document.CreateElement("Dependencies");

            document.LastChild.InsertBefore(dependenciesNode, propertiesNode);

            if (Service != null)
            {
                dependenciesNode.AppendChild(document.ImportNode(Service.XmlSerialize().LastChild, true));
            }

            if (ExclusionService != null)
            {
                dependenciesNode.AppendChild(document.ImportNode(ExclusionService.XmlSerialize().LastChild, true));
            }

            #endregion


            return(document);
        }
        public override Boolean Save()
        {
            Boolean useTransaction = (application.EnvironmentDatabase.OpenTransactions == 0); // NO NESTED TRANSACTIONS

            Boolean success = false;

            StringBuilder sqlStatement = new StringBuilder();

            String childIds = String.Empty;


            if (!application.HasEnvironmentPermission(Server.EnvironmentPermissions.CareLevelManage))
            {
                throw new ApplicationException("Permission Denied.");
            }

            Dictionary <String, String> validationResponse = Validate();

            if (validationResponse.Count != 0)
            {
                foreach (String validationKey in validationResponse.Keys)
                {
                    throw new ApplicationException("Invalid [" + validationKey + "]: " + validationResponse[validationKey]);
                }
            }

            modifiedAccountInfo = new Mercury.Server.Data.AuthorityAccountStamp(application.Session);

            try {
                if (useTransaction)
                {
                    application.EnvironmentDatabase.BeginTransaction();
                }

                sqlStatement = new StringBuilder();

                sqlStatement.Append("EXEC dbo.CareLevelActivity_InsertUpdate ");

                sqlStatement.Append(Id.ToString() + ", ");

                sqlStatement.Append("'" + NameSql + "', ");

                sqlStatement.Append("'" + DescriptionSql + "', ");

                sqlStatement.Append(CareLevelId.ToString() + ", ");


                sqlStatement.Append(((Int32)ActivityType).ToString() + ", ");

                sqlStatement.Append(Reoccurring.ToString() + ", ");

                sqlStatement.Append(((Int32)InitialAnchorDate).ToString() + ", ");

                sqlStatement.Append(((Int32)AnchorDate).ToString() + ", ");


                sqlStatement.Append(((Int32)ScheduleType).ToString() + ", ");

                sqlStatement.Append(ScheduleValue.ToString() + ", ");

                sqlStatement.Append(((Int32)ScheduleQualifier).ToString() + ", ");

                sqlStatement.Append(ConstraintValue.ToString() + ", ");

                sqlStatement.Append(((Int32)ConstraintQualifier).ToString() + ", ");


                sqlStatement.Append(((Int32)PerformActionDate).ToString() + ", ");

                if (Action == null)
                {
                    sqlStatement.Append("NULL, NULL, NULL, ");
                }

                else
                {
                    sqlStatement.Append(Action.Id.ToString() + ", ");

                    sqlStatement.Append("'" + Action.ActionParametersXmlSqlParsedString + "', ");

                    sqlStatement.Append("'" + Action.Description + "', ");
                }


                sqlStatement.Append(modifiedAccountInfo.AccountInfoSql);


                success = application.EnvironmentDatabase.ExecuteSqlStatement(sqlStatement.ToString(), 0);

                if (!success)
                {
                    application.SetLastException(application.EnvironmentDatabase.LastException);

                    throw application.EnvironmentDatabase.LastException;
                }

                SetIdentity();

                if (!success)
                {
                    application.SetLastException(application.EnvironmentDatabase.LastException);

                    throw application.EnvironmentDatabase.LastException;
                }


                String deleteStatement = "DELETE FROM CareLevelActivityThreshold WHERE CareLevelActivityId = " + Id.ToString();

                success = application.EnvironmentDatabase.ExecuteSqlStatement(deleteStatement);


                foreach (Activity.ActivityThreshold currentThreshold in Thresholds)
                {
                    currentThreshold.ActivityId = Id;

                    currentThreshold.Application = application;


                    sqlStatement = new StringBuilder();

                    sqlStatement.Append("EXEC dbo.CareLevelActivityThreshold_InsertUpdate ");

                    sqlStatement.Append(currentThreshold.Id.ToString() + ", ");

                    sqlStatement.Append("'" + currentThreshold.Name.Replace("'", "''") + "', ");

                    sqlStatement.Append("'" + currentThreshold.Description.Replace("'", "''") + "', ");

                    sqlStatement.Append(Id.ToString() + ", ");


                    sqlStatement.Append(currentThreshold.RelativeDateValue.ToString() + ", ");

                    sqlStatement.Append(((Int32)currentThreshold.RelativeDateQualifier).ToString() + ", ");

                    sqlStatement.Append(((Int32)currentThreshold.Status).ToString() + ", ");


                    if (Action == null)
                    {
                        sqlStatement.Append("NULL, NULL, NULL, ");
                    }

                    else
                    {
                        sqlStatement.Append(Action.Id.ToString() + ", ");

                        sqlStatement.Append("'" + Action.ActionParametersXmlSqlParsedString + "', ");

                        sqlStatement.Append("'" + Action.Description + "', ");
                    }


                    sqlStatement.Append(modifiedAccountInfo.AccountInfoSql);


                    success = application.EnvironmentDatabase.ExecuteSqlStatement(sqlStatement.ToString(), 0);

                    if (!success)
                    {
                        application.SetLastException(application.EnvironmentDatabase.LastException);

                        throw application.EnvironmentDatabase.LastException;
                    }

                    currentThreshold.SetIdentity();
                }


                success = true;

                if (useTransaction)
                {
                    application.EnvironmentDatabase.CommitTransaction();
                }
            }

            catch (Exception applicationException) {
                success = false;

                if (useTransaction)
                {
                    application.EnvironmentDatabase.RollbackTransaction();
                }

                application.SetLastException(applicationException);
            }

            return(success);
        }
        public override System.Xml.XmlDocument XmlSerialize()
        {
            System.Xml.XmlDocument document = base.XmlSerialize();

            System.Xml.XmlNode propertiesNode = document.ChildNodes[1].ChildNodes[0];


            #region Properties

            CommonFunctions.XmlDocumentAppendPropertyNode(document, propertiesNode, "ScheduleTypeInt32", ((Int32)ScheduleType).ToString());

            CommonFunctions.XmlDocumentAppendPropertyNode(document, propertiesNode, "ScheduleType", ScheduleType.ToString());

            CommonFunctions.XmlDocumentAppendPropertyNode(document, propertiesNode, "ScheduleValue", ScheduleValue.ToString());

            CommonFunctions.XmlDocumentAppendPropertyNode(document, propertiesNode, "ScheduleQualifierInt32", ((Int32)ScheduleQualifier).ToString());

            CommonFunctions.XmlDocumentAppendPropertyNode(document, propertiesNode, "ScheduleQualifier", ScheduleQualifier.ToString());

            CommonFunctions.XmlDocumentAppendPropertyNode(document, propertiesNode, "AnchorDateInt32", ((Int32)AnchorDate).ToString());

            CommonFunctions.XmlDocumentAppendPropertyNode(document, propertiesNode, "AnchorDate", AnchorDate.ToString());

            CommonFunctions.XmlDocumentAppendPropertyNode(document, propertiesNode, "Reoccurring", Reoccurring.ToString());

            CommonFunctions.XmlDocumentAppendPropertyNode(document, propertiesNode, "PerformActionDateTypeInt32", ((Int32)PerformActionDateType).ToString());

            CommonFunctions.XmlDocumentAppendPropertyNode(document, propertiesNode, "PerformActionDateType", PerformActionDateType.ToString());

            #endregion


            #region Object Nodes

            if (Action != null)
            {
                document.LastChild.AppendChild(document.ImportNode(Action.XmlSerialize().LastChild, true));
            }

            #endregion


            return(document);
        }