Sunday, August 22, 2010

Adding StringUtils to the String Class in Groovy/Grails

This one has been blogged about before - but I thought it was just such an easy way to add very useful functionality to the String class that I thought I would call it out.

I had the need to use the Apache StringUtils methods so often in my application that I started to wish that the methods were just native to the String class. Adding this kind of capability is called mixins in Groovy.

To check if a string is null, empty or whitespace, with the Apache package you have to write:

If( StringUtils.isBlank( myStringToCheck) ) {
// then is blank
}

what I wanted to do was:

if( myStringToCheck.isBlank() ) {
// then is blank
}

I just thought this was easier to read and easier to write.

This is actually incredibly easy to do in Groovy/Grails.

The way I went about do this was to add the following line of code to the Bootstrap.groovy file:

// add apache commons lang StringUtils methods to the String class
String.metaClass.mixin StringUtils

add the commons-lang jar file to the lib directory and that is it.

Now for any String I can use any of the methods in the StringUtils package as though they were native methods to the String class.

Nice!

Wednesday, May 5, 2010

Grails: Dynamically adding a save method to Domain objects that show errors

I was writing some integration tests and I noticed that one of my asserts was failing on a save. It was not clear to me why, so I started to add some diagnostic printing of errors. Then I thought - what happens if another domain object has a problem saving? Am I going to have to do this kind of error check all of the time? Ugh..

Using what I have already blogged about in terms of adding metaprogramming for Services, and a quick google search: See this link where I got most of the inspiration.

What I did was add the following method to Bootstrap.groovy.

  def addDebugSaveToDomainObjects() {
grailsApplication.domainClasses.each {
it.metaClass.debugSave = {
def obj = delegate.save()
if( obj == null ) {
println "Error saving " + delegate.getClass().name
delegate.errors.allErrors.each {
println it
}
}
return obj
}
}
}



The above code will add a new method to all Domain objects call debugSave. If there is a problem while saving the domain object this new method will print all of the errors. These errors can be seen in the junit reports.

I did it this way because I really only want these methods when I am testing and I did not want to clutter the domain API with this debug code.

In the bootstrap, I look at the environment and based on the environment name I will call the above closure ( or not as the case may be ).

Next in my integration test, I can execute code like the following:

def myDomain = new Domain(....)
assertNotNull myDomain.debugSave()

If the assert fails, then the Junit reports will tell me so and the output will tell me why.

I hope you found this information useful.

Tuesday, March 30, 2010

Grails: Intercepting Service class methods - part 3

Well - I have a couple of updates to my previous post:

1) Grails 1.2.2 is out and it does look like the Spring DSL for AOP definitions is fixed.
2) using the Spring xml definition in part 2 performed very poorly. I am not sure why but if I changed the pointcut definition to:

pointcut="execution(* com.redpointtech.flex..*Service..*(..))"


it performed MUCH better

Given that the DSL is fixed and I needed it to perform better I have changed the AOP definition again to the following in the resources.groovy file.

 beans = {  
xmlns aop:"http://www.springframework.org/schema/aop"
securityAspect(SecurityAspect)
aop {
config("proxy-target-class":true) {
aspect(id:'theSecurityAspectDef', ref:'securityAspect') {
around method: "invoke", pointcut:"execution(* com.redpointtech.flex..*Service..*(..))"
}
}
}
}


I think this concludes my saga to get Flex services intercepted. I hope this helps you out.

Tuesday, March 16, 2010

Grails: Intercepting Service class methods - part 2

As a continuation from my earlier post about intercepting Grails Service methods - as it turns out that approach will not work when using flex-remoting. If you want to intercept your services that you expose as Flex services you have to use Spring AOP. Because of a bug: http://jira.codehaus.org/browse/GRAILS-5932 you will have to use the XML version of AOP definition.

To do that you have to create a resources.xml file in grails-app/conf/spring directory and it would look like the following:

 <?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!--
we are defining the AOP beans in the xml file because of this bug which will
not be fixed until 1.2.2 and we are using 1.2.1
http://jira.codehaus.org/browse/GRAILS-5932
aspect are called twice because of the above bug. See
http://n4.nabble.com/Advice-called-2-or-more-times-for-aspect-with-execution-pointcut-Bug-td1588831.html
toward the bottom Graeme says that 1.2.2 will have the fixes, and 1.3 M1 already does have the
fix. For now we have to live with the interceptor called twice.
-->
<bean id="myAroundAspect" class="com.redpointtech.aop.MyAroundAspect" />
<aop:config proxy-target-class="true">
<aop:aspect id="theAspect" ref="myAroundAspect">
<aop:around method="invoke" pointcut="target(com.redpointtech.aop.AOPMarkerInterface)" />
</aop:aspect>
</aop:config>
</beans>


This assumes that you want to intercept any class that implements the AOPMarkerInterface.

The Aspect looks like the following:

 class MyAroundAspect {  
public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable {
Object returnVal = null
println "MyAroundAspect called...."
returnVal = joinPoint.proceed()
return returnVal
}
}


A couple of take aways:
1) do not try to do what I suggested in my previous blog post. That will NOT work for Flex services
2) do not use resources.groovy and the Spring DSL - there is a bug in the 1.2.x version that prevents that from working. This is suppose to be fixed in the 1.2.2 release ( not yet released) and the 1.3 M1 ( available now )
3) do use resources.xml and define your Aspect the old fashion way - the old ways work just great.

I hope this saves you some time.

Saturday, March 13, 2010

Grails: Intercepting Service class methods

It took me a little while to fully understand how to add some kind of interceptor to a Grails Service class and I wanted to add a blog post incase others have a similar question. After doing some research, and piecing together information from various sites I ended up with the information here. I hope it is helpful to you.

What problem am I trying to solve?
Adding interceptors to Controllers is really easy and I wanted to add some interceptors to my Service classes in a similar fashion. But it appears that you cannot do this for Service classes - just Controller classes. If I am wrong, please someone show me how best to do this.

Why am I trying to do this?
I use Flex in many of my application implementations and I use BlazeDS/AMF in many of the cases. I needed to add a security check on every exposed Flex service.

How did I do this?
In my service class I added a custom property that I called 'secureService'. But as you will see you can use anything.

 class TeslaService {  
boolean transactional = true
// secureService is a custom property that is looked for in the BootStrap.groovy file and if
// found in a ServiceClass, then it modifies the invokeMethod to add some code before and
// after the call.
boolean secureService = true
def serviceMethod() {
println 'Tesla Service Method called'
}
}


Next I updated my BootStrap.groovy file and for each ServiceClass, checked for the 'secureService' property being set to true. If it was, I then used the MetaClass InvokeMethod and assigned a new closure that could add a security check.

The updated bootstrap file looked like the following:
 class BootStrap {  
def grailsApplication
def init = { servletContext ->
grailsApplication.serviceClasses.each {
def isSecured = it.getPropertyValue('secureService')
if( isSecured ) {
// example of how to 'intercept' service classes.
it.metaClass.invokeMethod = { name, args ->
println '''SECURE before $name'''
def res = delegate.metaClass.getMetaMethod(name,args).invoke( delegate, args)
println '''SECURE after $name. res=$res'''
res
}
}
}
}
def destroy = {
}
}


To test this I created an Integration test. This test looked like the following:

 class TeslaServiceIntegrationTests extends GrailsUnitTestCase {  
def teslaService
protected void setUp() {
super.setUp()
}
protected void tearDown() {
super.tearDown()
}
void testServiceMethod() {
teslaService.serviceMethod()
}
}


The output of this was the following:
SECURE before serviceMethod
Tesla Service Method called
SECURE after serviceMethod. res=null

Some of the links that I used are below as the information from these posts might also be helpful to you.

http://www.pubbs.net/grails/201001/9868/
http://mrhaki.blogspot.com/2010/01/grails-goodness-access-grails.html
http://www.grails.org/doc/latest/api/org/codehaus/groovy/grails/commons/ClassPropertyFetcher.html

Tuesday, January 5, 2010

FlexBuilder 3 and FlexUnit4 RC1

This will be a quick post on how to setup FlexBuilder project to use FlexUnit4. This post will be more prescriptive with just the steps without a lot of explanation. The FlexUnit site has good documentation.

1) Setup a Flex Project as you normally would.
2) Download the FlexUnit4 from this link.
3) Unzip and copy all of the swc files to your projects libs directory. There should be 4 swc files.
4) create a package/folder under the src directory called flexUnitTests. It could be called anything.
5) Create a test Suite class. This is just a POAO ( plain old actionscript object ). I called mine FlexUnit4Suite and it looked like the following:
1:  package flexUnitTests  
2: {
3: import org.flexunit.runners.Suite;
4: [Suite]
5: [RunWith("org.flexunit.runners.Suite")]
6: public class FlexUnit4Suite
7: {
8: public var testCase1:MyTestCase;
9: }
10: }


Lines 4 and 5 are the most interesting. Use the Suite and RunsWith metadata tags. Line 8 is a reference to your TestCase class. As you add more TestCase classes, just add a new variable here. You dont have to instantiate it, or anything - just declare it.

6) Create your TestCase class, which again is a POAO. Mine looked like the following:

1:  package flexUnitTests  
2: {
3: import flexunit.framework.Assert;
4: public class MyTestCase
5: {
6: public function MyTestCase()
7: {
8: }
9: [BeforeClass]
10: public static function runBeforeClass():void {
11: trace("runBeforeClass");
12: }
13: [AfterClass]
14: public static function runAfterClass():void {
15: trace("runAfterClass");
16: }
17: [Before]
18: public function runBeforeEveryTest():void
19: {
20: trace("runBeforeEveryTest");
21: }
22: [After]
23: public function runAfterEveryTest():void
24: {
25: trace("runAfterEveryTest");
26: }
27: [Test]
28: public function testTrue():void
29: {
30: trace("checkMethod");
31: Assert.assertTrue( true );
32: }
33: [Ignore("Not Ready to Run")]
34: [Test]
35: public function testNotReady():void
36: {
37: Assert.assertFalse( true );
38: }
39: }
40: }


7) Create a new Application for the test runner. You dont have to create a new application, but I assume you already have an Application class that really runs your application. I created another Application called FlexUnitTestRunner. It looked like the following:

1:  <?xml version="1.0" encoding="utf-8"?>  
2: <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
3: xmlns:flexunitrunning="http://www.adobe.com/2009/flexUnitUIRunner"
4: minWidth="1024" minHeight="768" creationComplete="onCreationComplete()">
5: <mx:Script>
6: <![CDATA[
7: import flexUnitTests.FlexUnit4Suite;
8: import org.flexunit.listeners.UIListener;
9: import org.flexunit.runner.FlexUnitCore;
10: private var flexUnitCore:FlexUnitCore;
11: private function onCreationComplete():void {
12: flexUnitCore = new FlexUnitCore();
13: flexUnitCore.addListener(new UIListener( testRunner));
14: flexUnitCore.run(FlexUnit4Suite);
15: }
16: ]]>
17: </mx:Script>
18: <flexunitrunning:TestRunnerBase id="testRunner" width="100%" height="100%" />
19: </mx:Application>



Run the FlexUnitTestRunner application and my console output looked like:
1:  runBeforeClass  
2: runBeforeEveryTest
3: checkMethod
4: runAfterEveryTest
5: runAfterClass


and a browser window popped up with the results of my test case.

Thats it in a nutshell how to get a Flex Builder 3, sdk 3.5, FlexUnit4 up and running quickly.

Sunday, January 3, 2010

FlexORM

What seems like many years ago now, because it was - I saw a presentation by Christophe Coenraets where he talked about a simple ORM for AIR. You can see the original blog post here.

I have used Hibernate for many years, and now most recently GORM and I am a big fan of ORM tools when used right and in the right measure.

When I started looking at an AIR application I decided to revisit the work done in that original blog posting and I was very pleased to see that work had continued on with the ORM mapping, now called FlexORM. (Which is an interesting name since it only works with AIR ). You can find more information about the FlexORM tool here.

What I am going to do is present some examples of how I used it. I am hoping that my examples will help answer your questions on how to use the tool or inspire you start using it.

In my simple application my domain model is one where a Project can have sub-projects to form a project hierarchy and each project can have many TimeEntry references but a TimeEntry is only allocated to a single Project.

Lets look at the Project domain class first.
package com.redpointtech.domain
{
import mx.collections.ArrayCollection;
import mx.collections.IList;

[Bindable]
[Table( name="PROJECT")]
public class Project
{
// each project can contain a number of child or
// sub projects.
private var _subProjects:IList = new ArrayCollection();
private var _timeEntries:IList = new ArrayCollection();

[Id]
public var id:int;

[Column( name="proj_name")]
public var name:String;

[Column( name="proj_desc")]
public var desc:String;

[Column(name="color")]
public var color:Number;

[ManyToOne(name="parent_id", inverse="true")]
public var parent:Project;

[OneToMany(type="com.redpointtech.domain.Project", fkColumn="parent_id", lazy="false", cascade="save-update", indexed="true")]
public function set subProjects(value:IList):void {
_subProjects = value;
}
public function get subProjects():IList {
return _subProjects;
}
public function addSubProject(value:Project):void {
value.parent = this;
_subProjects.addItem(value);
}

[OneToMany(type="com.redpointtech.domain.TimeEntry", fkColumn="project_id", lazy="true", cascade="all", indexed="true")]
public function set timeEntries(value:IList):void {
_timeEntries = value;
}
public function get timeEntries():IList {
return _timeEntries;
}
public function addTimeEntry(value:TimeEntry):void {
value.project = this;
_timeEntries.addItem(value);
}


public function Project()
{
}

}
}


The primary metadata tags I used user:
[Table( name="tbd")] to define the table name for the domain object.

[Id] to tell the ORM which field is used for an id. As of right now the id value must be of type 'int'.

[Column( name="tbd")] to define the column name for a property. If the name field is not specified then it uses the property name.

[ManyToOne(name="col name", inverse="true/false")] to define the ManyToOne relationship with itself.

[OneToMany(...)] metadata tag is specific on the set/get methods. Also noticed that we added an 'add' method which sets the parent of the Project and adds the Project to the collection of sub projects.

The TimeEntry class looks like the following:
package com.redpointtech.domain
{
import com.redpointtech.util.Constants;

[Bindable]
[Table( name="TIMEENTRY")]
public class TimeEntry
{

[Id]
public var id:int;

[Column( name="full_year")]
public var fullYear:int;

[Column( name="month")]
public var month:int;

[Column( name="day_of_month")]
public var dayOfMonth:int; // Date.date equivalent

[Column( name="start_hour")]
public var startHour:int;

[Column( name="start_min")]
public var startMin:int;

[Column( name="end_hour")]
public var endHour:int;

[Column( name="end_min")]
public var endMin:int;

[Column( name="notes")]
public var notes:String="";

[Column( name="summary")]
public var summary:String="";

[ManyToOne(name="project_id", inverse="true")]
public var project:Project;

public function TimeEntry()
{
}

public function setDate(date:Date):void {
fullYear = date.fullYear;
month = date.month;
dayOfMonth = date.date;

}
public function setStartTime( hour:int, min:int):void {
startHour = hour;
startMin = min;
}
public function getStartDate():Date {
var startDate:Date = new Date(fullYear, month,dayOfMonth,startHour,startMin);
return startDate;
}

public function setEndTime(hour:int, min:int):void {
endHour = hour;
endMin = min;
}
public function getEndDate():Date {
var endDate:Date = new Date(fullYear,month,dayOfMonth,endHour,endMin);
return endDate;

}
private function _getTimeDiffInMillis():Number {
var startDate:Date = getStartDate();

var endDate:Date = getEndDate();

var diff:Number = endDate.getTime() - startDate.getTime();

return diff;
}

[Transient]
public function get elapsedHours():Number {
var diff:Number = _getTimeDiffInMillis();

var hours:Number = diff / Constants.millisecondsPerHour;

var floorHours:Number = Math.floor(hours);


return floorHours;
}

[Transient]
public function get elapsedMinutes():Number {
var hours:Number = elapsedHours;
var milliHours:Number = hours * Constants.millisecondsPerHour;

var diff:Number = _getTimeDiffInMillis();

var minuteDiff:Number = diff - ( milliHours );

var minutes:Number = minuteDiff / Constants.millisecondsPerMinute;
var roundedMinutes:Number = Math.min(Math.round(minutes),59);
return roundedMinutes;

}

}
}


The only real difference is the use of Transient to tell FlexORM that these are not used in the persistence of the object.

Now that we have a feel for how to annotate our domain classes to be used by FlexORM, lets see how to persist them to the database.

I am using FlashBuilder Beta2 with FlexUnit4 RC1. I will go over the integration of FlashBuilder and FlexUnit in another blog posting. For this one I wanted to remain focused on using FlexORM.

Below is a unit test function to test Project CRUD ( create read update delete )

    [Test]
public function testCRUDNoHier():void {
trace("----------------testCRUDNoHier----------------");
var em:EntityManager = EntityManager.instance;

var p:Project = new Project();
p.name="P1";
p.desc = "proj from unit test";
p.color=0xFF001E;
em.save(p);

Assert.assertEquals("P1",p.name);

var p2:Project = new Project();
p2.name="P2";
p2.desc = "proj2 from unit test";
p2.color=0xAAAAAA;
em.save(p2);

Assert.assertEquals("P2",p2.name);
Assert.assertEquals("P1",p.name);

var p1:Project = em.loadItem(Project, p.id) as Project;
Assert.assertEquals("P1",p1.name);

var ps:ArrayCollection = em.findAll(Project);
Assert.assertEquals(2,ps.length);

p1.desc = "New P1 desc";
em.save(p1);

var p3:Project = em.loadItem(Project,p1.id) as Project;
Assert.assertEquals("New P1 desc", p3.desc);

em.remove(p2);
ps = em.findAll(Project);
Assert.assertEquals(1,ps.length);


}



We start the test method by getting a reference to the EntityManager via EntityManager.instance. The first thing we do in the test case, is to create a Project and the save it to the DB via the em.save(p).

That is all you have to do to save an entity, you do not need to write any sql yet.

To load an item for which you have the id, you use the loadItem method passing in the Project class and the id value.

To find all records for a particular class, you use the findAll method.

FlexORM even has a Criteria API. While it is not on the same level as the Hibernate Criteria, it is still very useful.

Below is an example method to find TimeEntries on a particular day:

    public static function findAllOn(fullYear:Number,month:Number=-1,dayOfMonth:Number=-1):ArrayCollection {
var teCriteria:Criteria = entityManager.createCriteria(TimeEntry);
teCriteria.addEqualsCondition("fullYear", fullYear);
if( month > -1 ) teCriteria.addEqualsCondition("month",month);
if( dayOfMonth > -1 ) teCriteria.addEqualsCondition("dayOfMonth",dayOfMonth);
teCriteria.addSort("startHour");

return entityManager.fetchCriteria(teCriteria);
}



You use the EntityManager to create the criteria for an annotated class. In this example I am using simple conditions to see that the TimeEntry has values that equal those passed in, and then sort the returned values based on the starting hour.

Again - so far I have created no sql myself. I am sure I will have a case where I need to hand craft some sql, and this will be fairly easy even with the EntityManager.

The idea behind the ORM tools, IMHO, is not to take over all of the persistence work but instead take over the mundane and tedious persistence work so we can concentrate on the more difficult tasks. So far FlexORM has done that.

I hope this quick example was useful in getting you interested in looking at FlexORM.

Next time I will discuss how I used FlashBuilder Beta2 and FlexUnit to test the application.