private void ExecutePasteAsset(object parameter)
 {
     if (parameter.GetType().Equals(typeof(TVAssetViewModel)))
     {
         if (parameter is TVAssetViewModel targetasset &&
             !(targetasset.Asset.CustomerID == movingasset.Asset.CustomerID && targetasset.Asset.ID == movingasset.Asset.ParentAssetID))
         {
             UpdateParentAssetID(movingasset.Asset.ID, targetasset.Asset.ID, targetasset.Asset.CustomerID);
             MoveAsset(movingasset.Asset.ID, targetasset.Asset.ID, targetasset.Asset.CustomerID);
             movingasset      = null;
             MovingAssetLabel = string.Empty;
         }
     }
     else
     if (parameter.GetType().Equals(typeof(TVCustomerViewModel)))
     {
         if (parameter is TVCustomerViewModel targetcustomer &&
             !(targetcustomer.Customer.ID == movingasset.Asset.CustomerID && movingasset.Asset.ParentAssetID == 0))
         {
             UpdateParentAssetID(movingasset.Asset.ID, 0, targetcustomer.Customer.ID);
             MoveAsset(movingasset.Asset.ID, 0, targetcustomer.Customer.ID);
             movingasset      = null;
             MovingAssetLabel = string.Empty;
         }
     }
 }
        /// <summary>
        /// Non-recursive version
        /// </summary>
        /// <param name="theseNodes"></param>
        /// <param name="cm"></param>
        public void GetFilteredAsset(ObservableCollection <TreeViewItemViewModel> theseNodes, TVCustomerViewModel cm)
        {
            var stack = new Stack <TVAssetViewModel>();

            foreach (TVAssetViewModel tv in theseNodes)
            {
                stack.Push(tv);
            }
            while (stack.Count != 0)
            {
                TVAssetViewModel current = stack.Pop();
                //  if (assets.Contains(current.Asset.AssetID))
                if (assets.ContainsKey(current.Asset.ID))
                {
                    if (current.Parent != null)
                    {
                        current.Parent.IsExpanded = true;
                    }
                    current.IsFiltered = true;
                    current.IsExpanded = true;
                    cm.IsExpanded      = true;
                    //optimise search by removing element when found
                    //          var id = assets.Where(x => x == current.Asset.AssetID).FirstOrDefault();
                    //        assets.Remove(id);

                    assets.Remove(current.Asset.ID);
                }
                foreach (TVAssetViewModel child in (current.Children).Reverse())
                {
                    stack.Push(child);
                }
            }
        }
 public TVAssetViewModel(Models.AssetModel _asset, TVAssetViewModel _assetvm) : base(_assetvm, true)
 {
     Asset      = _asset;
     IsExpanded = false;
     IsSelected = false;
     //Asset.Label = GlobalClass.MakeLabel(Asset.AssetAreaID, Asset.AssetGroupID, Asset.LabelID);
 }
Exemple #4
0
 public TVAssetViewModel(AssetModel asset, TVAssetViewModel assetvm) : base(assetvm, true)
 {
     Asset              = asset;
     IsExpanded         = false;
     IsSelected         = false;
     CheckBoxVisibility = Visibility.Visible;
 }
 public AssetViewModel(AssetTreeExViewModel tva, TVAssetViewModel obj)
 {
     original = obj;
     atvm     = tva;
     Asset    = GetAsset(original.Asset.ID);
     LoadViewSettings(original.Asset.ID);
     IsError = false;
 }
        public void ProcessAssetChanges(TVAssetViewModel original, AssetModel asset)
        {
            if (asset.Category != null)
            {
                original.Asset.Category = asset.Category;
            }

            original.Asset.Label = MakeLabel(asset.AssetAreaID, asset.AssetGroupID, asset.LabelID);
        }
        private void GetAllSubItems(TVAssetViewModel tvi)
        {
            FullyObservableCollection <AssetModel> assets = GetChildAssets(tvi.Asset.ID);

            foreach (AssetModel tvasset in assets)
            {
                TVAssetViewModel asset = new TVAssetViewModel(tvasset, tvi);
                tvi.Children.Add(asset);
                GetAllSubItems(asset);
            }
        }
 private void ExecuteMoveAsset(object parameter)
 {
     if (parameter.GetType().Equals(typeof(TVAssetViewModel)))
     {
         if (parameter is TVAssetViewModel asset)
         {
             movingasset      = asset;
             asset.IsSelected = true;
             movingassetlabel = "asset.Asset.Label";
         }
     }
 }
 private void ExecuteDeleteAsset(object parameter)
 {
     if (parameter.GetType().Equals(typeof(TVAssetViewModel)))
     {
         TVAssetViewModel   asset = parameter as TVAssetViewModel;
         IMessageBoxService msg   = new MessageBoxService();
         if (msg.ShowMessage("Are you sure you want to delete this Asset: " + "asset.Asset.Label" + " and any associated child Assets?", "Removing Asset: " + "asset.Asset.Label", GenericMessageBoxButton.YesNo, GenericMessageBoxIcon.Question).Equals(GenericMessageBoxResult.Yes))
         {
             //change to delete all children too and set parentid to 0 .
             DeleteAssetDFS(asset);
         }
     }
 }
        public static void AddAssetNode(AssetModel asset)
        {
            TVCustomerViewModel customer;
            TVAssetViewModel    parentnode = null;

            //determine if asset is in tree
            //if found then update via bindings
            //else add to tree

            if (asset.ParentAssetID == 0) //customer is parent
            {
                //get customer node
                customer = GetCustomerNode(asset.CustomerID);
                //look for asset under this customer
                if (GetCustomerAsset(customer, asset.ID) == null) //not found
                {
                    //new Nov 2016 - added tvasset.Parent = customer;
                    TVAssetViewModel tvasset = new TVAssetViewModel(asset, null)
                    {
                        Parent = customer
                    };
                    customer.Children.Add(tvasset);

                    //add to customer node
                    // customer.Children.Add(new TVAssetViewModel(asset, null));
                }
            }
            else //asset is parent
            {
                //get customer node
                customer = GetCustomerNode(asset.CustomerID);
                //get parent node of asset
                parentnode = GetAssetDFS(customer, asset.ParentAssetID);

                //new Nov 2016 - added tvasset.Parent = parentnode;
                TVAssetViewModel tvasset = new TVAssetViewModel(asset, null)
                {
                    Parent = parentnode
                };
                parentnode.Children.Add(tvasset);

                //        parentnode.Children.Add(new TVAssetViewModel(asset, null));
            }
            LogMovement(ActivityType.NewAsset, asset.ID, 0, asset.CustomerID);
        }
        /// <summary>
        /// Create list of child nodes that cannot be dropped onto.
        /// </summary>
        /// <param name="tvasset"></param>
        /// <returns></returns>
        public static Collection <int> GetChildIDs(TVAssetViewModel tvasset)
        {
            ChildNodeIDs.Clear();
            var stack = new Stack <TVAssetViewModel>();

            stack.Push(tvasset);
            while (stack.Count != 0)
            {
                TVAssetViewModel current = stack.Pop();
                //add current asset id to collecton
                ChildNodeIDs.Add(current.Asset.ID);

                foreach (TVAssetViewModel child in (current.Children).Reverse())
                {
                    stack.Push(child);
                }
            }
            return(null);
        }
        private void GetAllItems()
        {
            AllItems?.Clear();
            FullyObservableCollection <TVCustomerModel> customers = GetTVCustomers();

            foreach (TVCustomerModel cm in customers)
            {
                //add customer to tree
                TreeViewItemViewModel customer = new TVCustomerViewModel(cm);

                FullyObservableCollection <AssetModel> assets = GetCustomerChildAssets(cm.ID);
                foreach (AssetModel am in assets)
                {
                    TVAssetViewModel asset = new TVAssetViewModel(am, null);
                    GetAllSubItems(asset);
                    customer.Children.Add(asset);
                }
                AllItems.Add(customer);
            }
        }
        /// <summary>
        /// Delete asset matching assetid. Child assets are also deleted
        /// </summary>
        /// <param name="assetid">Current Asset ID</param>
        /// <param name="assetparentid">Asset parent ID</param>
        /// <param name="customerid">Asset customer ID</param>
        public static void DeleteAssetDFS(TVAssetViewModel tvasset)
        {
            Window owner              = Application.Current.Windows[0];
            TVCustomerViewModel cm    = GetCustomerNode(tvasset.Asset.CustomerID);
            TVAssetViewModel    asset = null;

            asset = GetAssetDFS(cm, tvasset.Asset.ID);
            TVAssetViewModel assetparent = null;

            assetparent = GetAssetDFS(cm, tvasset.Asset.ParentAssetID);

            if (asset != null)
            {
                if (assetparent != null)
                {
                    assetparent.Children.Remove(asset);
                }
                else
                {
                    cm.Children.Remove(asset);
                }

                SetParentAssetID(tvasset.Asset.ID, 0);

                var stack = new Stack <TVAssetViewModel>();
                stack.Push(asset);
                while (stack.Count != 0)
                {
                    TVAssetViewModel current = stack.Pop();
                    DeleteAsset((current).Asset);
                    CloseRelatedWindow(current.Asset.ID, owner);
                    LogMovement(ActivityType.Deleted, current.Asset.ID, tvasset.Asset.CustomerID, 0);

                    foreach (TVAssetViewModel child in (current.Children).Reverse())
                    {
                        stack.Push(child);
                    }
                }
            }
        }
        /// <summary>
        /// Non-recursive option
        /// </summary>
        /// <param name="theseNodes"></param>
        public void ResetFilteredAssets(ObservableCollection <TreeViewItemViewModel> theseNodes)
        {
            var stack = new Stack <TVAssetViewModel>();

            foreach (TVAssetViewModel tv in theseNodes)
            {
                stack.Push(tv);
            }
            while (stack.Count != 0)
            {
                TVAssetViewModel current = stack.Pop();
                current.IsFiltered = false;
                current.IsExpanded = false;
                if (current.Parent != null)
                {
                    current.Parent.IsExpanded = false;
                }
                foreach (TVAssetViewModel child in (current.Children).Reverse())
                {
                    stack.Push(child);
                }
            }
        }
        /// <summary>
        /// Depth first search of asset tree for the given node
        /// If found the node is returned as a static variable
        /// else null
        /// </summary>
        /// This avoids recursion and finds the first occurrence of the target node
        /// in the case where there could be multiple instances or circular references.
        /// Need to be careful about size of stack. Data tree shape might prefer BFS instead.
        /// Loop should run faster than recursion
        /// <returns></returns>
        public static TVAssetViewModel GetAssetDFS(TVCustomerViewModel cv, int targetid)
        {
            var stack = new Stack <TVAssetViewModel>();

            foreach (TVAssetViewModel tv in cv.Children)
            {
                stack.Push(tv);
            }
            while (stack.Count != 0)
            {
                TVAssetViewModel current = stack.Pop();
                //assign current node to found asset
                if (current.Asset.ID == targetid)
                {
                    return(current);
                }

                foreach (TVAssetViewModel child in (current.Children).Reverse())
                {
                    stack.Push(child);
                }
            }
            return(null);
        }
        private void ExecuteAddNewAssetCM(object parameter)
        {
            IMessageBoxService msg = new MessageBoxService();

            if (parameter.GetType().Equals(typeof(TVAssetViewModel)))
            {
                TVAssetViewModel asset  = parameter as TVAssetViewModel;
                AssetModel       result = msg.OpenAssetDlg(asset.Asset.CustomerID, asset.Asset.ID);
                if (result != null)
                {
                    AddAssetNode(result);
                }
            }
            else
            if (parameter.GetType().Equals(typeof(TVCustomerViewModel)))
            {
                TVCustomerViewModel customer = parameter as TVCustomerViewModel;
                AssetModel          result   = msg.OpenAssetDlg(customer.Customer.ID, 0);
                if (result != null)
                {
                    AddAssetNode(result);
                }
            }
        }
 public TVAssetViewModel(TVAssetViewModel _parentasset) : base(_parentasset, true)
 {
     Asset = _parentasset.Asset;
 }