@@ -886,6 +886,55 @@ public Map<Long, List<Long>> findReferencedPagesByItems(String siteId) {
886886 return pageToReferencedPages ;
887887 }
888888
889+ /**
890+ * Builds a parent map from subpage references, considering only pages in pageMap (for selective import).
891+ *
892+ * @param subpageRefs Map of parent page IDs to lists of child page IDs
893+ * @param pageMap Map of old page IDs to new page IDs (determines which pages are being imported)
894+ * @param calculatedParentMap Output map to populate with child -> parent relationships
895+ */
896+ Map <Long , Long > buildParentMapFromReferences (Map <Long , List <Long >> subpageRefs , Map <Long , Long > pageMap , Map <Long , Long > calculatedParentMap ) {
897+ for (Map .Entry <Long , List <Long >> entry : subpageRefs .entrySet ()) {
898+ Long oldParentPageId = entry .getKey ();
899+ List <Long > oldChildPageIds = entry .getValue ();
900+
901+ if (!pageMap .containsKey (oldParentPageId )) continue ;
902+
903+ for (Long oldChildPageId : oldChildPageIds ) {
904+ if (pageMap .containsKey (oldChildPageId )) {
905+ calculatedParentMap .put (oldChildPageId , oldParentPageId );
906+ }
907+ }
908+ }
909+ return calculatedParentMap ;
910+ }
911+
912+ /**
913+ * Calculates top parent relationships by walking up the parent chain.
914+ *
915+ * @param calculatedParentMap Map of child -> parent relationships
916+ * @param calculatedTopParentMap Output map to populate with page -> top parent relationships
917+ */
918+ Map <Long , Long > calculateTopParentMap (Map <Long , Long > calculatedParentMap , Map <Long , Long > calculatedTopParentMap ) {
919+ for (Long pageId : calculatedParentMap .keySet ()) {
920+ Long currentPageId = pageId ;
921+ Long topParent = null ;
922+ Set <Long > visited = new HashSet <>();
923+ while (calculatedParentMap .containsKey (currentPageId )) {
924+ if (!visited .add (currentPageId )) {
925+ log .warn ("Cycle detected in page hierarchy at page {}" , currentPageId );
926+ break ;
927+ }
928+ topParent = calculatedParentMap .get (currentPageId );
929+ currentPageId = topParent ;
930+ }
931+ if (topParent != null ) {
932+ calculatedTopParentMap .put (pageId , topParent );
933+ }
934+ }
935+ return calculatedTopParentMap ;
936+ }
937+
889938 /**
890939 * Finds the top-level pages that should become new Lessons from the selected pages
891940 * Only considers pages that were originally selected by the user, not those added automatically as references
@@ -1914,18 +1963,7 @@ public String mergeInternal(String siteId, Element root, String archivePath, Str
19141963 Map <Long , List <Long >> subpageRefs = findReferencedPagesByItems (fromSiteId );
19151964
19161965 // Build parent map from subpage references
1917- for (Map .Entry <Long , List <Long >> entry : subpageRefs .entrySet ()) {
1918- Long oldParentPageId = entry .getKey ();
1919- List <Long > oldChildPageIds = entry .getValue ();
1920-
1921- if (!pageMap .containsKey (oldParentPageId )) continue ;
1922-
1923- for (Long oldChildPageId : oldChildPageIds ) {
1924- if (pageMap .containsKey (oldChildPageId )) {
1925- calculatedParentMap .put (oldChildPageId , oldParentPageId );
1926- }
1927- }
1928- }
1966+ buildParentMapFromReferences (subpageRefs , pageMap , calculatedParentMap );
19291967
19301968 // For cross-server imports, fall back to XML attributes if subpageRefs is empty
19311969 if (!isSameServer && subpageRefs .isEmpty ()) {
@@ -1945,22 +1983,7 @@ public String mergeInternal(String siteId, Element root, String archivePath, Str
19451983 }
19461984
19471985 // Calculate top parents by walking up the tree
1948- for (Long pageId : calculatedParentMap .keySet ()) {
1949- Long currentPageId = pageId ;
1950- Long topParent = null ;
1951- Set <Long > visited = new HashSet <>();
1952- while (calculatedParentMap .containsKey (currentPageId )) {
1953- if (!visited .add (currentPageId )) {
1954- log .warn ("Cycle detected in page hierarchy at page {}" , currentPageId );
1955- break ;
1956- }
1957- topParent = calculatedParentMap .get (currentPageId );
1958- currentPageId = topParent ;
1959- }
1960- if (topParent != null ) {
1961- calculatedTopParentMap .put (pageId , topParent );
1962- }
1963- }
1986+ calculateTopParentMap (calculatedParentMap , calculatedTopParentMap );
19641987
19651988 // Apply calculated relationships to imported pages
19661989 int hierarchyUpdates = 0 ;
0 commit comments