Logging database content changes in a Play Framework app

To do this we use Hibernate Envers.

There are 3 easy steps to adding change logging to the data in your existing Play Framework app (I am using 1.2.4 – version 2 may well be different):

  1. Add the library
  2. Add the standard configuration
  3. Add an annotation to each class where you want to track data changes

Add the library in conf/dependencies.yml

require:
    - play
    - play -> crud
    - org.hibernate -> hibernate-envers 3.6.1.Final:
        exclude:
            - org.hibernate -> hibernate-tools 3.2.0.ga
            - org.beanshell -> bsh 2.0b4
            - freemarker -> freemarker 2.3.8
            - org.hibernate -> jtidy r8-20060801
            - ant -> ant 1.6.5

Add the Envers configuration to conf/application.conf

# Hibernate Envers Auditing Library conf
hibernate.ejb.event.post-insert=org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener
hibernate.ejb.event.post-update=org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener
hibernate.ejb.event.post-delete=org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener
hibernate.ejb.event.pre-collection-update=org.hibernate.envers.event.AuditEventListener
hibernate.ejb.event.pre-collection-remove=org.hibernate.envers.event.AuditEventListener
hibernate.ejb.event.post-collection-recreate=org.hibernate.envers.event.AuditEventListener

Add an annotation to each class you want to audit

...
import org.hibernate.envers.Audited;
...

@Entity
@Audited
public class Book extends Model {
  public String title;
  public String author;
  public String description;
}

If you run up your app and make a change to an entity you will see a new table appear in your database. If your entity table is BOOK your new table will be BOOK_AUD. It contains a version reference (a normalised reference to a new revinfo table containing the timestamp of that audit entry) and the values of all the fields on the entity after the changes were saved.

You can go wild on the config by auditing only particular fields on an entity (just add @Audited to those fields), or adding new fields to the audit log (like who made the change or what method was called to do it), and change the name of table suffix.

It’s also quite simple to retrieve the list of versions, and this has been written up quite clearly by Matthieu Guillermin (it’s in French but Google Translate does a good job, and you can read the code anyway).

All of this information had already been written in several places, but hopefully this has brought it together in a useful way.

My config changes are from https://gist.github.com/1148850 and https://gist.github.com/1148852 by Marc Deschamps.

Samsung TV SDK – setting your project path

I have a Samsung Smart TV which means I can write apps for it. The SDK you can download is really a visual editor and a shorcut to launching the emulator – the apps themselves are mostly written in HTML, JavaScript and CSS. It comes with jQuery, and all-in-all is surprisingly acceptable.

The biggest problem is that your projects must live in the “C:Program FilesSamsungSamsung TV SDK(3.1.1)apps” folder. After a brief moment of outrage at not being able to store my code where I want, I found this blog post by Peter Friese which reminded me that Windows does actually support symlinks via the mklink tool – so running up command as the local Administrator, going to the Samsung SDK folder, deleting the apps folder (after first moving my code where I wanted it to be!) and typing

mklink /D apps c:codesamsung_tv_apps

has fixed my problem!

Devices in my house running WebKit

  • Android phone
  • Android tablet
  • iPod touch
  • iPhone
  • iPad
  • 2 x laptop (Windows Vista)
  • desktop (Ubuntu)
  • TV (Samsung)

The browser in my Nintendo DSlite and 3DS is Opera.

I don’t use the desktop version of Google Talk, nor Windows Media Player so my most frequent exposure to Trident is probably via Skype. I almost feel sorry for the Microsoft IE team. Almost.

Punished for being a good boy

My son is 2.5 years old. Two of his favourite TV programmes are Timmy Time and Octonauts . There are 78 episodes of Timmy Time and 52 episodes of Octonauts.

There is one DVD of Octonauts, which contains eight episodes. Another DVD is due next month containing another eight episodes. This will mean that 31% of this programme is available to buy.

There are eight Timmy Time DVDs totalling around 65 episodes. This means that around 83% of this programme is available to buy.

To my knowledge, the BBC have aired 100% of both these series.

A very quick look on popular, er, file distribution sites reveals about 50 episodes of Octonauts (96%), and I think every episode of Timmy Time (100%).

I want to buy stuff, and it isn’t even possible; yet it is available for free if I download it illegally. It’s been, what, five years now? Have we really not moved on?

Google Currents

At first blush I didn’t know what to make of Google’s new iOS and Android app, Currents, other than it is an almost infinitely better way to consume The Guardian than their own Android app.

After a few hours I’ve got a much better idea, and broadly, I’m not impressed. No URLs, No view-source and half the content I’ve read seems to be partial with a link through to the source website, but within the app so still no URLs or view-source. Very unsatisfactory.

When the iPad came out it got slated for being a consumption device and nothing more. That’s been shown to be false, but Currents takes that thought and runs with it. It’s the antithesis of what reading on the web should be about.

Kindle browsing

The Kindle Touch 3G is the only new Kindle with 3G available and you won’t be able to access anything except the Kindle Store and Wikipedia with it.

My Kindle Keyboard has 3G and I’ve used it, in the case of flat-phone-battery-syndrome, when out and about, to send emails to my family to let them know where I am, that I’m goin to be late, and more. I’ve used it to go on Facebook, and to read Hacker News (via http://ihackernews.com/). I think that yes, OK, it’s a shame that the absence of wikitravel means that this xkcd is no longer accurate, but the loss is greater than that – having such a cheap device with unfettered access to the internet is A Good Thing – I guess Amazon decided the bills just weren’t worth it.

Plus worries

So, I’m in on that whole Google+ thing, and my experiences of 2007 are coming back to haunt me. It’s like some kind of existential self-doubt: if my social network accounts are shut down, was I ever there? Did I ever have those memories? Did people reply to me in the way I remember? Are my social connections in the state I think they are?

On a more practical note, at least now all of my photos (which go to both Facebook and Flickr and now Plus depending on topic) are all backed up at an off-site location. The same can’t be said for the Facebook wall posts, although I do use the "Download your information" feature to take infrequent data dumps. Twitter is pretty much dead to me, so although I have historical backups, I’m don’t plan on taking any more.

Related to all this, I feel sad that my son will likely grow up in a world where licensing content will be more normal than owning it, which reminds me of this story.