<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JavaBeat &#187; Groovy</title>
	<atom:link href="http://www.javabeat.net/category/groovy-2/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javabeat.net</link>
	<description>Java Technology News</description>
	<lastBuildDate>Sun, 16 Jun 2013 11:17:41 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>CRUD Operations in Gaelyk- Part 2- Read Operation</title>
		<link>http://www.javabeat.net/2012/06/crud-operations-gaelyk-part-2/</link>
		<comments>http://www.javabeat.net/2012/06/crud-operations-gaelyk-part-2/#comments</comments>
		<pubDate>Sun, 24 Jun 2012 10:00:24 +0000</pubDate>
		<dc:creator>Mohamed Sanaulla</dc:creator>
				<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Gaelyk]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=4459</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>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 Read- CRUD Operations in Gaelyk We would [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><a id="dd_start"></a><p>In our <a title="CRUD Operations in Gaelyk- Part 1" href="http://www.javabeat.net/2012/06/crud-operations-in-gaelyk-part-1/">previous post</a> 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</p>
<h3>Read- C<strong>R</strong>UD Operations in Gaelyk</h3>
<p>We would continue with the project created in the <a title="CRUD Operations in Gaelyk- Part 1" href="http://www.javabeat.net/2012/06/crud-operations-in-gaelyk-part-1/">previous post</a>, so for those who haven&#8217;t please read the <a title="CRUD Operations in Gaelyk- Part 1" href="http://www.javabeat.net/2012/06/crud-operations-in-gaelyk-part-1/">previous post</a> and then continue with this post.</p>
<p>The <a href="https://developers.google.com/appengine/docs/java/javadoc/com/google/appengine/api/datastore/DatastoreService" target="_blank">DatastoreService</a> 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 <strong>datastore</strong> shorthand.One can read <a href="http://gaelyk.appspot.com/tutorial/app-engine-shortcuts#datastore" target="_blank">here</a> about the shorthands provided by Gaelyk for DatastoreService.</p>
<p>And as mentioned in <a title="CRUD Operations in Gaelyk- Part 1" href="http://www.javabeat.net/2012/06/crud-operations-in-gaelyk-part-1/" target="_blank">previous post</a> PROJECT_HOME is the location where the <a href="http://gaelyk.appspot.com/tutorial/template-project" target="_blank">Gaelyk Template Project</a> was extracted. Lets create listBooks.groovy groovlet in PROJECT_HOME/src/main/webapp/WEB-INF/groovy folder:</p>
<pre class="brush: java; title: ; notranslate">
def books = datastore.execute{
    select all from book
}
request.books = books
forward '/WEB-INF/pages/listBook.gtpl'
</pre>
<p>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 <strong>datastore.execute</strong> syntax <a href="http://gaelyk.appspot.com/tutorial/app-engine-shortcuts#query" target="_blank">here</a>. We fetch all the entities named &#8220;book&#8221; and store it in books reference and set this data to the request attribute. The <strong>books</strong> 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 &#8220;Duck Typing&#8221;. 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:</p>
<pre class="brush: xml; title: ; notranslate">
//listBook.gtpl
&lt;% include '/WEB-INF/includes/header.gtpl' %&gt;
&lt;div class=&quot;row&quot;&gt;
  &lt;div class=&quot;span1&quot;&gt;&lt;h4&gt;ISBN&lt;/h4&gt;&lt;/div&gt;
  &lt;div class=&quot;span3&quot;&gt;&lt;h4&gt;Title&lt;/h4&gt;&lt;/div&gt;
  &lt;div class=&quot;span3&quot;&gt;&lt;h4&gt;Authors&lt;/h4&gt;&lt;/div&gt;
  &lt;div class=&quot;span2&quot;&gt;&lt;h4&gt;Publisher&lt;/h4&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;%
request.books.each{
  book -&gt;
%&gt;
  &lt;div class=&quot;row&quot;&gt;
    &lt;div class=&quot;span1&quot;&gt;&lt;%= book.isbn %&gt;&lt;/div&gt;
    &lt;div class=&quot;span3&quot;&gt;&lt;%= book.title %&gt;&lt;/div&gt;
    &lt;div class=&quot;span3&quot;&gt;&lt;%= book.authors %&gt;&lt;/div&gt;
    &lt;div class=&quot;span2&quot;&gt;&lt;%= book.publisher %&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;%
}
%&gt;
&lt;% include '/WEB-INF/includes/footer.gtpl' %&gt;
</pre>
<p>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.</p>
<p>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:</p>
<pre class="brush: java; title: ; notranslate">
get &quot;/book/all&quot;, forward: &quot;/listBooks.groovy&quot;
</pre>
<p>Run your application by executing <strong>gradlew gaeRun</strong> 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 <a title="CRUD Operations in Gaelyk- Part 1" href="http://www.javabeat.net/2012/06/crud-operations-in-gaelyk-part-1/" target="_blank">previous post</a> then you must be able to add new data by using the URL: http://localhost:8080/book/new.</p>
<p>Once the application is up and running, on visiting the browser, one would see some content similar to:<br />
<a href="http://www.javabeat.net/wp-content/uploads/2012/06/listALl.png"><img class="aligncenter size-full wp-image-4460" src="http://www.javabeat.net/wp-content/uploads/2012/06/listALl.png" alt="" width="300" height="122" /></a></p>
<p>I have committed the source code into my github project <a href="https://github.com/sanaulla123/my-place" target="_blank">here</a>. Though the git hub project is not exclusively for this example, but it does contain the files/code necessary for these CRUD operation examples.</p>
<div class='dd_outer'><div class='dd_inner'><div id='dd_ajax_float'><div class='dd_button_v'><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Fwww.javabeat.net%2Fcategory%2Fgroovy-2%2Ffeed%2F" send="false" show_faces="false"  layout="box_count" width="50"  ></fb:like></div><div style='clear:left'></div><div class='dd_button_v'><script type='text/javascript' src='https://apis.google.com/js/plusone.js'></script><g:plusone size='tall' href='http://www.javabeat.net/category/groovy-2/feed/'></g:plusone></div><div style='clear:left'></div><div class='dd_button_v'><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.javabeat.net/category/groovy-2/feed/" data-count="vertical" data-text="Groovy" data-via="javabeat" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div style='clear:left'></div><div class='dd_button_extra_v'><script type="text/javascript">jQuery(document).load(function(){ stLight.options({publisher:'bab47279-62c9-46af-addc-79fd1fe8fee0'}); });</script><div class="st_email_custom"><span id='dd_email_text'>email</span></div></div><div style='clear:left'></div><div class='dd_button_extra_v'><div id='dd_print_button'><span id='dd_print_text'><a href='javascript:window:print()'>print</a></span></div></div><div style='clear:left'></div></div></div></div><script type="text/javascript">var dd_offset_from_content = 44; var dd_top_offset_from_content = 0;</script><script type="text/javascript" src="http://www.javabeat.net/wp-content/plugins/digg-digg//js/diggdigg-floating-bar.js?ver=5.3.0"></script><div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2012/06/crud-operations-gaelyk-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>CRUD Operations in Gaelyk- Part 1- Create Operation</title>
		<link>http://www.javabeat.net/2012/06/crud-operations-in-gaelyk-part-1/</link>
		<comments>http://www.javabeat.net/2012/06/crud-operations-in-gaelyk-part-1/#comments</comments>
		<pubDate>Sat, 23 Jun 2012 16:11:21 +0000</pubDate>
		<dc:creator>Mohamed Sanaulla</dc:creator>
				<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Gaelyk]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=4453</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>We have seen here and here on creating simple mashup using Gaelyk. In this series of articles lets see how we can implement CRUD operations in Gaelyk. CRUD Operations in Gaelyk would involve: Create an entity Read an entity Update an entity Delete an entity In this article we will look at creating an entity [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><p>We have seen <a title="Developing Groovy based web application and deploying to Google App Engine" href="http://www.javabeat.net/2012/06/developing-groovy-based-web-application-and-deploying-to-google-app-engine/">here</a> and <a title="A simple Location and Weather mashup using Gaelyk Framework" href="http://www.javabeat.net/2012/06/obtaining-weather-information-using-the-weather-underground-api-gaelyk-sample/">here</a> on creating simple mashup using Gaelyk. In this series of articles lets see how we can implement CRUD operations in Gaelyk.</p>
<p>CRUD Operations in Gaelyk would involve:</p>
<ul>
<li>Create an entity</li>
<li>Read an entity</li>
<li>Update an entity</li>
<li>Delete an entity</li>
</ul>
<p>In this article we will look at creating an entity in the datastore.</p>
<h3>Create of <strong>C</strong>RUD operations</h3>
<p><a href="https://developers.google.com/appengine/" target="_blank">Google App Engine</a> supports both NoSQL and SQL based database. The NoSQL counterpart uses <a href="https://developers.google.com/appengine/docs/java/datastore/overview" target="_blank">BigTable datastore</a>. And we would be using the same for our CRUD operations.</p>
<p>The data in BigTable is held in <a href="https://developers.google.com/appengine/docs/java/javadoc/com/google/appengine/api/datastore/Entity" target="_blank">Entity</a> where each entity has some fields, associated types and values for each field. It also adds a Key to each data row in the entity.</p>
<p>We will look at the details of the datastore as we proceed further, but lets setup the Gaelyk project first. The easiest way is to download the template project from <a href="http://gaelyk.appspot.com/tutorial/template-project" target="_blank">here</a> and extract it to some directory. Lets call this directory as PROJECT_HOME. One can run the command <strong>gradlew gaeRun</strong> to deploy the template project into the embedded Jetty server and access it from http://localhost:8080/.</p>
<p>In this series of articles we would be focused on a Book entity which has the following fields: isbn, title, authors, publisher, number of pages. To create an entity and store in the database we need to create a form for data entry, let go ahead and create a newBook.gtpl in PROJECT_HOME/src/main/webapp/WEB-INF/pages folder:</p>
<pre class="brush: xml; title: ; notranslate">
//newBook.gtpl
&lt;% include '/WEB-INF/includes/header.gtpl' %&gt;
&lt;form action=&quot;/book&quot; method=&quot;POST&quot;&gt;
  &lt;div class=&quot;row&quot;&gt;
    &lt;div class=&quot;span5&quot;&gt;
      &lt;label for=&quot;isbn&quot;/&gt;ISBN&lt;/label&gt;
      &lt;input id=&quot;isbn&quot; type=&quot;text&quot;
             name=&quot;isbn&quot;/&gt;

      &lt;label for=&quot;title&quot;/&gt;Title&lt;/label&gt;
      &lt;input id=&quot;title&quot; type=&quot;text&quot;
             name=&quot;title&quot;/&gt;

      &lt;label for=&quot;authors&quot;/&gt;Author(s)&lt;/label&gt;
      &lt;input id=&quot;authors&quot; type=&quot;text&quot;
             name=&quot;authors&quot;/&gt;

      &lt;label for=&quot;publisher&quot;/&gt;Publisher&lt;/label&gt;
      &lt;input id=&quot;publisher&quot; type=&quot;text&quot;
             name=&quot;publisher&quot;/&gt;

      &lt;label for=&quot;numberOfPages&quot;/&gt;Number of Pages&lt;/label&gt;
      &lt;input id=&quot;numberOfPages&quot; type=&quot;text&quot;
             name=&quot;numberOfPages&quot;/&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;row&quot;&gt;
    &lt;div class=&quot;span4&quot;&gt;
    &lt;input type=&quot;submit&quot; value=&quot;Save&quot;&gt;
    &lt;input type=&quot;reset&quot; value=&quot;Reset&quot;&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/form&gt;

&lt;% include '/WEB-INF/includes/footer.gtpl' %&gt;
</pre>
<p>The template project uses <a href="http://twitter.github.com/bootstrap/" target="_blank">bootstrap</a> and therefor the styling aspects would be taken care! Now that we have a form to enter the data, we would need to setup up the <a href="http://gaelyk.appspot.com/tutorial/url-routing" target="_blank">route</a> to access this page, that way we will have clean urls and not ending with .gtpl or .groovy and so on. The routes.groovy file handles all the routing information and is present in PROJECT_HOME/src/main/webapp/WEB-INF folder. Lets add the following entry to the routes.groovy file</p>
<pre class="brush: java; title: ; notranslate">
//routes.groovy
get &quot;/book/new&quot;, forward: &quot;/WEB-INF/pages/newBook.gtpl&quot;
</pre>
<p>the above code says that when there is a GET request for &#8216;/book/new&#8217;, forward the request to the newBook.gtpl which renders the html markup for the page. On running the application locally using <strong> gradlew gaeRun </strong> and navigating to http://localhost:8080/book/new you should be able to see your page as something like:<br />
<a href="http://www.javabeat.net/wp-content/uploads/2012/06/newBook.png"><img class="aligncenter size-full wp-image-4454" src="http://www.javabeat.net/wp-content/uploads/2012/06/newBook.png" alt="" width="221" height="322" /></a></p>
<p>Wait! we havent still configured the routes for Save operations, lets go ahead of do that. Lets create a groovy script addBook.groovy in the PROJECT_HOME/src/main/webapp/WEB-INF/groovy folder where all the groovy scripts(groovlets) are to be added.</p>
<pre class="brush: java; title: ; notranslate">
//addBook.groovy
import com.google.appengine.api.datastore.Entity
def entity = new Entity(&quot;book&quot;)
entity &lt;&lt; params
entity.save()
request.operation = &quot;saved&quot;
request.savedTitle = params.title

forward '/WEB-INF/pages/success.gtpl'
</pre>
<p><strong>params</strong> is a map which contains the data submitted by the form as key-value pairs. One can inspect the data by logging params.isbn or params.title or any of the params.fieldName data on to the console. Just in case someone wants to try use <strong>log.info params.title</strong> in the above groovy script.</p>
<pre class="brush: java; title: ; notranslate">
entity &lt;&lt; params
entity.save()
</pre>
<p>the above code appends the key-value pairs in the params object to the entity object we have created and then save the contents on the entity object to the datastore. After saving the entity we forward to another page- success.gtpl which is created in PROJECT_HOME/src/main/webapp/WEB-INF/pages folder.</p>
<pre class="brush: xml; title: ; notranslate">
//success.gtpl
&lt;% include '/WEB-INF/includes/header.gtpl' %&gt;
&lt;div class=&quot;row&quot;&gt;
    &lt;p&gt;Successfully &lt;%=  request.operation %&gt; the
       book &lt;%= request.savedTitle %&gt;&lt;/p&gt;
    &lt;p&gt;&lt;a href=&quot;/book/new&quot;&gt;Add another book&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;

&lt;% include '/WEB-INF/includes/footer.gtpl' %&gt;
</pre>
<p>We are yet to setup the route information for the groovy script we created to add the books. So go back to routes.groovy and add the following to the routes.groovy</p>
<pre class="brush: java; title: ; notranslate">
//routes.groovy
post &quot;/book&quot;, forward: &quot;/addBook.groovy&quot;
</pre>
<p>which says, that any POST requests for /book should be forwarded to addBook.groovy script/groovlet. So save all the files and reload the URL http://localhost:8080/book/new and add some entries to the form and click on &#8220;Save&#8221; button and you should be able to see the &#8220;success&#8221; page.</p>
<p>Google App Engine provides a way to access the entities/data stored in the datastores by navigating to: http://localhost:8080/_ah/admin/datastore.</p>
<p>In the <a href="http://www.javabeat.net/2012/06/crud-operations-gaelyk-part-2/" target="_blank">next article</a> lets see how we can list all the entities we have stored in the datastore.</p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2012/06/crud-operations-in-gaelyk-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Developing Groovy based web application and deploying to Google App Engine</title>
		<link>http://www.javabeat.net/2012/06/developing-groovy-based-web-application-and-deploying-to-google-app-engine/</link>
		<comments>http://www.javabeat.net/2012/06/developing-groovy-based-web-application-and-deploying-to-google-app-engine/#comments</comments>
		<pubDate>Tue, 05 Jun 2012 18:32:35 +0000</pubDate>
		<dc:creator>Mohamed Sanaulla</dc:creator>
				<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Web Frameworks]]></category>
		<category><![CDATA[Gaelyk]]></category>
		<category><![CDATA[Google App Engine]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=4131</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>We have seen how to develop a ZK based application and deploy it to OpenShift. Google has also a Paas offering called Google App Engine(GAE) which supports Java, Python and Dart applications. Gaelyk is a simple Groovy based toolkit to develop and deploy application to GAE. In this article lets build a simple Gaelyk based [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><p>We have seen how to develop a ZK based application and deploy it to OpenShift. Google has also a Paas offering called <a href="https://appengine.google.com/" title="Google App Engine" target="_blank">Google App Engine(GAE)</a> which supports Java, Python and Dart applications. <a href="http://gaelyk.appspot.com/" title="Gaelyk" target="_blank">Gaelyk</a> is a simple Groovy based toolkit to develop and deploy application to GAE. In this article lets build a simple Gaelyk based application and deploy it to GAE.</p>
<p>The sample application developed would show the details of location information from where this application was accessed, information lie Name of the city, country, region, latitude, longitude and timezone would be shown.</p>
<h3>Setting up Gaelyk</h3>
<p>But before that we need to download and setup Gaelyk. Faster way to start with Gaelyk is to download the <a href="http://gaelyk.appspot.com/tutorial/template-project" title="Gaelyk Template Project" target="_blank">Template project</a> and extract its contents. Gaelyk template project uses <a href="http://www.gradle.org/" title="Gradle" target="_blank">Gradle</a> build system to manage all the dependencies. To draw an analogy Gradle is similar to Maven. The template project has defined all the require dependencies and also provides a Gradle wrapper <code>gradlew</code> using which you can run gradle tasks without having to install Gradle. Also Gradle manages downloading and configuring the Google App Engine SDK, so you need not worry about that.</p>
<pre class="brush: bash; title: ; notranslate">
$ gradlew tasks
</pre>
<p>shows the available tasks, it might download the dependencies it doesnt find in its local repository. To run the template project:</p>
<pre class="brush: bash; title: ; notranslate">
$ gradlew gaeRun
</pre>
<p>The application will be deployed in the embedded jetty container and can be accessed at http://localhost:8080/. Gaelyk builds on top of <a target="_blank" href="http://groovy.codehaus.org/Groovlets" title="Groovlets">Groovlets</a> and Groovy templates. Groovy templates and Groovlets reside in the WEB-INF/pages and WEB-INF/groovy folders respectively. Also all the GAE services can be accessed from with in these Groovlets and Groovy Templates using the shortcodes provided by Gaelyk like memcache for accessing the caching service, datastore for accessing the GAE Datastore service and so on. For more details one can refer to more detailed <a href="http://gaelyk.appspot.com/tutorial/" title="Gaelyk Tutorial" target="_blank">Gaelyk tutorials</a>.</p>
<h3>Location Information from IP Address</h3>
<p>For this we make use of the API provided by the <a href="http://ipinfodb.com/ip_location_api.php" target="_blank">IPInfoDb</a>. We are interested in City wise precision and the results in JSON, hence make use of the following API:</p>
<pre class="brush: xml; title: ; notranslate">

http://api.ipinfodb.com/v3/ip-city/?key=&lt;your_api_key&gt;&#038;ip=&lt;ip_address&gt;&#038;format=json

</pre>
<p>The above api returns result which is something like</p>
<pre class="brush: jscript; title: ; notranslate">
{
  &quot;statusCode&quot; : &quot;OK&quot;,
  &quot;statusMessage&quot; : &quot;&quot;,
  &quot;ipAddress&quot; : &quot;74.125.45.100&quot;,
  &quot;countryCode&quot; : &quot;US&quot;,
  &quot;countryName&quot; : &quot;UNITED STATES&quot;,
  &quot;regionName&quot; : &quot;CALIFORNIA&quot;,
  &quot;cityName&quot; : &quot;MOUNTAIN VIEW&quot;,
  &quot;zipCode&quot; : &quot;94043&quot;,
  &quot;latitude&quot; : &quot;37.3956&quot;,
  &quot;longitude&quot; : &quot;-122.076&quot;,
  &quot;timeZone&quot; : &quot;-08:00&quot;
}
</pre>
<p>parsing the above JSON in Groovy we would have something like</p>
<pre class="brush: java; title: ; notranslate">
//Setting up the JSON data.
def myIp = request.remoteAddr
def api_key=&quot;&lt;enter the api key for IpInfoDb here&gt;&quot;
def ipUrl = &quot;http://api.ipinfodb.com/v3/ip-city/?key=${api_key}&amp;ip=${myIp}&amp;format=json&quot;
def jsonSlurper = new JsonSlurper()
def locationInformation = jsonSlurper.parseText(new URL(ipUrl).text)
</pre>
<p>In the above code <code>request</code> represents the <a href="http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServletRequest.html" title="HttpServletRequest" target="_blank">HttpServletRequest</a> object and to access the IP address of the client we make use of the getRemoteAddr() method of HttpServletRequest. In groovy one can invoke getPropertyName as just propertyName, hence <code>request.remoteAddr</code>. If you are running this locally then you might want to replace the IP address 127.0.0.1 with your actual IP address. Something like:</p>
<pre class="brush: java; title: ; notranslate">
def myIp = request.remoteAddr
if(myIp.equals(&quot;127.0.0.1&quot;)){
    myIp = &quot;YOUR IP ADDRESS&quot;
}
</pre>
<p>The subsequent code makes a GET request to the API URL and <a href="http://www.javabeat.net/2012/04/parsing-json-using-groovy/" title="Parsing JSON using Groovy">parses the obtained JSON</a> response to get the location information. </p>
<pre class="brush: java; title: ; notranslate">
//Parsing the JSON to get the required information
locationInformation = jsonSlurper.parseText(new URL(ipUrl).text)

//We set this as parameters to the request object
request.ipAddress = locationInformation.ipAddress
request.country = locationInformation.countryName
request.region = locationInformation.regionName
request.city = locationInformation.cityName
request.latitude = locationInformation.latitude
request.longitude = locationInformation.longitude
request.timezone = locationInformation.timeZone
</pre>
<p>Gaelyk provides a flexible URL routing system where in we can map the URL to corresponding Groovlets. These routes are declared in the <code>routes.groovy</code> file which is present in the <code>WEB-INF</code> directory. Lets create a Groovlet for our Index page and call it: <strong>index.groovy</strong> and this groovlet would be responsible for intercepting the request and obtaining the required data and forwarding to the right groovy template. The template project which you would have downloaded would have a groovy template by name: <strong>index.gtpl</strong>, and the groovlet which we created would forward the request to index.gtpl. The template project has the &#8220;/&#8221; route pointing to a different handler, we would update that to point to our groovlet. </p>
<pre class="brush: java; title: ; notranslate">
//Updating the routes.groovy
get &quot;/&quot;, forward: &quot;/index.groovy&quot;
</pre>
<p>As already mentioned the groovlet: <strong>index.groovy</strong> would be responsible for obtaining the location information from the IP address and setting those values in the request, which are then displayed by the groovy template- index.gtpl</p>
<pre class="brush: java; title: ; notranslate">
//Contents of index.groovy
import groovy.json.JsonSlurper

def myIp = request.remoteAddr
if(myIp.equals(&quot;127.0.0.1&quot;)){
  myIp = &quot;YOUR IP ADDRESS&quot;
}

def api_key=&quot;YOUR IPINFODB API KEY&quot;

def ipUrl = &quot;http://api.ipinfodb.com/v3/ip-city/?key=${api_key}&amp;ip=${myIp}&amp;format=json&quot;
def jsonSlurper = new JsonSlurper()
def locationInformation = null
//Use memcache to cache the location information
if (memcache[myIp] != null){
  locationInformation = memcache[myIp]
}
else{
  locationInformation = jsonSlurper.parseText(new URL(ipUrl).text)
  memcache[myIp] = locationInformation
}
request.ipAddress = locationInformation.ipAddress
request.country = locationInformation.countryName
request.region = locationInformation.regionName
request.city = locationInformation.cityName
request.latitude = locationInformation.latitude
request.longitude = locationInformation.longitude
request.timezone = locationInformation.timeZone

//Forward to the corresponding groovy template
forward '/WEB-INF/pages/index.gtpl'
</pre>
<p>The interesting piece of information in the above code snippet is:<strong>memcache</strong>. This binding refers to the <a href="http://code.google.com/appengine/docs/java/javadoc/com/google/appengine/api/memcache/MemcacheService.html" title="Memcache" target="_blank">Memcache</a> service provided by Google App Engine. We cache the location information against the IP address and any subsequent requests from the same IP would fetch from the cache instead.</p>
<p>The values set as part of the request parameters are then retrieved by the index.gtpl as:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;% include '/WEB-INF/includes/header.gtpl' %&gt;
&lt;div class=&quot;row&quot;&gt;
  &lt;div class=&quot;span12&quot;&gt;
    &lt;h3&gt;You are accessing from&lt;/h3&gt;
    &lt;ul&gt;
      &lt;li&gt;Your IP Address: ${request.ipAddress}&lt;/li&gt;
      &lt;li&gt;Country: ${request.country}&lt;/li&gt;
      &lt;li&gt;Region: ${request.region}&lt;/li&gt;
      &lt;li&gt;City: ${request.city}&lt;/li&gt;
      &lt;li&gt;Latitude: ${request.latitude}&lt;/li&gt;
      &lt;li&gt;Longitude: ${request.longitude}&lt;/li&gt;
      &lt;li&gt;Timezone: ${request.timezone}&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;% include '/WEB-INF/includes/footer.gtpl' %&gt;
</pre>
<p>the <strong>include</strong> is used to include other groovy templates. Groovy templates lets you embed groovy code by just like we do in JSP and PHP.</p>
<pre class="brush: bash; title: ; notranslate">
#Run the application
$ ./gradlew gaeRun
</pre>
<p>The application would be running http://localhost:8080/ </p>
<p>One can even create a eclipse/intellij project for this by:</p>
<pre class="brush: bash; title: ; notranslate">
#For Eclipse
gradlew cleanEclipse eclipse

#For IntelliJ 
gradlew cleanIdea idea
</pre>
<p>and then load the project in your IDE.</p>
<p>Once we are done with the development, we can deploy to Google App Engine by using:</p>
<pre class="brush: bash; title: ; notranslate">
$ ./gradlew gaeUpload
</pre>
<p>but before that you must create an application in your <a href="https://appengine.google.com/" target="_blank">Google App Engine account</a> and update the appengine-web.xml file in src/main/webapp/WEB-INF directory by adding the appid to the XML. </p>
<p>To try out this sample, you need to create a new <strong>index.groovy</strong> file in <strong>src/main/webapp/WEB-INF/groovy</strong> folder. Then create index.gtpl in <strong>src/main/webapp/WEB-INF/pages</strong>. Also dont forget to update the appengine-web.xml file. </p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2012/06/developing-groovy-based-web-application-and-deploying-to-google-app-engine/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Using JsonSlurper Groovy API from Java to parse JSON</title>
		<link>http://www.javabeat.net/2012/05/using-jsonslurper-groovy-api-from-java-to-parse-json/</link>
		<comments>http://www.javabeat.net/2012/05/using-jsonslurper-groovy-api-from-java-to-parse-json/#comments</comments>
		<pubDate>Thu, 03 May 2012 06:00:28 +0000</pubDate>
		<dc:creator>Mohamed Sanaulla</dc:creator>
				<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[JavaScript Object Notation (JSON)]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/examples/?p=1309</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>We have seen here on how to parse JSON in Java using Gson and here on how to parse JSON in Groovy. I also touched upon in brief about Groovy here. The beauty of these JVM languages is that one can invoke these APIs from Java, the only requirement is that you need to have [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><p style="text-align: justify">We have seen <a title="Parsing JSON using Java and GSON library" href="http://www.javabeat.net/examples/2012/04/30/parsing-json-using-java-and-gson-library/" target="_blank">here</a> on how to parse JSON in Java using Gson and <a title="Parsing JSON using Groovy" href="http://www.javabeat.net/examples/2012/04/30/parsing-json-using-groovy/" target="_blank">here</a> on how to parse JSON in Groovy. I also touched upon in brief about Groovy <a title="Parsing JSON using Groovy" href="http://www.javabeat.net/examples/2012/04/30/parsing-json-using-groovy/" target="_blank">here</a>. The beauty of these JVM languages is that one can invoke these APIs from Java, the only requirement is that you need to have the language jar on the classpath. In our example below we use <a title="JsonSlurper Code" href="https://github.com/groovy/groovy-core/blob/master/src/main/groovy/json/JsonSlurper.java" target="_blank">JsonSlurper</a> in Groovy to parse the JSON documents from Java. Sounds cool right! In one of the talks on Functional Programming, <a title="Venkat Subramaniam" href="http://twitter.com/#!/venkat_s" target="_blank">Venkat</a> showed us how we could exploit the Software Transactional Memory feature in Clojure to create concurrent applications in Java with less pain.</p>
<p style="text-align: justify">With so many <a title="JVM Languages" href="http://radar.oreilly.com/2011/07/jvm-languages.html" target="_blank">JVM languages</a> coming up i.e languages which run on JVM, developers are more becoming ployglot i.e use multiple langauges in their code. Going on same lines, in the below example I show you how we could exploit the JSON API in Groovy and use it in our Java program. So all you readers out there keep in mind to add the language specific jar to your classpath. This jar tells the compiler where to find the required API information. For Groovy you can download the required Groovy Development Kit from <a title="Groovy Download" href="http://groovy.codehaus.org/Download" target="_blank">here</a>. Let GROOVY_HOME be the home directory of your Groovy installation. You would find a jar in the GROOVY_HOME/embeddable folder which would be named like: groovy-all-&lt;version&gt;.jar. This is the jar which you need to add it to the classpath for your Java code to find the right APIs.</p>
<p style="text-align: justify">If you are not familiar with JSON, do stop by to read about it <a title="What is JavaScript Object Notation (JSON)?" href="http://www.javabeat.net/examples/2012/04/28/what-is-javascript-object-notation-json/" target="_blank">here</a>, if you are not familiar with Json parsing in Groovy, do read it over <a title="Parsing JSON using Groovy" href="http://www.javabeat.net/examples/2012/04/30/parsing-json-using-groovy/" target="_blank">here</a>.</p>
<p style="text-align: justify">The json document which I used is:</p>
<pre class="brush: jscript; title: ; notranslate">
[
  {
    &quot;id&quot;:&quot;6253282&quot;,
    &quot;name&quot;:&quot;Twitter API&quot;,
    &quot;screen_name&quot;:&quot;twitterapi&quot;,
    &quot;url&quot;:&quot;http://dev.twitter.com&quot;,
    &quot;followers_count&quot;:1004641,
    &quot;friends_count&quot;:33,
    &quot;favourites_count&quot;:24,
    &quot;statuses_count&quot;:3277
  },
  {
    &quot;id&quot;:&quot;15082387&quot;,
    &quot;name&quot;:&quot;Sanaulla&quot;,
    &quot;screen_name&quot;:&quot;sanaulla&quot;,
    &quot;url&quot;:&quot;http://blog.sanaulla.info&quot;,
    &quot;followers_count&quot;:241,
    &quot;friends_count&quot;:302,
    &quot;favourites_count&quot;:41,
    &quot;statuses_count&quot;:1876

  }
]
</pre>
<p>The Java code which does the parsing is:</p>
<pre class="brush: java; title: ; notranslate">
import groovy.json.JsonSlurper;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;
import java.util.Map;

public class JavaJsonParserDemo {

  public static void main(String [] args){

    String jsonSrouce = &quot;/home/mohamed/twitterUser.json&quot;;
    JsonSlurper jsonParser = new JsonSlurper();
    try {
      List&lt;Map&gt; parsedData = (List&lt;Map&gt;)jsonParser.parse( new FileReader(jsonSrouce));
      for ( Map aObject : parsedData){
        System.out.println(aObject);
        System.out.println(aObject.get(&quot;screen_name&quot;));
      }
    }catch (FileNotFoundException e) {
      e.printStackTrace();
    }
  }
}
</pre>
<p style="text-align: justify">If you happen to explore the source of JsonSlurper <a title="JsonSlurper Code" href="https://github.com/groovy/groovy-core/blob/master/src/main/groovy/json/JsonSlurper.java" target="_blank">here</a>, you can see that the parse() method invokes either parseObject() or parseArray(). In our sample JSON it would invoke parseArray(), which would return some List. Looking into parseArray() we can find that the List is actually a List of Maps. Keeping this in mind, the declaration used above is List.</p>
<p style="text-align: justify">Running this program the output would be:</p>
<pre class="brush: bash; title: ; notranslate">
{id=6253282, favourites_count=24, friends_count=33, name=Twitter API,
screen_name=twitterapi, statuses_count=3277, followers_count=1004641,
url=http://dev.twitter.com}
twitterapi
{id=15082387, favourites_count=41, friends_count=302, name=Sanaulla,
screen_name=sanaulla, statuses_count=1876, followers_count=241,
url=http://blog.sanaulla.info}
sanaulla
</pre>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2012/05/using-jsonslurper-groovy-api-from-java-to-parse-json/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Parsing JSON using Groovy</title>
		<link>http://www.javabeat.net/2012/04/parsing-json-using-groovy/</link>
		<comments>http://www.javabeat.net/2012/04/parsing-json-using-groovy/#comments</comments>
		<pubDate>Mon, 30 Apr 2012 09:33:02 +0000</pubDate>
		<dc:creator>Mohamed Sanaulla</dc:creator>
				<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/examples/?p=1171</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>In our previous post we saw how we used Gson API to parse the Json using Java. Not spending too much time on the introduction, I would want to straight away dive into the same parsing which can be done using Groovy language. Groovy is a scripting language which runs on the JVM. It can [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><p style="text-align: justify">In our previous post we saw how we used Gson API to parse the Json using Java. Not spending too much time on the introduction, I would want to straight away dive into the same parsing which can be done using <a href="http://en.wikipedia.org/wiki/Groovy_%28programming_language%29" target="_blank">Groovy</a> language. <a href="http://en.wikipedia.org/wiki/Groovy_%28programming_language%29" target="_blank">Groovy</a> is a scripting language which runs on the JVM. It can inter-operate with the Java APIs and compiles down into byte code which can then be executed on the JVM.</p>
<p style="text-align: justify">In this example as well we would make use of the same json document described here.</p>
<p style="text-align: justify">Groovy support for Json was added in the 1.8 version when a new groovy.json package was added. So you dont have to use an external API for parsing the json documents. <a href="http://groovy.codehaus.org/api/groovy/json/JsonSlurper.html" target="_blank">JsonSlurper</a> parses the given json document into a data structure of lists and maps. Due to this conversion the name=value pairs in the json document can be accessed by object.name notation where object represents the json object and name represents the name of the attribute.</p>
<p>The model bean in this case is defined as:</p>
<pre class="brush: groovy; title: ; notranslate">
class GroovyUser{
  def id
  def screenName
  def name
  def url
  def followersCount
  def friendsCount
  def favouritesCount
  def statusesCount

  GroovyUser(id,
        screenName,
        name,
        url,
        followersCount,
        friendsCount,
        favouritesCount,
        statusesCount){
    this.id = id
    this.screenName = screenName
    this.name = name
    this.url = url
    this.followersCount = followersCount
    this.friendsCount = friendsCount
    this.favouritesCount = favouritesCount
    this.statusesCount = statusesCount

  }

  def String toString(){
    return &quot;Name: &quot;+this.name+&quot;n&quot;+
    &quot;ScreenName:&quot;+this.screenName+&quot;n&quot;+
    &quot;Followers: &quot;+this.followersCount+&quot;n&quot;+
    &quot;Friends: &quot;+this.friendsCount+&quot;n&quot;+
    &quot;Favourites: &quot;+this.favouritesCount+&quot;n&quot;+
    &quot;Statuses: &quot;+this.statusesCount+&quot;n&quot;
  }
}
</pre>
<p>As Groovy can be used a scripting language, the following code need not be compiled instead one could directly run the code by:</p>
<pre class="brush: bash; title: ; notranslate">$ groovy filename.groovy</pre>
<p>You would have to download and install the required binaries and libraries for running the groovy programs. More can be found at the <a href="http://groovy.codehaus.org/" target="_blank">official site</a>.</p>
<pre class="brush: groovy; title: ; notranslate">
import groovy.json.JsonSlurper

def jsonSlurper = new JsonSlurper();

/*
Read the JSON from the file system
*/
def reader = new BufferedReader(
              new FileReader(&quot;/home/mohamed/twitterUser.json&quot;))
def parsedData = jsonSlurper.parse(reader)
def usersList = new ArrayList&lt;GroovyUser&gt;()
parsedData.each {
  aUser -&gt;
    def user = new GroovyUser(aUser.id,
    aUser.screen_name,
    aUser.name,
    aUser.url,
    aUser.followers_count,
    aUser.friends_count,
    aUser.favourites_count,
    aUser.statuses_count)
    usersList.add(user)
}

usersList.each {aUser -&gt; println aUser}
</pre>
<p>There are few new things to note here:</p>
<ul>
<li>As Groovy supports closures, we are making use of the methods like &#8220;each&#8221; which is provided by the Groovy API on collections. The each method would for each element in the collection, execute the block of code provided with the each method. Something like-myCollection.each{ aData -&gt; println aData }</li>
<li>Semi colons are optional</li>
<li>Type details for the variables are optional, so you can see that the variables have been declared by using &#8220;def&#8221; keyword but no type is declared.</li>
<li>Look at the tremendous decrease in the number of lines of code written above. Parsing Json in Groovy is such a pleasure.</li>
</ul>
<p style="text-align: justify">On a closing note, the difference in the lines of code in Java and Groovy might tempt few of them to learn the syntax of the language. I am sure learning groovy is pretty easy as it has a short learning curve. Over the last few years there has been a transition to polyglot programming and with the advent of new languages which run on the JVM, they can inter-operate with an existing java application with no additional overhead.</p>
<p>We would over the coming days try to provide a few getting started like tutorials for Groovy and other JVM languages like Scala.</p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2012/04/parsing-json-using-groovy/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
