public bool Delete(int Id, ref string statusMessage)
        {
            bool isValid = false;

            if (Id == 0)
            {
                statusMessage = "Error - missing an identifier for the PathwayComponent";
                return(false);
            }
            using (var context = new EntityContext())
            {
                DBEntity efEntity = context.PathwayComponent
                                    .SingleOrDefault(s => s.Id == Id);

                if (efEntity != null && efEntity.Id > 0)
                {
                    context.PathwayComponent.Remove(efEntity);
                    int count = context.SaveChanges();
                    if (count > 0)
                    {
                        isValid = true;
                    }
                }
                else
                {
                    statusMessage = "Error - delete failed, as record was not found.";
                }
            }

            return(isValid);
        }
        public int AddPendingRecord(Guid entityUid, string ctid, string registryAtId, ref string status)
        {
            DBEntity efEntity = new DBEntity();

            try
            {
                //var pathwayCTIDTemp = "ce-abcb5fe0-8fde-4f06-9d70-860cd5bdc763";
                using (var context = new EntityContext())
                {
                    if (!IsValidGuid(entityUid))
                    {
                        status = thisClassName + " - A valid GUID must be provided to create a pending entity";
                        return(0);
                    }
                    //quick check to ensure not existing
                    ThisEntity entity = GetByCtid(ctid);
                    if (entity != null && entity.Id > 0)
                    {
                        return(entity.Id);
                    }

                    //only add DB required properties
                    //NOTE - an entity will be created via trigger
                    efEntity.Name        = "Placeholder until full document is downloaded";
                    efEntity.Description = "Placeholder until full document is downloaded";
                    efEntity.PathwayCTID = "";
                    //temp
                    efEntity.ComponentTypeId = 1;
                    //realitically the component should be added in the same workflow
                    efEntity.EntityStateId = 1;
                    efEntity.RowId         = entityUid;
                    //watch that Ctid can be  updated if not provided now!!
                    efEntity.CTID           = ctid;
                    efEntity.SubjectWebpage = registryAtId;

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

                    context.PathwayComponent.Add(efEntity);
                    int count = context.SaveChanges();
                    if (count > 0)
                    {
                        return(efEntity.Id);
                    }

                    status = thisClassName + " Error - the save was not successful, but no message provided. ";
                }
            }

            catch (Exception ex)
            {
                string message = FormatExceptions(ex);
                LoggingHelper.LogError(ex, thisClassName + string.Format(".AddPendingRecord. entityUid:  {0}, ctid: {1}", entityUid, ctid));
                status = thisClassName + " Error - the save was not successful. " + message;
            }
            return(0);
        }
        public static void MapToDB(ThisEntity input, DBEntity output)
        {
            output.Id = input.Id;
            if (output.Id < 1)
            {
                //will need output be carefull here, will this exist in the input??
                //there could be a case where an external Id was added output bulk upload for an existing record
                output.PathwayCTID = input.PathwayCTID;
            }
            else
            {
                //don't map rowId, CTID, or dates as not on form
                //output.RowId = input.RowId;
                output.Name        = input.Name;
                output.Description = input.Description;
                //output.CodedNotation = input.CodedNotation;
                output.EntityStateId     = 3;
                output.ComponentCategory = input.ComponentCategory;
                output.ComponentTypeId   = GetComponentTypeId(input.PathwayComponentType);
                if (string.IsNullOrWhiteSpace(output.PathwayCTID))
                {
                    output.PathwayCTID = input.PathwayCTID;
                }
                //output.ComponentTypeId = input.ComponentTypeId;
                //will be validated before getting here!
                output.CredentialType = input.CredentialType;

                //output.ExternalIdentifier = input.ExternalIdentifier;
                //not sure if this will just be a URI, or point output a concept
                //if a concept, would probably need entity.hasConcept
                //output.HasProgressionLevel = input.HasProgressionLevels;
                if (input.HasProgressionLevels.Any())
                {
                    output.HasProgressionLevel = string.Join("|", input.HasProgressionLevels.ToArray());
                }
                else
                {
                    output.HasProgressionLevel = null;
                }

                //need output change ??
                //output.IsDestinationComponentOf = input.IsDestinationComponentOf;
                //this is now in JsonProperties
                //output.PointValue = input.PointValueOld;
                output.ProgramTerm    = input.ProgramTerm;
                output.SubjectWebpage = input.SubjectWebpage;
                output.SourceData     = input.SourceData;

                output.Properties = JsonConvert.SerializeObject(input.JsonProperties);
            }
        }
        /// <summary>
        /// Get component by Guid
        /// </summary>
        /// <param name="id"></param>
        /// <param name="childComponentsAction">1-default of summary</param>
        /// <returns></returns>
        public static ThisEntity Get(Guid id, int childComponentsAction = 1)
        {
            ThisEntity entity = new ThisEntity();

            using (var context = new EntityContext())
            {
                DBEntity item = context.PathwayComponent
                                .SingleOrDefault(s => s.RowId == id);

                if (item != null && item.Id > 0)
                {
                    MapFromDB(item, entity, childComponentsAction);
                }
            }

            return(entity);
        }
        /// <summary>
        /// Get a basic PathwayComponent by CTID
        /// </summary>
        /// <param name="ctid"></param>
        /// <returns></returns>
        public static ThisEntity GetByCtid(string ctid, int childComponentsAction = 1)
        {
            PathwayComponent entity = new PathwayComponent();

            if (string.IsNullOrWhiteSpace(ctid))
            {
                return(entity);
            }

            using (var context = new EntityContext())
            {
                context.Configuration.LazyLoadingEnabled = false;
                EM.PathwayComponent item = context.PathwayComponent
                                           .FirstOrDefault(s => s.CTID.ToLower() == ctid.ToLower()
                                                           );

                if (item != null && item.Id > 0)
                {
                    MapFromDB(item, entity, childComponentsAction);
                }
            }

            return(entity);
        }
        //


        /// <summary>
        ///
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="childComponentsAction">0-none; 1-summary; 2-deep </param>
        public static void MapFromDB(DBEntity input, ThisEntity output, int childComponentsAction = 1)
        {
            output.Id            = input.Id;
            output.RowId         = input.RowId;
            output.EntityStateId = input.EntityStateId < 1 ? 1 : input.EntityStateId;
            output.CTID          = input.CTID;
            output.Name          = input.Name;
            output.Description   = input.Description;
            output.PathwayCTID   = input.PathwayCTID;
            //if ( input.Entity_HasPathwayComponent != null && input.Entity_HasPathwayComponent.Count > 0 )
            //{
            //	//
            //}

            var relatedEntity = EntityManager.GetEntity(output.RowId, false);

            if (relatedEntity != null && relatedEntity.Id > 0)
            {
                output.EntityLastUpdated = relatedEntity.LastUpdated;
            }

            //need output get parent pathway?
            //output.IsDestinationComponentOf = Entity_PathwayComponentManager.GetPathwayForComponent( output.Id, PathwayComponent.PathwayComponentRelationship_HasDestinationComponent );

            //ispartof. Should be single, but using list for flexibility?
            //actually force one, as we are using pathway identifier an external id for a unique lookup
            //may not want output do this every time?
            //output.IsPartOf = Entity_PathwayComponentManager.GetPathwayForComponent( output.Id, PathwayComponent.PathwayComponentRelationship_HasPart );

            //may want output get all and split out
            if (childComponentsAction == 2)
            {
                output.AllComponents = Entity_PathwayComponentManager.GetAll(output.RowId, componentActionOfSummary);
                foreach (var item in output.AllComponents)
                {
                    if (item.ComponentRelationshipTypeId == PC.PathwayComponentRelationship_HasChild)
                    {
                        output.HasChild.Add(item);
                    }
                    else if (item.ComponentRelationshipTypeId == PC.PathwayComponentRelationship_IsChildOf)
                    {
                        output.IsChildOf.Add(item);
                    }
                    else if (item.ComponentRelationshipTypeId == PC.PathwayComponentRelationship_Preceeds)
                    {
                        output.Preceeds.Add(item);
                    }
                    else if (item.ComponentRelationshipTypeId == PC.PathwayComponentRelationship_Prerequiste)
                    {
                        output.Prerequisite.Add(item);
                    }
                }
                //child components - details of condition, but summary of components
                output.HasCondition = PathwayComponentConditionManager.GetAll(output.Id, true);
            }

            //output.CodedNotation = input.CodedNotation;
            output.ComponentCategory = input.ComponentCategory;
            output.ComponentTypeId   = input.ComponentTypeId;
            if (input.Codes_PathwayComponentType != null && input.Codes_PathwayComponentType.Id > 0)
            {
                output.PathwayComponentType = input.Codes_PathwayComponentType.Title;
            }
            else
            {
                output.PathwayComponentType = GetComponentType(output.ComponentTypeId);
            }
            //will be validated before getting here!
            output.CredentialType = input.CredentialType;
            if (!string.IsNullOrWhiteSpace(output.CredentialType) && output.CredentialType.IndexOf("ctdl/terms") > 0)
            {
                int pos = output.CredentialType.IndexOf("ctdl/terms");
                output.CredentialType = output.CredentialType.Substring(pos + 11);
            }

            //not sure if this will just be a URI, or point output a concept
            //if a concept, would probably need entity.hasConcept
            //output.HasProgressionLevel = input.HasProgressionLevel;
            //if ( !string.IsNullOrWhiteSpace( input.HasProgressionLevel ) )
            //{
            //	output.ProgressionLevel = ConceptSchemeManager.GetByConceptCtid( output.HasProgressionLevel );
            //	output.HasProgressionLevelDisplay = output.ProgressionLevel.PrefLabel;
            //}
            //20-10-28 now storing separated list
            if (!string.IsNullOrWhiteSpace(input.HasProgressionLevel))
            {
                string[] array = input.HasProgressionLevel.Split('|');
                if (array.Count() > 0)
                {
                    foreach (var i in array)
                    {
                        if (!string.IsNullOrWhiteSpace(i))
                        {
                            var pl = ConceptSchemeManager.GetByConceptCtid(i);
                            output.ProgressionLevels.Add(pl);

                            output.HasProgressionLevelDisplay += pl.PrefLabel + ", ";
                        }
                    }
                    output.HasProgressionLevelDisplay.Trim().TrimEnd(',');
                }
            }

            output.ProgramTerm    = input.ProgramTerm;
            output.SubjectWebpage = input.SubjectWebpage;
            output.SourceData     = input.SourceData;

            //where output store ComponentDesignation - textvalue
            //Json
            if (!string.IsNullOrEmpty(input.Properties))
            {
                PathwayComponentProperties pcp = JsonConvert.DeserializeObject <PathwayComponentProperties>(input.Properties);
                if (pcp != null)
                {
                    //unpack ComponentDesignation
                    output.ComponentDesignationList = pcp.ComponentDesignationList;
                    //credit value
                    output.CreditValue = pcp.CreditValue;
                    //this is now QuantitativeValue
                    output.PointValue = pcp.PointValue;

                    output.Identifier = new List <IdentifierValue>();
                    if (pcp.Identifier != null)
                    {
                        output.Identifier = pcp.Identifier;
                    }
                    if (pcp.SourceCredential != null && pcp.SourceCredential.Id > 0)
                    {
                        output.SourceCredential = pcp.SourceCredential;
                        output.SourceData       = "";
                    }
                    if (pcp.SourceAssessment != null && pcp.SourceAssessment.Id > 0)
                    {
                        output.SourceAssessment = pcp.SourceAssessment;
                        output.SourceData       = "";
                    }
                    if (pcp.SourceLearningOpportunity != null && pcp.SourceLearningOpportunity.Id > 0)
                    {
                        output.SourceLearningOpportunity = pcp.SourceLearningOpportunity;
                        output.SourceData = "";
                    }
                }
            }

            //
            if (IsValidDate(input.Created))
            {
                output.Created = ( DateTime )input.Created;
            }
            if (IsValidDate(input.LastUpdated))
            {
                output.LastUpdated = ( DateTime )input.LastUpdated;
            }
        }
        /// <summary>
        /// add a PathwayComponent
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="statusMessage"></param>
        /// <returns></returns>
        public bool Save(ThisEntity entity, ref SaveStatus status)
        {
            bool isValid  = true;
            var  efEntity = new DBEntity();

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

                    if (entity.Id == 0)
                    {
                        MapToDB(entity, efEntity);

                        if (entity.RowId == null || entity.RowId == Guid.Empty)
                        {
                            efEntity.RowId = entity.RowId = Guid.NewGuid();
                        }
                        else
                        {
                            efEntity.RowId = entity.RowId;
                        }

                        efEntity.CTID    = "ce-" + efEntity.RowId.ToString().ToLower();
                        efEntity.Created = efEntity.LastUpdated = System.DateTime.Now;

                        context.PathwayComponent.Add(efEntity);

                        // submit the change to database
                        int count = context.SaveChanges();
                        if (count > 0)
                        {
                            entity.Id = efEntity.Id;

                            UpdateParts(entity, ref status);

                            return(true);
                        }
                        else
                        {
                            //?no info on error
                            status.AddError("Error - the profile was not saved. ");
                            string message = string.Format("PathwayComponentManager.Add Failed", "Attempted to add a PathwayComponent. The process appeared to not work, but was not an exception, so we have no message, or no clue.PathwayComponent. PathwayComponent: {0}, createdById: {1}", entity.Name, entity.CreatedById);
                            EmailManager.NotifyAdmin(thisClassName + ".Add Failed", message);
                        }
                    }
                    else
                    {
                        efEntity = context.PathwayComponent
                                   .SingleOrDefault(s => s.Id == entity.Id);

                        if (efEntity != null && efEntity.Id > 0)
                        {
                            //for updates, chances are some fields will not be in interface, don't map these (ie created stuff)
                            MapToDB(entity, efEntity);
                            efEntity.EntityStateId = 3;
                            if (HasStateChanged(context))
                            {
                                efEntity.LastUpdated = System.DateTime.Now;
                                int count = context.SaveChanges();
                                //can be zero if no data changed
                                if (count >= 0)
                                {
                                    isValid = true;
                                }
                                else
                                {
                                    //?no info on error
                                    status.AddError("Error - the update was not successful. ");
                                    string message = string.Format(thisClassName + ".Save Failed", "Attempted to update a PathwayComponent. The process appeared to not work, but was not an exception, so we have no message, or no clue. PathwayComponentId: {0}, Id: {1}, updatedById: {2}", entity.Id, entity.Id, entity.LastUpdatedById);
                                    EmailManager.NotifyAdmin(thisClassName + ". PathwayComponent_Update Failed", message);
                                }
                            }
                            //continue with parts regardless
                            UpdateParts(entity, ref status);
                        }
                        else
                        {
                            status.AddError("Error - update failed, as record was not found.");
                        }
                    }
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException dbex)
                {
                    //LoggingHelper.LogError( dbex, thisClassName + string.Format( ".ContentAdd() DBEntityValidationException, Type:{0}", entity.TypeId ) );
                    string message = thisClassName + string.Format(".Save() DBEntityValidationException, PathwayComponentId: {0}", entity.Id);
                    foreach (var eve in dbex.EntityValidationErrors)
                    {
                        message += string.Format("\rEntity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                                 eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            message += string.Format("- Property: \"{0}\", Error: \"{1}\"",
                                                     ve.PropertyName, ve.ErrorMessage);
                        }

                        LoggingHelper.LogError(message, true);
                    }
                }
                catch (Exception ex)
                {
                    var message = FormatExceptions(ex);
                    LoggingHelper.LogError(ex, thisClassName + string.Format(".PathwayComponent_Add(), PathwayComponentId: {0}", entity.ParentId));
                    status.AddError(string.Format("Error encountered saving component. Type: {0}, Name: {1}, Error: {2}. ", entity.PathwayComponentType, entity.Name, message));
                    isValid = false;
                }
            }

            return(isValid);
        }