-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathHierarchySiblingActionHandler.java
More file actions
70 lines (59 loc) · 2.3 KB
/
Copy pathHierarchySiblingActionHandler.java
File metadata and controls
70 lines (59 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package my.bookshop.handlers;
import static cds.gen.adminservice.AdminService_.GENRE_HIERARCHY;
import cds.gen.adminservice.AdminService_;
import cds.gen.adminservice.GenreHierarchy;
import cds.gen.adminservice.GenreHierarchyMoveSiblingContext;
import cds.gen.adminservice.GenreHierarchy_;
import com.sap.cds.ql.Select;
import com.sap.cds.ql.Update;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.handler.annotations.On;
import com.sap.cds.services.handler.annotations.ServiceName;
import com.sap.cds.services.persistence.PersistenceService;
import java.util.List;
import org.springframework.stereotype.Component;
@Component
@ServiceName(AdminService_.CDS_NAME)
/** Example of a custom handler for nextSiblingAction */
public class HierarchySiblingActionHandler implements EventHandler {
private final PersistenceService db;
HierarchySiblingActionHandler(PersistenceService db) {
this.db = db;
}
@On
void onMoveSiblingAction(GenreHierarchy_ ref, GenreHierarchyMoveSiblingContext context) {
// Find current node and its parent
GenreHierarchy toMove =
db.run(Select.from(ref).columns(c -> c.ID(), c -> c.parent_ID())).single();
// Find all children of the parent, which are siblings of the entry being moved
List<GenreHierarchy> siblingNodes =
db.run(
Select.from(GENRE_HIERARCHY)
.columns(c -> c.ID(), c -> c.siblingRank())
.where(c -> c.parent_ID().eq(toMove.getParentId())))
.list();
int oldPosition = 0;
int newPosition = siblingNodes.size();
for (int i = 0; i < siblingNodes.size(); ++i) {
GenreHierarchy sibling = siblingNodes.get(i);
if (sibling.getId().equals(toMove.getId())) {
oldPosition = i;
}
if (context.getNextSibling() != null
&& sibling.getId().equals(context.getNextSibling().getId())) {
newPosition = i;
}
}
// Move siblings
siblingNodes.add(
oldPosition < newPosition ? newPosition - 1 : newPosition,
siblingNodes.remove(oldPosition));
// Recalculate ranks
for (int i = 0; i < siblingNodes.size(); ++i) {
siblingNodes.get(i).setSiblingRank(i);
}
// Update DB
db.run(Update.entity(GENRE_HIERARCHY).entries(siblingNodes));
context.setCompleted();
}
}