Selecting Child Entities with GQL Queries

In Google App Engine, you can create owned one-to-many relationships between entities. The classic example you were taught at uni is that a Book owns a Chapter, which in turn, owns a Paragraph. For my site, stolencamerafinder, the example is that a camera Make owns a set of Models.

If you want to query for the model directly in GQL (from the app engine dashboard for example) you can create the key by specifying the full parent chain:

SELECT * FROM Model where __key__ = KEY('Make', 'canon', 'Model', 'canon eos 5d')

Google’s GQL reference was a little sparse on this syntax so I’m posting it here in the hope you find this faster than I worked it out.

For completeness, this is the difference it makes in JDO. My old code looked something like this:

PersistenceManager pm = PMF.get().getPersistenceManager();
Key makeKey = KeyFactory.createKey(Make.class.getSimpleName(), makeName);
Make make = pm.getObjectById(Make.class, key);
List<Model> models = make.getModels();

for (Model model : models) {
    if (model.getName().equals(modelName)) {
        return model;
    }
}

It doesn’t take much imagination to realise this doesn’t scale very nicely as the number of models increases. The new code looks like this:

PersistenceManager pm = PMF.get().getPersistenceManager();
Key makeKey = KeyFactory.createKey(Make.class.getSimpleName(), makeName);
// Can create the Model key by specifying parent key...
Key modelKey = KeyFactory.createKey(makeKey, Model.class.getSimpleName(), name);
// Bam! Get the child entity in a single query
Model model = pm.getObjectById(Model.class, modelKey);

I wouldn’t have noticed this performance bottle neck if it wasn’t for appwrench. It looks a little dead to be honest, but works well.

Tips for better low-light wedding photos with an Canon DSLR.

I just got this in an email:

A friend has asked me to take some of their wedding photos with their Canon 1000D. Had a play with it yesterday and I’m finding the flash a bit harsh. Is there a top tip to minimise this?

Firstly, if you’re using the camera in the day near natural light, the camera will be fine and it’s all down to your artistic flair.

For evening shots, I’ve written a bunch of tips, but really, there is only one:

  • Borrow a Canon Speedlite that supports ETTL. (eg, the 430EXii or 580EXii).

Seriously. If you know someone into their photography that may have one you can borrow, give them a ring. Slap it on the camera, press “mode” until it says “ETTL” on the screen, aim the flash so the light bounces of a wall/ceiling to your subject (pool skills handy here) and snap away. The rest of the tips may help prevent you taking a load of unusable shots, but if you get a speedlite, you will take the best shots of the whole day (perhaps including the photographer’s).

If you can’t get a speedlite, my next-best advice is to wait for the sun to come up or get your subject under some other light source. On-camera flash is ugly, and unflattering. I have a top-end Canon and I never use the on-camera flash on people because they’ll just look sweaty and wrinkly. If you can get people next to a lamp or something, you’ll have much more luck.

If neither of those tips will help you, here are what I consider to be “last resort” tips:

  • Prop camera against wall or on table for a steadier shot.
  • Try self timer with camera on table.
  • Form triangles with your body (tuck elbows in to sides) to become human tripod. Remember to look like a pro by cupping the lens with left hand palm upwards.

Anti-tips! Don’t do this:

  • Raise the ISO above 800. Ideally, keep the ISO at 400 or lower unless you like grainy photos.
  • Stick a Rizla to the flash. It will not soften the light, it just drinks the battery.

Keeping the camera steady will mean sharper shots, but if your subjects move, you’ll still get blurry shots. But I say go with it. Asking people to stay still means awkward faces. A blurry shot of someone laughing / dancing is a good thing if it’s deliberate.

My favourite wedding photos are always in the evening when people are relaxed (read: drunk). I’m sure you’ll capture some gems.

How to update the lastmod date in your sitemap xml with Ant

Hmm, you want to add the “lastmod” node, but you’re too lazy to ever keep that up to date so you decide to update it with your Ant script whenever you deploy. Easy.

So you’ve just written a valid sitemap.xml file for your website because Google said it was a good idea. Great.

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
   <url>
      <loc>http://www.stolencamerafinder.com/</loc>
      <lastmod>2011-04-21</lastmod>
      <changefreq>weekly</changefreq>
   </url>
</urlset>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
   <url>
      <loc>http://www.stolencamerafinder.com/</loc>
      <lastmod>2011-04-21</lastmod>
      <changefreq>weekly</changefreq>
   </url>
</urlset>

First, you’ll need the jar for a third-party Ant task called XMLTask. Once you have that, the ant target you’ll need is simply:

1
2
3
4
5
6
7
8
9
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" classpath="test-lib/xmltask.jar"/>
<target name="update-sitemap" description="update the update date">
    <tstamp>
        <format property="today" pattern="yyyy-MM-dd" locale="en,UK"/>
    </tstamp>
    <xmltask source="war/sitemap.xml" dest="war/sitemap.xml" standalone="yes" report="true">
        <replace path="/:urlset/:url/:lastmod/text()" withText="${today}"/>
    </xmltask>
</target>
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" classpath="test-lib/xmltask.jar"/>
<target name="update-sitemap" description="update the update date">
	<tstamp>
	    <format property="today" pattern="yyyy-MM-dd" locale="en,UK"/>
	</tstamp>
    <xmltask source="war/sitemap.xml" dest="war/sitemap.xml" standalone="yes" report="true">
        <replace path="/:urlset/:url/:lastmod/text()" withText="${today}"/>
    </xmltask>
</target>

Yeah yeah, this is kid’s stuff, why blog about it? Well, embarrassingly, it took me 3 hours of messing around to get it working. If I don’t write it down somewhere, I’ll only forget the next time I need to do this. Sigh.

How to detect browser support for File Api and drag and drop with javascript

Here’s a bit of info for you crazy HTML5 kids. You’ll need to include the handy Modernizr script for it to work.

var browserIsSupported = !!window.FileReader && Modernizr.draganddrop;
If you want see it in action, just visit stolencamerafinder ;)
I’ve tested this in Firefox 3.6, Chrome 10, IE8, Safari 5 and Opera 11. The only browsers from that list to support FileReader from the File API and the drag and drop events are FF and Chrome.
Let’s hope the other catch up soon…

How to stop Google Chrome using your location when searching

When you use Chrome’s omnibar to search, Chrome will redirect you to search results based on when you are geographically. This is great when I’m at home because I see (more relevant) results from google.co.uk and not google.com.

However, I’m travelling at the moment and getting my results from google.co.th is painful since most of the results are not in english.

  1. Close Chrome.
  2. Open the file “Local State” in notepad. Mine was in: C:\Users\matt\AppData\Local\Google\Chrome\User Data\Local State. Linux users may find it in ~/.config/google-chrome/ or ~/.config/chromium/, and OS X users can try ~/Library/Application Support/Google/Chrome.
  3. Look for “last_known_google_url” and “last_prompted_google_url” and change their values to the local Google page of your choice.
  4. Save and close.
  5. Done.
You may find other pages telling you to hardcode the url in the Chrome options page when you “manage search engines”. This works to an extent but breaks other things like suggest.

Thanks to Siavash for originally posting this.

Challenge Update

At the end of April I set myself the challenge of gaining a stone in weight and running a 5km race 2.5 minutes faster.

I ran the same race on Tuesday in 19:15, painfully close to my speed target of 19 minutes. However, I also weighed myself again but have actually lost a pound.

Gah!

Must try harder…

Solved: How to rotate with GWTCanvas

Having trouble rotating something you’ve drawn with GWTCanvas?

It took me a little while to work out, but GWTCanvas works slightly differently to Graphic2D in Java. It’s important to realise that with GWTCanvas, when you invoke “rotate(r)” you’re actually transforming the coordinate space that will be used for subsequent drawing.

canvas.moveTo(50, 50);
canvas.lineTo(50, 10);

canvas.stroke();

Produces:

In order to rotate it, you must invoke rotate before you start any drawing.
canvas.translate(50, 50);
canvas.rotate(Math.toRadians(45));
canvas.translate(-50, -50);

canvas.moveTo(50, 50);
canvas.lineTo(50, 10);

canvas.stroke();

Tadaa!

The same is also true for other transformation methods such as “scale” and “translate”.

Bristol Bike Rental scheme comes to an end…

Bristol have been trialling a bike rental scheme for about a year and I had high hopes for it. I paid my £20 in support of the scheme even though there were no bike stations in the places I needed them. I feel if there were more bikes and more stations it would have been ideal. (see my comment from back then…).

Unfortunately, they’ve decided to end the scheme deeming it unsuccessful:

Dear Hourbike Member,

It is with regret that I have to report that the bike sharing

pilot project that we started in partnership with Bristol City
Council, The University of the West of England and First Great
Western Trains is coming to a close. The intention of the current
pilot was to test the acceptance of the concept and determine the
potential demand for such a scheme with the hope of expanding it
beyond a pilot. Feedback from yourselves and from enquiries has
clearly shown that there is an interest in this type of scheme
but the Cycling City project has determined that it has more
pressing priorities and therefore cannot commit further funding at
this point in time.

The support of Bristol City Council both in terms of finance but
also the credibility of the scheme is particularly important, and
though other funding was received from the other founding partners
- First Great Western and the University of West of England – the
significant majority of funding has come from private investment.
Without the support of the local authority the scheme is unable
to attract further private investment.

My apologies to any of you that have noticed the recent reduction
in the availability of bikes at the stands. Our discussions with
the city council have taken some time to come to a conclusion.

We are hoping to continue the rental stations at Parkway and UWE,
and of course your memberships are still valid at the growing list
of our other Hourbike operated schemes in the UK, so please keep
your card and membership number for future use. I am a strong
believer in the value of large scale public bicycle rental schemes
and we are being successful in other towns around the country that
are investing in similar services, and I believe you will shortly
be seeing other schemes becoming available across the UK. This is
not a decision that I have taken lightly, but hope you understand
some of the reasoning behind the change.

If you would like further information from us I will do my best to
respond to you personally.

Best Regards

Tim Caswell
Managing Director
Hourbike Ltd

I’m gutted :(

More details here (the Hourbike website isn’t particularly useful).

The Challenge: 1.5 stone heavier and 2.5 minutes faster

Last night I ran a pretty shonky 21:33 (~7m/mile) for the Bridge Inn 5km. I used to be faster but haven’t really been running since last year.

I also need to put on some weight because at 6 foot I’m pretty puny weighing in at 10st 9lb. According to the NHS weight calculator I should be over 12 Stone.

I’m setting myself the challenge of knocking 2.5 minutes off my 5km time (I’ve never run it under 19 minutes before) and reaching 12st 2lb before the Bridge Inn 5km summer series finishes on July 27th (the website hints there’s one in August but I’m pretty sure it’s wrong…).

I’m not sure how hard this challenge is going to be since I pretty much plucked the numbers out of the air. I also don’t know how to find out if I’m on track. Should I aim for linear gains and therefore expect to be half way to each of my targets halfway through the challenge? Or is there some kind of curved graph in which I get most the way very quickly but it soon flattens out?

Anyway, I’m sure it’s going to be tricky since they are both conflicting targets. The heavier I get, the harder it will be to run faster, but the more I train, the harder it’ll be to get heavier.

I’ll track my progress on here as often as possible. It should be an interesting experiment…