private IEnumerable <COMMODITY_CODE> ReorderAndSave(Guid guid_parent, bool dontSave = false)
        {
            IEnumerable <COMMODITY_CODE> childCommodityCodes = this.Entities.Where(x => x.GUID_PARENT == guid_parent).OrderBy(x => x.SORTORDER).ToList();
            int commodityCodeOrderCount = 10;

            foreach (COMMODITY_CODE childCommodityCode in childCommodityCodes)
            {
                if (childCommodityCode.SORTORDER != commodityCodeOrderCount)
                {
                    EntitiesUndoRedoManager.AddUndo(childCommodityCode, BindableBase.GetPropertyName(() => new COMMODITY_CODE().SORTORDER), childCommodityCode.SORTORDER, commodityCodeOrderCount, EntityMessageType.Changed);
                    childCommodityCode.SORTORDER = commodityCodeOrderCount;
                    int tryParseInt;
                    if (childCommodityCode.CODE == "temp")
                    {
                        childCommodityCode.CODE = GenerateOrderString(childCommodityCode.SORTORDER);
                    }
                    else
                    {
                        childCommodityCode.CODE = Int32.TryParse(childCommodityCode.CODE, out tryParseInt) ? AddUndoOnCODEChanges(childCommodityCode, GenerateOrderString(childCommodityCode.SORTORDER)) : childCommodityCode.CODE;
                    }
                }

                commodityCodeOrderCount += 10;
            }

            RecurseRenameChildrenFULLCODE(guid_parent);

            if (!dontSave)
            {
                this.BulkSave(childCommodityCodes);
            }

            return(childCommodityCodes);
        }
        private string AddUndoOnFULLCODEChanges(COMMODITY_CODE entity)
        {
            string newValue = GenerateFullCode(entity);

            EntitiesUndoRedoManager.AddUndo(entity, BindableBase.GetPropertyName(() => new COMMODITY_CODE().FULLCODE), entity.FULLCODE, newValue, EntityMessageType.Changed);
            return(newValue);
        }
        //Remove children before parent deletion
        private void EntitiesBeforeDeletion(IEnumerable <COMMODITY_CODE> entities)
        {
            //Undo manager is paused in bulk deletion and will be unpaused in bulk deletion too
            List <COMMODITY_CODE> childrenEntities = new List <COMMODITY_CODE>();

            foreach (var entity in entities)
            {
                var childrenEntitiesInTotal = RecurseFindChildren(entity, this.Entities);
                List <COMMODITY_CODE> childrenEntitiesNotInDeletionCollection = new List <COMMODITY_CODE>();
                foreach (var childrenEntityInTotal in childrenEntitiesInTotal)
                {
                    if (!entities.Any(x => x.GUID == childrenEntityInTotal.GUID))
                    {
                        childrenEntitiesNotInDeletionCollection.Add(childrenEntityInTotal);
                    }
                }

                childrenEntities = childrenEntities.Concat(childrenEntitiesNotInDeletionCollection).ToList();
            }

            uniqueGUID_PARENTS = new List <Guid>();
            //can't use bulk delete here due to stack overflow
            foreach (var childrenEntity in childrenEntities)
            {
                if (!uniqueGUID_PARENTS.Any(x => x == childrenEntity.GUID_PARENT))
                {
                    uniqueGUID_PARENTS.Add(childrenEntity.GUID_PARENT);
                }

                EntitiesUndoRedoManager.AddUndo(childrenEntity, null, null, null, EntityMessageType.Deleted);
                Delete(childrenEntity);
            }
        }
 public override void TreelistExistingRowAddUndoAndSave(TreeListCellValueChangedEventArgs e)
 {
     base.TreelistExistingRowAddUndoAndSave(e);
     if (e.Column.FieldName == BindableBase.GetPropertyName(() => new COMMODITY_CODE().CODE))
     {
         EntitiesUndoRedoManager.RewindActionId(1);
         EntitiesUndoRedoManager.PauseActionId();
         COMMODITY_CODE editedCOMMODITY_CODE = (COMMODITY_CODE)e.Row;
         AddUndoOnFULLCODEChanges(editedCOMMODITY_CODE);
         RecurseRenameChildrenFULLCODE(editedCOMMODITY_CODE.GUID);
         IEnumerable <COMMODITY_CODE> childrenCOMMODITY_CODES = RecurseFindChildren(editedCOMMODITY_CODE, this.Entities);
         BulkSave(childrenCOMMODITY_CODES);
     }
 }
        //Reorder tree after deletion
        private void EntitiesAfterDeletion(IEnumerable <COMMODITY_CODE> entities)
        {
            //Undo manager is paused in bulk deletion and will be unpaused in bulk deletion too
            //uniqueGUID_PARENTS is initialized in EntitiesBeforeDeletion
            foreach (var entity in entities)
            {
                if (!uniqueGUID_PARENTS.Any(x => x == entity.GUID_PARENT))
                {
                    uniqueGUID_PARENTS.Add(entity.GUID_PARENT);
                }
            }

            EntitiesUndoRedoManager.PauseActionId(); //save will unpause this
            ReorderAndSave(uniqueGUID_PARENTS);
        }
        List <Guid> uniqueGUID_PARENTS; //stores dropping entity parent guid before it gets reassigned
        public void dragDropManager_Drop(object sender, DevExpress.Xpf.Grid.DragDrop.TreeListDropEventArgs e)
        {
            uniqueGUID_PARENTS = new List <Guid>();
            if (e.TargetNode != null)
            {
                EntitiesUndoRedoManager.PauseActionId(); //save will unpause this
                foreach (object obj in e.DraggedRows)
                {
                    COMMODITY_CODE editCommodityCode = (e.SourceManager.GetObject(obj) as COMMODITY_CODE);

                    if (!uniqueGUID_PARENTS.Any(x => x == editCommodityCode.GUID_PARENT))
                    {
                        uniqueGUID_PARENTS.Add(editCommodityCode.GUID_PARENT);
                    }

                    COMMODITY_CODE targetCommodityCode = (e.TargetNode.Content as COMMODITY_CODE);
                    EntitiesUndoRedoManager.AddUndo(editCommodityCode, BindableBase.GetPropertyName(() => new COMMODITY_CODE().GUID_PARENT), editCommodityCode.GUID_PARENT, targetCommodityCode.GUID, EntityMessageType.Changed);
                }
            }
        }
        private void AddCommodityCodeRow(bool isAfter)
        {
            if (LookUpDISCIPLINES.Entities.Count() == 0)
            {
                MessageBoxService.ShowMessage(CommonResources.CommodityCode_NoDiscipline);
                return;
            }

            int  commodityCodeOrder = 0;
            Guid guid_parent        = Guid.Empty;

            if (SelectedEntity != null)
            {
                if (isAfter)
                {
                    commodityCodeOrder = SelectedEntity.SORTORDER + 1;
                }
                else
                {
                    commodityCodeOrder = SelectedEntity.SORTORDER - 1;
                }

                guid_parent = SelectedEntity.GUID_PARENT;
            }

            COMMODITY_CODE newCommodityCode = this.CreateEntity();

            newCommodityCode.CODE            = "temp";
            newCommodityCode.FULLCODE        = "temp";
            newCommodityCode.NAME            = "new commodity";
            newCommodityCode.GUID_DISCIPLINE = LookUpDISCIPLINES.Entities.First().GUID;
            newCommodityCode.SORTORDER       = commodityCodeOrder;
            newCommodityCode.GUID_PARENT     = guid_parent;

            EntitiesUndoRedoManager.PauseActionId(); //Save will unpause this
            EntitiesUndoRedoManager.AddUndo(newCommodityCode, null, null, null, EntityMessageType.Added);
            this.Save(newCommodityCode);
            ReorderAndSave(guid_parent);
        }
 private string AddUndoOnCODEChanges(COMMODITY_CODE entity, string newValue)
 {
     EntitiesUndoRedoManager.AddUndo(entity, BindableBase.GetPropertyName(() => new COMMODITY_CODE().CODE), entity.CODE, newValue, EntityMessageType.Changed);
     return(newValue);
 }