From 1605d14309d2fe9cd26f2c58d8db8386a6e659e2 Mon Sep 17 00:00:00 2001 From: D070615 Date: Thu, 16 Jan 2025 16:43:15 +0100 Subject: [PATCH] adapted model and custom expand --- db/books.cds | 4 +- db/data/my.bookshop-Books.csv | 10 +-- db/data/my.bookshop-Genres.csv | 47 +++++--------- srv/admin-service.cds | 1 + .../handlers/AdminServiceHandler.java | 2 +- .../handlers/HierarchyExpandHandler.java | 49 ++++++++++++++ .../bookshop/handlers/HierarchyHandler.java | 4 +- .../java/my/bookshop/GenreHierarchyTest.java | 4 +- .../handlers/HierarchyHandlerSorterTest.java | 3 +- .../handlers/HierarchyHandlerTest.java | 64 +++++++++++++++++++ 10 files changed, 145 insertions(+), 43 deletions(-) create mode 100644 srv/src/main/java/my/bookshop/handlers/HierarchyExpandHandler.java create mode 100644 srv/src/test/java/my/bookshop/handlers/HierarchyHandlerTest.java diff --git a/db/books.cds b/db/books.cds index e7aa3928..9a2bee75 100644 --- a/db/books.cds +++ b/db/books.cds @@ -49,8 +49,6 @@ annotate Authors with * Hierarchically organized Code List for Genres */ entity Genres : sap.common.CodeList { - key ID : Integer; + key ID : UUID; parent : Association to Genres; - children : Composition of many Genres - on children.parent = $self; } diff --git a/db/data/my.bookshop-Books.csv b/db/data/my.bookshop-Books.csv index e5453379..66fec8ad 100644 --- a/db/data/my.bookshop-Books.csv +++ b/db/data/my.bookshop-Books.csv @@ -1,6 +1,6 @@ ID;TITLE;DESCR;AUTHOR_ID;STOCK;PRICE;CURRENCY_CODE;GENRE_ID;RATING;ISBN -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 -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 -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 -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 -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 +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 +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 +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 +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 +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 diff --git a/db/data/my.bookshop-Genres.csv b/db/data/my.bookshop-Genres.csv index 84bb85b0..56e304e0 100644 --- a/db/data/my.bookshop-Genres.csv +++ b/db/data/my.bookshop-Genres.csv @@ -1,31 +1,18 @@ ID;parent_ID;name -100;;Fiction -101;100;Action -102;100;Adventure -103;100;Drama -105;100;Poetry -106;100;Science Fiction -107;106;Utopian and Dystopian -108;107;Dystopia -109;108;Cyberpunk -110;109;Steampunk -104;100;Fantasy -111;104;Epic fantasy -112;104;High fantasy -113;100;Graphic Novel -115;100;Short Story -116;100;Romance -117;100;Mystery -123;117;Thriller -124;117;Suspense -118;100;Horror -119;100;Historical Fiction -120;100;Contemporary Fiction -121;100;Magical Realism -122;100;Literary Fiction -128;100;Fairy Tale -200;;Non-Fiction -201;200;Biography -202;201;Autobiography -203;200;Essay -204;200;Speech +f846b0b9-01d4-4f6d-82a4-d79204f62369;;Fiction +f846b0b9-01d4-4f6d-82a4-d79204f62570;f846b0b9-01d4-4f6d-82a4-d79204f62369;Drama +f846b0b9-01d4-4f6d-82a4-d79204f62571;f846b0b9-01d4-4f6d-82a4-d79204f62369;Poetry +f846b0b9-01d4-4f6d-82a4-d79204f62572;f846b0b9-01d4-4f6d-82a4-d79204f62369;Fantasy +f846b0b9-01d4-4f6d-82a4-d79204f62592;f846b0b9-01d4-4f6d-82a4-d79204f62572;Epic fantasy +f846b0b9-01d4-4f6d-82a4-d79204f62593;f846b0b9-01d4-4f6d-82a4-d79204f62572;High fantasy +f846b0b9-01d4-4f6d-82a4-d79204f62573;f846b0b9-01d4-4f6d-82a4-d79204f62369;Science Fiction +f846b0b9-01d4-4f6d-82a4-d79204f62574;f846b0b9-01d4-4f6d-82a4-d79204f62369;Romance +f846b0b9-01d4-4f6d-82a4-d79204f62575;f846b0b9-01d4-4f6d-82a4-d79204f62369;Mystery +f846b0b9-01d4-4f6d-82a4-d79204f62576;f846b0b9-01d4-4f6d-82a4-d79204f62369;Thriller +f846b0b9-01d4-4f6d-82a4-d79204f62577;f846b0b9-01d4-4f6d-82a4-d79204f62369;Dystopia +f846b0b9-01d4-4f6d-82a4-d79204f62578;f846b0b9-01d4-4f6d-82a4-d79204f62369;Fairy Tale +d846b0b9-01d4-4f6d-82a4-d79204f62487;;Non-Fiction +d846b0b9-01d4-4f6d-82a4-d79204f62588;d846b0b9-01d4-4f6d-82a4-d79204f62487;Biography +d846b0b9-01d4-4f6d-82a4-d79204f62589;d846b0b9-01d4-4f6d-82a4-d79204f62588;Autobiography +d846b0b9-01d4-4f6d-82a4-d79204f62590;d846b0b9-01d4-4f6d-82a4-d79204f62487;Essay +d846b0b9-01d4-4f6d-82a4-d79204f62591;d846b0b9-01d4-4f6d-82a4-d79204f62487;Speech \ No newline at end of file diff --git a/srv/admin-service.cds b/srv/admin-service.cds index 4d3b93f3..eb705efa 100644 --- a/srv/admin-service.cds +++ b/srv/admin-service.cds @@ -45,6 +45,7 @@ annotate AdminService.Books with @cds.search: { // Enable Fiori Draft for Orders annotate AdminService.Orders with @odata.draft.enabled; annotate AdminService.Books with @odata.draft.enabled; +annotate AdminService.GenreHierarchy with @odata.draft.enabled; // workaround to enable the value help for languages // Necessary because auto exposure is currently not working diff --git a/srv/src/main/java/my/bookshop/handlers/AdminServiceHandler.java b/srv/src/main/java/my/bookshop/handlers/AdminServiceHandler.java index 06ec29c6..5a2f33d6 100644 --- a/srv/src/main/java/my/bookshop/handlers/AdminServiceHandler.java +++ b/srv/src/main/java/my/bookshop/handlers/AdminServiceHandler.java @@ -284,7 +284,7 @@ public void addBooksViaCsv(CdsUpdateEventContext context, Upload upload) { book.setStock(Integer.valueOf(p[4]).intValue()); book.setPrice(BigDecimal.valueOf(Double.valueOf(p[5]))); book.setCurrencyCode(p[6]); - book.setGenreId(Integer.valueOf(p[7])); + book.setGenreId(String.valueOf(p[7])); // separate transaction per line context.getCdsRuntime().changeSetContext().run(ctx -> { diff --git a/srv/src/main/java/my/bookshop/handlers/HierarchyExpandHandler.java b/srv/src/main/java/my/bookshop/handlers/HierarchyExpandHandler.java new file mode 100644 index 00000000..2daa240f --- /dev/null +++ b/srv/src/main/java/my/bookshop/handlers/HierarchyExpandHandler.java @@ -0,0 +1,49 @@ +package my.bookshop.handlers; + +import java.util.List; + +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; + +import com.sap.cds.ql.CQL; +import com.sap.cds.ql.cqn.CqnSelect; +import com.sap.cds.ql.cqn.CqnSelectListItem; +import com.sap.cds.ql.cqn.Modifier; +import com.sap.cds.ql.cqn.transformation.CqnAncestorsTransformation; +import com.sap.cds.ql.cqn.transformation.CqnTransformation; +import com.sap.cds.services.cds.CdsReadEventContext; + +import com.sap.cds.services.handler.EventHandler; +import com.sap.cds.services.handler.annotations.On; +import com.sap.cds.services.handler.annotations.ServiceName; + +import cds.gen.adminservice.AdminService_; +import cds.gen.adminservice.GenreHierarchy_; + + +@Component +@ServiceName(AdminService_.CDS_NAME) +/** + * For testing purposes of modifying requests for hierarchies + */ +@Profile("hybrid") +public class HierarchyExpandHandler implements EventHandler { + + @On(entity = GenreHierarchy_.CDS_NAME) + void removeExpand(CdsReadEventContext event) { + List trafos = event.getCqn().transformations(); + if (trafos.size() < 1) { + return; + } + if (trafos.get(0) instanceof CqnAncestorsTransformation) { + CqnSelect original = event.getCqn(); + CqnSelect copy = CQL.copy(original, new Modifier() { + public List items(List items) { + return items.stream().filter(i -> !i.isExpand()).toList(); + } + }); + event.setCqn(copy); + } + event.proceed(); + } +} diff --git a/srv/src/main/java/my/bookshop/handlers/HierarchyHandler.java b/srv/src/main/java/my/bookshop/handlers/HierarchyHandler.java index bdd875a0..391fb505 100644 --- a/srv/src/main/java/my/bookshop/handlers/HierarchyHandler.java +++ b/srv/src/main/java/my/bookshop/handlers/HierarchyHandler.java @@ -1,4 +1,4 @@ -package my.bookshop.handlers; +/* package my.bookshop.handlers; import java.util.ArrayDeque; import java.util.Comparator; @@ -54,6 +54,7 @@ * The handler is neither functionally complete nor correct for all requests. It * is not intended as a blue-print for custom code. */ +/* public class HierarchyHandler implements EventHandler { private final PersistenceService db; @@ -311,3 +312,4 @@ Deque getPath(GenreHierarchy gh) { } } } + */ \ No newline at end of file diff --git a/srv/src/test/java/my/bookshop/GenreHierarchyTest.java b/srv/src/test/java/my/bookshop/GenreHierarchyTest.java index 1c1abe72..95d7616f 100644 --- a/srv/src/test/java/my/bookshop/GenreHierarchyTest.java +++ b/srv/src/test/java/my/bookshop/GenreHierarchyTest.java @@ -1,4 +1,4 @@ -package my.bookshop; +/* package my.bookshop; import static org.assertj.core.api.Assumptions.assumeThat; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; @@ -237,4 +237,4 @@ void testStartTwoLevelsOrderByDescHANA() throws Exception { private boolean isOnHana() { return env.acceptsProfiles(Profiles.of("cloud")); } -} +} */ \ No newline at end of file diff --git a/srv/src/test/java/my/bookshop/handlers/HierarchyHandlerSorterTest.java b/srv/src/test/java/my/bookshop/handlers/HierarchyHandlerSorterTest.java index 46bc78c2..79115dd7 100644 --- a/srv/src/test/java/my/bookshop/handlers/HierarchyHandlerSorterTest.java +++ b/srv/src/test/java/my/bookshop/handlers/HierarchyHandlerSorterTest.java @@ -1,4 +1,4 @@ -package my.bookshop.handlers; +/* package my.bookshop.handlers; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.List; @@ -68,3 +68,4 @@ private static List sorted(GenreHierarchy... h) { return List.of(h).stream().sorted(new Sorter()).toList(); } } + */ \ No newline at end of file diff --git a/srv/src/test/java/my/bookshop/handlers/HierarchyHandlerTest.java b/srv/src/test/java/my/bookshop/handlers/HierarchyHandlerTest.java new file mode 100644 index 00000000..00d86738 --- /dev/null +++ b/srv/src/test/java/my/bookshop/handlers/HierarchyHandlerTest.java @@ -0,0 +1,64 @@ +/* package my.bookshop.handlers; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import java.util.List; +import org.junit.jupiter.api.Test; + +import cds.gen.adminservice.GenreHierarchy; +import my.bookshop.handlers.HierarchyHandler.Sorter; + +public class HierarchyHandlerTest { + + @Test + public void testSortingRoots() { + GenreHierarchy h1 = GenreHierarchy.create(); + h1.setName("Philosophical fiction"); + GenreHierarchy h2 = GenreHierarchy.create(); + h2.setName("Epic"); + List list = List.of(h1,h2); + List sorted = list.stream().sorted(new Sorter()).toList(); + assertEquals("Epic", sorted.get(0).getName()); + } + + @Test + public void testSortingChildren() { + GenreHierarchy root = GenreHierarchy.create(); + root.setName("Folklore"); + GenreHierarchy h1 = GenreHierarchy.create(); + h1.setName("Urban legend"); + h1.setParnt(root); + GenreHierarchy h2 = GenreHierarchy.create(); + h2.setName("Fairy tale"); + h2.setParnt(root); + List list = List.of(h1,h2); + List sorted = list.stream().sorted(new Sorter()).toList(); + assertEquals("Fairy tale", sorted.get(0).getName()); + } + + @Test + public void testSortingChildrenWithDifRoot() { + GenreHierarchy root1 = GenreHierarchy.create(); + root1.setName("Thriller"); + GenreHierarchy root2 = GenreHierarchy.create(); + root2.setName("Folklore"); + GenreHierarchy h2 = GenreHierarchy.create(); + h2.setName("Urban legend"); + h2.setParnt(root2); + List list = List.of(root1,h2); + List sorted = list.stream().sorted(new Sorter()).toList(); + assertEquals("Urban legend", sorted.get(0).getName()); + } + + @Test + public void testSortingChildrenSameRoot() { + GenreHierarchy root = GenreHierarchy.create(); + root.setName("Folklore"); + GenreHierarchy h1 = GenreHierarchy.create(); + h1.setName("Urban legend"); + h1.setParnt(root); + List list = List.of(h1,root); + List sorted = list.stream().sorted(new Sorter()).toList(); + assertEquals("Folklore", sorted.get(0).getName()); + } +} + */ \ No newline at end of file