Paul Osborne - Java/PHP Developer

Grails and Groovy – Post Project Review

Having spent the last eight months working almost exclusively in Grails I wouldn’t hesitate to recommend it, it’s fantastic. A non-disclosure agreement I signed with the customer prevents me from giving detailed features of the project but here are some of my experiences and findings.

I recommended Grails as a development platform for the project because I wanted a platform which had all of the sturdiness of a test-driven Java project but with the speed and flexibility of a modern framework suitable for story-based, agile development. I’d been using a PHP framework based on Rails for a few months beforehand and the ‘Convention over Configuration’ paradigm was lovely to work with. The prospect of returning to configuration-heavy development usually associated with Java applications was no longer appealing.

Grails overlays the Spring Framework and Hibernate. Combining these with a Rails-like framework that provides seamless testing made it a compelling choice when I was looking for a platform at the beginning of the project.

The Groovy programming language used in Grails probably put the biggest smile on my face. Java has been my preferred development language because of the excellent testing frameworks, strict syntax and IDE integration; until now. Because Groovy is Java, libraries such as JUnit will all work. The free SpringSource Tool Suite (STS) is Eclipse with a few additions that make development in Grails simpler. Groovy’s syntax and its ‘dynamic’ features made it a pleasure to work with and unfortunately writing Java now seems very long-winded and rigid.

I initially began the project writing code in almost pure Java. It compiled and worked but it did not take long before I was using Groovy’s cleaner syntax wherever I could. Huge amounts of code began to become unnecessary and could be removed making it much clearer and manageable. Groovy automatically creates getter and setter methods behind the scenes; not needing to write these or having them cluttering up the code saves a lot of time and reduces the number of lines of code considerably.

Another powerful feature of Groovy not present in Java is the ‘Closure’. Closures provide the ability to pass ‘code blocks’ as arguments to methods.  For example, to perform a dynamic operation on a list:

// Create a method which takes a 'dynamic'
// operation operating on each item of a list
// or array.
def processAllMyItems(myItems, anOperation) {
    myItems.each {
        anOperation(item)
    }
}

// Create some items to process.
def someItems = ...

// Print items...
processAllMyItems(someItems)  {
    print "Calling closure for: ${item.name}"
}

// Update items...
processAllMyItems(someItems { it.update(); }

To start my Grails project I simply created a domain object; Grails then created everything I needed for an MVC application including all web pages, admin pages for CRUD operations, database and test files. When I needed to add a new field, I only had to add one line to the domain object and the database was updated automatically.  Grails also provides ‘scaffolding’ which dynamically creates screens from the domain object so even the user interface updates automatically. Since the requirements were still evolving as stories, this was exactly what I needed to avoid being tied-down to an implementation too early.

The project also involved a client Android app which accessed the main application using REST. Grails provides wrappers and plugins which make it very easy to work with web services and REST. The wrappers within Grails meant that the server-side code was largely agnostic of the nature of the data being received meaning that data could be received as JSON or HTTP parameters without any code changes.

The major downside when developing in Java, when compared with PHP or JavaScript, is the overhead of restarting the application to pick-up certain changes or re-run tests. Towards the end of the project my understanding of the testing features in Groovy and Grails had improved and it became possible to test more and more inside the IDE as unit tests and avoid the need to restart servers all together. Grails plugins are also available which reduce development test time considerably too.

It has been hard to tell yet whether the ‘dynamic’ nature of Groovy and ‘Convention over Configuration’ in Grails will have any noticeable negative impact on performance and scalability. From what I have seen so far these points seem to have been addressed by the Grails developers. And, the gains in development speed will certainly make up for this and free-up funds for performance testing and tuning.

Overall, development in Grails seems quick and robust. The framework coerces you into writing well-modelled code although as my knowledge of the platform improved I still found myself unpicking anti-patterns I had inadvertently blundered into early on.

There has been a lot to learn and I will do some things differently next time around, particularly with functional testing. On balance, I don’t think I’ve used a platform that is as good overall as Grails.

Some links:

Configuring Grails Fixtures for Functional Testing

Having used fixtures to configure test data within in other frameworks I thought I’d use the Grails Fixtures plugin for my latest Grails project. It may have simply been a bad-day-at-the-office but for whatever reason, this took me much longer than it probably should have! Having finally managed to piece everything together using the documentation at:

  1. http://www.grails.org/Fixtures+Plug
  2. http://www.keithnordstrom.com/post/2010/01/11/Grails-Fixtures-Plugin-PrePost-processing.aspx
  3. http://jira.codehaus.org/browse/GRAILSPLUGINS-830?page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel

The two key points I originally missed from the documentation were:

  1. Fixture files are .groovy files (rather than .dat, .txt etc.) because they contain closures. And, the .groovy extension is not included on the fixtureLoader.load() call. If the file is not found, the fixtures plugin will give a “pattern” error. The default root path for fixtures is “/fixtures”.
  2. The object classes need to be fully-qualified even if the calling class (BootStrap.groovy) has imported them. The fixtures plugin error messages will provide details of these within a parse error.

Here is a summary of my final code in case anyone else finds themselves struggling with this. This code does the following:

  1. Creates and persists a Spring Security Role.
  2. Defines an Organisation fixture.
  3. Defines a Person fixture belonging to the organisation above.
  4. Defines a User fixture for the person above, giving them a username and encrypted password.
  5. Assigns the User to the Role defined in step 1.

The two main files are involved are:

  1. /grails-app/conf/BootStrap.groovy
  2. /fixtures/myfixtures.groovy (Fixture files require the .groovy extension. Potentially, there should be separate fixture files for Organisation, Person and User.)

The steps involved are:

Step 1: Install and configure the Spring Security Plugin

Step 2: Install and configure the Build Test Data Plugin

The Build Test data Plugin automatically creates beans which satisfy the constraints on domain objects but still allow fields to be specified. Using this plugin to create fixtures helps to protect the fixtures from breaking when fields are added to domain objects in the future.

Step 3: Install the Grails fixtures plugin: grails install-plugin fixtures

Step 4: Edit BootStrap.groovy:

import com.myco.organisation.Organisation
import com.myco.organisation.Person
import com.myco.auth.Role
import com.myco.auth.User
import com.myco.auth.UserRole
import grails.util.GrailsUtil
import org.codehaus.groovy.grails.commons.GrailsApplication 

class BootStrap {

    def springSecurityService   // autowired by Spring Security plugin
    def fixtureLoader           // autowired by Fixtures plugin

    def init = { servletContext ->

	boolean isProd = GrailsUtil.environment == GrailsApplication.ENV_PRODUCTION

	// Create fixtures for non-production environment. E.g. Test and development.
	if (!isProd) {
		fixtureLoader.load('myfixtures')    // Ommit the .groovy extension!
		assert Organisation.count()	== 1
		assert Person.count()		== 1
		assert User.count()		== 1
		assert UserRole.count()		== 1
	}		

    }

    def destroy = {
    }

}

Step 5: Edit /fixtures/myfixtures.groovy

// NOTE 1: fully-qualified class names are required!
// NOTE 2: using "build-test-data" plugin building function for future flexibility
pre {
	// Expose a role for the fixture and post closures
	carrierRole = com.myco.auth.Role.findByAuthority('ROLE_ORGANISATION_ADMIN')
}
fixture {
	organisation1 = com.myco.organisation.Organisation.build(name:"Some Organisation")
	johnsmith      = com.myco.organisation.Person.build(firstName:"John",lastName:"Smith",organisation: organisation1)
	user             = com.myco.auth.User.build(person:johnsmith, username:"jsmith", password:springSecurityService.encodePassword('mypassword'), enabled: true)
}
post {
	// Assign a role to the user and flush the data to the database.
	com.myco.auth.UserRole.create(user, carrierRole, true)
}

Step 6: Create a functional test (in this case /selenium/FixtureTests.groovy )

import com.myco.organisation.Person
import grails.plugins.selenium.SeleniumAware
import groovy.util.GroovyTestCase;

@Mixin(SeleniumAware)
class FixtureTests extends GroovyTestCase {

	void setUp() {
	}

	void testLoad() {
		def p = Person.findByFirstName("John")
		assertNotNull p
		assertEquals "Smith", p.lastName
	}	

}

Iteration Complete: Custom Intranet CMS



Screenshots of the JMS after iteration

Custom CMS developed using the Yii PHP Framework, EXT JS and JQuery


This administration system was developed on behalf of Kiss Online Marketing for a client providing non-destructive testing services. The system will form part of the client’s intranet.

The application was built using the Yii PHP framework, EXT JS, JQuery and AJAX. Using the Yii framework allowed an agile approach to be taken for software development. Biweekly iteration cycles were used. At the end of each cycle, the client was able to view the progress, provide feedback and prioritize items for the next iterations.

The application allows work and staff to be administered and reports output, including PDF creation.

Launch: Pulham St Mary Parish Council Website


Screenshots of Pulham St Mary Website

WordPress Website for Pulham St Mary Parish Council

This website was developed on behalf of Amanda Stones for the Pulham St Mary Parish Council. Amanda managed the project and provided the designs for the website.

WordPress was used as the foundation of the website. Custom widgets were developed and 3rd party plugins were used and modified to give the functionality desired.

Visit:

http://www.pulhamstmarypc.org.uk/

http://www.ajstones.co.uk/


Completed: Project Management CMS Web Application



Screenshots for CMS Application developed using CodeIgniter, PHP, EXT JS, JQuery

CMS Application developed using CodeIgniter, PHP, EXT JS and JQuery


I provided the coding services for this content management system (CMS), search and booking functionality on behalf of Kiss Online Marketing for an international project management training company.

The administration system was developed using the EXT JS and CodeIgniter frameworks. This CMS allows for course details and schedules to be quickly and easily maintained using a desktop-like application.

Visitors are able to dynamically search upcoming courses by type, level and location. By using AJAX throughout, the website is very responsive and simple to use. Lightweight JQuery functionality was added to provide a polished finish.

Visit:

http://www.kiss-online.co.uk/

http://www.afaprojects.com/courses/search/


Launch: ‘Discover Hawes’ Java J2ME Mobile Phone Application



Screenshots of Hawes J2ME application.

J2ME Mobile Application for Audio Trails


The ‘Discover Hawes’ Java (J2ME) mobile phone application was developed on behalf of AudioTrails.co.uk for The Yorkshire Dales National Park Authority. Visitors to Hawes in the Yorkshire Dales can now discover the town via the latest mobile technology. Mobile phone users can download a free guide from the National Park Centre by using the bluetooth feature on their mobile – no phone reception required. The application is a custom-built Java application built using the J2ME Polish Framework for MIDP compatible mobile phones.


Dover Arts Development New Website Launched





Dover Arts Development, Drupal Website

Drupal CMS website developed for Dover Arts Development



Dover Arts Development promote contemporary visual arts in the Dover District. Their new website provides a full content management system (CMS) developed using Drupal. The CMS allows editors to manage projects, artist profiles, news and media from a single administration interface. Image resizing, cropping and formatting is performed automatically by the CMS to greatly reduce the amount of time required to update the site.

The design work was completed by Edda Jones (eddajones.com).

Visit: www.dadonline.eu



Betfair PHP Application Completed

Screenshot for the Betfair PHP Demo application

Screenshot for the Betfair PHP Demo application

Betfair PHP Demo is simple application developed for a customer wanting a basic application which they could use as a framework for building a more complex betting interface.

The application is developed in PHP5 and uses SOAP to interact with Betfair to login, select markets, get account details and place bets. Page data refreshes automatically and market prices are refreshed every 1 second.

The design uses object-oriented design patterns to keep the code well-structured and ready for extension.

The code was fully documented including UML class and sequence diagrams.

Site Launch: The Chocolution

The Chocolution screenshots.

The Chocolution screenshots.

The Chocolution sell healthy, raw chocolate making kits. Their chocolate is created using methods that preserve the natural goodness in the cacao bean which mainstream commercial processes destroy.

The site was completed in collaboration with Edda Jones who provided the designs.

Morley Computing used the designs to build the site using HTML, PHP and CSS. The website also uses a custom shopping cart built using the Google Web Toolkit (GWT). The cart was integrated with PayPal Express Checkout using the PayPal API.

Visit The Chocolution website.

Drupal: 500 Internal Server Error on Modules Page

When enabling Drupal modules a 500 Internal Server Error can often appear with the default installation. Too identify the cause of the problem look in the error_log file for your web server.

Each time I have encountered this problem the cause has been due to the PHP memory limit being exceeded. The typical error message if you have allocated 16MB is:

PHP Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 77824 bytes) in /xxx/yyy/zzz/xxx/drupal/modules/node/node.pages.inc on line 221

There are several very easy ways to fix this by increasing PHPs memory limit:

  • memory_limit = 16M to your php.ini file (recommended, if you have access)
  • ini_set('memory_limit', '16M'); in your sites/default/settings.php file
  • php_value memory_limit 16M in your .htaccess file in the Drupal root

See: http://drupal.org/node/76156 for more details.