public static bool IsParentBeingAddedAsChildToItself(int profileId, int childId, int childEntityTypeId)
        {
            bool isOk = false;

            using (var context = new EntityContext())
            {
                //get the profile that is the parent of the child
                DBEntity efEntity = context.Entity_ProcessProfile
                                    .SingleOrDefault(s => s.Id == profileId);

                if (efEntity != null &&
                    efEntity.Id > 0 &&
                    efEntity.Entity != null)
                {
                    //check if the parent entity is the same one being added as a child
                    if (efEntity.Entity.EntityTypeId == childEntityTypeId &&
                        efEntity.Entity.EntityBaseId == childId)
                    {
                        return(true);
                    }
                }
            }

            return(isOk);
        }
        }        //

        public static void MapToDB(ThisEntity from, DBEntity to)
        {
            //want to ensure fields from create are not wiped
            if (to.Id == 0)
            {
                to.ProcessTypeId = from.ProcessTypeId;
            }
            to.Id = from.Id;

            //to.ProfileName = from.ProcessProfileType;
            to.Description = from.Description;
            if (IsValidDate(from.DateEffective))
            {
                to.DateEffective = DateTime.Parse(from.DateEffective);
            }
            else             //handle reset
            {
                to.DateEffective = null;
            }

            if (from.ProcessingAgentUid == null || from.ProcessingAgentUid.ToString() == DEFAULT_GUID)
            {
                to.ProcessingAgentUid = null;                //
            }
            else
            {
                to.ProcessingAgentUid = from.ProcessingAgentUid;
            }

            to.SubjectWebpage = GetUrlData(from.SubjectWebpage);

            to.ProcessFrequency = from.ProcessFrequency;
            //to.TargetCompetencyFramework = from.TargetCompetencyFramework;

            to.ProcessMethod            = from.ProcessMethod;
            to.ProcessMethodDescription = from.ProcessMethodDescription;

            to.ProcessStandards            = from.ProcessStandards;
            to.ProcessStandardsDescription = from.ProcessStandardsDescription;

            to.ScoringMethodDescription        = from.ScoringMethodDescription;
            to.ScoringMethodExample            = from.ScoringMethodExample;
            to.ScoringMethodExampleDescription = from.ScoringMethodExampleDescription;
            to.VerificationMethodDescription   = from.VerificationMethodDescription;
        }
        public bool Delete(int recordId, ref string statusMessage)
        {
            bool isOK = true;

            using (var context = new EntityContext())
            {
                DBEntity p = context.Entity_ProcessProfile.FirstOrDefault(s => s.Id == recordId);
                if (p != null && p.Id > 0)
                {
                    context.Entity_ProcessProfile.Remove(p);
                    int count = context.SaveChanges();
                }
                else
                {
                    statusMessage = string.Format("Process Profile record was not found: {0}", recordId);
                    isOK          = false;
                }
            }
            return(isOK);
        }
        }        //

        public static ThisEntity Get(int profileId)
        {
            ThisEntity entity = new ThisEntity();

            try
            {
                using (var context = new EntityContext())
                {
                    DBEntity item = context.Entity_ProcessProfile
                                    .SingleOrDefault(s => s.Id == profileId);

                    if (item != null && item.Id > 0)
                    {
                        MapFromDB(item, entity, true, false);
                    }
                }
            }
            catch (Exception ex)
            {
                LoggingHelper.LogError(ex, thisClassName + ".Get");
            }
            return(entity);
        }        //
        /// <summary>
        /// Persist ProcessProfile
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="parentUid"></param>
        /// <param name="userId"></param>
        /// <param name="messages"></param>
        /// <returns></returns>
        public bool Save(ThisEntity entity, Guid parentUid, ref SaveStatus status)
        {
            status.HasSectionErrors = false;

            if (!IsValidGuid(parentUid))
            {
                status.AddError("Error: the parent identifier was not provided.");
                return(false);
            }


            int count = 0;

            DBEntity efEntity = new DBEntity();

            Entity parent = EntityManager.GetEntity(parentUid);

            if (parent == null || parent.Id == 0)
            {
                status.AddError("Error - the parent entity was not found.");
                return(false);
            }


            //determine type
            int profileTypeId = 0;

            if (entity.ProcessTypeId > 0)
            {
                profileTypeId = entity.ProcessTypeId;
            }
            else
            {
                //
                switch (entity.ProcessProfileType)
                {
                case "AppealProcess":
                    entity.ProcessTypeId = APPEAL_PROCESS_TYPE;
                    break;

                case "ComplaintProcess":
                    entity.ProcessTypeId = COMPLAINT_PROCESS_TYPE;
                    break;
                //case "CriteriaProcess":
                //	entity.ProcessTypeId = CRITERIA_PROCESS_TYPE;
                //	break;

                case "ReviewProcess":
                    entity.ProcessTypeId = REVIEW_PROCESS_TYPE;
                    break;

                case "RevocationProcess":
                    entity.ProcessTypeId = REVOKE_PROCESS_TYPE;
                    break;

                case "ProcessProfile":
                    entity.ProcessTypeId = DEFAULT_PROCESS_TYPE;
                    break;

                case "CredentialProcess":
                    entity.ProcessTypeId = DEFAULT_PROCESS_TYPE;
                    break;

                case "MaintenanceProcess":
                    entity.ProcessTypeId = MTCE_PROCESS_TYPE;
                    break;

                case "AdministrationProcess":
                    entity.ProcessTypeId = ADMIN_PROCESS_TYPE;
                    break;

                case "DevelopmentProcess":
                    entity.ProcessTypeId = DEV_PROCESS_TYPE;
                    break;

                //
                default:
                    entity.ProcessTypeId = 1;
                    status.AddError(string.Format("Error: Unexpected profile type of {0} was encountered.", entity.ProcessProfileType));
                    return(false);
                }
            }
            using (var context = new EntityContext())
            {
                if (ValidateProfile(entity, ref status) == false)
                {
                    status.AddError("Process Profile was invalid. " + SetEntitySummary(entity));
                    return(false);
                }


                if (entity.Id == 0)
                {
                    //add
                    efEntity = new DBEntity();
                    MapToDB(entity, efEntity);
                    efEntity.EntityId = parent.Id;

                    efEntity.Created = efEntity.LastUpdated = DateTime.Now;

                    if (IsValidGuid(entity.RowId))
                    {
                        efEntity.RowId = entity.RowId;
                    }
                    else
                    {
                        efEntity.RowId = Guid.NewGuid();
                    }

                    context.Entity_ProcessProfile.Add(efEntity);
                    count = context.SaveChanges();
                    //update profile record so doesn't get deleted
                    entity.Id       = efEntity.Id;
                    entity.ParentId = parent.Id;
                    entity.RowId    = efEntity.RowId;
                    if (count == 0)
                    {
                        status.AddError(string.Format(" Unable to add Profile: {0} <br\\> ", string.IsNullOrWhiteSpace(entity.ProfileName) ? "no description" : entity.ProfileName));
                    }
                    else
                    {
                        //other entity components use a trigger to create the entity Object. If a trigger is not created, then child adds will fail (as typically use entity_summary to get the parent. As the latter is easy, make the direct call?

                        UpdateParts(entity, ref status);
                    }
                }
                else
                {
                    entity.ParentId = parent.Id;

                    efEntity = context.Entity_ProcessProfile.SingleOrDefault(s => s.Id == entity.Id);
                    if (efEntity != null && efEntity.Id > 0)
                    {
                        entity.RowId = efEntity.RowId;
                        //update
                        MapToDB(entity, efEntity);
                        //has changed?
                        if (HasStateChanged(context))
                        {
                            efEntity.LastUpdated = System.DateTime.Now;

                            count = context.SaveChanges();
                        }
                        //always check parts
                        UpdateParts(entity, ref status);
                    }
                }
            }

            return(status.WasSectionValid);
        }
        public static void MapFromDB(DBEntity from, ThisEntity to, bool includingItems, bool getForList)
        {
            to.Id    = from.Id;
            to.RowId = from.RowId;
            //HANDLE PROCESS TYPES
            if (from.ProcessTypeId != null && ( int )from.ProcessTypeId > 0)
            {
                to.ProcessTypeId = ( int )from.ProcessTypeId;
            }
            else
            {
                to.ProcessTypeId = 1;
            }

            to.ProfileName = to.ProcessType;
            //need to distinguish if for detail
            to.ProcessProfileType = GetProfileType(to.ProcessTypeId);

            to.Description = from.Description;
            if ((to.Description ?? "").Length > 5)
            {
                //this should just be the type now
                to.ProfileName = GetProfileType(to.ProcessTypeId);

                //to.ProfileName = to.Description.Length > 100 ? to.Description.Substring(0,100) + " . . ." : to.Description;
            }

            if (from.Entity != null)
            {
                to.ParentId = from.Entity.Id;
            }

            to.ProfileSummary = SetEntitySummary(to);

            //- provide minimum option, for lists
            if (getForList)
            {
                return;
            }

            if (IsGuidValid(from.ProcessingAgentUid))
            {
                to.ProcessingAgentUid = ( Guid )from.ProcessingAgentUid;

                to.ProcessingAgent = OrganizationManager.GetBasics(to.ProcessingAgentUid);
            }

            if (IsValidDate(from.DateEffective))
            {
                to.DateEffective = (( DateTime )from.DateEffective).ToString("yyyy-MM-dd");
            }
            else
            {
                to.DateEffective = "";
            }
            to.SubjectWebpage = from.SubjectWebpage;

            to.ProcessFrequency = from.ProcessFrequency;
            //to.TargetCompetencyFramework = from.TargetCompetencyFramework;
            //to.RequiresCompetenciesFrameworks = Entity_CompetencyFrameworkManager.GetAll( to.RowId, "requires" );

            to.ProcessMethod            = from.ProcessMethod;
            to.ProcessMethodDescription = from.ProcessMethodDescription;

            to.ProcessStandards            = from.ProcessStandards;
            to.ProcessStandardsDescription = from.ProcessStandardsDescription;

            to.ScoringMethodDescription        = from.ScoringMethodDescription;
            to.ScoringMethodExample            = from.ScoringMethodExample;
            to.ScoringMethodExampleDescription = from.ScoringMethodExampleDescription;
            to.VerificationMethodDescription   = from.VerificationMethodDescription;

            if (IsValidDate(from.DateEffective))
            {
                to.DateEffective = (( DateTime )from.DateEffective).ToString("yyyy-MM-dd");
            }
            else
            {
                to.DateEffective = "";
            }

            //enumerations
            to.DataCollectionMethodType = EntityPropertyManager.FillEnumeration(to.RowId, CodesManager.PROPERTY_CATEGORY_DATA_COLLECTION_METHOD_TYPE);
            to.ExternalInputType        = EntityPropertyManager.FillEnumeration(to.RowId, CodesManager.PROPERTY_CATEGORY_EXTERNAL_INPUT_TYPE);



            if (includingItems)
            {
                to.Jurisdiction = Entity_JurisdictionProfileManager.Jurisdiction_GetAll(to.RowId);
                //will only be one, but could model with multiple
                to.TargetCredential = Entity_CredentialManager.GetAll(to.RowId, BaseFactory.RELATIONSHIP_TYPE_HAS_PART);
                to.TargetAssessment = Entity_AssessmentManager.GetAll(to.RowId, BaseFactory.RELATIONSHIP_TYPE_HAS_PART);

                to.TargetLearningOpportunity = Entity_LearningOpportunityManager.LearningOpps_GetAll(to.RowId, true);
                to.TargetCompetencyFramework = Entity_CompetencyFrameworkManager.GetAll(to.RowId);
            }


            if (IsValidDate(from.Created))
            {
                to.Created = ( DateTime )from.Created;
            }

            if (IsValidDate(from.LastUpdated))
            {
                to.LastUpdated = ( DateTime )from.LastUpdated;
            }
        }