11package my .bookshop .handlers ;
22
3+ import static cds .gen .adminservice .AdminService_ .GENRE_HIERARCHY ;
4+
35import java .util .List ;
4- import java .util .Optional ;
56
67import org .springframework .context .annotation .Profile ;
78import org .springframework .stereotype .Component ;
89
10+ import com .sap .cds .ql .CQL ;
911import com .sap .cds .ql .Select ;
1012import com .sap .cds .ql .Update ;
11- import com .sap .cds .ql .cqn .CqnAnalyzer ;
12- import com .sap .cds .ql .cqn .CqnSelect ;
13-
13+ import com .sap .cds .ql .cqn .CqnStructuredTypeRef ;
1414import com .sap .cds .services .handler .EventHandler ;
1515import com .sap .cds .services .handler .annotations .On ;
1616import com .sap .cds .services .handler .annotations .ServiceName ;
@@ -36,39 +36,40 @@ public class HierarchySiblingActionHandler implements EventHandler {
3636 }
3737
3838 @ On (entity = GenreHierarchy_ .CDS_NAME )
39- void onMoveSiblingAction (GenreHierarchyMoveSiblingContext event ) {
40- CqnSelect select = event .getCqn ();
41- // Get ID of the entry that is being moved
42- String idToMove = (String ) CqnAnalyzer .create (event .getModel ()).analyze (select ).targetKeys ()
43- .get (GenreHierarchy_ .ID );
44- // Find its' parent
45- String parentId = db .run (Select .from (GenreHierarchy_ .class )
46- .columns (c -> c .parent_ID ()).where (c -> c .ID ().eq (idToMove )))
47- .single (GenreHierarchy .class ).getParentId ();
39+ void onMoveSiblingAction (CqnStructuredTypeRef ref , GenreHierarchyMoveSiblingContext context ) {
40+ // Find current node and its parent
41+ GenreHierarchy toMove = db .run (Select .from (CQL .entity (GENRE_HIERARCHY , ref ))
42+ .columns (c -> c .ID (), c -> c .parent_ID ()))
43+ .single (GenreHierarchy .class );
44+
4845 // Find all children of the parent, which are siblings of the entry being moved
49- List <GenreHierarchy > siblingNodes = db .run (Select .from (GenreHierarchy_ . class )
46+ List <GenreHierarchy > siblingNodes = db .run (Select .from (GENRE_HIERARCHY )
5047 .columns (c -> c .ID (), c -> c .siblingRank ())
51- .where (c -> c .parent_ID ().eq (parentId )))
48+ .where (c -> c .parent_ID ().eq (toMove . getParentId () )))
5249 .listOf (GenreHierarchy .class );
5350
54- String nextSiblingId = event .getNextSibling () == null ? null : event .getNextSibling ().getId ();
55- Optional <GenreHierarchy > nextSibling = siblingNodes .stream ().filter (el -> el .getId ().equals (nextSiblingId ))
56- .findFirst ();
51+ int oldPosition = 0 ;
52+ int newPosition = siblingNodes .size ();
53+ for (int i = 0 ; i < siblingNodes .size (); ++i ) {
54+ GenreHierarchy sibling = siblingNodes .get (i );
55+ if (sibling .getId ().equals (toMove .getId ())) {
56+ oldPosition = i ;
57+ }
58+ if (context .getNextSibling () != null && sibling .getId ().equals (context .getNextSibling ().getId ())) {
59+ newPosition = i ;
60+ }
61+ }
62+
63+ // Move siblings
64+ siblingNodes .add (oldPosition < newPosition ? newPosition - 1 : newPosition , siblingNodes .remove (oldPosition ));
5765
58- GenreHierarchy nodeToMove = siblingNodes .stream ().filter (el -> idToMove .equals (el .getId ())).findFirst ().get ();
59- GenreHierarchy moved = siblingNodes .remove (siblingNodes .indexOf (nodeToMove ));
60- // Exchange siblings
61- nextSibling .ifPresentOrElse (n -> siblingNodes .add (siblingNodes .indexOf (n ), moved ),
62- () -> siblingNodes .addLast (moved ));
63-
64- // Apply ranks
65- int i = 0 ;
66- for (GenreHierarchy sibling : siblingNodes ) {
67- sibling .setSiblingRank (i ++);
66+ // Recalculate ranks
67+ for (int i = 0 ; i < siblingNodes .size (); ++i ) {
68+ siblingNodes .get (i ).setSiblingRank (i );
6869 }
6970
7071 // Update DB
71- db .run (Update .entity (GenreHierarchy_ . class ).entries (siblingNodes ));
72- event .setCompleted ();
72+ db .run (Update .entity (GENRE_HIERARCHY ).entries (siblingNodes ));
73+ context .setCompleted ();
7374 }
7475}
0 commit comments