<?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>Addictive Software &#187; Development</title>
	<atom:link href="http://blog.addictivesoftware.net/tag/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.addictivesoftware.net</link>
	<description>ramblings of a chaotic mind</description>
	<lastBuildDate>Thu, 08 Dec 2011 16:08:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Developing for the Cloud</title>
		<link>http://blog.addictivesoftware.net/2011/12/developing-for-the-cloud/</link>
		<comments>http://blog.addictivesoftware.net/2011/12/developing-for-the-cloud/#comments</comments>
		<pubDate>Thu, 08 Dec 2011 15:27:06 +0000</pubDate>
		<dc:creator>gertjan</dc:creator>
				<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Lift]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://blog.addictivesoftware.net/?p=315</guid>
		<description><![CDATA[Lately I&#8217;m becoming more and more aware of the possibilities that are out there for developers, both in building, testing and running your applications in the cloud. So I changed a hobby project i&#8217;m working on to be build, tested and deployed in the cloud. and I like to share my experiences here. Currently i&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I&#8217;m becoming more and more aware of the possibilities that are out there for developers, both in building, testing and running your applications in the cloud.<br />
So I changed a hobby project i&#8217;m working on to be build, tested and deployed in the cloud. and I like to share my experiences here.</p>
<p>Currently i&#8217;m using 3 parties to get the picture complete</p>
<ol>
<li><a title="GitHub" href="http://www.github.com" target="_blank">Github </a>- Online Git repositories with nice collaboration features, if you know it, you&#8217;ll be using it, if you don&#8217;t, have a look, you&#8217;ll never look back</li>
<li><a title="CloudBees" href="http://www.cloudbees.com/" target="_blank">Cloudbees</a> &#8211; Free PaaS with Git Repository and Jenkins CI instance, you could also run your apps here, but no support for Scala, Lift at the time I looked at it</li>
<li><a title="Cloud Foundry" href="http://www.cloudfoundry.com" target="_blank">Cloudfoundry</a> &#8211; Free for now  (Beta) PaaS from VMWare &#8211; Support Scala and Lift, which makes it easy for me.</li>
</ol>
<div>For this to work I needed to make a couple of changes to my application that I will explain below.</div>
<h3>Registering a app at cloudfoundry</h3>
<p>Once you have an account and installed the command-line tools you can claim your little application space at the hosted cloudfoundry (or download and run a micro.cloudfoundry instance in your local Vmware system)</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ vmc target api.cloudfoundry.com
$ vmc <span style="color: #c20cb9; font-weight: bold;">login</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>email<span style="color: #7a0874; font-weight: bold;">&#93;</span>
$ vmc push my-app <span style="color: #660033;">--path</span> target<span style="color: #000000; font-weight: bold;">/</span>war <span style="color: #660033;">--url</span> my-app.cloudfoundry.com <span style="color: #660033;">--instances</span> <span style="color: #000000;">2</span> <span style="color: #660033;">--mem</span> 512k <span style="color: #660033;">--runtime</span> java
$ vmc create-service mysql my-app-db my-app</pre></div></div>

<p>What this means is, i&#8217;m pushing my war based maven build webapplication to the hosted cloudfoundry as two instances with each 512k memory and runtime java, I&#8217;m also binding it to a mysql service called my-app-db.<br />
To see the result of my actions I can type vmc apps</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">$ vmc apps
+-------------+----+---------+---------------------------+-------------+
| Application | #  | Health  | URLS                      | Services    |
+-------------+----+---------+---------------------------+-------------+
| my-app      | 1  | RUNNING | my-app.cloudfoundry.com   | my-app-db   |
+-------------+----+---------+---------------------------+-------------+</pre></div></div>

<p>The commandline tools also give me options to check logs and crashes, start and stop the application etc.</p>
<h3>Database connectivity</h3>
<p>As cloud applications/instances/services are usually not as easily identifiable with an ip-address, your application needs to find out about services (like a database) in a different manner.<br />
I&#8217;m doing this by using the org.cloudfoundry.runtime library to create a mysql Service for me</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;">  <span style="color: #0000ff; font-weight: bold;">object</span> CloudFoundryConnection <span style="color: #0000ff; font-weight: bold;">extends</span> ConnectionManager <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> newConnection<span style="color: #F78811;">&#40;</span>name<span style="color: #000080;">:</span> ConnectionIdentifier<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Box<span style="color: #F78811;">&#91;</span>Connection<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">try</span> <span style="color: #F78811;">&#123;</span>
        <span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">cloudfoundry</span>.<span style="color: #000000;">runtime</span>.<span style="color: #000000;">env</span>.<span style="color: #000080;">_</span>
        <span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">cloudfoundry</span>.<span style="color: #000000;">runtime</span>.<span style="color: #000000;">service</span>.<span style="color: #000000;">relational</span>.<span style="color: #000080;">_</span>
        Full<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> MysqlServiceCreator<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> CloudEnvironment<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
                        .<span style="color: #000000;">createSingletonService</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">service</span>.<span style="color: #000000;">getConnection</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
      <span style="color: #F78811;">&#125;</span> <span style="color: #0000ff; font-weight: bold;">catch</span> <span style="color: #F78811;">&#123;</span>
        <span style="color: #0000ff; font-weight: bold;">case</span> e <span style="color: #000080;">:</span> Exception <span style="color: #000080;">=&gt;</span> Empty
      <span style="color: #F78811;">&#125;</span>
    <span style="color: #F78811;">&#125;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> releaseConnection<span style="color: #F78811;">&#40;</span>conn<span style="color: #000080;">:</span> Connection<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>conn.<span style="color: #000000;">close</span><span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>This object will return a connection if it is running in the cloud, otherwise I&#8217;ll fall back to the normal database connection of in my case an H2 database which I use for development<br />
so I also changed the DB initialisation part of Lift in its Boot class.<br />
The order of DB discovery is now 1) jndi connection, 2) cloudfoundry 3) connection in Properties file 4) H2 database</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span><span style="color: #000080;">!</span>DB.<span style="color: #000000;">jndiJdbcConnAvailable_</span><span style="color: #000080;">?</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">val</span> connection <span style="color: #000080;">=</span> CloudFoundryConnection
      connection.<span style="color: #000000;">newConnection</span><span style="color: #F78811;">&#40;</span>DefaultConnectionIdentifier<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">match</span> <span style="color: #F78811;">&#123;</span>
        <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #F78811;">&#40;</span>Full<span style="color: #F78811;">&#40;</span>connection<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span>
          DB.<span style="color: #000000;">defineConnectionManager</span><span style="color: #F78811;">&#40;</span>DefaultConnectionIdentifier, connection<span style="color: #F78811;">&#41;</span>
        <span style="color: #F78811;">&#125;</span>
        <span style="color: #0000ff; font-weight: bold;">case</span> Empty <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span>
          <span style="color: #0000ff; font-weight: bold;">val</span> vendor <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> StandardDBVendor<span style="color: #F78811;">&#40;</span>
            Props.<span style="color: #000000;">get</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;db.class&quot;</span><span style="color: #F78811;">&#41;</span> openOr <span style="color: #6666FF;">&quot;org.h2.Driver&quot;</span>,
            Props.<span style="color: #000000;">get</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;db.url&quot;</span><span style="color: #F78811;">&#41;</span> openOr <span style="color: #6666FF;">&quot;jdbc:h2:lift_proto.db;AUTO_SERVER=TRUE&quot;</span>,
            Props.<span style="color: #000000;">get</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;db.user&quot;</span><span style="color: #F78811;">&#41;</span>,
            Props.<span style="color: #000000;">get</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;db.password&quot;</span><span style="color: #F78811;">&#41;</span>
          <span style="color: #F78811;">&#41;</span>
          LiftRules.<span style="color: #000000;">unloadHooks</span>.<span style="color: #000000;">append</span><span style="color: #F78811;">&#40;</span>vendor.<span style="color: #000000;">closeAllConnections_</span><span style="color: #000080;">!</span> <span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span>
          DB.<span style="color: #000000;">defineConnectionManager</span><span style="color: #F78811;">&#40;</span>DefaultConnectionIdentifier, vendor<span style="color: #F78811;">&#41;</span>
        <span style="color: #F78811;">&#125;</span>
      <span style="color: #F78811;">&#125;</span>
    <span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<h3>Maven integration</h3>
<p>Cloudfoundry also provides a maven plugin that allows most of the functionality to be run as part of a maven goal, which is ideal when you want to deploy from your Jenkins build.<br />
Here are the changes to my pom.xml<br />
Add the repository for the org.cloudfoundry.runtime dependency</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;repository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>springsource-milestones<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>SpringSource Milestones Proxy<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>https://oss.sonatype.org/content/repositories/springsource-milestones<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/repository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Add the dependency itself</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.cloudfoundry<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>cloudfoundry-runtime<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.6.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>For the plugin add the plugin repository</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pluginRepository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>repository.springframework.maven.milestone<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Spring Framework Maven Milestone Repository<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://maven.springframework.org/milestone<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pluginRepository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>and the plugin itself</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.cloudfoundry<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>cf-maven-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.0.M1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;server<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mycloudfoundry-instance<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/server<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://api.cloudfoundry.com<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>medicate.cloudfoundry.com<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;memory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>512<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/memory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;instances<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/instances<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Username and password information you can specify in the servers section of your .m2/settings.xml file</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;server<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mycloudfoundry-instance<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;username<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>email@address.com<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/username<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;password<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>s3cr3t<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/password<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/server<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>now you can update your deployed application by a simple</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">$ mvn cf:update</pre></div></div>

<h3> Continous Integration on Cloudbees</h3>
<p>First setup an account at cloudbees.com, create a repository and a Jenkins instance, and make sure cloudbees has your public key to allow git pushes.</p>
<p>Then setup a maven 2/3 build in Jenkins, setting the scm section to watch for changes from your cloudbees git repository.</p>
<p>Make sure your build name doesn&#8217;t have any spaces in it, cloudbees will choke on that, at least with the scala compiler.</p>
<p>As we need the login information from the <strong>.m2/settings.xml</strong>, you can upload this through webdav to <strong>repository-[username].forge.cloudbees.com/private</strong> directory. </p>
<p>And then in our jenkins build set <strong>/private/[username]/settings.xml</strong> in the alternative maven settings field (advanced button in the maven section)</p>
<p>I&#8217;ve set the maven goal to <strong>clean scala:doc cf:update</strong> meaning it will compile, test, create scala docs and deploy the application to cloudfoundry.</p>
<p>to finish it up you need to add a remote to cloudbees in your local git repository and push your changes. this then will trigger a build and deploy your app.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">git remote add cloudbees ssh://git@git.cloudbees.com/[username]/my-app.git</pre></div></div>

<p>To conclude, besides the 2 i&#8217;ve mentioned, there are a number of solutions out there, that all work in similar ways, but each still has it own requirements, If you want to run Java, Ruby, python, php, node.js applications, with Mysql, postgres, MongoDb, CouchDb, or RabbitMQ, you can get it to work on one or more of these providers.</p>
<p>Last but not least here is a picture from the actual application i&#8217;m building to visualize all I&#8217;ve talked about above:<br />
<img src="/wp-content/uploads/2011/12/dev4cloud.png" alt="Develop for the Cloud" /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.addictivesoftware.net/2011/12/developing-for-the-cloud/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unittesting custom tags with Selenium Webdriver</title>
		<link>http://blog.addictivesoftware.net/2011/11/unittesting-custom-tags-with-selenium-webdriver/</link>
		<comments>http://blog.addictivesoftware.net/2011/11/unittesting-custom-tags-with-selenium-webdriver/#comments</comments>
		<pubDate>Tue, 29 Nov 2011 08:55:14 +0000</pubDate>
		<dc:creator>gertjan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Jsp]]></category>
		<category><![CDATA[Selenium]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unittest]]></category>
		<category><![CDATA[Webdriver]]></category>

		<guid isPermaLink="false">http://blog.addictivesoftware.net/?p=301</guid>
		<description><![CDATA[In the company I work in we have some complex tags that are not easily unit-testable because there are some dependencies between them, the best option here should be to re-factor them, unfortunately as this would break backward compatibility, we have a process of deprecation to go through until we can actually do that. But [...]]]></description>
			<content:encoded><![CDATA[<p>In the company I work in we have some complex tags that are not easily unit-testable because there are some dependencies between them, the best option here should be to re-factor them, unfortunately as this would break backward compatibility, we have a process of deprecation to go through until we can actually do that.</p>
<p>But code with hardly any tests on them always makes me feel uncomfortable. so I decided to do some (almost integration) tests but running as part of the normal unit test suite<br />
I&#8217;m doing this by placing the tags on a jsp, running this on a embedded Jetty Server and using <a href="http://seleniumhq.org/docs/03_webdriver.html">Selenium Webdriver</a> to test the resulting html.</p>
<p>First the base class, which takes care of starting/stopping the embedded Jetty Server and setting up the web driver.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> BaseTest <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> port <span style="color: #339933;">=</span> <span style="color: #cc66cc;">9595</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> Server server <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Server<span style="color: #009900;">&#40;</span>port<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> baseUrl <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://localhost:&quot;</span> <span style="color: #339933;">+</span> port <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">static</span> WebDriver driver<span style="color: #339933;">;</span>
&nbsp;
    @BeforeClass
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//configure jetty as an exploded war</span>
        <span style="color: #003399;">URL</span> webAppUrl <span style="color: #339933;">=</span> BaseTest.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getClassLoader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        WebAppContext wac <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WebAppContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        wac.<span style="color: #006633;">setContextPath</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        wac.<span style="color: #006633;">setWar</span><span style="color: #009900;">&#40;</span>webAppUrl.<span style="color: #006633;">toExternalForm</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        server.<span style="color: #006633;">setHandler</span><span style="color: #009900;">&#40;</span>wac<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        server.<span style="color: #006633;">setStopAtShutdown</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//makes sure the sever is stopped even if the @AfterClass is never reached</span>
        server.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//HtmlUnit drive doesn't give a popup, the rest of the drivers do</span>
        driver <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HtmlUnitDriver<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @AfterClass
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> teardown<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
        driver.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        server.<span style="color: #006633;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In my project I have setup a resources directory that contains a WEB-INF/ with a web.xml containing my custom tag definitions and my tld file as you would with a normal web project<br />
Webdriver comes with a set of Drivers for different browsers (Firefox, chrome, IE, iPhone, Android, etc) for testing browser compatibility, I don&#8217;t care about that, the tags I&#8217;m testing do not output any html/css that might require this.<br />
The HtmlUnit driver is internal and doesn&#8217;t popup a browser window so ideal for my purpose.</p>
<p>Writing a test now becomes very easy:</p>
<p>In this case I have a custom tag that when invoked simply outputs &#8220;Hello World&#8221;</p>
<p>my test jsp:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot; pageEncoding=&quot;UTF-8&quot;  isELIgnored=&quot;false&quot; %&gt;
&lt;%@ taglib uri=&quot;/customtaglib&quot; prefix=&quot;custom&quot; %&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Hello World&lt;/title&gt;
  &lt;/head&gt;        
  &lt;body&gt;
    &lt;p&gt;&lt;custom:helloworld /&gt;&lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>The Test class:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorldTest <span style="color: #000000; font-weight: bold;">extends</span> BaseTest <span style="color: #009900;">&#123;</span>
&nbsp;
    @Test
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testHelloWorldTag<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        driver.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>baseUrl <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;helloworld.jsp&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World&quot;</span>, driver.<span style="color: #006633;">getTitle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        WebElement element <span style="color: #339933;">=</span> driver.<span style="color: #006633;">findElement</span><span style="color: #009900;">&#40;</span>By.<span style="color: #006633;">cssSelector</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;body p&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World&quot;</span>, element.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Testing the resulting html is made very easy, by having the WebElement findElement() and List<WebElement> findElements() methods.<br />
and then setting predicates with the By class. for instance By.cssSelector(), By.tagName() etc.<br />
It is also possible to test click throughs and form submits, by calling click() or submit() on WebElements.</p>
<p>Added bonus is that although the tag code is running in the embedded jetty server and not in the unit tests itself it&#8217;s coverage is measured by our code coverage tools</p>
<p>One thing to look out for is for the embedded Jetty to work with JSP&#8217;s you need to add jetty&#8217;s version of the jsp spec to your project not the javax.servlet.* ones.<br />
my maven dependencies look likes this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.seleniumhq.selenium<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>selenium-java<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.12.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.mortbay.jetty<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>jetty<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>6.1.22<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.mortbay.jetty<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>jsp-api-2.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>6.1.14<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.mortbay.jetty<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>jsp-2.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>6.1.14<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
             <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>junit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
             <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>junit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
             <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4.7<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
             <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.addictivesoftware.net/2011/11/unittesting-custom-tags-with-selenium-webdriver/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MapReduce using Actors</title>
		<link>http://blog.addictivesoftware.net/2011/08/mapreduce-using-actors/</link>
		<comments>http://blog.addictivesoftware.net/2011/08/mapreduce-using-actors/#comments</comments>
		<pubDate>Thu, 11 Aug 2011 21:21:16 +0000</pubDate>
		<dc:creator>gertjan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Actor]]></category>
		<category><![CDATA[Akka]]></category>
		<category><![CDATA[MapReduce]]></category>

		<guid isPermaLink="false">http://blog.addictivesoftware.net/?p=228</guid>
		<description><![CDATA[In this blog post I would like to do a basic example on how to do map reduce using actors According to Wikipedia: In computer science, the Actor model is a mathematical model of concurrent computation that treats &#8220;actors&#8221; as the universal primitives of concurrent digital computation: in response to a message that it receives, [...]]]></description>
			<content:encoded><![CDATA[<p>In this blog post I would like to do a basic example on how to do map reduce using actors</p>
<p>According to <a href="http://en.wikipedia.org/wiki/Actor_model">Wikipedia</a>:</p>
<blockquote><p>In computer science, the Actor model is a mathematical model of concurrent computation that treats &#8220;actors&#8221; as the universal primitives of concurrent digital computation: in response to a message that it receives, an actor can make local decisions, create more actors, send more messages, and determine how to respond to the next message received.</p>
<p>The Actor model adopts the philosophy that everything is an actor. This is similar to the everything is an object philosophy used by some object-oriented programming languages, but differs in that object-oriented software is typically executed sequentially, while the Actor model is inherently concurrent.<br />
An actor is a computational entity that, in response to a message it receives, can concurrently:</p>
<ul>
<li>send a finite number of messages to other actors;</li>
<li>create a finite number of new actors;</li>
<li>designate the behavior to be used for the next message it receives.</li>
</ul>
<p>There is no assumed sequence to the above actions and they could be carried out in parallel.</p></blockquote>
<p>A number of languages have the actor model implemented in their core (Io, Erlang, Scala), for most other languages it&#8217;s available as a library (Akka for Java, Retlang for .Net, Haskell-Actor for Haskell, Parely and Pykka for Python)</p>
<p>In this example I&#8217;ll be using the <a href="http://www.akka.io">Akka</a> framework for Java and Scala, which brings actors to the enterprise level by adding for instance fault tolerance (supervisors, let it crash semantics) and remote actors, If you want to play around with this yourself the best place to start is to use the <a href="http://typesafe.com/">typesafe stack</a> which includes Akka and Scala</p>
<p>I&#8217;m using a typical map/reduce use case, namely counting words in a document<br />
I do this by having a &#8216;master&#8217; actor that divides the work in lines, have a number of &#8216;worker&#8217; actors that count the words for a single line, replying the result to the &#8216;master&#8217; to aggregate</p>
<p>Messages that actors send to each other are simply objects (case classes, the receive method typically uses pattern matching to determine which message has arrived), multiple messages to an actor will be queued, and handled sequentially</p>
<p>So let&#8217;s create the message objects</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>30
31
32
33
34
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;">  <span style="color: #008000; font-style: italic;">// Messages</span>
  <span style="color: #0000ff; font-weight: bold;">sealed</span> <span style="color: #0000ff; font-weight: bold;">trait</span> MapReduceMessage
  <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #0000ff; font-weight: bold;">class</span> CountDocument<span style="color: #F78811;">&#40;</span>document<span style="color: #000080;">:</span> Iterator<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">extends</span> MapReduceMessage
  <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #0000ff; font-weight: bold;">class</span> CountLine<span style="color: #F78811;">&#40;</span>line<span style="color: #000080;">:</span> String<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">extends</span> MapReduceMessage
  <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #0000ff; font-weight: bold;">class</span> Result<span style="color: #F78811;">&#40;</span>values<span style="color: #000080;">:</span> Map<span style="color: #F78811;">&#91;</span>String, Int<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">extends</span> MapReduceMessage</pre></td></tr></table></div>

<p>The <strong>CountDocument </strong>which will be send to the master, the <strong>CountLine </strong>to each worker, and each worker will return a <strong>Result </strong> when done</p>
<p>as each receive method returns a Partial Function[T] which allows for several classes to handle all the messages, the sealed trait super type will actually generate a warning when not all messages are handled. neat trick that.<br />
It will also make it impossible to add messages outside of your class <img src='http://blog.addictivesoftware.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  which could be something you want. or not. </p>
<p>Now on to the Master</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #008000; font-style: italic;">// Master Actor, creates Worker Actors, distributes work and gathers results</span>
  <span style="color: #0000ff; font-weight: bold;">class</span> Master<span style="color: #F78811;">&#40;</span>nrOfWorkers<span style="color: #000080;">:</span> Int, latch<span style="color: #000080;">:</span>CountDownLatch<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">extends</span> Actor <span style="color: #F78811;">&#123;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">val</span> workers <span style="color: #000080;">=</span> Vector.<span style="color: #000000;">fill</span><span style="color: #F78811;">&#40;</span>nrOfWorkers<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>actorOf<span style="color: #F78811;">&#91;</span>CountLineWorker<span style="color: #F78811;">&#93;</span>.<span style="color: #000000;">start</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> router <span style="color: #000080;">=</span> Routing.<span style="color: #000000;">loadBalancerActor</span><span style="color: #F78811;">&#40;</span>CyclicIterator<span style="color: #F78811;">&#40;</span>workers<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">start</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">val</span> resultMap <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> HashMap<span style="color: #F78811;">&#91;</span>String, Int<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">var</span> start <span style="color: #000080;">:</span> Long <span style="color: #000080;">=</span> <span style="color: #000080;">_</span>
    <span style="color: #0000ff; font-weight: bold;">var</span> count <span style="color: #000080;">:</span> Long <span style="color: #000080;">=</span> <span style="color: #F78811;">0</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">def</span> receive <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> CountDocument<span style="color: #F78811;">&#40;</span>lines <span style="color: #000080;">:</span> Iterator<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span>
&nbsp;
        lines.<span style="color: #000000;">foreach</span><span style="color: #F78811;">&#40;</span>line <span style="color: #000080;">=&gt;</span>
              <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span><span style="color: #000080;">!</span>line.<span style="color: #000000;">isEmpty</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
                count <span style="color: #000080;">=</span> count+<span style="color: #F78811;">1</span><span style="color: #000080;">;</span>
                router <span style="color: #000080;">!</span> CountLine<span style="color: #F78811;">&#40;</span>line<span style="color: #F78811;">&#41;</span>
              <span style="color: #F78811;">&#125;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
        <span style="color: #008000; font-style: italic;">//shutdown actors</span>
        router <span style="color: #000080;">!</span> Broadcast<span style="color: #F78811;">&#40;</span>PoisonPill<span style="color: #F78811;">&#41;</span>
        router <span style="color: #000080;">!</span> PoisonPill
&nbsp;
      <span style="color: #0000ff; font-weight: bold;">case</span> Result<span style="color: #F78811;">&#40;</span>values<span style="color: #000080;">:</span> Map<span style="color: #F78811;">&#91;</span>String, Int<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span>
&nbsp;
        <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>key, value<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">&lt;</span>- values<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
          resultMap.<span style="color: #000000;">put</span><span style="color: #F78811;">&#40;</span>key, resultMap.<span style="color: #000000;">getOrElse</span><span style="color: #F78811;">&#40;</span>key, <span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span>+value<span style="color: #F78811;">&#41;</span>
        <span style="color: #F78811;">&#125;</span>
        count <span style="color: #000080;">=</span> count - <span style="color: #F78811;">1</span><span style="color: #000080;">;</span>
        <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>count <span style="color: #000080;">&lt;=</span> <span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span> self.<span style="color: #000000;">stop</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> preStart<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
      start <span style="color: #000080;">=</span> System.<span style="color: #000000;">currentTimeMillis</span>
    <span style="color: #F78811;">&#125;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> postStop<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">val</span> end <span style="color: #000080;">=</span> System.<span style="color: #000000;">currentTimeMillis</span>-start
      println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Result after %s ms :&quot;</span>.<span style="color: #000000;">format</span><span style="color: #F78811;">&#40;</span>end<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
      <span style="color: #0000ff; font-weight: bold;">for</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>key, value<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">&lt;</span>- resultMap.<span style="color: #000000;">toList</span>.<span style="color: #000000;">sortBy</span><span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>.<span style="color: #000080;">_</span>2<span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">reverse</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
        println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;%s: %s&quot;</span>.<span style="color: #000000;">format</span><span style="color: #F78811;">&#40;</span>value, key<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
      <span style="color: #F78811;">&#125;</span>
      latch.<span style="color: #000000;">countDown</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>An actor class needs to implement the Actor interface which has one method called receive<br />
The  Routing.LoadbalancingActor is a built in actor that forwards messages send to it according to the scheme passed to it, in our case in a round robin style.<br />
When receiving a CountDocument messages it will send each line to a worker, it will then tell the workers and the router to shutdown by sending them a PosionPill message.<br />
When receiving a Result message it will aggregate the result and after receiving all results it will stop itself.<br />
we override the preStart() and postStop() methods to allow us to calculate spend time and reporting the result</p>
<p>the ! in</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">router <span style="color: #000080;">!</span> CountLine<span style="color: #F78811;">&#40;</span>line<span style="color: #F78811;">&#41;</span></pre></div></div>

<p>is a so called bang method, meaning fire and forget, there are also methods that will reply eventually (!!) and reply eventually with a Future (!!!)</p>
<p>The Worker looks as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #008000; font-style: italic;">//Actor that counts the words for a single line</span>
  <span style="color: #0000ff; font-weight: bold;">class</span> CountLineWorker <span style="color: #0000ff; font-weight: bold;">extends</span> Actor <span style="color: #F78811;">&#123;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">def</span> receive <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
       <span style="color: #0000ff; font-weight: bold;">case</span> CountLine<span style="color: #F78811;">&#40;</span>line<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span>
         self reply Result<span style="color: #F78811;">&#40;</span>countWords<span style="color: #F78811;">&#40;</span>line<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">def</span> countWords<span style="color: #F78811;">&#40;</span>line<span style="color: #000080;">:</span> String<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span>Map<span style="color: #F78811;">&#91;</span>String, Int<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">val</span> result <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> HashMap<span style="color: #F78811;">&#91;</span>String, Int<span style="color: #F78811;">&#93;</span>
&nbsp;
      <span style="color: #6666FF;">&quot;[^A-Za-z0-9<span style="color: #0000ff; font-weight: bold;">\u</span>0020]&quot;</span>.<span style="color: #000000;">r</span>.<span style="color: #000000;">replaceAllIn</span><span style="color: #F78811;">&#40;</span>line, <span style="color: #6666FF;">&quot;&quot;</span><span style="color: #F78811;">&#41;</span>
            .<span style="color: #000000;">split</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot; &quot;</span><span style="color: #F78811;">&#41;</span>
            .<span style="color: #000000;">foreach</span><span style="color: #F78811;">&#40;</span>word <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span>
              result.<span style="color: #000000;">put</span><span style="color: #F78811;">&#40;</span>word, result.<span style="color: #000000;">getOrElse</span><span style="color: #F78811;">&#40;</span>word, <span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span>+<span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span>
            <span style="color: #F78811;">&#125;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
      result
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>Should speak for itself, sanitizes and then count the words in a line, and replies to the sending actor with the result.</p>
<p>now wrap it in a main class that loads the document and sends it off to the master</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">object</span> MapReduce <span style="color: #0000ff; font-weight: bold;">extends</span> App <span style="color: #F78811;">&#123;</span>
&nbsp;
  countWordsInFile<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;src/main/resources/test.txt&quot;</span>, <span style="color: #F78811;">6</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> countWordsInFile<span style="color: #F78811;">&#40;</span>fileName<span style="color: #000080;">:</span> String, nrOfWorkers<span style="color: #000080;">:</span> Int<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">val</span> source <span style="color: #000080;">=</span> scala.<span style="color: #000000;">io</span>.<span style="color: #000000;">Source</span>.<span style="color: #000000;">fromFile</span><span style="color: #F78811;">&#40;</span>fileName<span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> document <span style="color: #000080;">=</span> source.<span style="color: #000000;">getLines</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">val</span> latch <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> CountDownLatch<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">val</span> master <span style="color: #000080;">=</span> actorOf<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> Master<span style="color: #F78811;">&#40;</span>nrOfWorkers, latch<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">start</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
    master <span style="color: #000080;">!</span> CountDocument<span style="color: #F78811;">&#40;</span>document<span style="color: #F78811;">&#41;</span>
&nbsp;
    latch.<span style="color: #000000;">await</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
    source.<span style="color: #000000;">close</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>This will load the document and sends it off to the master also instructing it to create 6 workers, as actors are called asynchronously, the Countdown latch is there to prevent the program from stopping before all the work is done.</p>
<p>The full source code is available as a <a href="https://gist.github.com/1123718">gist </a> on github</p>
<p>A couple of other interesting Akka features are</p>
<p><strong>Become</strong>:<br />
you can tell each actor to become another, this allows for hot code swapping, which is essential in fault-tolerant, always up systems.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">  <span style="color: #0000ff; font-weight: bold;">def</span> receive <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
     <span style="color: #0000ff; font-weight: bold;">case</span> updateYourSelf<span style="color: #F78811;">&#40;</span>newMe<span style="color: #000080;">:</span>ActorRef<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span>
        become newMe
     <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span></pre></div></div>

<p><strong>Typed Actors</strong> (example blatantly stolen from Akka documentation):<br />
extending any POJO with TypedActor will turn them into actors<br />
your class need to have a separate interface/implementation for this to work</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> RegistrationService <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> register<span style="color: #F78811;">&#40;</span>user<span style="color: #000080;">:</span> User, cred<span style="color: #000080;">:</span> Credentials<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Unit
  <span style="color: #0000ff; font-weight: bold;">def</span> getUserFor<span style="color: #F78811;">&#40;</span>username<span style="color: #000080;">:</span> String<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> User
<span style="color: #F78811;">&#125;</span>
public <span style="color: #0000ff; font-weight: bold;">class</span> RegistrationServiceImpl <span style="color: #0000ff; font-weight: bold;">extends</span> TypedActor <span style="color: #0000ff; font-weight: bold;">with</span> RegistrationService <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> register<span style="color: #F78811;">&#40;</span>user<span style="color: #000080;">:</span> User, cred<span style="color: #000080;">:</span> Credentials<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Unit <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    ... <span style="color: #008000; font-style: italic;">// register user</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> getUserFor<span style="color: #F78811;">&#40;</span>username<span style="color: #000080;">:</span> String<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> User <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    ... <span style="color: #008000; font-style: italic;">// fetch user by username</span>
   user
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">val</span> service <span style="color: #000080;">=</span> TypedActor.<span style="color: #000000;">newInstance</span><span style="color: #F78811;">&#40;</span>classOf<span style="color: #F78811;">&#91;</span>RegistrationService<span style="color: #F78811;">&#93;</span>, classOf<span style="color: #F78811;">&#91;</span>RegistrationServiceImpl<span style="color: #F78811;">&#93;</span>, <span style="color: #F78811;">1000</span><span style="color: #F78811;">&#41;</span>
<span style="color: #008000; font-style: italic;">//last parameter is timeout for Futures</span></pre></div></div>

<p><strong>Remote Actors</strong> (example blatantly stolen from the Akka homepage):</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #008000; font-style: italic;">// server code</span>
<span style="color: #0000ff; font-weight: bold;">class</span> HelloWorldActor <span style="color: #0000ff; font-weight: bold;">extends</span> Actor <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> receive <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">case</span> msg <span style="color: #000080;">=&gt;</span> self reply <span style="color: #F78811;">&#40;</span>msg + <span style="color: #6666FF;">&quot; World&quot;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span>
remote.<span style="color: #000000;">start</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;localhost&quot;</span>, <span style="color: #F78811;">9999</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">register</span><span style="color: #F78811;">&#40;</span>
  <span style="color: #6666FF;">&quot;hello-service&quot;</span>, actorOf<span style="color: #F78811;">&#91;</span>HelloWorldActor<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
<span style="color: #008000; font-style: italic;">// client code</span>
<span style="color: #0000ff; font-weight: bold;">val</span> actor <span style="color: #000080;">=</span> remote.<span style="color: #000000;">actorFor</span><span style="color: #F78811;">&#40;</span>
  <span style="color: #6666FF;">&quot;hello-service&quot;</span>, <span style="color: #6666FF;">&quot;localhost&quot;</span>, <span style="color: #F78811;">9999</span><span style="color: #F78811;">&#41;</span>
<span style="color: #0000ff; font-weight: bold;">val</span> result <span style="color: #000080;">=</span> actor <span style="color: #000080;">!!</span> <span style="color: #6666FF;">&quot;Hello&quot;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.addictivesoftware.net/2011/08/mapreduce-using-actors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lift by use case 4: getting a list of authors for the authenticated user</title>
		<link>http://blog.addictivesoftware.net/2011/06/lift-by-use-case-4-getting-a-list-of-authors-for-the-authenticated-user/</link>
		<comments>http://blog.addictivesoftware.net/2011/06/lift-by-use-case-4-getting-a-list-of-authors-for-the-authenticated-user/#comments</comments>
		<pubDate>Wed, 08 Jun 2011 12:50:23 +0000</pubDate>
		<dc:creator>gertjan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Lift]]></category>
		<category><![CDATA[rest]]></category>

		<guid isPermaLink="false">http://blog.addictivesoftware.net/?p=207</guid>
		<description><![CDATA[As our website grows and people can log in, add books to their own collection, a need to access this collection on their mobile arises. When starting the mobile application and entered the correct login information, a request is being made to get the list of authors. Clicking on an author will get a list [...]]]></description>
			<content:encoded><![CDATA[<p>As our website grows and people can log in, add books to their own collection, a need to access this collection on their mobile arises.</p>
<p>When starting the mobile application and entered the correct login information, a request is being made to get the list of authors.<br />
Clicking on an author will get a list of books from that author that the user owns, clicking on a book shows the details.</p>
<p>So the first query that needs implementing is: Give me a list of Authors for which the current user owned books</p>
<p>Given the  Following Mapper Data Model:<br />
Author <- (1) -> Book <- (2) -> User<br />
(1) An Author can have more books while books can be written by multiple authors (N-N)<br />
(2) A user can own multiple books, while a book can have multiple owners  (N-N)</p>
<p>note: When successfully authenticating in a user his API key is returned which is used in all other requests </p>
<p>Below we will see the code that executes this query<br />
what happens line by line is this:</p>
<ul>
<li>Line4: we get a list of books for the current user</li>
<li>Line5: we map the colllection to one that contains the authors for each book, the result is a list of lists of authors List[List[Author]]</li>
<li>Line6: foldLeft applies the method to each element in the list, from left to right, as the method ++ means adding two list&#8217;s together, we&#8217;ll end up with one list containing all authors</li>
<li>Line7: distinct removes any duplicates in there</li>
<li>Line8: We sort the list on the author&#8217;s lastName property</li>
<li>Line9: we map it to a List of Xml fragments</li>
</ul>
<p>Scala&#8217;s inline XML support allows us to simple put the Authors tag around it.<br />
The second case does the same but gives a JSON response</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;">serve <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #6666FF;">&quot;api&quot;</span> <span style="color: #000080;">::</span> key <span style="color: #000080;">::</span> <span style="color: #6666FF;">&quot;user&quot;</span> <span style="color: #000080;">::</span> <span style="color: #6666FF;">&quot;authors&quot;</span> <span style="color: #000080;">::</span> <span style="color: #6666FF;">&quot;all&quot;</span> <span style="color: #000080;">::</span> <span style="color: #000080;">_</span> XmlGet <span style="color: #000080;">_</span> <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #000080;">&lt;</span>Authors<span style="color: #000080;">&gt;</span> <span style="color: #F78811;">&#123;</span>
      Book.<span style="color: #000000;">findAll</span><span style="color: #F78811;">&#40;</span>In<span style="color: #F78811;">&#40;</span>Book.<span style="color: #000000;">id</span> ,BookUser.<span style="color: #000000;">book</span>, By<span style="color: #F78811;">&#40;</span>BookUser.<span style="color: #000000;">user</span>, getUserIdFromKey<span style="color: #F78811;">&#40;</span>key<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
        .<span style="color: #000000;">map</span><span style="color: #F78811;">&#40;</span>book <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span> book.<span style="color: #000000;">authors</span>.<span style="color: #000000;">get</span> <span style="color: #F78811;">&#125;</span><span style="color: #F78811;">&#41;</span>
        .<span style="color: #000000;">foldLeft</span><span style="color: #F78811;">&#40;</span>List<span style="color: #F78811;">&#91;</span>Author<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span> ++ <span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span>
        .<span style="color: #000000;">distinct</span>
        .<span style="color: #000000;">sortWith</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>a1<span style="color: #000080;">:</span>Author, a2<span style="color: #000080;">:</span>Author<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> a1.<span style="color: #000000;">lastName</span>.<span style="color: #000000;">is</span> <span style="color: #000080;">&lt;</span> a2.<span style="color: #000000;">lastName</span>.<span style="color: #000000;">is</span><span style="color: #F78811;">&#41;</span>
        .<span style="color: #000000;">map</span><span style="color: #F78811;">&#40;</span>author <span style="color: #000080;">=&gt;</span> author.<span style="color: #000000;">toXml</span><span style="color: #F78811;">&#41;</span>
      <span style="color: #F78811;">&#125;</span>
    <span style="color: #000080;">&lt;</span>/Authors<span style="color: #000080;">&gt;</span>
  <span style="color: #F78811;">&#125;</span>
  <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #6666FF;">&quot;api&quot;</span> <span style="color: #000080;">::</span> key <span style="color: #000080;">::</span> <span style="color: #6666FF;">&quot;user&quot;</span> <span style="color: #000080;">::</span> <span style="color: #6666FF;">&quot;authors&quot;</span> <span style="color: #000080;">::</span> <span style="color: #6666FF;">&quot;all&quot;</span> <span style="color: #000080;">::</span> <span style="color: #000080;">_</span> JsonGet <span style="color: #000080;">_</span> <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span>
    JsonWrapper <span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Authors&quot;</span>, <span style="color: #F78811;">&#123;</span>
      Book.<span style="color: #000000;">findAll</span><span style="color: #F78811;">&#40;</span>In<span style="color: #F78811;">&#40;</span>Book.<span style="color: #000000;">id</span> ,BookUser.<span style="color: #000000;">book</span>, By<span style="color: #F78811;">&#40;</span>BookUser.<span style="color: #000000;">user</span>, getUserIdFromKey<span style="color: #F78811;">&#40;</span>key<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
        .<span style="color: #000000;">map</span><span style="color: #F78811;">&#40;</span>book <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span> book.<span style="color: #000000;">authors</span>.<span style="color: #000000;">get</span> <span style="color: #F78811;">&#125;</span><span style="color: #F78811;">&#41;</span>
        .<span style="color: #000000;">foldLeft</span><span style="color: #F78811;">&#40;</span>List<span style="color: #F78811;">&#91;</span>Author<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span> ++ <span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span>
        .<span style="color: #000000;">distinct</span>
        .<span style="color: #000000;">sortWith</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>a1<span style="color: #000080;">:</span>Author, a2<span style="color: #000080;">:</span>Author<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> a1.<span style="color: #000000;">lastName</span>.<span style="color: #000000;">is</span> <span style="color: #000080;">&lt;</span> a2.<span style="color: #000000;">lastName</span>.<span style="color: #000000;">is</span><span style="color: #F78811;">&#41;</span>
        .<span style="color: #000000;">map</span><span style="color: #F78811;">&#40;</span>author <span style="color: #000080;">=&gt;</span> author.<span style="color: #000000;">toJson</span><span style="color: #F78811;">&#41;</span>
      <span style="color: #F78811;">&#125;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #008000; font-style: italic;">//helper method</span>
<span style="color: #0000ff; font-weight: bold;">def</span> getUserIdFromKey<span style="color: #F78811;">&#40;</span>key<span style="color: #000080;">:</span>String<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span>Long <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
  User.<span style="color: #000000;">find</span><span style="color: #F78811;">&#40;</span>By<span style="color: #F78811;">&#40;</span>User.<span style="color: #000000;">key</span>, key<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">match</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">case</span> Full<span style="color: #F78811;">&#40;</span>user<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> user.<span style="color: #000000;">id</span>
    <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">0</span> 
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.addictivesoftware.net/2011/06/lift-by-use-case-4-getting-a-list-of-authors-for-the-authenticated-user/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lift by use case 3: A REST interface</title>
		<link>http://blog.addictivesoftware.net/2011/03/lift-by-use-case-3-a-rest-interface/</link>
		<comments>http://blog.addictivesoftware.net/2011/03/lift-by-use-case-3-a-rest-interface/#comments</comments>
		<pubDate>Wed, 02 Mar 2011 09:39:33 +0000</pubDate>
		<dc:creator>gertjan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Lift]]></category>

		<guid isPermaLink="false">http://blog.addictivesoftware.net/?p=158</guid>
		<description><![CDATA[As the web application I&#8217;m currently building will have a mobile app, a REST Interface is a nice clean way for the mobile app to connect, retrieve and update information from the website. fortunately Liftweb makes it very easy to do this, all you need to do is extend your class with the RestHelper Trait [...]]]></description>
			<content:encoded><![CDATA[<p>As the web application I&#8217;m currently building will have a mobile app, a REST Interface is a nice clean way for the mobile app to connect, retrieve and update information from the website.<br />
fortunately Liftweb makes it very easy to do this, all you need to do is extend your class with the <a href="http://scala-tools.org/mvnsites/liftweb-2.0/framework/scaladocs/net/liftweb/http/rest/RestHelper.html">RestHelper</a> Trait and let liftweb know about it in your Boot.scala</p>
<p>Lets say we want to get a list of all books in the system when we access http://(server)/api/books/all</p>
<p>First we need to tell Lift we have a class that responds to requests in Boot.scala:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">LiftRules.<span style="color: #000000;">dispatch</span>.<span style="color: #000000;">append</span><span style="color: #F78811;">&#40;</span>BookRestApi<span style="color: #F78811;">&#41;</span></pre></div></div>

<p>then the class itself: BookRestApi.scala:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">object</span> BookRestApi <span style="color: #0000ff; font-weight: bold;">extends</span> RestHelper  <span style="color: #F78811;">&#123;</span>
  serve <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">// list of books</span>
    <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #6666FF;">&quot;api&quot;</span> <span style="color: #000080;">::</span> <span style="color: #6666FF;">&quot;books&quot;</span> <span style="color: #000080;">::</span> <span style="color: #6666FF;">&quot;all&quot;</span> <span style="color: #000080;">::</span> <span style="color: #000080;">_</span> XmlGet <span style="color: #000080;">_=&gt;</span>
      <span style="color: #F78811;">&#123;</span>
         <span style="color: #000080;">&lt;</span>Books<span style="color: #000080;">&gt;</span>
          <span style="color: #F78811;">&#123;</span>
            Book.<span style="color: #000000;">findAll</span>.<span style="color: #000000;">map</span><span style="color: #F78811;">&#40;</span>book <span style="color: #000080;">=&gt;</span> book.<span style="color: #000000;">toXml</span><span style="color: #F78811;">&#41;</span>
          <span style="color: #F78811;">&#125;</span>
         <span style="color: #000080;">&lt;</span>/Books<span style="color: #000080;">&gt;</span>
      <span style="color: #F78811;">&#125;</span>
    <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #6666FF;">&quot;api&quot;</span> <span style="color: #000080;">::</span> <span style="color: #6666FF;">&quot;books&quot;</span> <span style="color: #000080;">::</span> <span style="color: #6666FF;">&quot;all&quot;</span> <span style="color: #000080;">::</span> <span style="color: #000080;">_</span> JsonGet <span style="color: #000080;">_=&gt;</span>
      <span style="color: #F78811;">&#123;</span>
        JsonWrapper<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;books&quot;</span>, Book.<span style="color: #000000;">findAll</span>.<span style="color: #000000;">map</span><span style="color: #F78811;">&#40;</span>book <span style="color: #000080;">=&gt;</span> book.<span style="color: #000000;">toJS</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
      <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>The RestHelper trait parses the Url and makes the relevant parts available for Scala&#8217;s pattern matching capabilities.</p>
<p>for the first case statement:</p>
<ul>
<li>the <i>api :: books : all</i>  List will match if the url points to /api/books/all</li>
<li>the <i>XmlGet</i> method will match if the client has indicated he can accept xml by setting an accept header or by ending the url with a .xml suffix</li>
</ul>
<p>Scala&#8217;s inline XML capabilities make it very easy, to envelop the result in a &lt;Books&gt; element.</p>
<p>the second statement does the same but then when the client requests a json response.</p>
<p>For the Json part I used a JsonWrapper helper method because the inline code looked totally unreadable, this still isn&#8217;t very readable but at least it has a descriptive method name now <img src='http://blog.addictivesoftware.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;">  <span style="color: #0000ff; font-weight: bold;">def</span> JsonWrapper<span style="color: #F78811;">&#40;</span>name <span style="color: #000080;">:</span> String, content <span style="color: #000080;">:</span> JValue<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> JValue <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #F78811;">&#40;</span>name -<span style="color: #000080;">&gt;</span> content<span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>If you would call this method with the name &#8220;Books&#8221; and the content {&#8220;foo&#8221; : &#8220;bar&#8221; } it will output :</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;books&quot;</span> <span style="color: #339933;">:</span> 
  <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;foo&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;bar&quot;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This shows both a positive and negative aspect to the Scala language, it makes for a very flexible and fast development, but loses a lot in readability, which makes maintenance more difficult.</p>
<p>Now if would want to get a single book when I specify the id of that book, I&#8217;d add the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #6666FF;">&quot;api&quot;</span> <span style="color: #000080;">::</span> <span style="color: #6666FF;">&quot;books&quot;</span> <span style="color: #000080;">::</span> AsLong<span style="color: #F78811;">&#40;</span>id<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">::</span> <span style="color: #000080;">_</span> XmlGet <span style="color: #000080;">_=&gt;</span>
        <span style="color: #F78811;">&#123;</span>
          Book.<span style="color: #000000;">findByKey</span><span style="color: #F78811;">&#40;</span>id<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">match</span> <span style="color: #F78811;">&#123;</span>
            <span style="color: #0000ff; font-weight: bold;">case</span> Full<span style="color: #F78811;">&#40;</span>book<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span>book.<span style="color: #000000;">toXml</span><span style="color: #F78811;">&#125;</span>
            <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> NotFoundResponse<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Book with id &quot;</span> + id + <span style="color: #6666FF;">&quot; not found<span style="color: #0000ff; font-weight: bold;">\r</span><span style="color: #0000ff; font-weight: bold;">\n</span>&quot;</span><span style="color: #F78811;">&#41;</span>
          <span style="color: #F78811;">&#125;</span>
        <span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>This pattern will only match on /api/books/{id} and only if that Id can be converted to a Long, in which case it will try to look up a book with that id<br />
if the book isn&#8217;t found (the <a href="http://www.assembla.com/wiki/show/liftweb/Box">Box </a> returned from findByKey isn&#8217;t Full) it will give the text back</p>
<p>In the next article I&#8217;ll talk about PUT requests and authentication so we can add books through the rest interface</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.addictivesoftware.net/2011/03/lift-by-use-case-3-a-rest-interface/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lift by use case 2: News articles on the Homepage</title>
		<link>http://blog.addictivesoftware.net/2010/12/lift-by-use-case-2-news-articles-on-the-homepage/</link>
		<comments>http://blog.addictivesoftware.net/2010/12/lift-by-use-case-2-news-articles-on-the-homepage/#comments</comments>
		<pubDate>Fri, 24 Dec 2010 13:11:43 +0000</pubDate>
		<dc:creator>gertjan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Lift]]></category>

		<guid isPermaLink="false">http://blog.addictivesoftware.net/?p=126</guid>
		<description><![CDATA[In this second article I want to focus on the persistence layer of Lift by taking you through the following use case: As an admin I want to be able to add/edit news articles and choose whether or not they appear on the homepage For this we need to store the articles in a database, [...]]]></description>
			<content:encoded><![CDATA[<p>In this second article I want to focus on the persistence layer of Lift by taking you through the following use case:</p>
<blockquote><p>As an admin I want to be able to add/edit news articles and choose whether or not they appear on the homepage</p></blockquote>
<p>For this we need to store the articles in a database, A template and Snippet to show them and some way of managing them.<br />
Lift has 2 built-in persistence frameworks, <a href="http://www.assembla.com/wiki/show/liftweb/Mapper">Mapper</a> and Record, but also JPA is fully supported.<br />
As Mapper has CRUD and User management I&#8217;ve chosen to go with that one.</p>
<p>Our News article will have a Title, an Author, the content and a boolean that allows it to be seen on the homepage</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> News <span style="color: #0000ff; font-weight: bold;">extends</span> LongKeyedMapper<span style="color: #F78811;">&#91;</span>News<span style="color: #F78811;">&#93;</span>  <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> getSingleton <span style="color: #000080;">=</span> News
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">object</span> title <span style="color: #0000ff; font-weight: bold;">extends</span> MappedString<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">this</span>, <span style="color: #F78811;">100</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">object</span> author <span style="color: #0000ff; font-weight: bold;">extends</span> MappedString<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">this</span>, <span style="color: #F78811;">100</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">object</span> content <span style="color: #0000ff; font-weight: bold;">extends</span> MappedTextarea<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">this</span>, <span style="color: #F78811;">4000</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">object</span> showOnHomepage <span style="color: #0000ff; font-weight: bold;">extends</span> MappedBoolean<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">this</span><span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span>
<span style="color: #0000ff; font-weight: bold;">object</span> News <span style="color: #0000ff; font-weight: bold;">extends</span> News <span style="color: #0000ff; font-weight: bold;">with</span> LongKeyedMetaMapper<span style="color: #F78811;">&#91;</span>News<span style="color: #F78811;">&#93;</span></pre></td></tr></table></div>

<p>* A class and a object with the same name are called companion&#8217;s in Scala and the relationship is that the object is a singleton instance of the class, and they have access to each others private methods.</p>
<p>by extending News with LongKeyedMapper[News] we are able to persist it to the database.</p>
<p>Now we will mix-in some traits to add some extra functionality, a Trait in Scala is like a Java interface but allows for partial implementation. Say we want to add a primary key field and two date fields to store created and updated values, all we need to do is add the triats: IdPk (for primary key) and CreatedUpdated to the class definition.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> News <span style="color: #0000ff; font-weight: bold;">extends</span> LongKeyedMapper<span style="color: #F78811;">&#91;</span>News<span style="color: #F78811;">&#93;</span> <span style="color: #0000ff; font-weight: bold;">with</span> IdPK <span style="color: #0000ff; font-weight: bold;">with</span> CreatedUpdated</pre></td></tr></table></div>

<p>this will add the MappedLong object id, and 2 MappedDate objects created and updated to the News class</p>
<p>To add CRUD functionality we just add the CRUDify trait, this one needs to be added to the object instead of the class so that one now looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">object</span> News <span style="color: #0000ff; font-weight: bold;">extends</span> News <span style="color: #0000ff; font-weight: bold;">with</span> LongKeyedMetaMapper<span style="color: #F78811;">&#91;</span>News<span style="color: #F78811;">&#93;</span> <span style="color: #0000ff; font-weight: bold;">with</span> CRUDify<span style="color: #F78811;">&#91;</span>Long, News<span style="color: #F78811;">&#93;</span></pre></div></div>

<p>This will add the forms, validation and handling to the news object.</p>
<p>To hook this all up into Lift&#8217;s bootstrapping we have to add some lines to Boot.scala, which is the class that lift calls before anything else.<br />
The following line will make sure Lift knows about our persistence class and creates the database tables if needed</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">Schemifier.<span style="color: #000000;">schemify</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">true</span>, Schemifier.<span style="color: #000000;">infoF</span> <span style="color: #000080;">_</span>, News<span style="color: #F78811;">&#41;</span></pre></div></div>

<p>Lift works with a Sitemap, any page you want Lift to serve needs to be added to the sitemap. so we need to add our CRUD pages there fortunately CRUDify has a convenient helper method for this.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">val</span> menu <span style="color: #000080;">=</span> Menu<span style="color: #F78811;">&#40;</span>Loc<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;HomePage&quot;</span>, <span style="color: #6666FF;">&quot;index&quot;</span> <span style="color: #000080;">::</span> Nil, <span style="color: #6666FF;">&quot;Home Page&quot;</span>, Hidden<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:::</span> News.<span style="color: #000000;">menus</span>
LiftRules.<span style="color: #000000;">setSiteMap</span><span style="color: #F78811;">&#40;</span>SiteMap<span style="color: #F78811;">&#40;</span>menu<span style="color: #000080;">:_*</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span></pre></div></div>

<p>This will create a Homepage menu item and adds the news crud pages to it.</p>
<p>If we want to create a news item through code we could just execute the following line:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">News.<span style="color: #000000;">create</span>.<span style="color: #000000;">title</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;A news Item&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">author</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;gertjan&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">content</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Breaking news, an item has just been created&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">showOnHomePage</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">true</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">saveMe</span></pre></div></div>

<p>Next the snippet that will display a number of news items on the homepage</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> Homepage <span style="color: #F78811;">&#123;</span>
 <span style="color: #0000ff; font-weight: bold;">def</span> news<span style="color: #F78811;">&#40;</span>in<span style="color: #000080;">:</span> NodeSeq<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> NodeSeq <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> articles <span style="color: #000080;">=</span> News.<span style="color: #000000;">findAll</span><span style="color: #F78811;">&#40;</span>By<span style="color: #F78811;">&#40;</span>News.<span style="color: #000000;">showOnHomepage</span>, <span style="color: #0000ff; font-weight: bold;">true</span><span style="color: #F78811;">&#41;</span>, OrderBy<span style="color: #F78811;">&#40;</span>News.<span style="color: #000000;">createdAt</span>, Descending<span style="color: #F78811;">&#41;</span>, MaxRows<span style="color: #F78811;">&#91;</span>News<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">5</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">def</span> bindArticles<span style="color: #F78811;">&#40;</span>template<span style="color: #000080;">:</span> NodeSeq<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> NodeSeq <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
      articles.<span style="color: #000000;">flatMap</span> <span style="color: #F78811;">&#123;</span>
                <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #F78811;">&#40;</span>news<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> bind<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;article&quot;</span>, template, 
							<span style="color: #6666FF;">&quot;title&quot;</span> -<span style="color: #000080;">&gt;</span> news.<span style="color: #000000;">title</span>, 
							<span style="color: #6666FF;">&quot;author&quot;</span> -<span style="color: #000080;">&gt;</span> news.<span style="color: #000000;">author</span>, 
							<span style="color: #6666FF;">&quot;content&quot;</span> -<span style="color: #000080;">&gt;</span> StringHelper.<span style="color: #000000;">unescapeXml</span><span style="color: #F78811;">&#40;</span>news.<span style="color: #000000;">content</span><span style="color: #F78811;">&#41;</span>,
							<span style="color: #6666FF;">&quot;created&quot;</span> -<span style="color: #000080;">&gt;</span> news.<span style="color: #000000;">createdAt</span>,
							<span style="color: #6666FF;">&quot;updated&quot;</span> -<span style="color: #000080;">&gt;</span> news.<span style="color: #000000;">updatedAt</span><span style="color: #F78811;">&#41;</span>
      <span style="color: #F78811;">&#125;</span>
   <span style="color: #F78811;">&#125;</span>
&nbsp;
   Helpers.<span style="color: #000000;">bind</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;news&quot;</span>, in, <span style="color: #6666FF;">&quot;articles&quot;</span> -<span style="color: #000080;">&gt;</span> bindArticles <span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>The first line will lookup 5 news items that have the showOnHomepage boolean set to true and order them on date created.<br />
The method bindArticles method will create bindings for each item found. as Lift escapes all xml/html by default, we need to unescape it especially when we want rich content.<br />
The last line, will bind the collection to &lt;news:articles/&gt;</p>
<p>Then all is left is the html template:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;lift:surround</span> <span style="color: #000066;">with</span>=<span style="color: #ff0000;">&quot;default&quot;</span> <span style="color: #000066;">at</span>=<span style="color: #ff0000;">&quot;content&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;news&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;lift:homepage.news<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;news:articles<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h3<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;article:title</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/h3<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h4<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;i<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>by:<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/i<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;article:author</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;i<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>created:<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/i<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;article:created</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;i<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>updated:<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/i<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;article:updated</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/h4<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;article:content</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/news:articles<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/lift:homepage.news<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/lift:surround<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Because the &lt;news:articles&gt; contains a collection, lift will loop over it and process the inner html with each item in the collection.</p>
<p>A thing about authorization, we want the CRUD pages (and probably some other pages) to be accessible only to logged in users, this can be handled in several ways.<br />
1) The Menu class has a parameter that defines the visibility of it, this can be easily used to check if a user is logged in:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">val</span> adminLoc <span style="color: #000080;">=</span> Loc<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;AdminPage&quot;</span>, <span style="color: #6666FF;">&quot;admin&quot;</span> <span style="color: #000080;">::</span> Nil, <span style="color: #6666FF;">&quot;Admin Page&quot;</span>, Test<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>u<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> u <span style="color: #000080;">=</span> User.<span style="color: #000000;">loggedin_</span><span style="color: #000080;">?</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span></pre></div></div>

<p>or override the specific menu methods that are defined in the CRUDify trait</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> editMenuLocParams <span style="color: #000080;">=</span> If<span style="color: #F78811;">&#40;</span>User.<span style="color: #000000;">loggedIn_</span><span style="color: #000080;">?</span> <span style="color: #000080;">_</span>, RedirectResponse<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;/user_mgt/login&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">::</span> <span style="color: #0000ff; font-weight: bold;">super</span>.<span style="color: #000000;">editMenuLocParams</span></pre></div></div>

<p>And there it is, with surprisingly little code, we now have the beginnings of a content management system.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.addictivesoftware.net/2010/12/lift-by-use-case-2-news-articles-on-the-homepage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lift by use case: Import a list of ISBN numbers</title>
		<link>http://blog.addictivesoftware.net/2010/12/lift-by-use-case-import-a-list-of-isbn-numbers/</link>
		<comments>http://blog.addictivesoftware.net/2010/12/lift-by-use-case-import-a-list-of-isbn-numbers/#comments</comments>
		<pubDate>Tue, 21 Dec 2010 14:32:53 +0000</pubDate>
		<dc:creator>gertjan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Lift]]></category>

		<guid isPermaLink="false">http://blog.addictivesoftware.net/?p=90</guid>
		<description><![CDATA[I&#8217;ve been playing around with Lift for a while now, and I&#8217;d like to share some of my experiences with you all, although i&#8217;m inexperienced with both Scala and lift, I&#8217;m able to get things done pretty quickly in comparison with doing it in Java. which says a lot about both the language and the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been playing around with <a href="http://liftweb.net">Lift </a> for a while now, and I&#8217;d like to share some of my experiences with you all, although i&#8217;m inexperienced with both <a href="http://scala-lang.org">Scala </a>and lift, I&#8217;m able to get things done pretty quickly in comparison with doing it in Java. which says a lot about both the language and the framework.</p>
<p>I&#8217;m building a webapplication that holds my collection of books, so the first use case I want to present to you and go through the implementation is the following:</p>
<blockquote><p>As a user I want to be able to quickly import a list of isbn numbers</p></blockquote>
<p>Lift moves away from your typical model view controller framework, but focuses on keeping your template&#8217;s designer friendly without any logic or code in there. Besides that the main focus areas are security and scalability, read the documentation on the lift website if you want to know more about this.</p>
<p>Taking the use case, we need to build a form with a textarea that allows us to copy/paste a list of isbn numbers in and add those to the database. Later use cases will then look up title/author information from for instance amazon, google books, or isbndb</p>
<p>Our html template which contains the form looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;lift:surround</span> <span style="color: #000066;">with</span>=<span style="color: #ff0000;">&quot;default&quot;</span> <span style="color: #000066;">at</span>=<span style="color: #ff0000;">&quot;content&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h3<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Import Isbnn rumbers<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h3<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;lift:ImportIsbn.bulk</span> <span style="color: #000066;">form</span>=<span style="color: #ff0000;">&quot;POST&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry:text</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;br</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry:submit</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/lift:ImportIsbn.bulk<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/lift:surround<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<ul>
<li>The &lt;lift:surround&gt; tells lift to &#8216;surround&#8217; this template with the default.html template which contains the header, footer and navigation for our website, inserting this template at the &lt;lift:content /&gt; tag in that template.</li>
<li>The &lt;lift:ImportIsbn.bulk&gt; tells Lift to look for a class named ImportIsbn with a method bulk with the inner html as a parameter for us to manipulate</li>
<li>The &lt;entry:*&gt;&#8217;s we will bind to form elements in the snippet code later on</li>
</ul>
<p>Something more about templates:</p>
<ul>
<li>if a snippet takes long to complete, surround it with &lt;lift:lazy-load&gt; this will show an ajax loading gif while the snippet is executed.</li>
<li>if you have a lot of snippets that take a while, surround them with &lt;lift:parallel&gt; to execute them in different threads</li>
</ul>
<p>now to create our form:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> ImportIsbn
  <span style="color: #0000ff; font-weight: bold;">var</span> isbnnrs <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;&quot;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> bulk<span style="color: #F78811;">&#40;</span>in <span style="color: #000080;">:</span> NodeSeq<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> NodeSeq <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
        bind<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;entry&quot;</span>, in,
          <span style="color: #6666FF;">&quot;text&quot;</span> -<span style="color: #000080;">&gt;</span> SHtml.<span style="color: #000000;">textarea</span><span style="color: #F78811;">&#40;</span>isbnnrs, isbnnrs <span style="color: #000080;">=</span> <span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span>,
          <span style="color: #6666FF;">&quot;submit&quot;</span> -<span style="color: #000080;">&gt;</span> <span style="color: #F78811;">&#40;</span>SHtml.<span style="color: #000000;">hidden</span><span style="color: #F78811;">&#40;</span>processIsbnNrs<span style="color: #F78811;">&#41;</span> ++ <span style="color: #000080;">&lt;</span>input <span style="color: #0000ff; font-weight: bold;">type</span><span style="color: #000080;">=</span><span style="color: #6666FF;">&quot;submit&quot;</span> value<span style="color: #000080;">=</span><span style="color: #6666FF;">&quot;Add&quot;</span> /<span style="color: #000080;">&gt;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>The bind method is a helper method that looks in the html (NodeSeq) passed on for tags marked with the &#8220;entry&#8221; prefix and binds them to html elements, in good functional programming the SHtml.* methods take a function as a parameter, for the textarea, we assign the value of the textarea to it, the hidden one calls the method processIsbnNrs<br />
we could also have used</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">SHtml.<span style="color: #000000;">submit</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Add&quot;</span>, processIsbnNrs<span style="color: #F78811;">&#41;</span></pre></div></div>

<p>but the above allows us to quickly make a ajax form out of it.<br />
To do this we simple put a Shtml.ajaxForm around the bind.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"> SHtml.<span style="color: #000000;">ajaxForm</span><span style="color: #F78811;">&#40;</span>
        bind<span style="color: #F78811;">&#40;</span>.... <span style="color: #000000;">etc</span> <span style="color: #F78811;">&#41;</span>
      <span style="color: #F78811;">&#41;</span></pre></div></div>

<p>This will create the javascript methods to submit this form without doing a normal post</p>
<p>Due to Scala&#8217;s inline xml capabilities a snippet can also return xhtml directly, lift only works with xhtml (and if you use the 2.2-SNAPSHOT even html5)</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">  <span style="color: #0000ff; font-weight: bold;">def</span> mySnippet<span style="color: #F78811;">&#40;</span>in<span style="color: #000080;">:</span> NodeSeq<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> NodeSeq <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #000080;">&lt;</span>p<span style="color: #000080;">&gt;</span>This is returned from my snippet<span style="color: #000080;">&lt;</span>/p<span style="color: #000080;">&gt;</span>
  <span style="color: #F78811;">&#125;</span></pre></div></div>

<p>After submitting the method processIsbnNrs is called which looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;">      <span style="color: #0000ff; font-weight: bold;">def</span> processIsbnNrs<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
        isbnnrs split <span style="color: #6666FF;">&quot; &quot;</span> map <span style="color: #F78811;">&#123;</span>addBook <span style="color: #000080;">_</span><span style="color: #F78811;">&#125;</span>
      <span style="color: #F78811;">&#125;</span>
&nbsp;
      <span style="color: #0000ff; font-weight: bold;">def</span> addBook<span style="color: #F78811;">&#40;</span>nr <span style="color: #000080;">:</span> String<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
        Book.<span style="color: #000000;">find</span><span style="color: #F78811;">&#40;</span>By<span style="color: #F78811;">&#40;</span>Book.<span style="color: #000000;">isbn</span>, nr<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">match</span> <span style="color: #F78811;">&#123;</span>
          <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #F78811;">&#40;</span>Full<span style="color: #F78811;">&#40;</span>book<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> alreadyInDb +<span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot; &quot;</span> + nr
          <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> Book.<span style="color: #000000;">create</span>.<span style="color: #000000;">isbn</span><span style="color: #F78811;">&#40;</span>nr<span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">saveMe</span>
        <span style="color: #F78811;">&#125;</span>
        msg <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;Skipped: &quot;</span> + alreadyInDb
        S.<span style="color: #000000;">warning</span><span style="color: #F78811;">&#40;</span>Text<span style="color: #F78811;">&#40;</span>msg<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
      <span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>As the isbnnrs var is bound to the textarea it will now contains the list of isbnnrs, the first line will split it on a space and then calling the addBook method for each entry</p>
<p>The addBook method will try to find the book in the Database, (the following post will look at Mapper the persistence layer in Lift). if it doesn&#8217;t it will add it to the database.<br />
and that is all to it, I left out validation and initializing of the var&#8217;s here.  but the code is public at <a href="http://github.com/gertjana/Books/">github </a> if you want to take closer look.</p>
<p>A close observer will see the use of the method Full() in there, Scala has the Option class with which can be an instance of Some or None, which is designed to make continuous null pointer exception checking unnecessary.<br />
Lift takes this one step further by having a Box class, which can be Full, Empty or Failure.  This in combination with Scala&#8217;s match/case functionality is very powerful and creates very readable and thus maintable code.<br />
In our case if a Book is found (the box is full) do nothing, if not save a book with that isbn nr to the database.</p>
<p>See the <a href="http://www.assembla.com/wiki/show/liftweb/Box">Box article </a>on Liftweb for more information about this.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.addictivesoftware.net/2010/12/lift-by-use-case-import-a-list-of-isbn-numbers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Writing better unittests</title>
		<link>http://blog.addictivesoftware.net/2010/02/writing-better-unittests/</link>
		<comments>http://blog.addictivesoftware.net/2010/02/writing-better-unittests/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 12:37:35 +0000</pubDate>
		<dc:creator>gertjan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unittest]]></category>

		<guid isPermaLink="false">http://blog.addictivesoftware.net/?p=69</guid>
		<description><![CDATA[Unittests are an integral part of building quality software and the sole responsibility of the developer. Here I explain some of the lessons i&#8217;ve learned after the last couple of years. Unittests takes times to write I&#8217;m even inclined to say that writing your tests should take at least the time as writing your functionality [...]]]></description>
			<content:encoded><![CDATA[<p>Unittests are an integral part of building quality software and the sole responsibility of the developer.<br />
Here I explain some of the lessons i&#8217;ve learned after the last couple of years.</p>
<ul>
<li><strong>Unittests takes times to write</strong><br />
I&#8217;m even inclined to say that writing your tests should take at least the time as writing your functionality takes.</li>
<li><strong>Start with the unit tests</strong><br />
Start writing your unittests first, setting up just enough of your classes to get the test compiling, this will force you write your test based on the functionality required not the buggy implementation you&#8217;ve just written.</li>
<li><strong>Independent<br />
</strong>as the name says, a unit test will test a single unit of code, use integration tests to see how parts are working together.</li>
<li><strong>Small<br />
</strong>A unit test should test a small bit of functionality, one class, or even one method. I usually write extra unittests that test entire use-cases, but those work next to the normal ones.</li>
<li><strong>Test negatives as wel as positives<br />
</strong>Your first test will probably check whether the functionality works when specifying the correct parameters , but also test whether the right results are returned or the correct exceptions are thrown when specifying the wrong ones.</li>
<li><strong>Use code coverage tools to measure effectiveness<br />
</strong>Coverage tools, like for instance Clover will tell you how much of your code is covered, and also which classes are the most risk (high complexy, low coverage)</li>
<li><strong>Be careful when using mock objects<br />
</strong>Using mock objects keeps your unittests indepent but it has the danger that you are testing your mock objects instead of your own functionality, write integration tests</li>
<li><strong>Don&#8217;t change outputted results into asserts<br />
</strong>It can be very tempting to first see what your class outputs when running a test, and then writing asserts to test on those results, but you will just be validating that bug that is still in there.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.addictivesoftware.net/2010/02/writing-better-unittests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rest easy</title>
		<link>http://blog.addictivesoftware.net/2009/12/rest-easy/</link>
		<comments>http://blog.addictivesoftware.net/2009/12/rest-easy/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 14:10:09 +0000</pubDate>
		<dc:creator>gertjan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://blog.addictivesoftware.net/?p=49</guid>
		<description><![CDATA[I’ve been playing around with REST (REpresentational State Transfer) for a bit and decided to write a small tutorial to show how easy it is to create a rest interface to your business objects Advantages of rest Works over HTTP, known protocol with known data formats (mime types), no weird ports open in your firewall [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve been playing around with <a href="http://en.wikipedia.org/wiki/REST">REST</a> (REpresentational State Transfer) for a bit and decided to write a small tutorial to show how easy it is to create a rest interface to your business objects</p>
<p>Advantages of rest</p>
<ul>
<li>Works over HTTP, known protocol with known data formats (mime types), no weird ports open in your firewall</li>
<li>Every resource is uniquely identified by a URI</li>
<li>Can do CRUD on those resources (they are just called POST, GET, PUT, DELETE)</li>
<li>used by all the major players for cloud applications (<a href="http://kenai.com/projects/suncloudapis/pages/Home">SUN&#8217;s cloudApi</a>, <a href="http://www.microsoft.com/windowsazure/">Microsoft&#8217;s Azure services platform</a>)</li>
<li>we might finally get rid of webdav</li>
</ul>
<p>I&#8217;ll be using the <a href="https://jersey.dev.java.net/">Jersey</a> Framework for this.<br />
As i’ve been working on a backend that will allow you to manage <a href="http://en.wikipedia.org/wiki/Agile_development">agile development</a> processes (Scrum, Kanban etc) I thought I use that.</p>
<p>In this application every object is represented by a Card for instance a Story, Task, Bug, work Item, etc.<br />
Cards have a hierarchical structure and can each have custom properties<br />
The class looks something like this (removed methods for clarity):</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> Card <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> id<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">List</span> children <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">List</span> properties <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>and then for instance the Story class with some default values set in the constructor:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Story <span style="color: #000000; font-weight: bold;">extends</span> Card <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> Story<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		addProperty<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Title&quot;</span>, <span style="color: #0000ff;">&quot;A new story&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		addProperty<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Description&quot;</span>, <span style="color: #0000ff;">&quot;as a .. I want .. for the benefit of ..&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		addProperty<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Storypoints&quot;</span>, <span style="color: #0000ff;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>To be able to return these objects through REST they need to be in a format that can be distributed over HTTP, in this case we will be using XML and JSON.<br />
The <a href="https://jaxb.dev.java.net/">Java XML Bindings</a> are perfectly suited to create xml representations of our classes</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@XmlRootElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;card&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> Card <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> id<span style="color: #339933;">;</span>
&nbsp;
	@XmlAttribute<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;type&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> instance <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSimpleName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@XmlElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;card&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">List</span> children <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@XmlElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;property&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">List</span> properties <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@XmlAttribute
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">long</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>* the Entry class is a simple class with a name and value property, I choose this approach as JAXB has an issue in representing HashMaps() in XML<br />
this will produce the following xml</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">&nbsp;</pre></div></div>

<p>for JSON we don’t need to do anything special then include the jersey-json.jar to the project.</p>
<p>Now to configure and hookup the rest servlet<br />
I added the jersey servlet to my web.xml</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    	Rest Web Service
    	com.sun.jersey.spi.container.servlet.ServletContainer
    	1
&nbsp;
    	Rest Web Service
    	/services/*</pre></div></div>

<p>And create the service provider class</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@PerRequest
@Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/agileassistant&quot;</span><span style="color: #009900;">&#41;</span>
@Consumes<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> MediaType.<span style="color: #006633;">APPLICATION_JSON</span>, MediaType.<span style="color: #006633;">APPLICATION_XML</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
@Produces<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> MediaType.<span style="color: #006633;">APPLICATION_JSON</span>, MediaType.<span style="color: #006633;">APPLICATION_XML</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AgileAssistantService <span style="color: #009900;">&#123;</span>
 ...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This will tell Jersey that this is a provider that will respond to requests to /rest/agileassistant, accept JSON and XML and can output JSON or XML depending on the accept headers sent by the client</p>
<p>Now if we for instance want to return the first card (rootCard) and all its children we add this method</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Card rootCard <span style="color: #339933;">=</span> Card.<span style="color: #006633;">createCard</span><span style="color: #009900;">&#40;</span>Root.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@GET
	<span style="color: #000000; font-weight: bold;">public</span> Card getRoot<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> rootCard<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>(in my data model the rootCard is always present and functions as a starting point to add the rest of the cards in a hierarchical structure)<br />
So requesting this card will effectively return the entire structure for my test data:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">&nbsp;</pre></div></div>

<p>Doing a parameterized query is almost as easy</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	@GET
	@Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/card/{id}&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> Card getCard<span style="color: #009900;">&#40;</span>@PathParam<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">long</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> getCardById<span style="color: #009900;">&#40;</span>rootCard, id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>here the id that you specify will be injected into the parameter<br />
giving the following output</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">&nbsp;</pre></div></div>

<p>To conclude enabling rest for your applications is pretty easy, writing this blog took longer than writing the code.<br />
In a next post I will add POST/PUT/DELETE requests to store data in your application</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.addictivesoftware.net/2009/12/rest-easy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaFX data binding to Java</title>
		<link>http://blog.addictivesoftware.net/2009/11/data-binding-in-javafx/</link>
		<comments>http://blog.addictivesoftware.net/2009/11/data-binding-in-javafx/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 21:01:51 +0000</pubDate>
		<dc:creator>gertjan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaFX]]></category>

		<guid isPermaLink="false">http://blog.addictivesoftware.net/?p=3</guid>
		<description><![CDATA[Data binding in JavaFX is pretty straightforward. You should think binding to Java Objects should be just as simple, well it is a little complicater then just doing var someObject : bind myJavaclass.getProperty&#40;&#41;; JavaFX needs to be aware of any changes to the bound variables to be able to update the related objects. It does [...]]]></description>
			<content:encoded><![CDATA[<p>Data binding in JavaFX is pretty straightforward.<br />
You should think binding to Java Objects should be just as simple, well it is a little complicater then just doing</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">var someObject <span style="color: #339933;">:</span> bind myJavaclass.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>JavaFX needs to be aware of any changes to the bound variables to be able to update the related objects. It does this automagically for all javafx objects.<br />
It seems JavaFX uses the Observer/Observable pattern for all its internal objects, well we can do the same to bind with JavaObjects.<br />
To get myself familiar with JavaFX i decided to write a photoviewer/editor. This application has a java backend with in particular a Photo class which contains all the information of a certain picture, filename, path, tags, exif information etcetera</p>
<p>To get changes in the Java class to appear in the gui I create a PhotoAdapter javafx class that extends Observer and implemented the properties I need in my GUI.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PhotoAdapter <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Observer</span> <span style="color: #009900;">&#123;</span>
    public<span style="color: #339933;">-</span>read var filename<span style="color: #339933;">:</span> <span style="color: #003399;">String</span><span style="color: #339933;">;</span>
    public<span style="color: #339933;">-</span>read var tags<span style="color: #339933;">:</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    public<span style="color: #339933;">-</span>read var exif<span style="color: #339933;">:</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    public<span style="color: #339933;">-</span>init var photo<span style="color: #339933;">:</span> Photo
         on replace <span style="color: #009900;">&#123;</span>
            photo.<span style="color: #006633;">addObserver</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            filename<span style="color: #339933;">=</span>photo.<span style="color: #006633;">getFilename</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            tags <span style="color: #339933;">=</span> photo.<span style="color: #006633;">getTags</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            exif <span style="color: #339933;">=</span> photo.<span style="color: #006633;">getExif</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    override function update<span style="color: #009900;">&#40;</span>observable<span style="color: #339933;">:</span> <span style="color: #003399;">Observable</span>, arg<span style="color: #339933;">:</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        filename <span style="color: #339933;">=</span> photo.<span style="color: #006633;">getFilename</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        tags <span style="color: #339933;">=</span> photo.<span style="color: #006633;">getTags</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        exif <span style="color: #339933;">=</span> photo.<span style="color: #006633;">getExif</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>after that I had my existing Java Photo class extend Observable and in every method that made changes I added the methods setChanged() and NotifyObservers() to inform the Observers about the change</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Photo <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Observable</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> Map<span style="color: #339933;">&lt;</span>String, String<span style="color: #339933;">&gt;</span> exifInformation<span style="color: #339933;">;</span> 
    ....
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setExifInformation<span style="color: #009900;">&#40;</span><span style="color: #003399;">Map</span> exifInfo<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">exifInformation</span> <span style="color: #339933;">=</span> exifInfo<span style="color: #339933;">;</span>
        setChanged<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        notifyObservers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>in my main javafx class I can then use it as follows</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    var photoAdapter <span style="color: #339933;">=</span> PhotoAdapter <span style="color: #009900;">&#123;</span>
        photo <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">new</span> Photo<span style="color: #009900;">&#40;</span>startImage<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>and use to fill a ListView swing object with for instance the tags:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #003399;">ListView</span> <span style="color: #009900;">&#123;</span>
        items<span style="color: #339933;">:</span>     bind photoAdapter.<span style="color: #006633;">tags</span><span style="color: #339933;">;</span>
        width<span style="color: #339933;">:</span>     <span style="color: #cc66cc;">200</span><span style="color: #339933;">;</span>
        height<span style="color: #339933;">:</span>    <span style="color: #cc66cc;">300</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>et viola, thats all that there is to it</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.addictivesoftware.net/2009/11/data-binding-in-javafx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

