<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>REVERT TO CONSOLE &#187; Ajax</title>
	<atom:link href="http://www.reverttoconsole.com/blog/category/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.reverttoconsole.com</link>
	<description>for f in *;do echo &#124; sed 'i\rtc' &#62;&#62; $f;done;</description>
	<lastBuildDate>Sat, 10 Jul 2010 12:40:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>New Google Code Project</title>
		<link>http://www.reverttoconsole.com/blog/ajax/new-google-code-project/</link>
		<comments>http://www.reverttoconsole.com/blog/ajax/new-google-code-project/#comments</comments>
		<pubDate>Fri, 08 May 2009 01:41:40 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Grails]]></category>

		<guid isPermaLink="false">http://www.reverttoconsole.com/?p=356</guid>
		<description><![CDATA[I&#8217;ve started a new google code project, tuneshost, for a presentation I&#8217;m giving this week. It&#8217;s a bare minimal start, and in the short term I&#8217;m not going to be making many enhancements- I&#8217;ll just be using it to demonstrate htmlunit and selenium testing. It&#8217;s built on the grails platform, and uses extjs for the [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://code.google.com/p/tuneshost/">I&#8217;ve started a new google code project, tuneshost</a>, for a presentation I&#8217;m giving this week. It&#8217;s a bare minimal start, and in the short term I&#8217;m not going to be making many enhancements- I&#8217;ll just be using it to demonstrate htmlunit and selenium testing.</p>
<p>It&#8217;s built on the grails platform, and uses extjs for the UI. Communication is predominantly done in a restful fashion, with json. I&#8217;m using the <a href="http://www.grails.org/AcegiSecurity+Plugin">acegi</a> and <a href="http://www.grails.org/Searchable+Plugin">searchable</a> plugins. (btw, searchable is a @#$!@# awesome plugin!!!)</p>
<p>The concept is that you search through your media (it supports music right now, but the plan is to support all media), a la google style. I&#8217;m tired of importing 4 gigs into iTunes over my home network and having to wait 2 hours for it to finish. I&#8217;m also tired of syncing my ipod. So it&#8217;s streaming media for me all the way, and smart searching.</p>

<span class="slashdigglicious">
<a href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fnew-google-code-project%2F&amp;title=New+Google+Code+Project" title="Slashdot It!"><img src="http://slashdot.org/favicon.ico" height="16" width="16" alt="[Slashdot]" /></a>
<a href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fnew-google-code-project%2F&amp;title=New+Google+Code+Project" title="Digg This Story"><img src="http://digg.com/favicon.ico" width="16" height="16" alt="[Digg]" /></a>
<a href="http://reddit.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fnew-google-code-project%2F&amp;title=New+Google+Code+Project" title="Reddit"><img src="http://reddit.com/favicon.ico" width="16" height="16" alt="[Reddit]" /></a>
<a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fnew-google-code-project%2F&amp;title=New+Google+Code+Project" title="Save to del.icio.us" onclick="window.open('http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fnew-google-code-project%2F&amp;title=New+Google+Code+Project', 'delicious', 'toolbar=no,width=700,height=400'); return false;"><img src="http://images.del.icio.us/static/img/delicious.small.gif" width="16" height="16" alt="[del.icio.us]" /></a>
<a href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fnew-google-code-project%2F" title="Share on Facebook"><img src="http://www.facebook.com/favicon.ico" width="16" height="16" alt="[Facebook]" /></a>
<a href="http://technorati.com/faves?add=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fnew-google-code-project%2F" title="Add to my Technorati Favorites"><img src="http://technorati.com/favicon.ico" width="16" height="16" alt="[Technorati]" /></a>
<a href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fnew-google-code-project%2F&amp;title=New+Google+Code+Project" title="Save to Google Bookmarks"><img src="http://www.google.com/favicon.ico" width="16" height="16" alt="[Google]" /></a>
<a href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fnew-google-code-project%2F&amp;title=New+Google+Code+Project" title="Stumble it!"><img src="http://www.stumbleupon.com/favicon.ico" width="16" height="16" alt="[StumbleUpon]" /></a>
</span>]]></content:encoded>
			<wfw:commentRss>http://www.reverttoconsole.com/blog/ajax/new-google-code-project/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>JavaScript Unit Testing</title>
		<link>http://www.reverttoconsole.com/blog/ajax/javascript-unit-testing/</link>
		<comments>http://www.reverttoconsole.com/blog/ajax/javascript-unit-testing/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 22:28:38 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[javascript testing]]></category>

		<guid isPermaLink="false">http://www.reverttoconsole.com/?p=328</guid>
		<description><![CDATA[TechTalk: Testing Rich Client Web Applications by Jeff Hemminger from Object Partners on Vimeo.]]></description>
			<content:encoded><![CDATA[<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="400" height="300" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://vimeo.com/moogaloop.swf?clip_id=4198648&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed type="application/x-shockwave-flash" width="400" height="300" src="http://vimeo.com/moogaloop.swf?clip_id=4198648&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" allowscriptaccess="always" allowfullscreen="true"></embed></object><br />
<a href="http://vimeo.com/4198648">TechTalk: Testing Rich Client Web Applications by Jeff Hemminger</a> from <a href="http://vimeo.com/user1150787">Object Partners</a> on <a href="http://vimeo.com">Vimeo</a>.</p>

<span class="slashdigglicious">
<a href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fjavascript-unit-testing%2F&amp;title=JavaScript+Unit+Testing" title="Slashdot It!"><img src="http://slashdot.org/favicon.ico" height="16" width="16" alt="[Slashdot]" /></a>
<a href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fjavascript-unit-testing%2F&amp;title=JavaScript+Unit+Testing" title="Digg This Story"><img src="http://digg.com/favicon.ico" width="16" height="16" alt="[Digg]" /></a>
<a href="http://reddit.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fjavascript-unit-testing%2F&amp;title=JavaScript+Unit+Testing" title="Reddit"><img src="http://reddit.com/favicon.ico" width="16" height="16" alt="[Reddit]" /></a>
<a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fjavascript-unit-testing%2F&amp;title=JavaScript+Unit+Testing" title="Save to del.icio.us" onclick="window.open('http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fjavascript-unit-testing%2F&amp;title=JavaScript+Unit+Testing', 'delicious', 'toolbar=no,width=700,height=400'); return false;"><img src="http://images.del.icio.us/static/img/delicious.small.gif" width="16" height="16" alt="[del.icio.us]" /></a>
<a href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fjavascript-unit-testing%2F" title="Share on Facebook"><img src="http://www.facebook.com/favicon.ico" width="16" height="16" alt="[Facebook]" /></a>
<a href="http://technorati.com/faves?add=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fjavascript-unit-testing%2F" title="Add to my Technorati Favorites"><img src="http://technorati.com/favicon.ico" width="16" height="16" alt="[Technorati]" /></a>
<a href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fjavascript-unit-testing%2F&amp;title=JavaScript+Unit+Testing" title="Save to Google Bookmarks"><img src="http://www.google.com/favicon.ico" width="16" height="16" alt="[Google]" /></a>
<a href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fjavascript-unit-testing%2F&amp;title=JavaScript+Unit+Testing" title="Stumble it!"><img src="http://www.stumbleupon.com/favicon.ico" width="16" height="16" alt="[StumbleUpon]" /></a>
</span>]]></content:encoded>
			<wfw:commentRss>http://www.reverttoconsole.com/blog/ajax/javascript-unit-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Step by Step Tutorials &#8211; DWR with Spring, Ajax &#8211; Dynamic drop down lists</title>
		<link>http://www.reverttoconsole.com/blog/ajax/step-by-step-tutorials-dwr-ajax-with-spring-dynamic-drop-down-lists/</link>
		<comments>http://www.reverttoconsole.com/blog/ajax/step-by-step-tutorials-dwr-ajax-with-spring-dynamic-drop-down-lists/#comments</comments>
		<pubDate>Wed, 07 Nov 2007 21:53:49 +0000</pubDate>
		<dc:creator>Priyatam</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[CodeSnippets]]></category>
		<category><![CDATA[Con-(in)sulting]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://reverttoconsole.com/archives/146</guid>
		<description><![CDATA[Scenario: A simple jsp page with a standard drop down box. A Selection criteria (could be another drop down box and a couple of check boxes) to change the values in these drop down box dynamically, without submitting the page. What you will learn: Configure dwr.xml with spring beans Configure application contexts for dwr beans [...]]]></description>
			<content:encoded><![CDATA[<p><b>Scenario:</b> A simple jsp page with a standard drop down box. A Selection criteria (could be another drop down box and a couple of check boxes) to change the values in these drop down box dynamically, without submitting the page.</p>
<p>What you will learn:</p>
<ol>
<li>Configure dwr.xml with spring beans</li>
<li>Configure application contexts for dwr beans
<li>The basic structure of the jsp for dwr</li>
<li>The necessary js that executes the ajax call along with the code for rendering the new drop down values</li>
<li>Finally, the facade (ajax handler) in Java, for the action</li>
</ol>
<p><strong>1. Configure dwr.xml with spring beans</strong></p>
<pre class="brush: xml">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;dwr&gt;
&lt;allow&gt;
	&lt;create creator=&quot;spring&quot; javascript=&quot;ListOptionsAjaxFacade&quot;&gt;
		&lt;param name=&quot;beanName&quot; value=&quot;listOptionsAjaxFacade&quot; /&gt;
	&lt;/create&gt;
	&lt;convert converter=&quot;bean&quot; match=&quot;com.reverttoconsole.domain.*&quot; /&gt;
&lt;/allow&gt;
&lt;/dwr&gt;
</pre>
<p>listOptionsAjaxFacade is the bean name you would want configure in Spring&#8217;s application context. The advantage of using beans and  creator=&#8221;spring&#8221; here is that all your dependencies can be autowired by default with your existing Spring container. So if you have two dao dependencies in listOptionsAjaxFacade, you can now use your existing application context to resolve dependencies. Simply add autowire=&#8221;byType&#8221;. The converter=&#8221;bean&#8221; above ensures that all Types are converted to your domain object types. I assume a package structure for domain which are the only return types of my ajax handlers.</p>
<p><strong>2.Configure application contexts for dwr beans</strong></p>
<pre class="brush: xml">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
	xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
	xsi:schemaLocation=&quot;
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd&quot;&gt;
	&lt;description&gt;
		This file defines DWR Ajax Facade Beans.
	&lt;/description&gt;
	&lt;bean id=&quot;listOptionsAjaxFacade&quot; class=&quot;com.reverttoconsole.domain.ListOptionsAjaxFacade&quot; autowire=&quot;byType&quot;/&gt;
&lt;/beans&gt;
</pre>
<p>I assume, you already have an application context(s) for all your controller, dao, services beans etc</p>
<p><strong>3.The basic structure of the jsp for dwr</strong></p>
<pre class="brush: xml">
&lt;html&gt;
&lt;head&gt;
&lt;!-- dwr ajax scripts--&gt;
&lt;script src=&quot;\&#039;dwr/interface/ListOptionsAjaxFacade.js\&#039;&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;\&#039;dwr/engine.js\&#039;&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;\&#039;dwr/util.js\&#039;&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
// contents of your jsp

&lt;select name=&quot;listOptions&quot; id=&quot;listOptions&quot;&gt;
    &lt;c:forEach items=&quot;${listOptions}&quot; var=&quot;item&quot;&gt;
        &lt;option value=&quot;${item.id}&quot;&gt;${item.description}&lt;/option&gt;
    &lt;/c:forEach&gt;
&lt;/select&gt;

&lt;select name=&quot;groupBy&quot; id=&quot;groupBy&quot;&gt;
       &lt;option value=&quot;name&quot;&gt;first name&lt;/option&gt;
       &lt;option value=&quot;name&quot;&gt;last name&lt;/option&gt;
       &lt;option value=&quot;name&quot;&gt;age&lt;/option&gt;
       &lt;option value=&quot;name&quot;&gt;city&lt;/option&gt;
&lt;/select&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;isAscending&quot;&gt; Ascending
&lt;input type=&quot;button&quot; value=&quot;Refresh&quot; onclick=&quot;processListOptions()&quot;/&gt;

// other parts of jsp
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>As you see, include 3 js files on the top of your jsp. The js ListOptionsAjaxFacade.js is actually the name of the handler that you<br />
will see in a moment. The button &#8216;refresh&#8217; has a js handler processListOptions.</p>
<p><strong>4.The necessary js that executes the ajax call along with the code for rendering the new drop down values</strong><br />
Add the following js in the jsp</p>
<pre class="brush: javascript">
&lt;script&gt;
     function processListOptions(){
     	// get sortBy, isAscending values from current jsp elements
	    ListOptionsAjaxFacade.processListOptions(sortBy, isAscending,function(data){
		dwr.util.removeAllOptions(&quot;listOptions&quot;);
		DWRUtil.addOptions(&quot;listOptions&quot;,data,&quot;id&quot;,&quot;description&quot;);});
     }
&lt;/script&gt;
</pre>
<p>It retrieves the values of groupBy and ascendingorder fields from the current jsp and calls the ajax method processListOptions. Once retrieved, it uses the handy dwr.util method to replace all the list options with the new values. Note the callback function(date) used for this. Dwr replaces the return call from processListOptions and puts them in data (The complete java object collection)</p>
<p><strong>5.Finally, the facade (ajax handler) in Java, for the action </strong></p>
<pre class="brush: java">
public class ListOptionsAjaxFacade {
      private PersonDao personDao;

      public List processListOptions(String sortBy, boolean isAscending) {
       	return personDao.getAll(sortBy, isAscending);
      }

      //setters &amp; getters
}
</pre>
<p>Ofcourse, this is a happy-day-flow with no exception handling either in client side or server side, but you can add that later. Read the docs for DWR.<br />
Thats it. You should now be able to retrieve the lists dynamically</p>
<p><strong>Essential Reading:</strong><br />
Read more on <a href="http://getahead.org/dwr/browser/util">dwr.util</a> package. Just like drop down lists, you can pretty<br />
much create any other dynamic form element. The basic structure remains the same as above. Try using <a href="http://script.aculo.us/">scriptaculous </a>for fancy js effects in combination with the above code. For instance, on click of Refresh, you can fade in fade out the dropdown box.<br />
(and charge the customer an additional 8 hours for implementing the same)</p>

<span class="slashdigglicious">
<a href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fstep-by-step-tutorials-dwr-ajax-with-spring-dynamic-drop-down-lists%2F&amp;title=Step+by+Step+Tutorials+%26%238211%3B+DWR+with+Spring%2C+Ajax+%26%238211%3B+Dynamic+drop+down+lists" title="Slashdot It!"><img src="http://slashdot.org/favicon.ico" height="16" width="16" alt="[Slashdot]" /></a>
<a href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fstep-by-step-tutorials-dwr-ajax-with-spring-dynamic-drop-down-lists%2F&amp;title=Step+by+Step+Tutorials+%26%238211%3B+DWR+with+Spring%2C+Ajax+%26%238211%3B+Dynamic+drop+down+lists" title="Digg This Story"><img src="http://digg.com/favicon.ico" width="16" height="16" alt="[Digg]" /></a>
<a href="http://reddit.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fstep-by-step-tutorials-dwr-ajax-with-spring-dynamic-drop-down-lists%2F&amp;title=Step+by+Step+Tutorials+%26%238211%3B+DWR+with+Spring%2C+Ajax+%26%238211%3B+Dynamic+drop+down+lists" title="Reddit"><img src="http://reddit.com/favicon.ico" width="16" height="16" alt="[Reddit]" /></a>
<a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fstep-by-step-tutorials-dwr-ajax-with-spring-dynamic-drop-down-lists%2F&amp;title=Step+by+Step+Tutorials+%26%238211%3B+DWR+with+Spring%2C+Ajax+%26%238211%3B+Dynamic+drop+down+lists" title="Save to del.icio.us" onclick="window.open('http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fstep-by-step-tutorials-dwr-ajax-with-spring-dynamic-drop-down-lists%2F&amp;title=Step+by+Step+Tutorials+%26%238211%3B+DWR+with+Spring%2C+Ajax+%26%238211%3B+Dynamic+drop+down+lists', 'delicious', 'toolbar=no,width=700,height=400'); return false;"><img src="http://images.del.icio.us/static/img/delicious.small.gif" width="16" height="16" alt="[del.icio.us]" /></a>
<a href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fstep-by-step-tutorials-dwr-ajax-with-spring-dynamic-drop-down-lists%2F" title="Share on Facebook"><img src="http://www.facebook.com/favicon.ico" width="16" height="16" alt="[Facebook]" /></a>
<a href="http://technorati.com/faves?add=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fstep-by-step-tutorials-dwr-ajax-with-spring-dynamic-drop-down-lists%2F" title="Add to my Technorati Favorites"><img src="http://technorati.com/favicon.ico" width="16" height="16" alt="[Technorati]" /></a>
<a href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fstep-by-step-tutorials-dwr-ajax-with-spring-dynamic-drop-down-lists%2F&amp;title=Step+by+Step+Tutorials+%26%238211%3B+DWR+with+Spring%2C+Ajax+%26%238211%3B+Dynamic+drop+down+lists" title="Save to Google Bookmarks"><img src="http://www.google.com/favicon.ico" width="16" height="16" alt="[Google]" /></a>
<a href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fajax%2Fstep-by-step-tutorials-dwr-ajax-with-spring-dynamic-drop-down-lists%2F&amp;title=Step+by+Step+Tutorials+%26%238211%3B+DWR+with+Spring%2C+Ajax+%26%238211%3B+Dynamic+drop+down+lists" title="Stumble it!"><img src="http://www.stumbleupon.com/favicon.ico" width="16" height="16" alt="[StumbleUpon]" /></a>
</span>]]></content:encoded>
			<wfw:commentRss>http://www.reverttoconsole.com/blog/ajax/step-by-step-tutorials-dwr-ajax-with-spring-dynamic-drop-down-lists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generic search for auto complete using Hibernate, Ajax, Scriptaculous &#8211; Part 1</title>
		<link>http://www.reverttoconsole.com/blog/ajax/generic-search-for-auto-completers-using-hibernate-ajax-part-1/</link>
		<comments>http://www.reverttoconsole.com/blog/ajax/generic-search-for-auto-completers-using-hibernate-ajax-part-1/#comments</comments>
		<pubDate>Fri, 13 Apr 2007 15:49:21 +0000</pubDate>
		<dc:creator>Priyatam</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://reverttoconsole.com/archives/92</guid>
		<description><![CDATA[Ever wondered how to write a custom search function with pattern matching, generic enough to for an auto completer using Hibernate? In this part I will explain the back end portion of writing an auto completer, for the next part you will see an Ajax front end using DWR and scriptaculous. Use Case Scenario - [...]]]></description>
			<content:encoded><![CDATA[<p>Ever wondered how to write a custom search function with pattern matching, generic enough to for an auto completer using Hibernate? In this part I will explain the back end portion of writing an auto completer, for the next part you will see an Ajax front end using DWR and scriptaculous.</p>
<p><strong>Use Case Scenario </strong>- User has a text input field. User types in &#8220;Bi&#8221; and a request is sent to the server for searching all matching strings with &#8220;Bi&#8221;.</p>
<p><strong>Technical Challenges</strong> Implementing this functionality for a single scenario should be straight forward but having a solution generic enough that any single or a combination of attributes of your domain objects could be searched using hibernate in the dao layer, has some challenges. Also remember, we are not interesting in Pojos but returning &#8220;Hibernate Objects&#8221; to the UI.</p>
<p><strong>Solution</strong> In this example, I use a domain object, <a href="http://gforge.nci.nih.gov/plugins/scmcvs/cvsweb.php/~checkout~/c3prv2/codebase/projects/core/src/edu/duke/cabig/c3pr/domain/ResearchStaff.java?rev=1.2;content-type=text%2Fplain;cvsroot=c3prv2">ResearchStaff</a>. Let&#8217;s say, the first request from the client is matchResearchStaffs(String textPattern). Your Controller (or ajax facade) would look something similar.</p>
<pre>public List<researchStaff> matchResearchStaffs(String text, HttpServletRequest request) throws Exception{
    List <researchStaff> staffCol = researchStaffDao.getBySubnames(extractSubnames(text));

    // The below code is not required for simple searches. But if your ResearchStaff Object has a lot
    // of attributes and collections. You may want to strip it off or "reduce" it before sending it to the UI
    List <researchStaff> reducedStaffCol = new ArrayList <researchStaff>(staffCol.size());
    for (ResearchStaff staff : staffCol) {
        reducedStaffCol.add(buildReduced(staff, Arrays.asList("id", "firstName","lastName")));
    }

     return reducedStaffCol;
}
</pre>
<p>As you see, the controller delegates to Dao for pattern matching and this is where Dao is smart enough to figure out &#8216;which&#8217; domain object&#8217;s &#8216;what&#8217; attributes are being queried. <a href="http://reverttoconsole.com/archives/93">In my last article</a>, you&#8217;ve seen how to couple an abstract domain object to a Base Dao and make it &#8216;aware&#8217;. Well, here you see the real benefits of that coupling.</p>
<p><span id="more-84"></span></p>
<pre>
    /**
     * A query builder for use by subclass DAOs.  It makes it easy to match a fragment of a name
     * or identifier against multiple properties.  This is intended for use in implementing
     * user-friendly dynamic searches; e.g., autocompleters.
     *
     * @param subnames the name fragments to search on
     * @param extraConditions custom HQL conditions with which to constrain the fragment matches
     * @param extraParameters parameters for the custom conditions
     * @param substringMatchProperties a list of properties of the implementing object which should
     *             be matched as case-insensitive substrings
     * @param exactMatchProperties a list of properties which should be matched as case-insensitive
     *             full strings
     * @return a list of matching domain object instances
     */
    @SuppressWarnings("unchecked")
    protected List<t> findBySubname(
        String[] subnames, String extraConditions, List<object> extraParameters,
        List<string> substringMatchProperties, List<string> exactMatchProperties
    ) {
        StringBuilder query = new StringBuilder("from ")
            .append(domainClass().getName()).append(" o where ");
        if (extraConditions != null) query.append(extraConditions).append(" and ");
        List<object> params = new LinkedList<object>();
        if (extraParameters != null) params.addAll(extraParameters);

        for (int i = 0; i < subnames.length; i++) {
            buildSubnameQuery(subnames[i], query, params,
                substringMatchProperties, exactMatchProperties);
            if (i < subnames.length - 1) query.append(" and ");
        }

        log.debug("query string = " +query);
        return getHibernateTemplate().find(query.toString(), params.toArray());
    }

    protected void buildSubnameQuery(
        String subname, StringBuilder query, List<object> params,
        List<string> substringMatchProperties, List<string> exactMatchProperties) {
        query.append('(');
        if (hasAny(substringMatchProperties)) {
            for (Iterator<string> it = substringMatchProperties.iterator(); it.hasNext();) {
                String prop = it.next();
                query.append("LOWER(o.").append(prop).append(") LIKE ?");
                params.add('%' + subname.toLowerCase() + '%');
                if (it.hasNext()) query.append(" or ");
            }
            if (hasAny(exactMatchProperties)) {
                query.append(" or ");
            }
        }
        if (hasAny(exactMatchProperties)) {
            for (Iterator<string> it = exactMatchProperties.iterator(); it.hasNext();) {
                String prop = it.next();
                query.append("LOWER(o.").append(prop).append(") = ?");
                params.add(subname.toLowerCase());
                if (it.hasNext()) query.append(" or ");
            }
        }
        query.append(')');
    }

    private boolean hasAny(List<string> properties) {
        return properties != null &#038;&#038; properties.size() > 0;
    }</pre>
<p>For more working code and complex use cases, feel free to look into our <font face="courier new" size="2"><a href="http://gforge.nci.nih.gov/plugins/scmcvs/cvsweb.php/c3prv2/codebase/projects/?cvsroot=c3prv2">gforge codebase</a></font> in the core/web sub projects</p>
<p><font face="courier new" size="2"><a href="http://gforge.nci.nih.gov/plugins/scmcvs/cvsweb.php/~checkout~/c3prv2/codebase/projects/core/src/edu/duke/cabig/c3pr/dao/AbstractBaseDao.java?rev=1.10;content-type=text%2Fplain;cvsroot=c3prv2">AbstractBaseDao</a><br />
<a href="http://gforge.nci.nih.gov/plugins/scmcvs/cvsweb.php/c3prv2/codebase/projects/web/src/edu/duke/cabig/c3pr/web/ajax/?cvsroot=c3prv2">StudyAjaxFacade</a></font><code></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.reverttoconsole.com/blog/ajax/generic-search-for-auto-completers-using-hibernate-ajax-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
