Ejemplo n.º 1
0
 public static bool IsActiveDirect(this GStoreEntity record, DateTime dateTime)
 {
     if (record != null && !record.IsPending && (record.StartDateTimeUtc < dateTime) && (record.EndDateTimeUtc > dateTime))
     {
         return(true);
     }
     return(false);
 }
Ejemplo n.º 2
0
        public static string EntityTypeFullName(this GStoreEntity entity)
        {
            if (entity.IsPoco())
            {
                return(entity.GetType().FullName);
            }

            return(entity.GetType().BaseType.FullName);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns true if this object is an entity framework proxy object, not a POCO
        /// </summary>
        /// <param name="testObject"></param>
        /// <returns></returns>
        public static bool IsProxy(this GStoreEntity entity)
        {
            //check if the current object type is the base type, or an entity parent type
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            Type pocoType = System.Data.Entity.Core.Objects.ObjectContext.GetObjectType(entity.GetType());

            return(pocoType != entity.GetType());
        }
Ejemplo n.º 4
0
 public static bool IsActiveDirect(this GStoreEntity record)
 {
     return(record.IsActiveDirect(DateTime.UtcNow));
 }
Ejemplo n.º 5
0
 public static Type GetPocoType(this GStoreEntity entity)
 {
     return(System.Data.Entity.Core.Objects.ObjectContext.GetObjectType(entity.GetType()));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Returns true if this object is a POCO, not an entity framework proxy object
 /// </summary>
 /// <param name="testObject"></param>
 /// <returns></returns>
 public static bool IsPoco(this GStoreEntity entity)
 {
     return(!entity.IsProxy());
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Runs Savechanges on the context, plus other automatic options, defaults are all true
        /// </summary>
        /// <param name="updateAuditableRecords"></param>
        /// <param name="runEmailNotifications"></param>
        /// <param name="runSmsNotifications"></param>
        /// <param name="updateCategoryCounts"></param>
        /// <returns></returns>
        public int SaveChangesEx(bool updateAuditableRecords, bool runEmailNotifications, bool runSmsNotifications, bool updateCategoryCounts)
        {
            ChangeTracker.DetectChanges();
            List <Notification> notificationsToProcess     = new List <Notification>();
            List <int>          storeFrontIdsToRecalculate = new List <int>();

            if (ChangeTracker.HasChanges())
            {
                foreach (DbEntityEntry item in ChangeTracker.Entries())
                {
                    if (updateAuditableRecords && (item.State == EntityState.Added || item.State == EntityState.Modified) && item.Entity is Models.BaseClasses.AuditFieldsAllRequired)
                    {
                        Models.BaseClasses.AuditFieldsAllRequired record = item.Entity as Models.BaseClasses.AuditFieldsAllRequired;
                        UserProfile userProfile = this.GetCurrentUserProfile(false, false);
                        if (userProfile == null)
                        {
                            string entityType = record.EntityTypeFullName();
                            throw new ApplicationException("User profile not found. User may be anonymous. Profile needed for entity: " + entityType);
                        }
                        record.UpdateAuditFields(userProfile);
                    }

                    if (updateAuditableRecords && (item.State == EntityState.Added || item.State == EntityState.Modified) && item.Entity is Models.BaseClasses.AuditFieldsUserProfileOptional)
                    {
                        Models.BaseClasses.AuditFieldsUserProfileOptional recordOptional = item.Entity as Models.BaseClasses.AuditFieldsUserProfileOptional;
                        UserProfile userProfileOptional = this.GetCurrentUserProfile(false, false);
                        recordOptional.UpdateAuditFields(userProfileOptional);
                    }

                    if (updateCategoryCounts && (item.State == EntityState.Added || item.State == EntityState.Modified || item.State == EntityState.Deleted) && item.Entity is Models.ProductCategory)
                    {
                        StoreFront storeFront   = ((ProductCategory)item.Entity).StoreFront;
                        int        storeFrontId = storeFront == null?item.OriginalValues.GetValue <int>("StoreFrontId") : storeFront.StoreFrontId;

                        if (!storeFrontIdsToRecalculate.Contains(storeFrontId))
                        {
                            storeFrontIdsToRecalculate.Add(storeFrontId);
                        }
                    }

                    if (updateCategoryCounts && (item.State == EntityState.Added || item.State == EntityState.Modified || item.State == EntityState.Deleted) && item.Entity is Models.Product)
                    {
                        StoreFront storeFront   = ((Product)item.Entity).StoreFront;
                        int        storeFrontId = storeFront == null?item.OriginalValues.GetValue <int>("StoreFrontId") : storeFront.StoreFrontId;

                        if (!storeFrontIdsToRecalculate.Contains(storeFrontId))
                        {
                            storeFrontIdsToRecalculate.Add(storeFrontId);
                        }
                    }

                    if (updateCategoryCounts && (item.State == EntityState.Added || item.State == EntityState.Modified || item.State == EntityState.Deleted) && item.Entity is Models.ProductBundle)
                    {
                        StoreFront storeFront   = ((ProductBundle)item.Entity).StoreFront;
                        int        storeFrontId = storeFront == null?item.OriginalValues.GetValue <int>("StoreFrontId") : storeFront.StoreFrontId;

                        if (!storeFrontIdsToRecalculate.Contains(storeFrontId))
                        {
                            storeFrontIdsToRecalculate.Add(storeFrontId);
                        }
                    }

                    if (updateCategoryCounts && (item.State == EntityState.Added || item.State == EntityState.Modified || item.State == EntityState.Deleted) && item.Entity is Models.ProductBundleItem)
                    {
                        StoreFront storeFront   = ((ProductBundleItem)item.Entity).StoreFront;
                        int        storeFrontId = storeFront == null?item.OriginalValues.GetValue <int>("StoreFrontId") : storeFront.StoreFrontId;

                        if (!storeFrontIdsToRecalculate.Contains(storeFrontId))
                        {
                            storeFrontIdsToRecalculate.Add(storeFrontId);
                        }
                    }

                    //new notification, process email and sms notification
                    if (item.Entity is Notification && item.State == EntityState.Added && (runEmailNotifications || runSmsNotifications))
                    {
                        notificationsToProcess.Add(item.Entity as Notification);
                    }
                }
            }

            int returnValue = -1;

            try
            {
                returnValue = base.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                string errorDetails = "DbEntityValidationException Error saving to database.";
                if (ex.EntityValidationErrors != null && ex.EntityValidationErrors.Count() > 0)
                {
                    errorDetails += "\nEntity Errors: " + ex.EntityValidationErrors.Count();
                    foreach (DbEntityValidationResult valError in ex.EntityValidationErrors)
                    {
                        DbEntityEntry errorRow = valError.Entry;
                        if (errorRow != null)
                        {
                            GStoreEntity entity = errorRow.Entity as GStoreEntity;
                            if (entity != null)
                            {
                                errorDetails += "\n\tGStore Entity: " + entity.GetPocoType().FullName;
                            }
                            else
                            {
                                errorDetails += "\n\tMisc Entity: " + errorRow.Entity.ToString();
                            }
                        }
                        else
                        {
                            errorDetails += "\n\tUnknown Entity";
                        }

                        errorDetails += " (" + errorRow.State.ToString() + ")";
                        errorDetails += " Errors: " + valError.ValidationErrors.Count();

                        foreach (DbValidationError dbValError in valError.ValidationErrors)
                        {
                            errorDetails += "\n\t\tError in " + dbValError.PropertyName + ": " + dbValError.ErrorMessage;
                        }

                        foreach (string propName in valError.Entry.CurrentValues.PropertyNames)
                        {
                            object fieldObjectValue = valError.Entry.CurrentValues[propName];
                            string fieldValue       = fieldObjectValue == null ? "(null)" : fieldObjectValue.ToString();
                            errorDetails += "\n\t\t" + propName + " = '" + fieldValue + "'";
                            if (errorRow.State == EntityState.Modified)
                            {
                                if ((valError.Entry.OriginalValues != null) && (valError.Entry.OriginalValues.PropertyNames.Contains(propName)))
                                {
                                    object originalValueObject = valError.Entry.OriginalValues[propName];
                                    string originalValue       = originalValueObject == null ? "(null)" : originalValueObject.ToString();
                                    errorDetails += " Original Value: '" + originalValue + "'";
                                }
                            }
                        }
                    }
                }
                errorDetails += " \n BaseException: " + ex.GetBaseException().ToString();
                ApplicationException exNew = new ApplicationException(errorDetails, ex);
                Debug.WriteLine(errorDetails);
                throw exNew;
            }
            catch (DbUpdateException exUpdate)
            {
                string errorDetails = "DBUpdateException Error updating database.";
                if (exUpdate.Entries != null && exUpdate.Entries.Count() > 0)
                {
                    errorDetails += "\nEntity DB Update Exception Error Entries: " + exUpdate.Entries.Count();
                    foreach (DbEntityEntry errorEntry in exUpdate.Entries)
                    {
                        if (errorEntry.Entity != null)
                        {
                            GStoreEntity entity = errorEntry.Entity as GStoreEntity;
                            if (entity != null)
                            {
                                errorDetails += "\n\tGStore Entity: " + entity.GetPocoType().FullName;
                            }
                            else
                            {
                                errorDetails += "\n\tMisc Entity: " + errorEntry.Entity.ToString();
                            }
                        }
                        else
                        {
                            errorDetails += "\n\tUnknown Entity";
                        }
                        errorDetails += " (Entity State: " + errorEntry.State.ToString() + ")";

                        if (errorEntry.State == EntityState.Deleted)
                        {
                            foreach (string propName in errorEntry.OriginalValues.PropertyNames)
                            {
                                object fieldObjectValue = errorEntry.OriginalValues[propName];
                                string fieldValue       = (fieldObjectValue == null ? "(null)" : fieldObjectValue.ToString());
                                errorDetails += "\n\t\t" + propName + " = '" + fieldValue + "'";
                            }
                        }
                        else
                        {
                            foreach (string propName in errorEntry.CurrentValues.PropertyNames)
                            {
                                object fieldObjectValue = errorEntry.CurrentValues[propName];
                                string fieldValue       = (fieldObjectValue == null ? "(null)" : fieldObjectValue.ToString());
                                errorDetails += "\n\t\t" + propName + " = '" + fieldValue + "'";

                                if ((errorEntry.State == EntityState.Modified) && (errorEntry.OriginalValues != null) && (errorEntry.OriginalValues.PropertyNames.Contains(propName)))
                                {
                                    object originalValueObject = errorEntry.OriginalValues[propName];
                                    string originalValue       = originalValueObject == null ? "(null)" : originalValueObject.ToString();
                                    errorDetails += " Original Value: '" + originalValue + "'";
                                }
                            }
                        }
                    }
                }

                errorDetails += " \n BaseException: " + exUpdate.GetBaseException().ToString();
                ApplicationException exNew = new ApplicationException(errorDetails, exUpdate);
                Debug.WriteLine(errorDetails);
                throw exNew;
            }
            catch (Exception ex)
            {
                if (ex is System.Data.SqlClient.SqlException || ex.GetBaseException() is System.Data.SqlClient.SqlException)
                {
                    throw new Exceptions.DatabaseErrorException("SQL Client Error in SaveChangesEx: " + ex.Message, ex);
                }
                else
                {
                    throw new Exceptions.DatabaseErrorException("Unknown Database Error in SaveChangesEx: " + ex.Message, ex);
                }
            }

            if (notificationsToProcess.Count != 0)
            {
                foreach (Notification item in notificationsToProcess)
                {
                    StoreFrontExtensions.ProcessEmailAndSmsNotifications(this, item, runEmailNotifications, runSmsNotifications);
                }
            }

            if (storeFrontIdsToRecalculate.Count != 0)
            {
                foreach (int storeFrontId in storeFrontIdsToRecalculate)
                {
                    this.RecalculateProductCategoryActiveCount(storeFrontId);
                }
            }

            return(returnValue);
        }