Skip to content

Commit 1605d14

Browse files
committed
adapted model and custom expand
1 parent 1cc0602 commit 1605d14

File tree

10 files changed

+145
-43
lines changed

10 files changed

+145
-43
lines changed

db/books.cds

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ annotate Authors with
4949
* Hierarchically organized Code List for Genres
5050
*/
5151
entity Genres : sap.common.CodeList {
52-
key ID : Integer;
52+
key ID : UUID;
5353
parent : Association to Genres;
54-
children : Composition of many Genres
55-
on children.parent = $self;
5654
}

db/data/my.bookshop-Books.csv

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ID;TITLE;DESCR;AUTHOR_ID;STOCK;PRICE;CURRENCY_CODE;GENRE_ID;RATING;ISBN
2-
f846b0b9-01d4-4f6d-82a4-d79204f62278;Wuthering Heights;"Wuthering Heights, Emily Brontë's only novel, was published in 1847 under the pseudonym ""Ellis Bell"". It was written between October 1845 and June 1846. Wuthering Heights and Anne Brontë's Agnes Grey were accepted by publisher Thomas Newby before the success of their sister Charlotte's novel Jane Eyre. After Emily's death, Charlotte edited the manuscript of Wuthering Heights and arranged for the edited version to be published as a posthumous second edition in 1850.";335c7bcd-b826-4f14-a788-e0bf6738617a;12;11.11;GBP;103;4.5;979-8698267973
3-
9b084139-0b1e-43b6-b12a-7b3669d75f02;Jane Eyre;"Jane Eyre /ɛər/ (originally published as Jane Eyre: An Autobiography) is a novel by English writer Charlotte Brontë, published under the pen name ""Currer Bell"", on 16 October 1847, by Smith, Elder & Co. of London. The first American edition was published the following year by Harper & Brothers of New York. Primarily a bildungsroman, Jane Eyre follows the experiences of its eponymous heroine, including her growth to adulthood and her love for Mr. Rochester, the brooding master of Thornfield Hall. The novel revolutionised prose fiction in that the focus on Jane's moral and spiritual development is told through an intimate, first-person narrative, where actions and events are coloured by a psychological intensity. The book contains elements of social criticism, with a strong sense of Christian morality at its core and is considered by many to be ahead of its time because of Jane's individualistic character and how the novel approaches the topics of class, sexuality, religion and feminism.";e3da2c2e-72ee-45d5-8def-52964c7b252a;11;12.34;GBP;103;3.0;979-8598716472
4-
51061ce3-ddde-4d70-a2dc-6314afbcc73e;The Raven;"“The Raven"" is a narrative poem by American writer Edgar Allan Poe. First published in January 1845, the poem is often noted for its musicality, stylized language, and supernatural atmosphere. It tells of a talking raven's mysterious visit to a distraught lover, tracing the man's slow fall into madness. The lover, often identified as being a student, is lamenting the loss of his love, Lenore. Sitting on a bust of Pallas, the raven seems to further distress the protagonist with its constant repetition of the word ""Nevermore"". The poem makes use of folk, mythological, religious, and classical references.";e7643aae-2d2f-4656-bb2d-1328ad3c8045;333;13.13;USD;117;2.5;978-1092909747
5-
aebdfc8a-0dfa-4468-bd36-48aabd65e663;Eleonora;"""Eleonora"" is a short story by Edgar Allan Poe, first published in 1842 in Philadelphia in the literary annual The Gift. It is often regarded as somewhat autobiographical and has a relatively ""happy"" ending.";e7643aae-2d2f-4656-bb2d-1328ad3c8045;555;14;USD;117;1.0;979-8669820985
6-
4a519e61-3c3a-4bd9-ab12-d7e0c5329933;Catweazle;Catweazle is a British fantasy television series, starring Geoffrey Bayldon in the title role, and created by Richard Carpenter for London Weekend Television. The first series, produced and directed by Quentin Lawrence, was screened in the UK on ITV in 1970. The second series, directed by David Reid and David Lane, was shown in 1971. Each series had thirteen episodes, most but not all written by Carpenter, who also published two books based on the scripts.;3c081d9d-abda-4da9-8b6a-4f4555bb26bc;22;15;EUR;110;4.0;978-3473523023
2+
f846b0b9-01d4-4f6d-82a4-d79204f62278;Wuthering Heights;"Wuthering Heights, Emily Brontë's only novel, was published in 1847 under the pseudonym ""Ellis Bell"". It was written between October 1845 and June 1846. Wuthering Heights and Anne Brontë's Agnes Grey were accepted by publisher Thomas Newby before the success of their sister Charlotte's novel Jane Eyre. After Emily's death, Charlotte edited the manuscript of Wuthering Heights and arranged for the edited version to be published as a posthumous second edition in 1850.";335c7bcd-b826-4f14-a788-e0bf6738617a;12;11.11;GBP;f846b0b9-01d4-4f6d-82a4-d79204f62369;4.5;979-8698267973
3+
9b084139-0b1e-43b6-b12a-7b3669d75f02;Jane Eyre;"Jane Eyre /ɛər/ (originally published as Jane Eyre: An Autobiography) is a novel by English writer Charlotte Brontë, published under the pen name ""Currer Bell"", on 16 October 1847, by Smith, Elder & Co. of London. The first American edition was published the following year by Harper & Brothers of New York. Primarily a bildungsroman, Jane Eyre follows the experiences of its eponymous heroine, including her growth to adulthood and her love for Mr. Rochester, the brooding master of Thornfield Hall. The novel revolutionised prose fiction in that the focus on Jane's moral and spiritual development is told through an intimate, first-person narrative, where actions and events are coloured by a psychological intensity. The book contains elements of social criticism, with a strong sense of Christian morality at its core and is considered by many to be ahead of its time because of Jane's individualistic character and how the novel approaches the topics of class, sexuality, religion and feminism.";e3da2c2e-72ee-45d5-8def-52964c7b252a;11;12.34;GBP;f846b0b9-01d4-4f6d-82a4-d79204f62571;3.0;979-8598716472
4+
51061ce3-ddde-4d70-a2dc-6314afbcc73e;The Raven;"“The Raven"" is a narrative poem by American writer Edgar Allan Poe. First published in January 1845, the poem is often noted for its musicality, stylized language, and supernatural atmosphere. It tells of a talking raven's mysterious visit to a distraught lover, tracing the man's slow fall into madness. The lover, often identified as being a student, is lamenting the loss of his love, Lenore. Sitting on a bust of Pallas, the raven seems to further distress the protagonist with its constant repetition of the word ""Nevermore"". The poem makes use of folk, mythological, religious, and classical references.";e7643aae-2d2f-4656-bb2d-1328ad3c8045;333;13.13;USD;d846b0b9-01d4-4f6d-82a4-d79204f62487;2.5;978-1092909747
5+
aebdfc8a-0dfa-4468-bd36-48aabd65e663;Eleonora;"""Eleonora"" is a short story by Edgar Allan Poe, first published in 1842 in Philadelphia in the literary annual The Gift. It is often regarded as somewhat autobiographical and has a relatively ""happy"" ending.";e7643aae-2d2f-4656-bb2d-1328ad3c8045;555;14;USD;d846b0b9-01d4-4f6d-82a4-d79204f62590;1.0;979-8669820985
6+
4a519e61-3c3a-4bd9-ab12-d7e0c5329933;Catweazle;Catweazle is a British fantasy television series, starring Geoffrey Bayldon in the title role, and created by Richard Carpenter for London Weekend Television. The first series, produced and directed by Quentin Lawrence, was screened in the UK on ITV in 1970. The second series, directed by David Reid and David Lane, was shown in 1971. Each series had thirteen episodes, most but not all written by Carpenter, who also published two books based on the scripts.;3c081d9d-abda-4da9-8b6a-4f4555bb26bc;22;15;EUR;d846b0b9-01d4-4f6d-82a4-d79204f62590;4.0;978-3473523023

db/data/my.bookshop-Genres.csv

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,18 @@
11
ID;parent_ID;name
2-
100;;Fiction
3-
101;100;Action
4-
102;100;Adventure
5-
103;100;Drama
6-
105;100;Poetry
7-
106;100;Science Fiction
8-
107;106;Utopian and Dystopian
9-
108;107;Dystopia
10-
109;108;Cyberpunk
11-
110;109;Steampunk
12-
104;100;Fantasy
13-
111;104;Epic fantasy
14-
112;104;High fantasy
15-
113;100;Graphic Novel
16-
115;100;Short Story
17-
116;100;Romance
18-
117;100;Mystery
19-
123;117;Thriller
20-
124;117;Suspense
21-
118;100;Horror
22-
119;100;Historical Fiction
23-
120;100;Contemporary Fiction
24-
121;100;Magical Realism
25-
122;100;Literary Fiction
26-
128;100;Fairy Tale
27-
200;;Non-Fiction
28-
201;200;Biography
29-
202;201;Autobiography
30-
203;200;Essay
31-
204;200;Speech
2+
f846b0b9-01d4-4f6d-82a4-d79204f62369;;Fiction
3+
f846b0b9-01d4-4f6d-82a4-d79204f62570;f846b0b9-01d4-4f6d-82a4-d79204f62369;Drama
4+
f846b0b9-01d4-4f6d-82a4-d79204f62571;f846b0b9-01d4-4f6d-82a4-d79204f62369;Poetry
5+
f846b0b9-01d4-4f6d-82a4-d79204f62572;f846b0b9-01d4-4f6d-82a4-d79204f62369;Fantasy
6+
f846b0b9-01d4-4f6d-82a4-d79204f62592;f846b0b9-01d4-4f6d-82a4-d79204f62572;Epic fantasy
7+
f846b0b9-01d4-4f6d-82a4-d79204f62593;f846b0b9-01d4-4f6d-82a4-d79204f62572;High fantasy
8+
f846b0b9-01d4-4f6d-82a4-d79204f62573;f846b0b9-01d4-4f6d-82a4-d79204f62369;Science Fiction
9+
f846b0b9-01d4-4f6d-82a4-d79204f62574;f846b0b9-01d4-4f6d-82a4-d79204f62369;Romance
10+
f846b0b9-01d4-4f6d-82a4-d79204f62575;f846b0b9-01d4-4f6d-82a4-d79204f62369;Mystery
11+
f846b0b9-01d4-4f6d-82a4-d79204f62576;f846b0b9-01d4-4f6d-82a4-d79204f62369;Thriller
12+
f846b0b9-01d4-4f6d-82a4-d79204f62577;f846b0b9-01d4-4f6d-82a4-d79204f62369;Dystopia
13+
f846b0b9-01d4-4f6d-82a4-d79204f62578;f846b0b9-01d4-4f6d-82a4-d79204f62369;Fairy Tale
14+
d846b0b9-01d4-4f6d-82a4-d79204f62487;;Non-Fiction
15+
d846b0b9-01d4-4f6d-82a4-d79204f62588;d846b0b9-01d4-4f6d-82a4-d79204f62487;Biography
16+
d846b0b9-01d4-4f6d-82a4-d79204f62589;d846b0b9-01d4-4f6d-82a4-d79204f62588;Autobiography
17+
d846b0b9-01d4-4f6d-82a4-d79204f62590;d846b0b9-01d4-4f6d-82a4-d79204f62487;Essay
18+
d846b0b9-01d4-4f6d-82a4-d79204f62591;d846b0b9-01d4-4f6d-82a4-d79204f62487;Speech

srv/admin-service.cds

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ annotate AdminService.Books with @cds.search: {
4545
// Enable Fiori Draft for Orders
4646
annotate AdminService.Orders with @odata.draft.enabled;
4747
annotate AdminService.Books with @odata.draft.enabled;
48+
annotate AdminService.GenreHierarchy with @odata.draft.enabled;
4849

4950
// workaround to enable the value help for languages
5051
// Necessary because auto exposure is currently not working

srv/src/main/java/my/bookshop/handlers/AdminServiceHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public void addBooksViaCsv(CdsUpdateEventContext context, Upload upload) {
284284
book.setStock(Integer.valueOf(p[4]).intValue());
285285
book.setPrice(BigDecimal.valueOf(Double.valueOf(p[5])));
286286
book.setCurrencyCode(p[6]);
287-
book.setGenreId(Integer.valueOf(p[7]));
287+
book.setGenreId(String.valueOf(p[7]));
288288

289289
// separate transaction per line
290290
context.getCdsRuntime().changeSetContext().run(ctx -> {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package my.bookshop.handlers;
2+
3+
import java.util.List;
4+
5+
import org.springframework.context.annotation.Profile;
6+
import org.springframework.stereotype.Component;
7+
8+
import com.sap.cds.ql.CQL;
9+
import com.sap.cds.ql.cqn.CqnSelect;
10+
import com.sap.cds.ql.cqn.CqnSelectListItem;
11+
import com.sap.cds.ql.cqn.Modifier;
12+
import com.sap.cds.ql.cqn.transformation.CqnAncestorsTransformation;
13+
import com.sap.cds.ql.cqn.transformation.CqnTransformation;
14+
import com.sap.cds.services.cds.CdsReadEventContext;
15+
16+
import com.sap.cds.services.handler.EventHandler;
17+
import com.sap.cds.services.handler.annotations.On;
18+
import com.sap.cds.services.handler.annotations.ServiceName;
19+
20+
import cds.gen.adminservice.AdminService_;
21+
import cds.gen.adminservice.GenreHierarchy_;
22+
23+
24+
@Component
25+
@ServiceName(AdminService_.CDS_NAME)
26+
/**
27+
* For testing purposes of modifying requests for hierarchies
28+
*/
29+
@Profile("hybrid")
30+
public class HierarchyExpandHandler implements EventHandler {
31+
32+
@On(entity = GenreHierarchy_.CDS_NAME)
33+
void removeExpand(CdsReadEventContext event) {
34+
List<CqnTransformation> trafos = event.getCqn().transformations();
35+
if (trafos.size() < 1) {
36+
return;
37+
}
38+
if (trafos.get(0) instanceof CqnAncestorsTransformation) {
39+
CqnSelect original = event.getCqn();
40+
CqnSelect copy = CQL.copy(original, new Modifier() {
41+
public List<CqnSelectListItem> items(List<CqnSelectListItem> items) {
42+
return items.stream().filter(i -> !i.isExpand()).toList();
43+
}
44+
});
45+
event.setCqn(copy);
46+
}
47+
event.proceed();
48+
}
49+
}

srv/src/main/java/my/bookshop/handlers/HierarchyHandler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package my.bookshop.handlers;
1+
/* package my.bookshop.handlers;
22
33
import java.util.ArrayDeque;
44
import java.util.Comparator;
@@ -54,6 +54,7 @@
5454
* The handler is neither functionally complete nor correct for all requests. It
5555
* is not intended as a blue-print for custom code.
5656
*/
57+
/*
5758
public class HierarchyHandler implements EventHandler {
5859
5960
private final PersistenceService db;
@@ -311,3 +312,4 @@ Deque<String> getPath(GenreHierarchy gh) {
311312
}
312313
}
313314
}
315+
*/

srv/src/test/java/my/bookshop/GenreHierarchyTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package my.bookshop;
1+
/* package my.bookshop;
22
33
import static org.assertj.core.api.Assumptions.assumeThat;
44
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@@ -237,4 +237,4 @@ void testStartTwoLevelsOrderByDescHANA() throws Exception {
237237
private boolean isOnHana() {
238238
return env.acceptsProfiles(Profiles.of("cloud"));
239239
}
240-
}
240+
} */

srv/src/test/java/my/bookshop/handlers/HierarchyHandlerSorterTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package my.bookshop.handlers;
1+
/* package my.bookshop.handlers;
22
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import java.util.List;
@@ -68,3 +68,4 @@ private static List<GenreHierarchy> sorted(GenreHierarchy... h) {
6868
return List.of(h).stream().sorted(new Sorter()).toList();
6969
}
7070
}
71+
*/
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* package my.bookshop.handlers;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import java.util.List;
5+
import org.junit.jupiter.api.Test;
6+
7+
import cds.gen.adminservice.GenreHierarchy;
8+
import my.bookshop.handlers.HierarchyHandler.Sorter;
9+
10+
public class HierarchyHandlerTest {
11+
12+
@Test
13+
public void testSortingRoots() {
14+
GenreHierarchy h1 = GenreHierarchy.create();
15+
h1.setName("Philosophical fiction");
16+
GenreHierarchy h2 = GenreHierarchy.create();
17+
h2.setName("Epic");
18+
List<GenreHierarchy> list = List.of(h1,h2);
19+
List<GenreHierarchy> sorted = list.stream().sorted(new Sorter()).toList();
20+
assertEquals("Epic", sorted.get(0).getName());
21+
}
22+
23+
@Test
24+
public void testSortingChildren() {
25+
GenreHierarchy root = GenreHierarchy.create();
26+
root.setName("Folklore");
27+
GenreHierarchy h1 = GenreHierarchy.create();
28+
h1.setName("Urban legend");
29+
h1.setParnt(root);
30+
GenreHierarchy h2 = GenreHierarchy.create();
31+
h2.setName("Fairy tale");
32+
h2.setParnt(root);
33+
List<GenreHierarchy> list = List.of(h1,h2);
34+
List<GenreHierarchy> sorted = list.stream().sorted(new Sorter()).toList();
35+
assertEquals("Fairy tale", sorted.get(0).getName());
36+
}
37+
38+
@Test
39+
public void testSortingChildrenWithDifRoot() {
40+
GenreHierarchy root1 = GenreHierarchy.create();
41+
root1.setName("Thriller");
42+
GenreHierarchy root2 = GenreHierarchy.create();
43+
root2.setName("Folklore");
44+
GenreHierarchy h2 = GenreHierarchy.create();
45+
h2.setName("Urban legend");
46+
h2.setParnt(root2);
47+
List<GenreHierarchy> list = List.of(root1,h2);
48+
List<GenreHierarchy> sorted = list.stream().sorted(new Sorter()).toList();
49+
assertEquals("Urban legend", sorted.get(0).getName());
50+
}
51+
52+
@Test
53+
public void testSortingChildrenSameRoot() {
54+
GenreHierarchy root = GenreHierarchy.create();
55+
root.setName("Folklore");
56+
GenreHierarchy h1 = GenreHierarchy.create();
57+
h1.setName("Urban legend");
58+
h1.setParnt(root);
59+
List<GenreHierarchy> list = List.of(h1,root);
60+
List<GenreHierarchy> sorted = list.stream().sorted(new Sorter()).toList();
61+
assertEquals("Folklore", sorted.get(0).getName());
62+
}
63+
}
64+
*/

0 commit comments

Comments
 (0)