Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Modification of Hierarchies #428

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions db/books.cds
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
10 changes: 5 additions & 5 deletions db/data/my.bookshop-Books.csv
Original file line number Diff line number Diff line change
@@ -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
47 changes: 17 additions & 30 deletions db/data/my.bookshop-Genres.csv
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions srv/admin-service.cds
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand Down
49 changes: 49 additions & 0 deletions srv/src/main/java/my/bookshop/handlers/HierarchyExpandHandler.java
Original file line number Diff line number Diff line change
@@ -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<CqnTransformation> 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<CqnSelectListItem> items(List<CqnSelectListItem> items) {
return items.stream().filter(i -> !i.isExpand()).toList();
}
});
event.setCqn(copy);
}
event.proceed();
}
}
4 changes: 3 additions & 1 deletion srv/src/main/java/my/bookshop/handlers/HierarchyHandler.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package my.bookshop.handlers;
/* package my.bookshop.handlers;

import java.util.ArrayDeque;
import java.util.Comparator;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -311,3 +312,4 @@ Deque<String> getPath(GenreHierarchy gh) {
}
}
}
*/
4 changes: 2 additions & 2 deletions srv/src/test/java/my/bookshop/GenreHierarchyTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -237,4 +237,4 @@ void testStartTwoLevelsOrderByDescHANA() throws Exception {
private boolean isOnHana() {
return env.acceptsProfiles(Profiles.of("cloud"));
}
}
} */
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package my.bookshop.handlers;
/* package my.bookshop.handlers;

import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
Expand Down Expand Up @@ -68,3 +68,4 @@ private static List<GenreHierarchy> sorted(GenreHierarchy... h) {
return List.of(h).stream().sorted(new Sorter()).toList();
}
}
*/
64 changes: 64 additions & 0 deletions srv/src/test/java/my/bookshop/handlers/HierarchyHandlerTest.java
Original file line number Diff line number Diff line change
@@ -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<GenreHierarchy> list = List.of(h1,h2);
List<GenreHierarchy> 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<GenreHierarchy> list = List.of(h1,h2);
List<GenreHierarchy> 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<GenreHierarchy> list = List.of(root1,h2);
List<GenreHierarchy> 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<GenreHierarchy> list = List.of(h1,root);
List<GenreHierarchy> sorted = list.stream().sorted(new Sorter()).toList();
assertEquals("Folklore", sorted.get(0).getName());
}
}
*/
Loading