Esempio n. 1
0
        /// <summary>
        /// This handles the save operation for this page
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public virtual void SaveOperation(object sender, RoutedEventArgs e)
        {
            string user = WebContext.Current.User.DisplayName;
            if (Grid.IsValid)
            {
                //commit any unsaved changes to avoid an exception
                if (Grid.CommitEdit())
                {
                    //capture all the changes and their reasons
                    var modification = DomainSource.DomainContext.EntityContainer.GetChanges();

                    #region Change tracking for modified entries

                    foreach (var modified in modification.ModifiedEntities)
                    {
                        //var original = modified.GetOriginal();
                        object original;
                        Grid.OriginalEntities.TryGetValue((int)modified.GetIdentity(), out original);

                        //also try in this page level modified entries
                        if(original == null && OriginalEntities != null)
                        {
                            object pagelevelOriginal;
                            this.OriginalEntities.TryGetValue((int)modified.GetIdentity(), out pagelevelOriginal);

                            //this is assuming that page level there will be only one entity type at max. This is a reasonable
                            //assumption for this application
                            if (pagelevelOriginal != null)
                            {
                                if (pagelevelOriginal.GetType() == modified.GetType())
                                {
                                    original = pagelevelOriginal;
                                }
                            }
                        }

                        if (original != null)
                        {
                            Type type = modified.GetType();

                            PropertyInfo changeContextProp = null;

                            if (ChangeContextValue == null)
                            {
                                changeContextProp = type.GetProperty(ChangeContextProperty);
                            }

                            //check if change context value is set, else take it from the change context property name defined
                            var propertyValue = this.ChangeContextValue ??
                                                changeContextProp.GetValue(modified, null);
                            string changeContextString = String.Concat(ChangeContext, "-",
                                                                       (propertyValue ?? "").ToString());
                            List<Change> changes = Utility.GetChanges(original, modified, changeContextString,
                                                                        user, ExcludePropertiesFromTracking);
                            changes.ForEach(p => allChanges.Add(p));
                        }
                    }

                    #endregion

                    #region change tracking for deleted entries

                    foreach (var deleted in modification.RemovedEntities)
                    {
                        Type type = deleted.GetType();
                        PropertyInfo changeContextProp = null;

                        if(ChangeContextProperty != null)
                            changeContextProp= type.GetProperty(ChangeContextProperty);

                        var propertyValue = this.ChangeContextValue ?? changeContextProp.GetValue(deleted, null);
                        string changeContextString = String.Concat(ChangeContext, "-",
                                                                   (propertyValue ?? "").ToString());
                        Change change = new Change()
                                        {
                                            When = DateTime.Now,
                                            Where = changeContextString,
                                            FromValue = "",
                                            ToValue= "Deleted",
                                            ByWhom = user,
                                            Why = " "
                                        };

                        allChanges.Add(change);
                    }

                    #endregion

                    #region change tracking for added entries
                    //for added entities, whether changes will be tracked or not is determined by the page level
                    //hence keeping this there

                    if (ConsiderAdditions)
                    {

                        foreach (var modified in modification.AddedEntities)
                        {
                            Type type = modified.GetType();

                            //do not track addition of changes themselves, which are entities
                            if (type == typeof(Change))
                                continue;

                            PropertyInfo changeContextProp = type.GetProperty(ChangeContextProperty ?? "");

                            if (changeContextProp == null)
                                continue;

                            var propertyValue = changeContextProp.GetValue(modified, null);
                            string changeContextString = String.Concat(ChangeContext, "-",
                                                                        (propertyValue ?? "").ToString());
                            allChanges.Add(new Change()
                                            {
                                                When = DateTime.Now,
                                                Where = changeContextString,
                                                FromValue = "",
                                                ToValue = "Added",
                                                ByWhom = user,
                                                Why = " "
                                            });
                        }
                    }

                    #endregion

                    if (allChanges.Count != 0)
                    {
                        //get reason for all the changes - do not let user submit without all reasons
                        ChangeReasons cr = new ChangeReasons();
                        cr.Changes = allChanges;
                        cr.Closed += new EventHandler(cr_Closed);
                        cr.Show();
                    }
                    else
                    {
                        //if no changes worth tracking but still there are changes
                        SaveChanges();
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// This handles the save operation for this page
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public virtual void SaveOperation(object sender, RoutedEventArgs e)
        {
            string user = WebContext.Current.User.DisplayName;

            if (Grid.IsValid)
            {
                //commit any unsaved changes to avoid an exception
                if (Grid.CommitEdit())
                {
                    //capture all the changes and their reasons
                    var modification = DomainSource.DomainContext.EntityContainer.GetChanges();

                    #region Change tracking for modified entries

                    foreach (var modified in modification.ModifiedEntities)
                    {
                        //var original = modified.GetOriginal();
                        object original;
                        Grid.OriginalEntities.TryGetValue((int)modified.GetIdentity(), out original);

                        //also try in this page level modified entries
                        if (original == null && OriginalEntities != null)
                        {
                            object pagelevelOriginal;
                            this.OriginalEntities.TryGetValue((int)modified.GetIdentity(), out pagelevelOriginal);

                            //this is assuming that page level there will be only one entity type at max. This is a reasonable
                            //assumption for this application
                            if (pagelevelOriginal != null)
                            {
                                if (pagelevelOriginal.GetType() == modified.GetType())
                                {
                                    original = pagelevelOriginal;
                                }
                            }
                        }

                        if (original != null)
                        {
                            Type type = modified.GetType();

                            PropertyInfo changeContextProp = null;

                            if (ChangeContextValue == null)
                            {
                                changeContextProp = type.GetProperty(ChangeContextProperty);
                            }

                            //check if change context value is set, else take it from the change context property name defined
                            var propertyValue = this.ChangeContextValue ??
                                                changeContextProp.GetValue(modified, null);
                            string changeContextString = String.Concat(ChangeContext, "-",
                                                                       (propertyValue ?? "").ToString());
                            List <Change> changes = Utility.GetChanges(original, modified, changeContextString,
                                                                       user, ExcludePropertiesFromTracking);
                            changes.ForEach(p => allChanges.Add(p));
                        }
                    }

                    #endregion

                    #region change tracking for deleted entries

                    foreach (var deleted in modification.RemovedEntities)
                    {
                        Type         type = deleted.GetType();
                        PropertyInfo changeContextProp = null;

                        if (ChangeContextProperty != null)
                        {
                            changeContextProp = type.GetProperty(ChangeContextProperty);
                        }

                        var    propertyValue       = this.ChangeContextValue ?? changeContextProp.GetValue(deleted, null);
                        string changeContextString = String.Concat(ChangeContext, "-",
                                                                   (propertyValue ?? "").ToString());
                        Change change = new Change()
                        {
                            When      = DateTime.Now,
                            Where     = changeContextString,
                            FromValue = "",
                            ToValue   = "Deleted",
                            ByWhom    = user,
                            Why       = " "
                        };

                        allChanges.Add(change);
                    }

                    #endregion

                    #region change tracking for added entries
                    //for added entities, whether changes will be tracked or not is determined by the page level
                    //hence keeping this there

                    if (ConsiderAdditions)
                    {
                        foreach (var modified in modification.AddedEntities)
                        {
                            Type type = modified.GetType();

                            //do not track addition of changes themselves, which are entities
                            if (type == typeof(Change))
                            {
                                continue;
                            }

                            PropertyInfo changeContextProp = type.GetProperty(ChangeContextProperty ?? "");

                            if (changeContextProp == null)
                            {
                                continue;
                            }

                            var    propertyValue       = changeContextProp.GetValue(modified, null);
                            string changeContextString = String.Concat(ChangeContext, "-",
                                                                       (propertyValue ?? "").ToString());
                            allChanges.Add(new Change()
                            {
                                When      = DateTime.Now,
                                Where     = changeContextString,
                                FromValue = "",
                                ToValue   = "Added",
                                ByWhom    = user,
                                Why       = " "
                            });
                        }
                    }

                    #endregion

                    if (allChanges.Count != 0)
                    {
                        //get reason for all the changes - do not let user submit without all reasons
                        ChangeReasons cr = new ChangeReasons();
                        cr.Changes = allChanges;
                        cr.Closed += new EventHandler(cr_Closed);
                        cr.Show();
                    }
                    else
                    {
                        //if no changes worth tracking but still there are changes
                        SaveChanges();
                    }
                }
            }
        }