Mr. Ray Hilton
Wirestorm Ltd., Software Consultancy
Homepage http://wirestorm.net
Email office@wirestorm.net
Skype rhilton
Melbourne +61 (0) 3 9016 3042

Criteria for ActiveRecord

Over the last couple of days I’ve been working on a Criteria for ActiveRecord. The concept is quite simple; rather than writing strings of SQL to pass to the find() method, we build the criteria using an object-orientated approach. Anyone who has used Hibernate, Torque, Propel et al will know how powerful and useful this can be.

For example, imagine you have a series of filters, all of which may, or may not, add constraints or expressions to your query. Each filter will need to append it’s own SQL to the end of any previously generated SQL and may even need to introspect the existing SQL to determine if its doubling up on statements or resolve potential conflicts.

With Criteria this becomes simpler. For example:

criteria = Criteria.new
criteria.and do |c|
  c.or User.role.eq(:admin)
  c.or User.active.eq(false)
  c.or User.createdAt.gt(10.hours.ago)
end
criteria.and do |c|
  c.or User.role.eq(:editor)
  c.or User.active.eq(true)
  c.or do |c2|
    c2.and User.createdAt.gt(20.days.ago)
    c2.and User.createdAt.lt(10.hours.ago)
  end
end
users = User.find(:all, criteria)

We could, at any point, introspect the state of criteria by calling:

criteria.ands.each do |c|
  put "AND #{c}"
end
criteria.ors.each do |c|
  put "OR #{c}"
end

And, finally, we can remove arbitrary bits of criteria:

criteria.ands.delete_at 2

I hope to have a release out very soon, but for the time being some more examples and API docs are on the Criteria project page on RubyForge

Setting Swing Client Properties in JavaFX

I have been looking around for a way in which to set Swing client properties easily in JavaFX, without subclassing the whole widget. This seems like the simplest (and most obvious, in hindsight) way of doing this. Documentation is still a bit thin on the ground so I am unsure if this is the proper way to do things:


trigger on (new Button) {
button.putClientProperty("Quaqua.Button.style", "placard");
}

The above code allows me to inform the underlying Quaqua LookAndFeel that I wish to use the placard button style. If I wanted to use this selectively, I would have to subclass Button.fx, provide an additional attribute (say style) and then apply the property if the attribute is set.

Obviously, you need to know the internal name for the Swing component you are trying to access, this API documentation is pretty helpful for that.

Eclipse + PHP + J2EE + Rails

I have just decided to upgrade Eclipse, and thought it best to start from scratch. I do a lot of work in Java, PHP as well as Ruby on Rails and so I tried to set up an integrated environment using Eclipse. I have documented the steps here as much as a reminder for myself as a guide for other people trying to something similar.

First, download and unzip the Ecipse + J2EE package from http://www.eclipse.org/downloads/. You do not need to ‘install’ Eclipse as such, just put the uncompressed directory somewhere logical (/Applications on a Mac, for example).

  1. Launch Eclipse :P
  2. Select: Help -> Software Update -> Final and Install.
  3. Select Search for new features to install
  4. Check the Europa Discovery Service
  5. Select Add Remote Site
  1. Type in name: Aptana (For javascript support)
  2. Type in url: http://update.aptana.com/install/3.2/
  3. Select OK
  • Select Add Remote Site (For Ruby on Rails support)

    1. Type in name: RadRails
    2. Type in url: http://update.aptana.com/install/rails/3.2/
    3. Select OK
  • Select Add Remote Site (For SVN integration)
    1. Type in name: Subclipse
    2. Type in url: http://subclipse.tigris.org/update_1.2.x
    3. Select OK
  • Select Add Remote Site (for PHP support)

    1. Type in name: PDT
    2. Type in url: http://download.eclipse.org/tools/pdt/updates/
    3. Select OK
  • Select Add Remove Site (for Hibernate support)
    1. Type in name: Hibernate Tools
    2. Type in url: http://download.jboss.org/jbosside/updates/development
    3. Select OK
  • Select Finish
  • Select your nearest mirror, when prompted
  • Select Eurpoa Discovery Site -> Programming Languages -> Ruby Development Tools
  • Select PDT
  • Select Aptana
  • Select RadRails
  • Select Subclipse
  • Select Hibernate Tools
  • Select Select Required (to resolve dependencies)
  • Select Next
  • Accept the agreement(s) and select Next
  • Select Finish
  • Go for a walk.
  • Phew, so after all that, you should have an Eclipse IDE set up to support PHP, Ruby, Rails, Javascript, Java, Hibernate and Subversion. You can, of course, select many other tools from the Europa discovery site for other development tools, graphical editing and the like.

    Windy Road » (Re)Introducing Javascript And Hidden Applets (JAHA)

    Windy Road has a nice article on the use of Javascript And Hidden Applets, what they call JAHA.  This is similar to the technique I have used in Robonobo, however I am having all sorts of issues on Mactel Firefox, possibly due to the Apple MRJ / Firefox integration.

    It would be nice to have a small js library that allowed you to dynamically instantiate and destroy embedded, hidden applets.

    Elmo RDF Beans

    “Elmo is a JavaBean pool implementation for the Sesame RDF repository. Specifically, Elmo is a subject-oriented RDF Bean pool that allows JavaBeans to be cast to different roles and provides a unique context specific view of the subject.”

    Basically, you can annotate your classes with url’s describing the RDF namespace of the class or property. You can then use SPARQL to query/retrieve the beans, and serialize them to/from XML. Serialization can be acheived through the Sesame framework.

    Elmo User Guide

    Additionally, there is another project, JRDF, that builds upon Sesame and Jena. I have yet to use it, but sounds like in encompasses the best of all worlds.

    Applets + HTML/Javascript

    As part of our, seemingly unending, refactoring of Robonobo, we packaged the whole app into an applet. However, writing a UI in swing felt very un-webby, and would fail to integreate, visually, with the style of the site. We would also like our users to have the ability to be able to customize or rebuild their own UIs using simple standards.
    Luckily, however, applets have some very cool features that I only just learnt about, inlcuding LiveConnect (from whence XPConnect comes from). This provides a bridge between java and javascript, allowing me to call java functions from javascript and return real java objects to javascript.

    Using this, we created an applet with no UI, that sits in a 0×0 pixel container, and a javascript support library which communicates and wraps the functionality provided by robonobo. All the UI can now be rendered using html, css and javascript, in a very ajaxian fashion.

    There are a couple of gotchas though:

    • You cannot return primitive types such as int, float, etc. Instead you must use the object equivilents Integer, Float, etc.
    • Calling from javascript comes from an unprivileged thread, so despite your applet’s policy, it may well be denied permissions. You can simply wrap such requests on the java-side with an AccessController.doPrivileged()
    • Large applets can lock up the browser because the JVM and the jar are being loaded concurrently. This can be aleviated by splitting your applet into a smaller jar containing just the applet and interfaces, and the main jar containing the implementation. This way, the jvm and applet initialize quickly, and then do not block the browser when the second jar is downloaded.

    Wish: P2P Virtual Office

    I work for a couple of companies whose employees are distributed across timezones and countries and someone needs to write a bit of software to make this easier. Something like BoxCloud combined with Subversion. The idea being that all shared files exist on each individual machine and are synchronized when people come online. All previous versions of the documents are stored as well. A new file can be shared with everyone just by placing into a special folder on ones computer, which is monitored by the client.

    Then, you need a Push-to-talk voip system. Often you dont need to have a full conversation with someone, but instead let them know what your doing, or that you have just updated a document, etc. A special key on the keyboard (maybe caps lock? put it to some good use) will enable you to say a few words to a person or to the whole team.

    PHP’s Lacking

    I like PHP; its fun, quick and easy… most of the time. However, I have recently had the misfortune of attempting to make WordPress, Wikka Wiki and Vanilla work together. Although, individually, they are very powerful and superb examples of open source development, they suck at working with other projects.

    Two things immediately struck me: 1) lack of a common templating system and 2) lack of a common authentication /authorisation system

    Now, I don’t mean that there should be a single templating system used by all, but there should at least be support for a simple border template system. Such a system could let me define the basic outline, css, scripts, divs, etc and then nominate extention points, i.e. places in the template other systems can hook into and publish their content. HTML with some proprietry xml tags embedded could be transformed using XSLT into the project specific template format. Indeed, drivers could be provided to allow converstion to most popular templating systems.

    Templating is a big issue with me, everyone has their own way of doing it. I do not agree with systems like Smarty that try to seperate the user from the structures of php in order to prevent them from embedding control logic in their templates. Of course, a developer shouldn’t be doing this, but this is a self-discipline issue, not a need to develop a non-standard scripting language on top of another, perfectly functional, scripting language.

    The templating issue was overcome, if in a rather hacked manner, by reducing the templates of each project to the bare minimum, ie, no css, simple div structures, etc. A proxy-like service would then explode the head and body section and recombine it using a standard template. This could be seem as object-like inheritance of a standard template dom tree (similar to the concept of XUL overlays).

    Although templating is an irritating issue, it is not quite as irritating as the authentication issue. Here we have three seperate projects, each with their own authentication system, and although some allow the easy application of your own code, quite why the community cannot make efforts to agree on a common authentication API, is beyond me.

    PHP needs to be distributed with a set of APIs that cover these common problem areas. Additionally, if you want to include code from several projects into one, you NEED some form of namespacing standard. I would love to see some sort of PHP community standards effort that would lay down the law for such things. Projects built to these specifications would automagically behave well together and allow coders to get on with writing Cool Shit rather than hacking a project’s ludircrously over-complex templating system.

    Passing Arguments using JavaXPCOM

    Following on from yesterday, I needed to pass arguments to the window being opened. The data I needed to pass was quite simple, so I simply serialized it to JSON and then passed them as strings in a nsICollection (actually, a @mozilla.org/supports-array;1 collection of @mozilla.org/supports-string;1) and then just eval()’d the data within the xul window’s javascript.

    This is a bit of a hack, but its probably the simplest way of passing structured data. What would be nice is a bit of code that would automagically serialize a bean to JSON notation… I’m sure there are many out there (This apache struts class looks promising)

    Injecting Events onto XPCOM’s UI Thread

    Finally worked this out…

    These pages are rather helpful: Mozilla’s guide to using proxies and XulPlanet’s description of the nsIProxyObjectManager

    This interface defines a proxy manager. An instance of this manager is used to create a proxy for a particular object on which you wish to invoke a method. If you are calling from any other thread that which you initialized xpcom on, you will need to do this, and as far as I am aware, there is no trivial way of doing otherwise.

    Anyway, the code to open a new window would be as follows. Note, this method assumes that there is already a constructed object called windowWatcher, which is an instanceof nsIWindowWatcher, see previous post for details.

     public void openWindow(String chromeUri, String name) {
    nsIEventQueueService eventQueueServive =
    (nsIEventQueueService)serviceManager.getServiceByContractID(
    "@mozilla.org/event-queue-service;1",
    nsIEventQueueService.NS_IEVENTQUEUESERVICE_IID);
    
    nsIEventQueue eventQueue = eventQueueServive.getSpecialEventQueue(
    nsIEventQueueService.UI_THREAD_EVENT_QUEUE);
    
    nsIProxyObjectManager proxy =
    (nsIProxyObjectManager)componentManager.createInstanceByContractID(
    "@mozilla.org/xpcomproxy;1",
    null,
    nsIProxyObjectManager.NS_IPROXYOBJECTMANAGER_IID);
    
    nsIWindowWatcher windowProxy =
    (nsIWindowWatcher)proxy.getProxyForObject(
    eventQueue,
    windowWatcher.NS_IWINDOWWATCHER_IID,
    windowWatcher,
    nsIProxyObjectManager.INVOKE_SYNC );
    
    windowProxy.openWindow(null, chromeUri, name, "centerscreen", null);
    } 

    The code here uses the service manager to get an instance of the event queue manager, which is subsequently used to retreive the main UI event queue. This is then passed to the proxy manager’s getProxyForObject method, along with the IID of the nsIWindowWatcher interface, the actual instance, and flag representing the type of proxy operation (async, sync, etc, see the xulplanet page for more info).

    This method returns an instance of nsIWindowWatcher, which is actually a proxy which will inject events onto the UI thread for you. Then, just use the object as normal, sweet!