private static void MapFromDB(Entity_Reference_Summary from, ThisEntity to)
        {
            //core properties
            to.Id       = from.EntityReferenceId;
            to.EntityId = from.EntityId;

            to.TextTitle  = from.Title ?? "";
            to.CategoryId = from.CategoryId;
            if (to.CategoryId == CodesManager.PROPERTY_CATEGORY_PHONE_TYPE)
            {
                to.TextValue = PhoneNumber.DisplayPhone(from.TextValue);
            }
            else
            {
                to.TextValue = from.TextValue;
            }

            to.CodeId     = ( int )(from.PropertyValueId ?? 0);
            to.CodeTitle  = from.PropertyValue;
            to.CodeSchema = from.PropertySchema ?? "";

            to.ProfileSummary = to.TextTitle + " - " + to.TextValue;

            //from entity
            to.EntityBaseId = from.EntityBaseId;
        }
        private static void MapFromDB(DBEntity from, ThisEntity to)
        {
            to.Id         = from.Id;
            to.ParentId   = from.EntityId;
            to.TextTitle  = from.Title ?? "";
            to.CategoryId = from.CategoryId;
            if (to.CategoryId == CodesManager.PROPERTY_CATEGORY_PHONE_TYPE)
            {
                to.TextValue = PhoneNumber.DisplayPhone(from.TextValue);
            }
            else
            {
                to.TextValue = from.TextValue;
            }
            //else if ( to.CategoryId == CodesManager.PROPERTY_CATEGORY_ORGANIZATION_IDENTIFIERS )
            //{
            //    to.TextValue = from.TextValue;
            //}
            //else
            //{
            //    to.TextValue = from.TextValue;
            //}

            to.CodeId = from.PropertyValueId ?? 0;
            if (string.IsNullOrWhiteSpace(to.TextTitle))
            {
                to.ProfileSummary = to.TextValue;
            }
            else
            {
                to.ProfileSummary = to.TextTitle + " - " + to.TextValue;
            }
            to.CodeTitle  = "";
            to.CodeSchema = "";
            if (from.Codes_PropertyValue != null && from.Codes_PropertyValue.Id > 0)
            {
                to.CodeTitle  = from.Codes_PropertyValue.Title;
                to.CodeSchema = from.Codes_PropertyValue.SchemaName ?? "";
            }
            if (from.Entity != null && from.Entity.Id > 0)
            {
                to.EntityBaseId = from.Entity.EntityBaseId ?? 0;
                to.EntityTypeId = from.Entity.EntityTypeId;
            }
            if (IsValidDate(from.Created))
            {
                to.Created = ( DateTime )from.Created;
            }

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

        /// <summary>
        /// Get All Entity_Reference records for the parent
        /// </summary>
        /// <param name="parentUid"></param>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public static List <ThisEntity> GetAll(Guid parentUid, int categoryId)
        {
            ThisEntity        entity  = new ThisEntity();
            List <ThisEntity> list    = new List <ThisEntity>();
            List <ThisEntity> results = new List <ThisEntity>();
            Entity            parent  = EntityManager.GetEntity(parentUid);

            if (parent == null || parent.Id == 0)
            {
                return(list);
            }
            try
            {
                using (var context = new EntityContext())
                {
                    List <DBEntity> search = context.Entity_Reference
                                             .Where(s => s.EntityId == parent.Id &&
                                                    s.CategoryId == categoryId)
                                             .OrderBy(s => s.Title).ThenBy(x => x.TextValue)
                                             .ToList();
                    //this appears wrong, the ToList means query not deferred???
                    if (categoryId == CodesManager.PROPERTY_CATEGORY_SUBJECT ||
                        categoryId == CodesManager.PROPERTY_CATEGORY_KEYWORD)
                    {
                        search = search.OrderBy(s => s.TextValue).ToList();
                    }
                    else
                    {
                        search = search.OrderBy(s => s.Created).ToList();
                    }

                    if (search != null && search.Count > 0)
                    {
                        foreach (DBEntity item in search)
                        {
                            entity = new ThisEntity();
                            MapFromDB(item, entity);

                            list.Add(entity);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LoggingHelper.LogError(ex, thisClassName + ".GetAllDirect");
            }
            return(list);
        }//
        }//

        public static List <ThisEntity> GetAllSubjects(Guid parentUid)
        {
            ThisEntity        entity = new ThisEntity();
            List <ThisEntity> list   = new List <ThisEntity>();

            Entity parent = EntityManager.GetEntity(parentUid);

            if (parent == null || parent.Id == 0)
            {
                return(list);
            }
            try
            {
                string prevSubject = "";
                using (var context = new ViewContext())
                {
                    List <Entity_Subjects> results = context.Entity_Subjects
                                                     .Where(s => s.EntityUid == parentUid)
                                                     .OrderBy(s => s.Subject)
                                                     .ToList();

                    if (results != null && results.Count > 0)
                    {
                        foreach (Entity_Subjects item in results)
                        {
                            entity = new ThisEntity();
                            if (item.Subject != prevSubject)
                            {
                                entity.EntityId  = item.EntityId;
                                entity.TextValue = item.Subject;
                                list.Add(entity);
                            }
                            prevSubject = item.Subject;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LoggingHelper.LogError(ex, thisClassName + ".Entity_GetAllSubjects");
            }
            return(list);
        }//
        } //

        /// <summary>
        /// Get single entity reference, summary
        /// </summary>
        /// <param name="entityReferenceId"></param>
        /// <returns></returns>
        //public static ThisEntity Get( int entityReferenceId )
        //{
        //    ThisEntity entity = new ThisEntity();
        //    if ( entityReferenceId == 0 )
        //    {
        //        return entity;
        //    }
        //    try
        //    {
        //        using ( var context = new ViewContext() )
        //        {
        //            Entity_Reference_Summary item = context.Entity_Reference_Summary
        //                    .SingleOrDefault( s => s.EntityReferenceId == entityReferenceId );

        //            if ( item != null && item.EntityReferenceId > 0 )
        //            {
        //                MapFromDB( item, entity );
        //            }
        //        }
        //    }
        //    catch ( Exception ex )
        //    {
        //        LoggingHelper.LogError( ex, thisClassName + ".Entity_Get" );
        //    }
        //    return entity;
        //}//
        private static void MapToDB(ThisEntity from, DBEntity to)
        {
            //want to ensure fields from create are not wiped
            if (to.Id == 0)
            {
                //if ( IsValidDate( from.Created ) )
                //	to.Created = from.Created;
                //to.CreatedById = from.CreatedById;
            }
            //to.Id = from.Id;
            to.CategoryId = from.CategoryId;

            //in some cases may not require text, so fill with empty string
            to.Title = !string.IsNullOrWhiteSpace(from.TextTitle) ? from.TextTitle : "";
            if (to.CategoryId == CodesManager.PROPERTY_CATEGORY_PHONE_TYPE)
            {
                to.TextValue = PhoneNumber.StripPhone(GetData(from.TextValue));
            }
            else if (to.CategoryId == CodesManager.PROPERTY_CATEGORY_ORGANIZATION_IDENTIFIERS)
            {
                //if other, notify admin, and need to handle separately
                if (from.CodeId == 88)
                {
                    if (from.Id == 0)
                    {
                        //will want to create a property value, and send email
                        //could append to text for now
                        //op.OtherValue += "{" + ( frameworkName ?? "missing framework name" ) + "; " + schemaUrl + "}";
                        LoggingHelper.DoTrace(2, "A new organization identifier of 'other' has been added:" + from.TextValue);
                        SendNewOtherIdentityNotice(from);
                    }
                }
                else
                {
                    //should ignore to.Title
                    to.Title = "";
                }
                to.TextValue = from.TextValue;
            }
            else
            {
                to.TextValue = from.TextValue;
            }

            if (from.CodeId > 0)
            {
                to.PropertyValueId = from.CodeId;
            }
            else if (!string.IsNullOrWhiteSpace(from.CodeSchema))
            {
                CodeItem item = CodesManager.GetPropertyBySchema(to.CategoryId, from.CodeSchema);
                if (item != null && item.Id > 0)
                {
                    to.PropertyValueId = item.Id;
                    if (string.IsNullOrWhiteSpace(to.Title))
                    {
                        to.Title = item.Title;
                    }
                }
            }
            else
            {
                to.PropertyValueId = null;
            }
        }
        private bool Validate(ThisEntity profile, bool isTitleRequired,
                              ref bool isEmpty,
                              ref SaveStatus status)
        {
            status.HasSectionErrors = false;
            int maxKeywordLength       = UtilityManager.GetAppKeyValue("maxKeywordLength", 200);
            int maxReferenceTextLength = UtilityManager.GetAppKeyValue("maxReferenceTextLength", 600);
            int maxReferenceUrlLength  = UtilityManager.GetAppKeyValue("maxReferenceUrlLength", 600);


            isEmpty = false;
            //check if empty
            if (string.IsNullOrWhiteSpace(profile.TextTitle) &&
                string.IsNullOrWhiteSpace(profile.TextValue)
                )
            {
                isEmpty = true;
                return(true);
            }
            profile.TextTitle = (profile.TextTitle ?? "");
            profile.TextValue = (profile.TextValue ?? "");
            //16-07-22 mparsons - changed to, for now, let user enter one or the other (except for urls), this gives flexibility to the interface choosing which to show or require
            //ultimately, we will make the profile configurable
            if (profile.CategoryId == CodesManager.PROPERTY_CATEGORY_REFERENCE_URLS)
            {
                if (isTitleRequired && string.IsNullOrWhiteSpace(profile.TextTitle))
                {
                    status.AddWarning(string.Format("A title must be entered with this categoryId: {0}", profile.CategoryId));
                }
                //text is normally required, unless a competency item
                if (string.IsNullOrWhiteSpace(profile.TextValue))
                {
                    status.AddWarning(string.Format("A URL must be entered with this categoryId: {0}", profile.CategoryId));
                }
            }

            if (profile.CategoryId == CodesManager.PROPERTY_CATEGORY_REFERENCE_URLS ||
                profile.CategoryId == CodesManager.PROPERTY_CATEGORY_ORGANIZATION_SOCIAL_MEDIA ||
                profile.CategoryId == CodesManager.PROPERTY_CATEGORY_CREDENTIAL_URLS
                )
            {
                if ((profile.TextValue ?? "").Length > maxReferenceUrlLength)
                {
                    status.AddWarning(string.Format("The Url is too long. It must be less than {0} characters", maxReferenceUrlLength));
                }
                else if (!IsUrlValid(profile.TextValue, ref commonStatusMessage))
                {
                    //status.AddWarning( string.Format( "The Url is invalid: {0}. {1}", profile.TextValue, commonStatusMessage ) );
                }

                profile.TextValue = (profile.TextValue ?? "").TrimEnd('/');
            }
            else
            if (profile.CategoryId == CodesManager.PROPERTY_CATEGORY_KEYWORD)
            {
                if (!string.IsNullOrWhiteSpace(profile.TextValue) && profile.TextValue.Length > maxKeywordLength)
                {
                    status.AddWarning(string.Format("Error - the keyword must be less than {0} characters.", maxKeywordLength));
                }
            }
            else
            if (profile.CategoryId == CodesManager.PROPERTY_CATEGORY_SUBJECT)
            {
                if (!string.IsNullOrWhiteSpace(profile.TextValue) && profile.TextValue.Length > maxKeywordLength)
                {
                    status.AddWarning(string.Format("Error - the subject must be less than {0} characters.", maxKeywordLength));
                    profile.TextValue = profile.TextValue.Substring(0, maxKeywordLength);
                }
            } //
            else
            if (profile.CategoryId == CodesManager.PROPERTY_CATEGORY_SOC)
            {
                if (!string.IsNullOrWhiteSpace(profile.TextValue) && profile.TextValue.Length > maxKeywordLength)
                {
                    status.AddWarning(string.Format("Error - An other occupation must be less than {0} characters.", maxKeywordLength));
                }
            }
            else
            if (profile.CategoryId == CodesManager.PROPERTY_CATEGORY_NAICS)
            {
                if (!string.IsNullOrWhiteSpace(profile.TextValue) && profile.TextValue.Length > maxKeywordLength)
                {
                    status.AddWarning(string.Format("Error - An other industry must be less than {0} characters.", maxKeywordLength));
                }
            }
            else if (profile.CategoryId == CodesManager.PROPERTY_CATEGORY_PHONE_TYPE)
            {
                string phoneNbr = PhoneNumber.StripPhone(GetData(profile.TextValue));

                if (string.IsNullOrWhiteSpace(phoneNbr))
                {
                    status.AddWarning("Error - a phone number must be entered.");
                }
                else if (!string.IsNullOrWhiteSpace(phoneNbr) && phoneNbr.Length < 10)
                {
                    status.AddWarning(string.Format("Error - A phone number ({0}) must have at least 10 numbers.", profile.TextValue));
                }
                //need an other check
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(profile.TextTitle) && profile.TextTitle.Length > maxReferenceTextLength)
                {
                    status.AddWarning(string.Format("Warning - the TextTitle must be less than {0} characters, categoryId: {1}.", maxReferenceTextLength, profile.CategoryId));
                }

                if (!string.IsNullOrWhiteSpace(profile.TextValue) && profile.TextValue.Length > maxReferenceTextLength)
                {
                    status.AddWarning(string.Format("Warning - the text value must be less than {0} characters, categoryId: {1}.", maxReferenceTextLength, profile.CategoryId));
                }
            }
            if (profile.CategoryId != CodesManager.PROPERTY_CATEGORY_CONDITION_ITEM &&
                string.IsNullOrWhiteSpace(profile.TextTitle))
            {
                //status.AddWarning( "A title must be entered" );
                //isValid = false;
            }

            //text is normally required, unless a competency item
            //if ( profile.CategoryId != CodesManager.PROPERTY_CATEGORY_COMPETENCY
            //	&& string.IsNullOrWhiteSpace( profile.TextValue ) )
            //{
            //	status.AddWarning( "A text value must be entered" );
            //	isValid = false;
            //}
            return(status.WasSectionValid);
        }
        private static void SendNewOtherIdentityNotice(ThisEntity entity)
        {
            string message = string.Format("New identity. <ul><li>OrganizationId: {0}</li><li>PersonId: {1}</li><li>Title: {2}</li><li>Value: {3}</li></ul>", entity.EntityBaseId, entity.LastUpdatedById, entity.TextTitle, entity.TextValue);

            workIT.Utilities.EmailManager.NotifyAdmin("New Organization Identity has been created", message);
        }