Example #1
0
        public static Entity GetEntity(int entityTypeId, int entityBaseId)
        {
            Entity entity = new Entity();

            using (var context = new EntityContext())
            {
                DBentity item = context.Entity
                                .FirstOrDefault(s => s.EntityTypeId == entityTypeId &&
                                                s.EntityBaseId == entityBaseId);

                if (item != null && item.Id > 0)
                {
                    entity.Id           = item.Id;
                    entity.EntityTypeId = item.EntityTypeId;
                    //not why this was commented. Reusing with care
                    if (item.Codes_EntityTypes != null)
                    {
                        entity.EntityType = item.Codes_EntityTypes.Title;
                    }
                    else
                    {
                        entity.EntityType = GetEntityType(entity.EntityTypeId);
                    }
                    entity.EntityUid      = item.EntityUid;
                    entity.EntityBaseId   = item.EntityBaseId ?? 0;
                    entity.EntityBaseName = item.EntityBaseName;
                    entity.Created        = ( DateTime )item.Created;
                }
                return(entity);
            }
        }
        public static Entity GetEntity(Guid entityUid, bool includingAllChildren = true)
        {
            Entity entity = new Entity();

            using (var context = new EntityContext())
            {
                if (includingAllChildren == false)
                {
                    context.Configuration.LazyLoadingEnabled = false;
                }
                DBentity item = context.Entity
                                .FirstOrDefault(s => s.EntityUid == entityUid);

                if (item != null && item.Id > 0)
                {
                    entity.Id           = item.Id;
                    entity.EntityTypeId = item.EntityTypeId;

                    //entity.EntityType = item.Codes_EntityType.Title;

                    entity.EntityUid      = item.EntityUid;
                    entity.EntityBaseId   = item.EntityBaseId ?? 0;
                    entity.EntityBaseName = item.EntityBaseName;
                    entity.Created        = (DateTime)item.Created;
                    entity.LastUpdated    = item.LastUpdated != null ?( DateTime )item.LastUpdated : entity.Created;
                }
                return(entity);
            }
        }
        public bool UpdateModifiedDate(Guid entityUid, ref SaveStatus status)
        {
            bool isValid = false;

            if (!IsValidGuid(entityUid))
            {
                status.AddError(thisClassName + ".UpdateModifiedDate(). Error - missing a valid identifier for the Entity");
                return(false);
            }
            using (var context = new EntityContext())
            {
                DBentity efEntity = context.Entity
                                    .FirstOrDefault(s => s.EntityUid == entityUid);

                if (efEntity != null && efEntity.Id > 0)
                {
                    efEntity.LastUpdated = DateTime.Now;
                    int count = context.SaveChanges();
                    if (count >= 0)
                    {
                        isValid = true;
                        LoggingHelper.DoTrace(7, thisClassName + string.Format(".UpdateModifiedDate - update last updated for TypeId: {0}, BaseId: {1}", efEntity.EntityTypeId, efEntity.EntityBaseId));
                    }
                }
                else
                {
                    status.AddError(thisClassName + ".UpdateModifiedDate(). Error - Entity  was not found.");
                    LoggingHelper.LogError(thisClassName + string.Format(".UpdateModifiedDate - record was not found. entityUid: {0}", entityUid), true);
                }
            }

            return(isValid);
        }///
Example #4
0
        /// <summary>
        /// Add an Entity mirror
        /// NOTE: ALL ENTITY ADDS WOULD NORMALLY BE DONE VIA TRIGGERS.
        /// However, as on import, we want to delete all the child entities for a top level entity like credential. The latter is accomplished by deleting the Entity. We then need to re- add the Entity.
        /// </summary>
        /// <param name="entityUid">RowId of the base Object</param>
        /// <param name="baseId">Integer PK of the base object</param>
        /// <param name="entityTypeId"></param>
        /// <param name="baseName"></param>
        /// <param name="statusMessage"></param>
        /// <returns></returns>
        private int Add(Guid entityUid, int baseId, int entityTypeId, string baseName, ref string statusMessage)
        {
            DBentity efEntity = new DBentity();

            using (var context = new EntityContext())
            {
                try
                {
                    efEntity.EntityUid      = entityUid;
                    efEntity.EntityBaseId   = baseId;
                    efEntity.EntityTypeId   = entityTypeId;
                    efEntity.EntityBaseName = baseName;
                    efEntity.Created        = efEntity.LastUpdated = System.DateTime.Now;

                    context.Entity.Add(efEntity);

                    // submit the change to database
                    int count = context.SaveChanges();
                    if (count > 0)
                    {
                        statusMessage = "successful";

                        return(efEntity.Id);
                    }
                    else
                    {
                        //?no info on error
                        statusMessage = "Error - the add was not successful. ";
                        string message = thisClassName + string.Format(". Add Failed", "Attempted to add an Entity. The process appeared to not work, but was not an exception, so we have no message, or no clue. entityUid: {0}, entityTypeId: {1}", entityUid.ToString(), entityTypeId);
                        EmailManager.NotifyAdmin("AssessmentManager. Assessment_Add Failed", message);
                        return(0);
                    }
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException dbex)
                {
                    string message = HandleDBValidationError(dbex, thisClassName + ".Add() ", "Entity");
                    statusMessage = "Error - the save was not successful. " + message;
                }
                catch (Exception ex)
                {
                    LoggingHelper.LogError(ex, thisClassName + string.Format(".Add(). entityUid: {0}, entityTypeId: {1}", entityUid.ToString(), entityTypeId));
                }
            }

            return(0);
        }
        public static Entity GetEntity(int entityId)
        {
            Entity entity = new Entity();

            using (var context = new EntityContext())
            {
                DBentity item = context.Entity
                                .FirstOrDefault(s => s.Id == entityId);

                if (item != null && item.Id > 0)
                {
                    entity.Id           = item.Id;
                    entity.EntityTypeId = item.EntityTypeId;
                    //entity.EntityType = item.Codes_EntityType.Title;
                    entity.EntityUid      = item.EntityUid;
                    entity.EntityBaseId   = item.EntityBaseId ?? 0;
                    entity.EntityBaseName = item.EntityBaseName;
                    entity.Created        = ( DateTime )item.Created;
                }
                return(entity);
            }
        }
Example #6
0
        public static Entity GetEntity(Guid entityUid, bool includingAllChildren = true)
        {
            Entity entity = new Entity();

            using (var context = new EntityContext())
            {
                if (includingAllChildren == false)
                {
                    context.Configuration.LazyLoadingEnabled = false;
                }
                DBentity item = context.Entity
                                .FirstOrDefault(s => s.EntityUid == entityUid);

                if (item != null && item.Id > 0)
                {
                    entity.Id           = item.Id;
                    entity.EntityTypeId = item.EntityTypeId;
                    //20-12-18 mp - the following was commented, not sure why - probably related to lazy loading. Change to do conditionally
                    //entity.EntityType = item.Codes_EntityTypes.Title;
                    if (item.Codes_EntityTypes != null)
                    {
                        entity.EntityType = item.Codes_EntityTypes.Title;
                    }
                    else
                    {
                        entity.EntityType = GetEntityType(entity.EntityTypeId);
                    }

                    entity.EntityUid      = item.EntityUid;
                    entity.EntityBaseId   = item.EntityBaseId ?? 0;
                    entity.EntityBaseName = item.EntityBaseName;
                    entity.Created        = (DateTime)item.Created;
                    entity.LastUpdated    = item.LastUpdated != null ?( DateTime )item.LastUpdated : entity.Created;
                }
                return(entity);
            }
        }
Example #7
0
        }        ///

        /// <summary>
        /// Delete an Entity
        /// This should be handled by triggers as well, or at least with the child entity
        /// </summary>
        /// <param name="entityUid"></param>
        /// <param name="forTableIdentifer">Table identifer for tracing</param>
        /// <param name="statusMessage"></param>
        /// <returns></returns>
        public bool Delete(Guid entityUid, string forTableIdentifer, ref string statusMessage, int attemptsRemaining = 0)
        {
            bool isValid = false;

            statusMessage = "";
            if (!IsValidGuid(entityUid))
            {
                statusMessage = "Error - missing a valid identifier for the Entity";
                return(false);
            }
            try
            {
                using (var context = new EntityContext())
                {
                    DBentity efEntity = context.Entity
                                        .FirstOrDefault(s => s.EntityUid == entityUid);

                    if (efEntity != null && efEntity.Id > 0)
                    {
                        int entityTypeId = efEntity.EntityTypeId;
                        //string entityType = efEntity.Codes_EntityTypes.Title;

                        context.Entity.Remove(efEntity);
                        int count = context.SaveChanges();
                        if (count > 0)
                        {
                            isValid = true;
                        }
                        else
                        {
                            statusMessage = string.Format("Entity delete returned count of zero - potential inconsistant state. entityTypeId: {0}, entityUid: {1}", entityTypeId, entityUid);
                            LoggingHelper.LogError(thisClassName + string.Format(".Delete - Entity delete returned count of zero - potential inconsistant state. entityTypeId: {0}, entityUid: {1}", entityTypeId, entityUid), true);
                        }
                    }
                    else
                    {
                        statusMessage = "Error - Entity delete unnecessary, as record was not found.";
                        LoggingHelper.DoTrace(1, thisClassName + string.Format(".Delete - WIERD - delete failed, as record was not found. entityUid: {0} for {1}.", entityUid, forTableIdentifer));
                    }
                }
            }
            catch (SqlException sex)
            {
                statusMessage = FormatExceptions(sex);
                if (statusMessage.ToLower().IndexOf("was deadlocked on lock resources with another process") > -1)
                {
                    LoggingHelper.DoTrace(4, thisClassName + string.Format(".Delete(). Attempt to delete entity: {0} failed with deadlock. Retrying {1} more times.", entityUid, attemptsRemaining));
                    if (attemptsRemaining > 0)
                    {
                        attemptsRemaining--;
                        return(Delete(entityUid, forTableIdentifer, ref statusMessage, attemptsRemaining));
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception ex)
            {
                statusMessage = FormatExceptions(ex);
            }

            return(isValid);
        }