Thursday, May 24, 2012

Grails data binding does not work on injected methods

Problem:

For Grails projects, you can use inject whole method definitions in the BootStrap.groovy file.

However, there is a drawback to method injection:

Grails will not be able to perform data binding on an injected method

 For example if we define the following method in BootStrap.groovy:

Bootstrap.groovy
domainClass.metaClass.getAttributeMap = {
   return new Map()
}

And then we our GSP file:

GSP file
<g:textField name="attributeMap[123].value" />

Grails will not be able to bind the value of the input element in the GSP defined by "attributeMap[123].value", to the domain object through the injected method getAttributeMap() .

Workaround:

Method injection has a lot of advantages and it would be a shame if you had to give it up simply because data binding could not work. Oneway around this problem, is to define a concrete method that will internally invoke the injected method.

For example,


     public Map<String, Attribute> getAttributeMapForDataBinding() { // <--- Define concrete method
         return this.getAttributeMap() // <--- Invoke the injected method
     }

Then in your GSP you can have:
<g:textField name="attributeMapForDataBinding[123].value" />

Sunday, May 13, 2012

Spring STS plugin manager issue

Problem: The Plugin Manager window does not appear

I am using Spring STS 2.9.1 when I encountered an issue where the Plugin Manager was refusing to show up

Discussion:

My first instinct was to try a newest release of Spring STS which at the time was 3.0.0.M1
I downloaded and installed it, and was able to successfully open the plugins manager. YAY!
At first, I thought I was finished and ready to continue coding. Then when I went to open up a file, just about any file, I get a NullPointerException. I realized this version had it's own set up problems, that I was not ready to deal with.

I returned back to version 2.9.1 and found a way to patch it up. The solution is below:

Solution:


I managed to find a patch with a fix to the "Grails support" extension from a nightly build. To do this, in the STS menu select:

Window -> Preferences -> Install/Update-> Available software sites


Under "Available software sites" add the following sites:
  1. http://dist.springsource.com/snapshot/TOOLS/nightly/e3.7
  2. http://download.eclipse.org/mylyn/snapshots/3.7/
 Then click "Ok" and close the window.


 To activate the update process do the following:

Help -> Check for Updates

Follow the instructions to perform the update and restart STS.

And your good to go!

References:


Liferay LDAP com.liferay.portal.UserScreenNameException

I was attempting to configure Liferay to use LDAP.

After configuration was complete, I attempted to login using my LDAP credentials and I encountered this error:


com.liferay.portal.UserScreenNameException
I looked around for answers and it appears that the "Screen name" parameter is sensitive to space characters and underscores as mentioned in this forum:

http://www.liferay.com/community/forums/-/message_boards/message/5681334

So I switched the "Screen name" value from "cn" to "uid", and this resolved the issue




Monday, April 16, 2012

Grails boostrap method injection

When getting the domain class name use the following:

String className = domainClass.clazz.name

Monday, April 9, 2012

iframe resizing

Problem:

The problem is getting the iframe to automatically resize based on the content of the child iframe.

The majority of the solutions to this problem found on the web, expect that both the parent and the child iframe belong to the same domain, and thus avoid the "cross domain" security restriction when accessing the childIFrame.body.scrollHeight  property.



Solution:

When I found this post
he gave me the idea of posting event messages between the parent and child iframe.

I decided to use the jQuery plugin found here for posting messages

Basically, it works as follows. The child iframe posts a message to the parent with its scroll height upon loading the page. The parent listens for messages and when it is received, it adjusts the height of the iframe according to the scroll height it receives from the child iframe. See the example code below:

Parent:
<script type="text/javascript">

    $(document).ready(function() {

        $.receiveMessage( 
                function(e) {
                    alert(e.data);
                    var h = Number( e.data.replace( /.*if_height=(\d+)(?:&|$)/, '$1' ) );
                    //alert('h='+h);
                    if ( !isNaN( h ) && h > 0 ) {                        
                        $('#snvFrame').height(h);                        
                    }
                    
                },                
                /*
                function(e) {
                    //alert(e);
                    return true;
                }*/
            );
         
     });


</script>

<iframe id="snvFrame" src="<%= PhenomeConfig.getInstance().getIncidentalSNPsUrl() %>" width="100%" frameborder="0" height="100%" allowtransparency="true">


</iframe>


Child:
        <script type="text/javascript">

             $(document).ready(function() {                
                $.postMessage({ if_height: $(document).height() }, 'http://localhost:8989/phenbank/incidentalSNPs.html');                
             });

        </script>




References:

Monday, April 2, 2012

Grails Domain class reflection on properties

If you want to determine what the persistent fields are for a domain class at runtime using reflection, try the following:

         DefaultGrailsDomainClass dgdc = new DefaultGrailsDomainClass(clazz)
         logger.info("properties="+dgdc.getPersistentProperties())

Tuesday, March 20, 2012

Grails instanceOf vs Java instanceof

In Grails, there is a method attached to each domain object called "instanceOf" click here

It is important to note that this method is different from the Java instanceof operator.

The Grails version of instanceOf, is used to test instances that have been wrapped as hibernate proxies for their true object type.
These proxies only subclass the generic type used to identify it's type. The problem of dealing with Hibernate proxies has been explained in more detail in this other blog entry of mine here.