}        //

        /// <summary>
        /// Get a competency record
        /// </summary>
        /// <param name="profileId"></param>
        /// <returns></returns>
        public static ThisEntity Get(int profileId)
        {
            ThisEntity entity = new ThisEntity();

            if (profileId == 0)
            {
                return(entity);
            }
            try
            {
                using (var context = new EntityContext())
                {
                    DBEntity item = context.Reference_Frameworks
                                    .SingleOrDefault(s => s.Id == profileId);

                    if (item != null && item.Id > 0)
                    {
                        MapFromDB(item, entity);
                    }
                }
            }
            catch (Exception ex)
            {
                LoggingHelper.LogError(ex, thisClassName + ".Get");
            }
            return(entity);
        }        //
        /// <summary>
        /// Check if the provided framework has already been sync'd.
        /// If not, it will be added.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="userId"></param>
        /// <param name="messages"></param>
        /// <param name="frameworkId"></param>
        /// <returns></returns>
        //public bool HandleFrameworkRequest( CassFramework request,
        //		int userId,
        //		ref SaveStatus status,
        //		ref int frameworkId )
        //{
        //	bool isValid = true;
        //	if ( request == null || string.IsNullOrWhiteSpace(request._IdAndVersion) )
        //	{
        //		status.AddWarning( "The Cass Request doesn't contain a valid Cass Framework class." );
        //		return false;
        //	}
        //	ThisEntity item = Get( request._IdAndVersion );
        //	if (item != null && item.Id > 0)
        //	{
        //		//TODO - do we want to attempt an update - if changed
        //		//		- if we plan to implement a batch refresh of sync'd content, then not necessary
        //		frameworkId = item.Id;
        //		return true;
        //	}
        //	//add the framework...
        //	ThisEntity entity = new ThisEntity();
        //	entity.Name = request.Name;
        //	entity.Description = request.Description;
        //	entity.FrameworkUrl = request.Url;
        //	entity.RepositoryUri = request._IdAndVersion;

        //	//TDO - need owning org - BUT, first person to reference a framework is not necessarily the owner!!!!!
        //	//actually, we may not care here. Eventually get a ctid from CASS
        //	//entity.OwningOrganizationId = 0;

        //	isValid = Save( entity, userId, ref status );
        //	frameworkId = entity.Id;
        //	return isValid;
        //}


        //actually not likely to have a separate list of frameworks
        //public bool SaveList( List<ThisEntity> list, ref SaveStatus status )
        //{
        //	if ( list == null || list.Count == 0 )
        //		return true;

        //	bool isAllValid = true;
        //	foreach ( ThisEntity item in list )
        //	{
        //		Save( item, ref status );
        //	}

        //	return isAllValid;
        //}

        /// <summary>
        /// Add/Update a Reference_Framework
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="messages"></param>
        /// <returns></returns>
        public bool Save(ThisEntity entity,
                         ref SaveStatus status)
        {
            bool isValid = true;
            int  count   = 0;

            DBEntity efEntity = new DBEntity();

            using (var context = new EntityContext())
            {
                if (ValidateProfile(entity, ref status) == false)
                {
                    return(false);
                }

                if (entity.Id == 0)
                {
                    // - need to check for existance
                    DoesItemExist(entity);
                }

                if (entity.Id == 0)
                {
                    // - Add
                    efEntity = new DBEntity();
                    MapToDB(entity, efEntity);


                    efEntity.Created = DateTime.Now;
                    //efEntity.RowId = Guid.NewGuid();

                    context.Reference_Frameworks.Add(efEntity);

                    count = context.SaveChanges();

                    entity.Id = efEntity.Id;
                    //entity.RowId = efEntity.RowId;
                    if (count == 0)
                    {
                        status.AddWarning(string.Format(" Unable to add Profile: {0} <br\\> ", string.IsNullOrWhiteSpace(entity.Name) ? "no description" : entity.Name));
                    }
                }
                else
                {
                    efEntity = context.Reference_Frameworks.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))
                        {
                            count = context.SaveChanges();
                        }
                    }
                }
            }
            return(isValid);
        }
        }        //

        public static ThisEntity GetByUrl(string frameworkUrl)
        {
            ThisEntity entity = new ThisEntity();

            if (string.IsNullOrWhiteSpace(frameworkUrl))
            {
                return(entity);
            }
            try
            {
                using (var context = new EntityContext())
                {
                    DBEntity item = context.Reference_Frameworks
                                    .FirstOrDefault(s => s.TargetNode == frameworkUrl);

                    if (item != null && item.Id > 0)
                    {
                        MapFromDB(item, entity);
                    }
                }
            }
            catch (Exception ex)
            {
                LoggingHelper.LogError(ex, thisClassName + ".Get");
            }
            return(entity);
        }        //
        public void DoesItemExist(ThisEntity entity)
        {
            int frameworkId = 0;

            if (DoesItemExist(entity.CategoryId, entity.CodedNotation, entity.Name, ref frameworkId))
            {
                entity.Id = frameworkId;
            }
        }
        }         //

        public static void MapFromDB(DBEntity from, ThisEntity to)
        {
            to.Id = from.Id;
            //to.RowId = from.RowId;
            to.Name          = from.Name;
            to.CategoryId    = from.CategoryId;
            to.CodedNotation = from.CodedNotation;
            if (!string.IsNullOrWhiteSpace(from.CodedNotation) && from.CodedNotation.Length > 1)
            {
                to.CodeGroup = from.CodedNotation.Substring(0, 2);
            }
            to.Description = from.Description;
            to.TargetNode  = from.TargetNode;
            //to.ExternalFrameworkId = (int) (from.ExternalFrameworkId ?? 0);
        }
Example #6
0
        public static void MapToDB(ThisEntity from, DBEntity to)
        {
            //want to ensure fields from create are not wiped
            //to.Id = from.Id;

            to.Name          = from.Name;
            to.CategoryId    = from.CategoryId;
            to.CodedNotation = (from.CodedNotation ?? "");
            if (!string.IsNullOrWhiteSpace(from.CodedNotation) && from.CodedNotation.Length > 1)
            {
                to.CodeGroup = from.CodedNotation.Substring(0, 2);
            }
            to.Description         = from.Description;
            to.TargetNode          = from.TargetNode ?? "";
            to.ExternalFrameworkId = from.ExternalFrameworkId;
        }         //
        public bool ValidateProfile(ThisEntity profile, ref SaveStatus status)
        {
            status.HasSectionErrors = false;

            if (string.IsNullOrWhiteSpace(profile.Name))
            {
                status.AddError("A framework name must be entered");
            }


            if (profile.CategoryId == 0)
            {
                status.AddError("A categoryId is required for a reference framework ");
            }
            //if we don't require url, we can't resolve potentially duplicate framework names


            return(status.WasSectionValid);
        }