<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Heikki Uljas</title>
 <link href="http://huljas.github.io/" rel="self"/>
 <link href="http://huljas.github.io"/>
 <updated>2013-11-25T21:44:06-08:00</updated>
 <id>http://huljas.github.io</id>
 <author>
   <name>Heikki Uljas</name>
   <email>huljas@gmail.com</email>
 </author>

 
 <entry>
   <title>Most efficient way</title>
   <link href="http://huljas.github.io/philosophy/2013/11/25/most-efficient-way"/>
   <updated>2013-11-25T00:00:00-08:00</updated>
   <id>http://huljas.github.io/philosophy/2013/11/25/most-efficient-way</id>
   <content type="html">&lt;h4 id=&#39;the_most_efficient_way_to_implement_something_is_to_realize_that_you_dont_have_to&#39;&gt;The most efficient way to implement something is to realize that you don&amp;#8217;t have to.&lt;/h4&gt;

&lt;p&gt;You could ask the &amp;#8220;Why?&amp;#8221; question and figure out that something is not needed at all.&lt;/p&gt;

&lt;p&gt;Or you could find out that there already exists a solution for you problem.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Formatting in play scala templates</title>
   <link href="http://huljas.github.io/playframework/2013/08/06/formatting-in-play-scala-templates"/>
   <updated>2013-08-06T00:00:00-07:00</updated>
   <id>http://huljas.github.io/playframework/2013/08/06/formatting-in-play-scala-templates</id>
   <content type="html">&lt;p&gt;While play1.x had rich support for formatting with &lt;a href=&#39;http://www.playframework.com/documentation/1.2.5/javaextensions&#39;&gt;javaextensions&lt;/a&gt; in play2 we rely on what scala language can provide us.&lt;/p&gt;

&lt;h2 id=&#39;formatting_numbers&#39;&gt;Formatting numbers&lt;/h2&gt;

&lt;p&gt;Probably the most convenient way to format numbers is to use the scala Strings &lt;code&gt;format&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@* left padding zeroes *@
@(&amp;quot;%03d&amp;quot;.format(7))

@* leading spaces *@
[@(&amp;quot;% 4d&amp;quot;.format(11))]

@* with 2 decimals *@
@(&amp;quot;%.2f&amp;quot;.format(1123.345566))

@* with locale *@
@(&amp;quot;%.2f&amp;quot;.formatLocal(Locale.US, 1123.345566))&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;generates&lt;/p&gt;
&lt;pre&gt;
007

[  11]

1123,35

1123.35
&lt;/pre&gt;
&lt;h2 id=&#39;formatting_dates&#39;&gt;Formatting dates&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;format&lt;/code&gt; also works with dates&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@* just time *@
@(&amp;quot;%tT&amp;quot;.format(1312180002230L))

@* date *@
@(&amp;quot;%tF&amp;quot;.format(1312180002230L))

@* date and time *@
@(&amp;quot;%1$tH:%1$tM:%1$tS.%1$tL %1$tY.%1$tm.%1$td&amp;quot;.format(1312180002231L))&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;converts our timestamp to&lt;/p&gt;
&lt;pre&gt;
09:26:42

2011-08-01

09:26:42.231 2011.08.01
&lt;/pre&gt;
&lt;h2 id=&#39;formatting_strings&#39;&gt;Formatting strings&lt;/h2&gt;

&lt;p&gt;For string formatting we use the functional power of scala&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@* lower case *@
@(&amp;quot;AbC&amp;quot;.toLowerCase)

@* upper case *@
@(&amp;quot;aBc&amp;quot;.toUpperCase)

@* capitalize *@
@(&amp;quot;abc&amp;quot;.capitalize)

@* capitalize each word *@
@(&amp;quot;The quick brown FOX jumps over the lazy dog&amp;quot;
    .split(&amp;quot; &amp;quot;)
    .map(_.toLowerCase.capitalize)
    .mkString(&amp;quot; &amp;quot;))&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;converts them to&lt;/p&gt;
&lt;pre&gt;
abc
ABC
Abc
The Quick Brown Fox Jumps Over The Lazy Dog
&lt;/pre&gt;</content>
 </entry>
 
 <entry>
   <title>Searching for the perfect DAO pattern</title>
   <link href="http://huljas.github.io/code/2013/06/07/searching-the-perfect-dao"/>
   <updated>2013-06-07T00:00:00-07:00</updated>
   <id>http://huljas.github.io/code/2013/06/07/searching-the-perfect-dao</id>
   <content type="html">&lt;p&gt;I really liked the way the database access was simplified to static methods in the data object in &lt;a href=&#39;http://www.playframework.com/documentation/1.2.5/jpa&#39;&gt;Play framework version 1.x&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;MyModel found = MyModel.findById(id);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I wanted to achieve something similar in my current project. My starting point was a common helper class which contained the database specific implementation:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class DatabaseHelper {
    public static &amp;lt;T&amp;gt; findById(String id, Class&amp;lt;T&amp;gt; type) {...}
}

MyModel found = DatabaseHelper.findById(id, MyModel.class);&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#39;static_methods&#39;&gt;Static methods&lt;/h2&gt;

&lt;p&gt;My first attempt was a base class with static methods:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class BaseModel {
    public static BaseModel findById(String id) {
        return DatabaseHelper.findById(id, getType());
    }
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So now I would only have to implement the &lt;code&gt;getType&lt;/code&gt; with reflection and be done with it. Well easier said than done. Since the static context of the base class has no knowledge of the implementing class I have no way of getting that class with reflection. In play 1.x this was done with runtime code generation which I don&amp;#8217;t want to use because of its drawbacks.&lt;/p&gt;

&lt;p&gt;Okay so maybe I could use generics somehow?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class BaseModel&amp;lt;T extends BaseModel&amp;gt; {
    public static T findById(String id) {
        return DatabaseHelper.findById(id, getType());
    }
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Well this doesn&amp;#8217;t even compile because the type variable &lt;code&gt;T&lt;/code&gt; cannot be accessed from the static context since it has no knowledge of the actual instance of the class defining the type argument.&lt;/p&gt;

&lt;h2 id=&#39;normal_base_class&#39;&gt;Normal base class&lt;/h2&gt;

&lt;p&gt;Since static methods are out of the question what could we do with a base class with non-static methods?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class BaseModel {
    public BaseModel findById(String id) {
        return DatabaseHelper.findById(id, getClass());
    }
}

class MyModel extends BaseModel {}

MyModel found = (MyModel) new MyModel().findById(id);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We can get rid of the cast with generics&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class BaseModel&amp;lt;T extends BaseModel&amp;gt; {
    public T findById(String id) {
        return DatabaseHelper.findById(id, getClass());
    }
}

class MyModel extends BaseModel {}

MyModel found = new MyModel().findById(id);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Sort of there but I still don&amp;#8217;t want to create a new instance of my model.&lt;/p&gt;

&lt;h2 id=&#39;encapsulation_to_the_rescue&#39;&gt;Encapsulation to the rescue&lt;/h2&gt;

&lt;p&gt;Okay so since direct inheritance is out of the question maybe we could use encapsulation somehow?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class BaseModel {}

class BaseDAO&amp;lt;T extends BaseModel&amp;gt; {

    private final Class&amp;lt;T&amp;gt; type;

    public BaseDAO(Class&amp;lt;T&amp;gt; type) {
        this.type=type;
    }

    public Class&amp;lt;T&amp;gt; getType() {
        return type;
    }

    public T findById(String id) {
        return DatabaseHelper.findById(id, type);
    }
}

class MyModel extends BaseModel {
    public static final BaseDAO&amp;lt;MyModel&amp;gt; DAO = new BaseDao&amp;lt;&amp;gt;(MyModel.class);
}

MyModel found = MyModel.DAO.findById(id);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For a finishing touch lets get rid of type parameter with some reflection:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class BaseDAO&amp;lt;T extends BaseModel&amp;gt; {

    public BaseDAO() {
        this.type = (Class&amp;lt;T&amp;gt;) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

}

class MyModel extends BaseModel {
    public static final BaseDAO&amp;lt;MyModel&amp;gt; DAO = new BaseDao&amp;lt;&amp;gt;();
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And with this and some code in our class we get what we wanted:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class MyModel extends BaseModel {
    private static final BaseDAO&amp;lt;MyModel&amp;gt; DAO = new BaseDao&amp;lt;&amp;gt;();

    public static MyModel findById(String id) {return DAO.findById(id);}
}

MyModel found = MyModel.findById(id);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Happiness!&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>New start, new layout</title>
   <link href="http://huljas.github.io/blog/2013/04/26/new-start-new-layout"/>
   <updated>2013-04-26T00:00:00-07:00</updated>
   <id>http://huljas.github.io/blog/2013/04/26/new-start-new-layout</id>
   <content type="html">&lt;p&gt;So after a year I&amp;#8217;m starting to write my blog again. A lot has happened since my last post: new exiting job at Rovio, learned new stuff and gained some weight ;).&lt;/p&gt;
&lt;p&gt;And what better way to start than with a fresh layout!&lt;/p&gt;
&lt;p&gt;Well it&amp;#8217;s just the jekyll bootstrap default one, but I can&amp;#8217;t be arsed to do any design now so it&amp;#8217;ll have to do!&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Little unzip utility - In Java</title>
   <link href="http://huljas.github.io/code/2012/03/30/little-unzip-utility"/>
   <updated>2012-03-30T00:00:00-07:00</updated>
   <id>http://huljas.github.io/code/2012/03/30/little-unzip-utility</id>
   <content type="html">&lt;p&gt;It&amp;#8217;s always fun when you bump into a problem you haven&amp;#8217;t encountered before.&lt;/p&gt;
&lt;p&gt;This time I had a database dump file that was so large that I had to zip it. And since I wanted to unzip it in Java I had to find out how it&amp;#8217;s done.&lt;/p&gt;
&lt;p&gt;Well it turned out to be pretty straight forward, here&amp;#8217;s the gist:&lt;/p&gt;
&lt;script src=&quot;https://gist.github.com/2254197.js&quot;&gt; &lt;/script&gt;</content>
 </entry>
 
 <entry>
   <title>Spring controller monitoring aspect</title>
   <link href="http://huljas.github.io/performance/2012/01/12/spring-controller-jamon-annotation"/>
   <updated>2012-01-12T00:00:00-08:00</updated>
   <id>http://huljas.github.io/performance/2012/01/12/spring-controller-jamon-annotation</id>
   <content type="html">&lt;p&gt;I wanted to monitor our Spring &lt;span class=&quot;caps&quot;&gt;MVC&lt;/span&gt; application with &lt;a href=&quot;http://jamonapi.sourceforge.net/&quot;&gt;Jamon&lt;/a&gt;. We rely on the use of the &lt;code&gt;@Controller&lt;/code&gt; annotation so aspects seemed like the right way to go.&lt;/p&gt;
&lt;p&gt;After some digging on the &lt;a href=&quot;http://static.springsource.org/spring/docs/3.0.6.RELEASE/spring-framework-reference/html/aop.html#aop-pointcuts-designators&quot;&gt;documentation&lt;/a&gt; I came up with the following aspect:&lt;/p&gt;
&lt;script src=&quot;https://gist.github.com/1600772.js?file=MonitoredSpringBeanAspect.java&quot;&gt;&lt;/script&gt;&lt;p&gt;Now I just needed to enable autoproxying with&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;aop:aspectj-autoproxy /&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;line in my application context file and all my controllers started showing in Jamon!&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Unshredder challenge part 2</title>
   <link href="http://huljas.github.io/learning/2011/12/06/unshredder-challenge-part-2"/>
   <updated>2011-12-06T00:00:00-08:00</updated>
   <id>http://huljas.github.io/learning/2011/12/06/unshredder-challenge-part-2</id>
   <content type="html">&lt;p&gt;After my &lt;a href=&quot;/code/2011/11/16/unshredder-challenge.html&quot;&gt;first solution&lt;/a&gt; to &lt;a href=&quot;http://instagram-engineering.tumblr.com/post/12651721845/instagram-engineering-challenge-the-unshredder&quot;&gt;unshredder challenge&lt;/a&gt; I started working on the bonus part of the problem: auto-detecting how wide the uniform slices are.&lt;/p&gt;
&lt;p&gt;My plan was pretty simple:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;for each vertical column of pixels X[i]
    calculate distance D[i] = distance(X[i], X[i+1]
Dmax = sort D desc
for each slice width W
    M = number of borders with slice width *W*
    P = number of borders in the first *M* elements of Dmax with slice width *W*
    return w with max(P / M)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The plan worked otherwise great except that slice width 256 (2 slices) maximized my &lt;code&gt;P / M&lt;/code&gt;. I fixed that by returning the first &lt;code&gt;W&lt;/code&gt; that exceeds a given threshold.&lt;/p&gt;
&lt;p&gt;The result is the following python code:&lt;/p&gt;
&lt;script src=&quot;https://gist.github.com/1371476.js?file=find_slice_width.py&quot;&gt;&lt;/script&gt;
&lt;h3&gt;Links&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/huljas/unshredder-python&quot;&gt;sources&lt;/a&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Unshredder challenge</title>
   <link href="http://huljas.github.io/learning/2011/11/16/unshredder-challenge"/>
   <updated>2011-11-16T00:00:00-08:00</updated>
   <id>http://huljas.github.io/learning/2011/11/16/unshredder-challenge</id>
   <content type="html">&lt;p&gt;My good friend &lt;a href=&quot;https://plus.google.com/113699582561117500948/posts&quot;&gt;+Zsolt Szasz&lt;/a&gt; challenged me to the &lt;a href=&quot;http://instagram-engineering.tumblr.com/post/12651721845/instagram-engineering-challenge-the-unshredder&quot;&gt;unshredder challenge&lt;/a&gt;. I decided to try something different this time and solve the problem using &lt;strong&gt;python&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;Setting up&lt;/h3&gt;
&lt;p&gt;So my first task was to setup python and the &lt;a href=&quot;http://www.pythonware.com/products/pil/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;PIL&lt;/span&gt;&lt;/a&gt; image library on my windows machine. I had python installed but the &lt;span class=&quot;caps&quot;&gt;PIL&lt;/span&gt; installer couldn&amp;#8217;t find it, so I had reinstall python. After that I did a quick check:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; from PIL import Image
&amp;gt;&amp;gt;&amp;gt; quit()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;No errors so I was ready to go!&lt;/p&gt;
&lt;h3&gt;Learning python as you code&lt;/h3&gt;
&lt;p&gt;I had used python a bit earlier but honestly &lt;strong&gt;too little&lt;/strong&gt;. So this was a nice chance to learn some more. Luckily they had some sample code to get started with.&lt;/p&gt;
&lt;p&gt;After fixing some syntax issues from my copy-paste, I had a the example program running and producing the image seen below!&lt;/p&gt;
&lt;p&gt;&lt;img title=&quot;I already feel like a winner!&quot; src=&quot;https://lh4.googleusercontent.com/-PLASdwRXd3U/TsP5cYP7BsI/AAAAAAAAAuw/mlRPhXAjb8U/s144/unshredded.jpg&quot;&gt;&lt;/p&gt;
&lt;h3&gt;First solution&lt;/h3&gt;
&lt;p&gt;It took me 2-3 hours to get the &lt;strong&gt;image unshredded&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img title=&quot;Not bad for a sort of beginner&quot; src=&quot;https://lh5.googleusercontent.com/-1xcbi3ABYVY/TsQsSi16W6I/AAAAAAAAAvE/yv0qXsjd1uk/s144/unshredded.jpg&quot;&gt;&lt;/p&gt;
&lt;p&gt;I used the nice &lt;a href=&quot;http://docs.python.org/tutorial/index.html&quot;&gt;python tutorial&lt;/a&gt; a lot and getting used to the syntax took a while &lt;em&gt;(no semicolons! no curly braces!)&lt;/em&gt;. But I found python to be really easy and powerful language!&lt;/p&gt;
&lt;p&gt;Here&amp;#8217;s the solution, not very elegant but pretty readable &lt;span class=&quot;caps&quot;&gt;IMO&lt;/span&gt; ;).&lt;/p&gt;
&lt;script src=&quot;https://gist.github.com/1371476.js?file=unshredder.py&quot;&gt;&lt;/script&gt;&lt;h3&gt;Better solution&lt;/h3&gt;
&lt;p&gt;I have updated my solution with a solution to the &lt;a href=&quot;/code/2011/12/06/unshredder-challenge-part-2.html&quot;&gt;bonus challenge&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;Links&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/huljas/unshredder-python&quot;&gt;sources&lt;/a&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Facebook cookie server side authentication</title>
   <link href="http://huljas.github.io/web/2011/11/15/facebook-cookie-authentication"/>
   <updated>2011-11-15T00:00:00-08:00</updated>
   <id>http://huljas.github.io/web/2011/11/15/facebook-cookie-authentication</id>
   <content type="html">&lt;p&gt;In this example I show how to validate a user&amp;#8217;s facebook session in the server side using the fbsr cookie created by the FB javascript &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;I use the following, &lt;a href=&quot;http://developers.facebook.com/docs/reference/javascript/&quot;&gt;basic&lt;/a&gt; html to initialize the login button.&lt;/p&gt;
&lt;script src=&quot;https://gist.github.com/1367782.js?file=index.html&quot;&gt;&lt;/script&gt;&lt;p&gt;The facebook login will in this case create a cookie named &lt;code&gt;fbsr_[your facebook application id]&lt;/code&gt; which is in the form of &lt;a href=&quot;http://developers.facebook.com/docs/authentication/signed_request/&quot;&gt;signed request&lt;/a&gt; consisting of two parts:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[mac sha256 signature].[base64 url encoded json]&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now I can validate the cookie on server side by signing the &lt;strong&gt;second part&lt;/strong&gt; of the cookie with my &lt;strong&gt;facebook secret&lt;/strong&gt; and making sure that it matches to the &lt;strong&gt;first part&lt;/strong&gt;. To get the json data I just have to json decode the second part of the cookie.&lt;/p&gt;
&lt;p&gt;Here&amp;#8217;s code that I use to do it in Java:&lt;/p&gt;
&lt;script src=&quot;https://gist.github.com/1367782.js?file=FacebookCookie.java&quot;&gt;&lt;/script&gt;&lt;p&gt;So now we have json object in the format of:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;{ 
  &quot;algorithm&quot; : &quot;HMAC-SHA256&quot;,
  &quot;code&quot; : &quot;2.AQDC1d-ZE90999-6.3600.1321387200.1-676786217|X1VAtqRDS_3NR6lduKISj50Uozw&quot;, 
  &quot;issued_at&quot; : 1321381928, 
  &quot;user_id&quot; : &quot;12345678&quot;
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In order to get the access token, we need to request from the graph api using the following request:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;https://graph.facebook.com/oauth/access_token?
client_id=[facebook application id]
&amp;amp;redirect_uri=
&amp;amp;client_secret=[application secret]
&amp;amp;code=[code from the json]&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that you need to include  &lt;strong&gt;an empty redirect_uri&lt;/strong&gt; parameter in the request for it to work. From the response you get the actual access token, which you can use to get more information about the user.&lt;/p&gt;
&lt;p&gt;You can find a simple example application &lt;a href=&quot;https://github.com/huljas/fb-cookie-example&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Drive by performance testing</title>
   <link href="http://huljas.github.io/performance/2011/11/08/drive-by-performance-testing"/>
   <updated>2011-11-08T00:00:00-08:00</updated>
   <id>http://huljas.github.io/performance/2011/11/08/drive-by-performance-testing</id>
   <content type="html">&lt;p&gt;Typically the performance tests are run by a specialist who runs his tests for a day, sends his report and then drives away. And you, the poor developer are left with a plot chart illustrating some weird performance issue you haven&amp;#8217;t the slightest idea what is causing it.&lt;/p&gt;
&lt;p&gt;Now it doesn&amp;#8217;t have to be like that. Just be prepared!&lt;/p&gt;
&lt;h3&gt;Use performance monitoring in your code&lt;/h3&gt;
&lt;p&gt;Add measurement into the important parts of your code and enable seeing the reports in runtime. I have used &lt;a href=&quot;http://jamonapi.sourceforge.net/&quot;&gt;Jamon&lt;/a&gt; in Java development with great success.&lt;/p&gt;
&lt;h3&gt;Run your own performance tests&lt;/h3&gt;
&lt;p&gt;By running your own tests you can fix all the silly mistakes before the official tests are run. Focus on covering the critical parts of your application.&lt;/p&gt;
&lt;h3&gt;Participate in the official performance testing&lt;/h3&gt;
&lt;p&gt;If you have proper monitoring in place you should be able to analyze and fix the issues as they are found. It is also the only way to analyze the &lt;strong&gt;really weird&lt;/strong&gt; ones.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Fun with javascript and canvas</title>
   <link href="http://huljas.github.io/web/2011/10/15/fun-with-javascript-canvas"/>
   <updated>2011-10-15T00:00:00-07:00</updated>
   <id>http://huljas.github.io/web/2011/10/15/fun-with-javascript-canvas</id>
   <content type="html">&lt;script type=&quot;text/javascript&quot; src=&quot;/js/animation-part-1.js&quot;&gt;&lt;/script&gt;&lt;script type=&quot;text/javascript&quot;&gt;
$(function() {
  Animation.init(&quot;stars1&quot;);  
});
&lt;/script&gt;&lt;p&gt;I had this &lt;strong&gt;crazy&lt;/strong&gt; idea of putting some canvas animation into the layout of this new site. The idea didn&amp;#8217;t fly, but I managed to create this &lt;strong&gt;quite nice&lt;/strong&gt; animation of stars:&lt;/p&gt;
&lt;canvas id=&quot;stars1&quot; width=&quot;660&quot; height=&quot;300&quot; style=&quot;border: 3px solid #999;&quot;&gt;
&lt;/canvas&gt;
&lt;p&gt;Here&amp;#8217;s the code:&lt;/p&gt;
&lt;script src=&quot;https://gist.github.com/1290219.js?file=animation-part-1.js&quot;&gt;&lt;/script&gt;</content>
 </entry>
 
 <entry>
   <title>Blogging in github is like writing code</title>
   <link href="http://huljas.github.io/blog/2011/10/15/blogging-is-github-is-like-coding"/>
   <updated>2011-10-15T00:00:00-07:00</updated>
   <id>http://huljas.github.io/blog/2011/10/15/blogging-is-github-is-like-coding</id>
   <content type="html">&lt;p&gt;My &lt;a href=&quot;http://pragmatastic.blogspot.com&quot;&gt;old blog&lt;/a&gt; with it&amp;#8217;s text area bound interface felt really disconnected from the code related topics I wanted to write about. But this git thing feels already much more natural, it feels like writing code. Or perhaps it&amp;#8217;s because I&amp;#8217;m doing this in vi. ;)&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Managing database with play carbonate</title>
   <link href="http://huljas.github.io/playframework/2011/04/04/managing-database-with-play-carbonate"/>
   <updated>2011-04-04T00:00:00-07:00</updated>
   <id>http://huljas.github.io/playframework/2011/04/04/managing-database-with-play-carbonate</id>
   <content type="html">&lt;p&gt;Managing the database is important aspect of modern application development, typically done with database migrations. I used migrations before I started working with play so naturally the first module I wrote for it was a database migrations module with my favorite migrations library, &lt;a href=&quot;http://code.google.com/p/c5-db-migration/&quot;&gt;Carbon Five Migrations&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The initial version of the module was really basic and after a twenty or so models created and equal amount of copy-pasted create table statements I got this &lt;a href=&quot;http://pragmatastic.blogspot.com/2011/02/migrations-to-play-why-not-use.html&quot;&gt;idea&lt;/a&gt; of updating my module with the Hibernate schema update. And the result is the &lt;a href=&quot;https://github.com/huljas/play-carbonate&quot;&gt;play-carbonate&lt;/a&gt; module.&lt;/p&gt;
&lt;p class=&quot;warn&quot;&gt;Remember that the schema update is just a tool. Always check the &lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt; it generates!&lt;/p&gt;
&lt;h3&gt;Setting up&lt;/h3&gt;
&lt;p&gt;To add this module as dependency of your application, add it to the &lt;strong&gt;dependencies.yml&lt;/strong&gt; file:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;require:
    - play -&amp;gt; carbonate {version}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next configure the database and add the carbonate path in to the &lt;strong&gt;application.conf&lt;/strong&gt; file, I am using play id &lt;strong&gt;local-mysql&lt;/strong&gt; and database &lt;strong&gt;play-test&lt;/strong&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;%local-mysql.db=
%local-mysql.db.url=jdbc:mysql://localhost:3306/play-test
%local-mysql.db.driver=com.mysql.jdbc.Driver
%local-mysql.db.user=root
%local-mysql.db.pass=
%local-mysql.jpa.ddl=none
%local-mysql.carbonate.path=conf/migrations&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now our carbonate module is configured and ready to use.&lt;/p&gt;
&lt;h3&gt;Usage example&lt;/h3&gt;
&lt;p&gt;Now lets consider a simple model shown below&lt;/p&gt;
&lt;script src=&quot;https://gist.github.com/902204.js?file=SimpleEntity.java&quot;&gt;&lt;/script&gt;
&lt;p&gt;We run the the following play command with our play id &lt;strong&gt;local-mysql&lt;/strong&gt; to generate a new migration file under the &lt;strong&gt;conf/migrations&lt;/strong&gt; folder:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;play carbonate:new --%local-mysql
Please give description for you migration:
simple model
21:00:18,136 WARN  ~ Changes from schema update:
create table SimpleEntity (id bigint not null auto_increment, age integer not null, isEternal bit not null, name varchar(255), primary key (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
21:00:18,138 WARN  ~ New migration file created ~/code/test-app/conf/migrations/20110404220338_simple_model.sql&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can check the generated file&lt;/p&gt;
&lt;script src=&quot;https://gist.github.com/902204.js?file=20110404220338_simple_model.sql&quot;&gt;&lt;/script&gt;&lt;p&gt;To run the migration we just start our play application:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;play run --%local-mysql
22:08:20,709 INFO  ~ Running migrations from path conf/migrations
22:08:20,757 INFO  ~ Migrating database... applying 1 migration.
22:08:20,758 INFO  ~ Running migration 20110404220338_simple_model.sql.
22:08:20,812 INFO  ~ Migrated database in 0:00:00.250.&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we want to change our model and add a &lt;strong&gt;SimpleCategory&lt;/strong&gt; to our model:&lt;/p&gt;
&lt;script src=&quot;https://gist.github.com/902204.js?file=SimpleEntityWithCategory.java&quot;&gt;&lt;/script&gt;
&lt;p&gt;So after the change we run the command again&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;play carbonate:new --%local-mysql&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;ending up with a migration looking like this:&lt;/p&gt;
&lt;script src=&quot;https://gist.github.com/902204.js?file=20110404221245_added_category_for_the_entity.sql&quot;&gt;&lt;/script&gt;&lt;p class=&quot;info&quot;&gt;Note that errors in migrations are easier to manage if you split every &lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt; statement to its own migration file.&lt;/p&gt;
&lt;p&gt;To apply the migration we just run our application again:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;play run --%local-mysql
22:19:28,016 INFO  ~ Running migrations from path conf/migrations
22:19:28,058 INFO  ~ Migrating database... applying 1 migration.
22:19:28,059 INFO  ~ Running migration 20110404221245_added_category_for_the_entity.sql.
22:19:28,439 INFO  ~ Migrated database in 0:00:00.380.
 &lt;/code&gt;&lt;/pre&gt;</content>
 </entry>
 
 <entry>
   <title>Localizing your play application with @messages</title>
   <link href="http://huljas.github.io/playframework/2011/04/01/localizing-your-play-application-with-messages"/>
   <updated>2011-04-01T00:00:00-07:00</updated>
   <id>http://huljas.github.io/playframework/2011/04/01/localizing-your-play-application-with-messages</id>
   <content type="html">&lt;p&gt;I wrote this module to help me with the localization of our application. I used to have similar tool in our old struts based system, so I first copied it to play and later rewrote it all together. I even forced myself to learn regexp, which I have been trying to avoid so far ;).&lt;/p&gt;
&lt;h3&gt;Setting up&lt;/h3&gt;
&lt;p&gt;To add this module as dependency of your application, add it to the &lt;strong&gt;dependencies.yml&lt;/strong&gt; file:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;require:
    - play -&amp;gt; carbonate {version}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then you need to add the module routes to your application &lt;strong&gt;conf/routes&lt;/strong&gt; file:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;   *    /    module:messages&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now once you restart your application you can access the tool at &lt;a href=&quot;http://localhost:9000/@messages&quot;&gt;http://localhost:9000/@messages&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;Example 1: Managing existing keys&lt;/h3&gt;
&lt;p&gt;&lt;a class=&quot;thumb&quot; href=&quot;http://1.bp.blogspot.com/-Vcs8tke5GeA/TW6PG7vpYUI/AAAAAAAAAOk/TxEWTTJIxQk/s1600/messages_existing.png&quot; rel=&quot;nofollow&quot; imageanchor=&quot;1&quot; style=&quot;clear:right; float:right; margin-left:1em; margin-bottom:1em&quot;&gt;  &lt;img border=&quot;0&quot; height=&quot;173&quot; width=&quot;400&quot; src=&quot;http://1.bp.blogspot.com/-Vcs8tke5GeA/TW6PG7vpYUI/AAAAAAAAAOk/TxEWTTJIxQk/s400/messages_existing.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This is the tab where you can modify the &lt;strong&gt;existing keys&lt;/strong&gt;, &lt;em&gt;keys that are found in sources and messages.&lt;/em&gt; Header section has selection for the language to edit and another for the language to compare to. Keys can be removed by checking the remove check box.&lt;/p&gt;
&lt;p&gt;The keep check box is reserved for valid keys that the tool doesn&amp;#8217;t find in the sources, for example keys that are generated in code.&lt;/p&gt;
&lt;h3&gt;Example 2: Localizing new keys&lt;/h3&gt;
&lt;p&gt;&lt;a class=&quot;thumb&quot; href=&quot;http://2.bp.blogspot.com/-hDqZ2gsKkX8/TW6WFopL2gI/AAAAAAAAAOs/_LqV0sIAZo4/s1600/messages_new.png&quot; imageanchor=&quot;1&quot; style=&quot;clear:right; float:right; margin-left:1em; margin-bottom:1em&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;161&quot; width=&quot;400&quot; src=&quot;http://2.bp.blogspot.com/-hDqZ2gsKkX8/TW6WFopL2gI/AAAAAAAAAOs/_LqV0sIAZo4/s400/messages_new.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;New keys&lt;/strong&gt; are &lt;em&gt;those keys that are found in the sources but are not yet localized in the messages file for the selected language&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;thumb&quot; href=&quot;http://2.bp.blogspot.com/-nSbjbL6G8ZI/TW6aUhrXqbI/AAAAAAAAAO0/jWwXo_j0cmo/s1600/messages_new_2.png&quot; imageanchor=&quot;1&quot; style=&quot;clear:right; float:right; margin-left:1em; margin-bottom:1em&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;165&quot; width=&quot;400&quot; src=&quot;http://2.bp.blogspot.com/-nSbjbL6G8ZI/TW6aUhrXqbI/AAAAAAAAAO0/jWwXo_j0cmo/s400/messages_new_2.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Since we are not sure about the first key we &lt;strong&gt;click on the key to check the sources&lt;/strong&gt;. From the sources we can see that this key is in fact part of a generated key. We choose to ignore this key, meaning that it will no longer be shown in the new keys list in any language.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;thumb&quot; href=&quot;http://2.bp.blogspot.com/-OsLxPPeS-6w/TW6a2Q-MLNI/AAAAAAAAAO8/lc13zdQmu2w/s1600/messages_new_3.png&quot; imageanchor=&quot;1&quot; style=&quot;clear:right; float:right; margin-left:1em; margin-bottom:1em&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;165&quot; width=&quot;400&quot; src=&quot;http://2.bp.blogspot.com/-OsLxPPeS-6w/TW6a2Q-MLNI/AAAAAAAAAO8/lc13zdQmu2w/s400/messages_new_3.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;After save we can find the key in the ignored keys section. Ignored keys are saved in the &lt;strong&gt;messages.ignore&lt;/strong&gt; file.&lt;/p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;/div&gt;
&lt;h3&gt;Example 3: Separating generated keys from obsolete keys&lt;/h3&gt;
&lt;p&gt;&lt;a class=&quot;thumb&quot; href=&quot;http://1.bp.blogspot.com/-G0jNKpDXhGI/TW6b26OB25I/AAAAAAAAAPE/S6hyXWudnDM/s1600/messages_obsolete.png&quot; imageanchor=&quot;1&quot; style=&quot;clear:right; float:right; margin-left:1em; margin-bottom:1em&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;155&quot; width=&quot;400&quot; src=&quot;http://1.bp.blogspot.com/-G0jNKpDXhGI/TW6b26OB25I/AAAAAAAAAPE/S6hyXWudnDM/s400/messages_obsolete.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Obsolete keys&lt;/strong&gt; are those &lt;em&gt;keys that are in the messages file, but cannot be found in the sources&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;We mark keys that are valid with the &lt;strong&gt;keep&lt;/strong&gt; check box. These keys will be added to the keep list and will be displayed among the existing keys. The keep list is saved in &lt;strong&gt;messages.keep&lt;/strong&gt; file.&lt;/p&gt;
&lt;p&gt;Keys that are old can be removed by checking the &lt;strong&gt;remove&lt;/strong&gt; check box.&lt;/p&gt;
&lt;h3&gt;References&lt;/h3&gt;
&lt;p&gt;You can find the sources at github here  &lt;a href=&quot;https://github.com/huljas/play-messages&quot;&gt;https://github.com/huljas/play-messages&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 
</feed>