CRUD Operations in Gaelyk- Part 2- Read Operation

In our previous post we saw about implementing Create operation for creating a new entity and persisting to the database. In this article lets see how we can retrieve all the entities stored in the datastore. This would be the Read operation from the CRUD operations in Gaelyk

also read:

Read- CRUD Operations in Gaelyk

We would continue with the project created in the previous post, so for those who haven’t please read the previous post and then continue with this post.

The DatastoreService provides synchronous access to the datastore to perform create, read, delete, update operations on the Entity. It also provides transaction support. Gaelyk provides shortcuts for the operations supported by Google App Engine and on similar lines DatastoreService can be accessed by using datastore shorthand.One can read here about the shorthands provided by Gaelyk for DatastoreService.

And as mentioned in previous post PROJECT_HOME is the location where the Gaelyk Template Project was extracted. Lets create listBooks.groovy groovlet in PROJECT_HOME/src/main/webapp/WEB-INF/groovy folder:

def books = datastore.execute{
    select all from book
request.books = books
forward '/WEB-INF/pages/listBook.gtpl'

All the groovy scripts in PROJECT_HOME/src/main/webapp/WEB-INF/groovy folder are the groovlets, just like the servlets we have in Java world. One can read about the datastore.execute syntax here. We fetch all the entities named “book” and store it in books reference and set this data to the request attribute. The books reference is actually some iterator which can be traversed using the each function. In Dynamically typed languages such as Groovy you really not care what the type is, we are concerned with what methods can be invoked on that object. Its something called “Duck Typing”. Lets have a look at listBook.gtpl which renders the data retrieved from above. Create listBook.gtpl in PROJECT_HOME/src/main/webapp/WEB-INF/pages:

<% include '/WEB-INF/includes/header.gtpl' %>
<div class='row'>
  <div class='span1'><h4>ISBN</h4></div>
  <div class='span3'><h4>Title</h4></div>
  <div class='span3'><h4>Authors</h4></div>
  <div class='span2'><h4>Publisher</h4></div>
  book ->
  <div class='row'>
    <div class='span1'><%= book.isbn %></div>
    <div class='span3'><%= book.title %></div>
    <div class='span3'><%= book.authors %></div>
    <div class='span2'><%= book.publisher %></div>
<% include '/WEB-INF/includes/footer.gtpl' %>

As I said before we are more interested in the methods which can be invoked on the data set in the request. And I also said that the data set is some form of iterator and hence we iterate through it by providing a closure to the each method. In the closure provided we render the HTML content for each item in the iterator. This is quite familiar for people from the JSP/PHP world.

We havent yet setup the route to access the above page, lets go ahead and edit the routes.groovy file in PROJECT_HOME/src/main/webapp/WEB-INF/ and add the following line:

get '/book/all', forward: '/listBooks.groovy'

Run your application by executing gradlew gaeRun on the terminal and then access the page using: http://localhost:8080/book/all. If you dont see any data that means you havent added any. If you followed our previous post then you must be able to add new data by using the URL: http://localhost:8080/book/new.

Once the application is up and running, on visiting the browser, one would see some content similar to:

I have committed the source code into my github project here. Though the git hub project is not exclusively for this example, but it does contain the files/code necessary for these CRUD operation examples.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!