@@ -123,7 +123,7 @@ class ModelImporter(private val root: INode) {
123
123
124
124
private fun syncAllChildOrders (root : INode , rootData : NodeData ) {
125
125
syncChildOrder(root, rootData)
126
- for ((node, data) in root.allChildren. zip( rootData.children) ) {
126
+ for ((node, data) in root.allChildren zip rootData.children) {
127
127
syncAllChildOrders(node, data)
128
128
}
129
129
}
@@ -133,15 +133,22 @@ class ModelImporter(private val root: INode) {
133
133
val specifiedChildren = nodeData.children
134
134
require(existingChildren.size == specifiedChildren.size)
135
135
136
- for ((actualNode, specifiedNode) in existingChildren zip specifiedChildren) {
137
- val actualId = actualNode.originalId()
138
- if (actualId != specifiedNode.originalId() ) {
139
- val targetIndex = specifiedChildren.indexOfFirst { actualId == it.originalId() }
140
- node.moveChild(actualNode.roleInParent, targetIndex, actualNode)
141
- }
136
+ val targetIndices = HashMap <String ?, Int >(nodeData.children.size)
137
+ for (specifiedChild in specifiedChildren) {
138
+ val index = specifiedChild.getIndexWithinRole(nodeData)
139
+ targetIndices[specifiedChild.originalId()] = index
140
+ }
141
+
142
+ for (child in existingChildren) {
143
+ val targetIndex = targetIndices[child.originalId()] ? : - 1
144
+ node.moveChild(child.roleInParent, targetIndex, child)
142
145
}
143
146
}
144
147
148
+ private fun NodeData.getIndexWithinRole (parent : NodeData ) : Int {
149
+ return parent.children.filter { it.role == this .role }.indexOf(this )
150
+ }
151
+
145
152
private fun INode.originalId (): String? {
146
153
return this .getPropertyValue(NodeData .idPropertyKey)
147
154
}
0 commit comments