{"id":100,"date":"2019-11-17T19:54:01","date_gmt":"2019-11-17T19:54:01","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/microservices\/?p=100"},"modified":"2019-11-17T20:04:09","modified_gmt":"2019-11-17T20:04:09","slug":"cqrs-with-axon-tutorial-part-7-display-data-in-the-grid","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/microservices\/cqrs-with-axon-tutorial-part-7-display-data-in-the-grid\/","title":{"rendered":"CQRS With Axon Tutorial: Part 7 \u2013 Display Data in the Grid"},"content":{"rendered":"<p>In this part, we are going to implement and Grid and populate the grid with data. We would consider the following sub-headings<\/p>\n<ol>\n<li><a href=\"#t1\">How we would Achieved This<\/a><\/li>\n<li><a href=\"#t2\">Creating the Grid<\/a><\/li>\n<li><a href=\"#t3\">Refresh Grid After Changes<\/a><\/li>\n<li><a href=\"#t4\">Add Grid to Layout<\/a><\/li>\n<li><a href=\"#t5\">Next Steps<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t1\">1. How we would Achieve This<\/strong><\/h5>\n<p>We would first create method that returns a grid. Inside this method, we would create a new grid.Then add three columns to the grid. Finally, we set the grid&#8217;s data provider property to our data provider.<\/p>\n<p>After this, we would go to the init method and create a vertical layout. In this vertical layout, we first add the original command bar(with Command and Issue Panel). The we also add the grid we created.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t2\">2. Creating the Grid<\/strong><\/h5>\n<p>Follow the steps below:<\/p>\n<p><strong>Step 1<\/strong>: Create a method named summaryGrid that returns a Grid&lt;CardSummary&gt;<\/p>\n<p><strong>Step 2<\/strong>: Inside this method, create a new Grid&lt;CardSummary&gt; grid.<\/p>\n<p><strong>Step 3:<\/strong> Add three columns to the grid. The column captions are: Id, Initial Balance and Remaining Balance. The syntax for adding\u00a0 columns is given below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">grid<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">addColumn<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #997700; font-weight: bold;\">CardSummary:<\/span><span style=\"color: #333333;\">:<\/span>getId<span style=\"color: #333333;\">).<\/span><span style=\"color: #0000cc;\">setCaption<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Id\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 4<\/strong>: Set the dataProvider property of the grid to the dataProvider<\/p>\n<p><strong>Step 5<\/strong>: Return the grid<\/p>\n<p>The complete code for the summaryGrid method is given below:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> Grid<span style=\"color: #333333;\">&lt;<\/span>CardSummary<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">summaryGrid<\/span><span style=\"color: #333333;\">(){<\/span>\r\n    Grid<span style=\"color: #333333;\">&lt;<\/span>CardSummary<span style=\"color: #333333;\">&gt;<\/span> grid <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Grid<span style=\"color: #333333;\">&lt;<\/span>CardSummary<span style=\"color: #333333;\">&gt;();\r\n<\/span>    grid.SetSizeFull<span style=\"color: #333333;\">();<\/span> \r\n    grid<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">addColumn<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #997700; font-weight: bold;\">CardSummary:<\/span><span style=\"color: #333333;\">:<\/span>getId<span style=\"color: #333333;\">).<\/span><span style=\"color: #0000cc;\">setCaption<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Id\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n    grid<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">addColumn<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #997700; font-weight: bold;\">CardSummary:<\/span><span style=\"color: #333333;\">:<\/span>getInitialBalance<span style=\"color: #333333;\">).<\/span><span style=\"color: #0000cc;\">setCaption<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Initial Balance\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n    grid<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">addColumn<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #997700; font-weight: bold;\">CardSummary:<\/span><span style=\"color: #333333;\">:<\/span>getRemainingBalance<span style=\"color: #333333;\">).<\/span><span style=\"color: #0000cc;\">setCaption<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Remaining Balance\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\r\n    grid<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">setDataProvider<\/span><span style=\"color: #333333;\">(<\/span>dataProvider<span style=\"color: #333333;\">);<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> grid<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t3\">3. Refresh Grid After Changes<\/strong><\/h5>\n<p>Anytime a change is made, whether a GiftCard is issued or redeemed, we need to refresh the grid view. One way to do that is to add a closeListener to the Notification message. So anytime the notification message closes, the grid refreshed<\/p>\n<p>To achieve that, follow the steps below:<br \/>\n<strong>Step 1:<\/strong> Append the code below to the end of the Notification.show() message<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">addCloseListener<\/span><span style=\"color: #333333;\">(<\/span>x <span style=\"color: #333333;\">-&gt;<\/span> dataProvider<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">refreshAll<\/span><span style=\"color: #333333;\">());<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 2:<\/strong> Do the same for the second Notification.show() message.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t4\">4. Add the Grid to the Layout<\/strong><\/h5>\n<p>Take the steps below to add the grid to the\u00a0 layout.<\/p>\n<p><strong>Step 1:<\/strong> In the init() method, just before the setContent() line, create a new vertical layout<\/p>\n<p><strong>Step 2:<\/strong> Set the size to Full<\/p>\n<p><strong>Step 3:<\/strong> Add commands and summaryGrid to the layout using layout.setContent() and passing two arguments<\/p>\n<p>Remember commands is a horizontal layout that contains the two panels while summaryGrid is the grid.<\/p>\n<p>The complete init method is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Override<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">protected<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">init<\/span><span style=\"color: #333333;\">(<\/span>VaadinRequest vaadinRequest<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    HorizontalLayout commands <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> HorizontalLayout<span style=\"color: #333333;\">();<\/span>\r\n    commands<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">setSizeFull<\/span><span style=\"color: #333333;\">();<\/span>\r\n    commands<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">addComponents<\/span><span style=\"color: #333333;\">(<\/span>issuePanel<span style=\"color: #333333;\">(),<\/span> redeemPanel<span style=\"color: #333333;\">());<\/span>\r\n\r\n    VerticalLayout layout <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> VerticalLayout<span style=\"color: #333333;\">();<\/span>\r\n    layout<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">setSizeFull<\/span><span style=\"color: #333333;\">();<\/span>\r\n    layout<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">addComponents<\/span><span style=\"color: #333333;\">(<\/span>commands<span style=\"color: #333333;\">,<\/span> summaryGrid<span style=\"color: #333333;\">());<\/span>\r\n\r\n    setContent<span style=\"color: #333333;\">(<\/span>layout<span style=\"color: #333333;\">);<\/span>\r\n\r\n    <span style=\"color: #888888;\">\/\/Error Handler<\/span>\r\n    setErrorHandler<span style=\"color: #333333;\">(<\/span><span style=\"color: #008800; font-weight: bold;\">new<\/span> DefaultErrorHandler<span style=\"color: #333333;\">(){<\/span>\r\n        <span style=\"color: #555555; font-weight: bold;\">@Override<\/span>\r\n        <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">error<\/span><span style=\"color: #333333;\">(<\/span>com<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">vaadin<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">server<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">ErrorEvent<\/span> event<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n            Throwable cause <span style=\"color: #333333;\">=<\/span> event<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getThrowable<\/span><span style=\"color: #333333;\">();<\/span>\r\n            <span style=\"color: #008800; font-weight: bold;\">while<\/span> <span style=\"color: #333333;\">(<\/span>cause<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getCause<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">!=<\/span> <span style=\"color: #008800; font-weight: bold;\">null<\/span><span style=\"color: #333333;\">)<\/span> cause <span style=\"color: #333333;\">=<\/span> cause<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getCause<\/span><span style=\"color: #333333;\">();<\/span>\r\n            Notification<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">show<\/span><span style=\"color: #333333;\">(<\/span>cause<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getMessage<\/span><span style=\"color: #333333;\">(),<\/span> Notification<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">Type<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">ERROR_MESSAGE<\/span><span style=\"color: #333333;\">)<\/span>\r\n            <span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">addCloseListener<\/span><span style=\"color: #333333;\">(<\/span>x <span style=\"color: #333333;\">-&gt;<\/span> dataProvider<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">refreshAll<\/span><span style=\"color: #333333;\">());<\/span>\r\n        <span style=\"color: #333333;\">}<\/span>\r\n    <span style=\"color: #333333;\">});<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 4<\/strong>: Run the application and test it by issuing and redeeming some cards. Then check that the history appears in the grid below. The output is shown below:<\/p>\n<figure id=\"attachment_105\" aria-describedby=\"caption-attachment-105\" style=\"width: 555px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/GiftCard-GUI-final-Output.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-105 \" src=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/GiftCard-GUI-final-Output.jpg\" alt=\"GiftCard GUI final Output\" width=\"555\" height=\"418\" srcset=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/GiftCard-GUI-final-Output.jpg 728w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/GiftCard-GUI-final-Output-300x226.jpg 300w\" sizes=\"auto, (max-width: 555px) 100vw, 555px\" \/><\/a><figcaption id=\"caption-attachment-105\" class=\"wp-caption-text\">GiftCard GUI final Output<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t5\">5. Next Steps<\/strong><\/h5>\n<p>Now we have out application running on our local machine. Of course, it&#8217;s still a monolithic application.<\/p>\n<p>So next we would split this application into microservices using spring profiles<\/p>\n<p>Then we use AxonHub to connect the various microservices. Finally, we deploy to kubernetes cluster in Google cloud. So we have more interesting things to to. However if you have gotten to this point successfully, big thumbs up to you!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this part, we are going to implement and Grid and populate the grid with data. We would consider the following sub-headings How we would &hellip; <\/p>\n","protected":false},"author":1,"featured_media":102,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,27],"tags":[],"class_list":["post-100","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-axon","category-axonframework"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/comments?post=100"}],"version-history":[{"count":4,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/100\/revisions"}],"predecessor-version":[{"id":106,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/100\/revisions\/106"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/media\/102"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/media?parent=100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/categories?post=100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/tags?post=100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}