Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Need some help with the algorithm
Message
 
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01484275
Message ID:
01484354
Views:
45
>Maybe I'm missing something but why do all this.
>If a node in the treeview changes just find the relevant record and update it accordingly ?
>

The data updates are done, no problem. But what about re-sequencing? In other words, if you insert a node in the middle or remove a node in the middle, you need to re-sequence nodes in that particular branch.

Now, the re-sequencing happens at the very end, when we're saving all the modifications made. The user could have done many modifications. So, by traversing the tree we're comparing the current order of each node with the SortOrder in the ItemTree table and adjust the Order if necessary.

My problem is, that this process does extra changes - I want to do only the necessary (minimum changes).

>>I have a tree view structure, which is stored in this table
>>
>>select node_id, parent_id, root_id, descrip, sort_order  from ItemTree order by sort_order -- table has more fields, of course
>>
>>Sort_Order is relevant to the parent_id.
>>
>>I have the following procedure which is called every time the changes of any type are made to the tree view:
>>
>>with thisform
>>	nChanges   = 0
>>	nSortOrder = 1
>>* Find a root node in the TreeView
>>	oTree = .TList2
>>	if oTree.Nodes.count > 0
>>		for i = 1 to oTree.Nodes.count
>>			oTmp = oTree.Nodes(i).parent
>>			if isnull(oTmp) then
>>				iTmpIndex = i
>>				exit
>>			endif
>>		next i
>>* Get the index of the root node that is at the top of the TreeView
>>		iIndex = oTree.Nodes(iTmpIndex).FirstSibling.index
>>		iTmpIndex = iIndex
>>
>>		lnSeekVal = val(substr(oTree.Nodes(iIndex).key, 3))
>>		select itemtree
>>		set order to node_id
>>		seek lnSeekVal
>>		if node_id == lnSeekVal and lnSeekVal <> 0
>>			if thisform.nBaseRoot_id >= 0
>>				nSortOrder = sort_order
>>			else
>>				if sort_order <> nSortOrder
>>					replace sort_order with nSortOrder
>>					nChanges = nChanges + 1
>>					.lUpdateItemTree = .t.
>>				endif
>>			endif
>>		else
>>			.log("Seek Failed on node id:" + oTree.Nodes(iIndex).key, program(), 1)
>>		endif
>>
>>* If the Node has Children call the sub that writes the children
>>		if oTree.Nodes(iIndex).Children > 0
>>			nChanges = nChanges + .SaveChild(iIndex)
>>		endif
>>
>>		do while iIndex <> oTree.Nodes(iTmpIndex).LastSibling.index
>>*Loop through all the root nodes
>>			nSortOrder = nSortOrder + 1
>>			lnSeekVal  = val(substr(oTree.Nodes(iIndex).next.key, 3))
>>			select itemtree
>>			seek lnSeekVal
>>			if node_id == lnSeekVal and lnSeekVal <> 0
>>				if sort_order <> nSortOrder
>>					replace sort_order with nSortOrder
>>					nChanges = nChanges + 1
>>					.lUpdateItemTree = .t.
>>				endif
>>			else
>>				.log("Seek Failed on node id:" + oTree.Nodes(iIndex).next.key, program(), 1)
>>			endif
>>			if oTree.Nodes(iIndex).next.Children > 0
>>				nChange = nChanges + .SaveChild(oTree.Nodes(iIndex).next.index)
>>			endif
>>			iIndex = oTree.Nodes(iIndex).next.index
>>		enddo
>>	endif
>>	.log("Total number of changed records in the Item tree: " + alltrim(transform(m.nChanges,'@999,999,999')), program(), 1)
>>endwith
>>
>>return nChanges
>>
>>
>>I found that even if I add a new item to the very bottom of one of the subtrees (and so I should not change more than 1 item), I still end up with the lots of changes.
>>
>>I'm wondering if this algorithm can be improved to minimize number of changes written in the database?
>>
>>Thanks a lot in advance for the suggestions.
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform