AJAX with Yahoo UI components and DWR

I’ve been playing lately with YUI, the Yahoo User Interface library. So far I’ve used the autocomplete and treeview components, and I have to say they are quite easy to use and very complete. I had used Script.aculo.us autocomplete before, but YUI autocomplete has a lot of configuraiton options and all of them really well documented, like caching or being able to easily use it as a normal dropdown when the user just clicks on the field without typing anything. Event handling is also pretty easy.

DWR in the other side is a really easy way to expose your service interfaces through javascript. It is included in Appfuse if you want to see examples.

This is an example configuration that uses Spring to get the myService bean and exposes its getEmployeesByPartialName method. You can also use DWR with Spring namespaces from Spring version 2


<dwr>
  <allow>
    <create creator="spring" javascript="MyService">
      <param name="class" value="com.acme.MyService" />
      <param name="beanName" value="myService" />
      <include method="getEmployeesByPartialName"/>
      <convert converter="bean" match="com.acme.Employee"/>
    </create>
  </allow>
</dwr>

Cool, isn’t it? now prepare to be scared. If you expose a method in your service through javascript users can do all kinds of nasty things. If the user needs to be logged in, not too bad, but still you will most likely need some security framework that can secure java objects and not just http request. Spring Security AKA Acegi can do that using the AOP MethodInvocation interceptor, you just need to configure what permissions needs each method.

A tricky part was integrating YUI with DWR, at least for me that haven’t done much javascript in my life. Here you can see how the html looks like for a employees dropdown that lists the ones that match what you type in the input box, querying the service for the values. The values received can be any object, not just strings, in populateArray you can see how the values to display and the ids are stored for later retrieval.

<input type="text" id="employeeList" />
<div id="employeeListAutocomplete" class="autocomplete"></div>
<script type="text/javascript">
     <!-- populate suggestions array -->  
     function populateArray(dataFromServer, dataFromBrowser){
      for (var i = 0; i < dataFromServer.length; i++) {
       <!-- each employee object has a firstName and lastName property -->
       dataFromBrowser.push([dataFromServer[i].firstName + ' ' + dataFromServer[i].lastName, dataFromServer[i].partyId]);
      }  
     }  
       
     <!-- call dwr remote method and returns suggestions --> 
     function getEmployees(sQuery){
      var aResults = [];
      var callbackProxy = function(results){
          populateArray(results, aResults); 
      }; 

      <!-- disable async req ! --> 
      var callMetaData = { callback:callbackProxy, async:false}; 
      <!-- Call remote method --> 
      MyService.getEmployeesByPartialName(sQuery, callMetaData); 

      return aResults; 
     }

    var managerAutoComp;

    YAHOO.example.ACJSFunction = new function(){
        <!-- Instantiate JS Function DataSource -->
        this.employeesDS = new YAHOO.widget.DS_JSFunction(getEmployees);
        this.employeesDS.maxCacheEntries = 50;
   
        <!-- Instantiate Employee AutoComplete -->
        this.employeeAutoComp = new YAHOO.widget.AutoComplete('employeeList','employeeListAutocomplete', this.employeesDS);
        this.employeeAutoComp.minQueryLength = 0;
        this.employeeAutoComp.maxResultsDisplayed = 50;
        this.employeeAutoComp.prehighlightClassName = "yui-ac-prehighlight";
        this.employeeAutoComp.useShadow = true;

        <!-- autocomplete on employee field focus without typing anything -->
        this.employeeAutoComp.textboxFocusEvent.subscribe(function(){
            var sInputValue = YAHOO.util.Dom.get('employeeList').value;
            if(sInputValue.length === 0) {
                var oSelf = this;
                setTimeout(function(){oSelf.sendQuery("");},0);
            }
        });

        <!-- handler for employee selections -->
        this.employeeAutoComp.itemSelectEvent.subscribe(function(sType, aArgs){
            var aData = aArgs[2]; <!-- array of the data for the item as returned by the DataSource [value,id] -->
            var selectedId = aData[1];
            alert('you have selected id: ' + selectedId);
        });

        <!-- Preload content in the container
        this.employeeAutoComp.sendQuery("");
        -->
    };
</script> 

Happy AJAX coding!

Update: fixed the code listing, I hate Roller!

Maven at EclipseCON

I’ll be next March 17th-20th at EclipseCON in Santa Clara, California, giving two talks. if you are going to be attending and you want some specific topic to be covered please leave a comment and I’ll try to make your time worthwhile.

Q4E, Maven integration for Eclipse short talk

"Q for Eclipse" (Q4E) is a new Open Source project that integrates
Apache Maven and the Eclipse IDE for faster, more agile, and more
productive development.
The plugin allows you to run Maven from the IDE, import existing Maven
projects without intermediate steps, create new projects using Maven
archetypes, synchronize dependency management, search artifact
repositories for dependencies that are automatically downloaded, view a
graph of dependencies and more!
If you are a Maven and Eclipse user join us to discover how to take
advantage of all these features, as well as how they can help you to
improve your development process. 

Maven, Eclipse and OSGi working together tutorial

With the growing popularity of Apache Maven, Eclipse, and OSGi, the
most frequently-asked questions are: "Can they work together?" and, "Do
they fight for the same space?"
This tutorial will cover the strengths and weaknesses of each, explains
where they overlap, and how they complement each other so you can get
maximum productivity. It pays special attention to the build process,
dependency management, collaboration, repository management, and
available tools, as well as the future direction of the technologies.
The solutions proposed will be based on the work done in the Apache
Maven and Apache Felix projects, along with several Eclipse Foundation
projects like Eclipse PDE
and Eclipse Buckminster.

Q for Eclipse 0.4.0 released

A new release of Q4E, the Eclipse plugin for Maven, is out. I’m going to quote Abel announcement, only adding something that he forgot, the new 0.5.0 release we are working on has initial support for importing parent projects (pom projects).

After some delay (you can blame it on Christmas� we committers need to have some fun far away from the computer), there is a new version of q4e waiting for you.

This version is 0.4.0 and has some interesting new features. Probably the best one is that it is much faster.

On the fancier side of things, the dependency viewer has been cleaned a bit and now more cleanly draws the dependencies. Judge for yourself:

q4e dependency viewer

For 0.5.0, we have a new contributor, Jake Pezaro who is working on integrating his dependency analysis tool into q4e.

Also, we are introducing a new and noteworthy page for q4e, and you can follow the current status from the wiki (what�s pending, what�s done and previous releases� changelog). You can use the list of pending tasks to take a look at what we�re trying to get into 0.5.0� but also to help us! We always welcome helping hands (remember that you can look for easy tasks to get started).

If you like living on the edge, the development update site already has a preview of the upcoming 0.5.0 version. Come and try it!

Exist Global Acquires DevZuz

It’s finally public, Exist Global has acquired DevZuz. Exist is based in the Philippines, and we are very familiar with the great people there, as we have been using their services for the last years.

I’m proud to join Exist in a role that will allow me to get involved in new projects and technologies, helping Exist leverage open source, but still continuing forward with Q4E and involved in the Apache and Eclipse Foundations. No, I’m not moving to Philippines ( yet 😉 ) although I’ll probably spend there even more time.

Read the news at Reuters.

Eclipse jars now in Maven repository

After some time trying to figure out what the best conversion from Eclipse plugins / OSGi bundles naming conventions to Maven naming I had finally put many Eclipse plugins in the central Maven repository (see only the Eclipse plugins that got copied in this batch). It’s sometimes a bit tricky, but the main goal was a bidirectional automated translation from Bundle-SymbolicName and Bundle-Version to groupId, artifactId and version in Maven.

Basically you take the BundleId and use the dots to split it. The last section is the artifactId, the rest is the groupId. For the versions they are pretty much the same, only changing dashes and underscores. There’s a maven-osgi library very light that you can use to do these conversions, used by the Maven Eclipse and Felix bundle plugins.

This improves the support for building Eclipse plugins from Maven, although you can do some things already, or using Eclipse libraries from Maven projects. We’ll see more activity in these two fields soon.

Some people complained that you get weird jar names like net-1.0.0-I20070531.jar. Something I’d like to point out is that the repository is just an interface, the way artifacts are stored internally shouldn’t matter to the clients (Maven, Ivy,…). Plugins that generate a flat directory with jars (like assembly, war,…) should take care of renaming the jars to groupId.artifactId, to avoid clashes in the namespace, and so you get a meaningful name out of a hierarchical directory structure.

So you want Maven integration in Eclipse?

You are lucky, the Eclipse integration for Apache Maven (Eclipse IAM) proposal has been approved, and is the first step to have a project at the Eclipse Foundation.

The proposal is based on the work done at Q4E and we hope that by taking it into the next level at the Eclipse Foundation more people will be interested and will collaborate to make it completely successful and hopefully part of the Eclipse IDE distribution.

Please join us at the eclipse.technology.iam newsgroup at news.eclipse.org (web interface) for suggestions, discussions and information on the project.

DevZuz CodeAtlas now live

I’m really proud to announce that one of the projects I’ve been working on for the last weeks has finally gone live. DevZuz CodeAtlas will enable you to search for open source components and see their information, relationships, comments, ratings,…

We are launching a private beta for now trying to gather feedback, improve the usability,… I have some invitations to give away for the readers of this weblog, just leave a comment if you are interested.

I’m really convinced that it’s going to be useful for all those developers that are using actively open source and need to be aware of the latest releases or find specific libraries. Enjoy.

OSSummit Asia postponed

Unfortunately the OSSummit Asia that I talked about last week has been postponed for 2008. It’s definitely a pity.

OSSummit Asia

If you happen to be close by Hong Kong ( who isn’t? 😉 ), check out OSSummit, 26-30 November, the first joined conference between The Apache Software Foundation and Eclipse Foundation. I’ll be giving a talk Maven, Eclipse and OSGi working together.

We also hope to have a Birds-of-a-feather (BOF) session during the conference for those interested in Maven.

The Apache Software Foundation and Eclipse Foundation are joining together for the first time at OSSummit ASIA 2007 – November 26 through 30 at the Cyberport in Hong Kong

OSSummit combines 2 days of in-depth hands on trainings followed by a 3 day conference featuring over 60 presentations, Birds of a Feather gatherings to interact throughout each day, interactive keynote panel, and Li Gong Chairman and CEO of Mozilla Online Ltd presenting the featured keynote.

OS Summit Asia

Kepler going through the creation review as an Eclipse project

So Kepler is finally going through the creation review this Wednesday and hopefully in a matter of a couple of weeks it will be setup as a proper Eclipse project with source control, issue tracking and all those goodies that will allow us to commit our code and show the world what we’ve been working on.

And some more pictures…

Greenland glacier
Greenland

Grand Canyon
Grand Canyon

Grand Canyon panorama
Grand Canyon panorama

Grand Canyon
Grand Canyon

Grand Canyon
Grand Canyon