Hierarchical outlining with five numeric levels — how to insert sibling or child rows and adjust existing records?

49 Views Asked by At

I have a table with a "points" (or outlining) hierarchy: the fields are L1 L2 L3 L4 L5 (L = level)

For example:

1.0.0.0.0
1.1.0.0.0
1.1.1.0.0
1.1.2.0.0
1.2.0.0.0

If I want to insert a sibling at 1.1.1.0.0 I should get a new row of 1.1.2.0.0 - and the already existing 1.1.2.0.0 should be adjusted up to 1.1.3.0.0, etc.

If I want to insert a child 1.1.1.0.0 I should get a new row of 1.1.1.1.0 with not adjustments required since there are no siblings existing at that level.

I have created procedural code for this - but it is turning into spaghetti - and I want to have an OOP solution with a class that handles these insertions and adjustments.

Can anyone please recommend even pseudo-code for handling these 2 types of insertions and required adjustments to already existing "rows"?

Any help or suggestions would be greatly appreciated!

1

There are 1 best solutions below

0
jdweng On BEST ANSWER

The people who gave you comments I don't think really understood the issue. You already have a table so using a LinkedList does not do anything more than a table. You really need to pass the method a row to insert and a field to insert. Just adding a new row with value 1.1.1.0.0 does not give enough information to renumber.

The code below I used a DataTable with each column a Field. To make code simply I'm assuming the indexes are integers. Code is not very complicated

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Outlining outling = new Outlining();
            outling.Add(2,2);
            outling.Add(0, 2);
            outling.Add(5, 2);
        }
    }
    public class Outlining
    {
        public DataTable dt = null;

        public Outlining()
        {
            dt = new DataTable();
            dt.Columns.Add("L1", typeof(int));
            dt.Columns.Add("L2", typeof(int));
            dt.Columns.Add("L3", typeof(int));
            dt.Columns.Add("L4", typeof(int));
            dt.Columns.Add("L5", typeof(int));

            dt.Rows.Add(new object[] { 1, 0, 0, 0, 0 });
            dt.Rows.Add(new object[] { 1, 1, 0, 0, 0 });
            dt.Rows.Add(new object[] { 1, 1, 1, 0, 0 });
            dt.Rows.Add(new object[] { 1, 2, 0, 0, 0 });
        }
        public void Add(int at, int level)
        {
            DataRow newRow = dt.Rows.Add();
            if (at < dt.Rows.Count - 1)
            {
                //move row if not last row
                dt.Rows.Remove(newRow);
                dt.Rows.InsertAt(newRow, at);
            }
            newRow.BeginEdit();
            newRow.ItemArray = dt.Rows[at + 1].ItemArray.Select(x => (object)x).ToArray();
            newRow.EndEdit();

            Renumber(at, level);
        }
        public void Renumber(int rowInsertIndex, int level)
        {
            for (int row = rowInsertIndex; row < dt.Rows.Count - 1; row++)
            {
                Boolean match = true;
                //check if columns to left still match, if no we are done
                for (int i = 0; i < level - 1; i++)
                {
                    if (dt.Rows[i][level] != dt.Rows[i + 1][level])
                    {
                        match = false;
                        break;
                    }
                }
                if (!match) break;
                dt.Rows[row + 1][level] = ((int)(dt.Rows[row + 1][level])) + 1;
            }
        }

    }
}