Abhijat M

Abhijat M

Monday, 10 September 2012

Predicates Java Pattern to save Database Queries


The apache commons CollectionUtils has a collection of very interesting methods to allow using Ruby style collection filtering, rejecting,collecting injecting ...

I found the select method quite useful to avoid multiple DB Queries with a DB similar predicate and syntactically clean.

Problem:

I wish to append a List/Collection to my Bean based on some predicate (orgId="mcool").

Solution:


Use of Predicates:

Basic Example:

BeanPropertyValueEqualsPredicate  predicate = new BeanPropertyValueEqualsPredicate("orgId", value);


 BeanPropertyValueEqualsPredicate predicate = new BeanPropertyValueEqualsPredicate("orgId", value);  
 Collection<Organisation> organisationCool = CollectionUtils.select(allOrgansations, predicate);  
allOrganisations above was fetched with a single sql select.

bean.setOrganisationsList(organisationCool );

Caution: 

Needless to say but typically small datasets and only for cases where in memory selection is beneficial.



More about the Ruby style collection handling in this blog here
http://ytoh.wordpress.com/2008/07/17/ruby-like-collection-handling-in-verbose-java-collectionutils/


Wednesday, 27 June 2012

Loading Jquery with RichFaces 4.0 inbuilt File

There is a one line script at the head required to ensure the order of scripts and to allow you to use native Jquery

<h:outputScript name="jquery.js" />

ie6 Jquery Select Labels and IE6

Yes if you have to do it theres this sinppet that resolves an Ie6 bug wherein the selectLabels do not hold their values when clicked from their corresponding label.

More Here: http://support.microsoft.com/kb/314279

 function ie6(){  
  return jQuery.browser.msie && /msie 6\.0/i.test(navigator.userAgent);  
 }  
 function fixIe6SelectLabel(){  
  if(ie6()){  
   jQuery('select').bind({  
    focusin: function(event) {  
     try {  
     var eSrc = event.target;  
     if ( eSrc ) eSrc.tmpIndex = eSrc.selectedIndex;  
     } catch(e) {}  
    },  
    focus: function(event) {  
     try {  
    var eSrc = event.target;  
    if ( eSrc ) eSrc.selectedIndex = eSrc.tmpIndex;  
    } catch(e) {}  
    }  
   });  
  }  
 }  

Sunday, 17 June 2012

Start

Revamping my Tech Space.

Will be posting interesting tech bits here.

watch this space for ruby, rails, java and similar.