Need Generalized search component on Value Objects

Hi,

i need to develop one search component on Value Objects.

i did some basic work on this, it's working fine for exact value search on VO. But my requirement is to search like values (name like '% nag %') and etc for other fields.

if we have answered for this , please let me know the link.

Thanks in Advance.

plese find the details below

My Value Object is:

public class TestVO {

private String name = null;

private String id= null;

private String city = null;

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getCity() {

return city;

}

public void setCity(String city) {

this.city = city;

}

}

My TestCollection program is :

public class TestCollections {

/**

* @param args

*/

public static void main(String[] args) {

List mLstTestVO = new ArrayList();

List mLstSearcgTestVO = new ArrayList();

TestVO tempTestVO = new TestVO();

TestVO mTestVO = new TestVO();

mTestVO.setCity("HYD");

mTestVO.setId("I001");

mTestVO.setName("Nagaraju");

mLstTestVO.add(mTestVO);

TestVO mTestVO1 = new TestVO();

mTestVO1.setCity("Sullurupet");

mTestVO1.setId("I001");

mTestVO1.setName("Kingnraju");

mLstTestVO.add(mTestVO1);

BeanPropertyValueEqualsPredicate predicate = new BeanPropertyValueEqualsPredicate( "id", "I001" );

mLstSearcgTestVO=(List)CollectionUtils.select( mLstTestVO, predicate );

for(int i=0;i<mLstSearcgTestVO.size();i++)

{

tempTestVO=(TestVO) mLstSearcgTestVO.get(i);

System.out.println("Size of the TestVO is getId :"+tempTestVO.getId());

System.out.println("Size of the TestVO is getCity :"+tempTestVO.getCity());

System.out.println("Size of the TestVO is getName :"+tempTestVO.getName());

}

}

}>

[2119 byte] By [Collectionsa] at [2007-11-27 2:36:24]
# 1

HI,

when were writing some similar functionality we had declared a bunch of predicates ">, <, =, like and etc." as abstract classes and defined them in the poin of use as inner classes. Thus we have flexible code with good readability.

So, I would recomend to use AttrNNLikePredicate to have like comparision on Attribute NN.

_Dima_a at 2007-7-12 2:55:41 > top of Java-index,Core,Core APIs...
# 2
Thanks for your immediate repply.but i am not exactly clear about your explination. if you dont mind can you please explan it bit clearly ..thank and regardsNagaraju
Collectionsa at 2007-7-12 2:55:41 > top of Java-index,Core,Core APIs...
# 3

Hi,

Sorry, we had holidays here.

The idea is not to instantiate some known predicate, but extend it to a set of abstract predicates You may need in Yours application. And instantiate that You need at the point of need defining needed behavior inline as anonymous class. See for details http://www.flipcode.com/articles/article_innerclasses.shtml

As well You can define Your own hierarchy of predicates.

See http://www.matchups.com/aduni/gamedoc/Predicate.html

Let say LikePredicate extends Predicate and You can define evaluate(MyComparable) staticaly if it is universal or leav it for future as I sayd.

It would be useful to define some MyComparable interface as well and implement it for every type You want to work with Your predicates.

Feel free to ask more ;)

_Dima_a at 2007-7-12 2:55:41 > top of Java-index,Core,Core APIs...
# 4

Hi ,

Thank you very much,

i got your point, i need to develop my own predicate class , which will serve my purpose like BeanPropertyValueEqualsPredicate and use CollectionUtils to search for required out put.

in fact i was trying in same fashion , but instead of extending the Predicate i was trying with extending the BeanPropertyValueEqualsPredicate.

Thank you very much for your valuable suggestion, i will inform you once it is done.

Collectionsa at 2007-7-12 2:55:41 > top of Java-index,Core,Core APIs...
# 5

Hi ,

Thank you very much, i completed that search, i need to formalize the search.. once again thanks for your help..

please find my search component..

This component will do search on two key fields,same can be used for n number of fields.

please find the details..

public class TestCollections {

/**

* @param args

*/

public static void main(String[] args) {

Hashtable searchTable = new Hashtable();

List mLstTestVO = new ArrayList();

List mLstSearcgTestVO = new ArrayList();

TestVO tempTestVO = new TestVO();

TestVO mTestVO = new TestVO();

mTestVO.setCity("HYD");

mTestVO.setId("I001");

mTestVO.setName("Nagaraju");

mLstTestVO.add(mTestVO);

TestVO mTestVO1 = new TestVO();

mTestVO1.setCity("Sullurupet");

mTestVO1.setId("I002");

mTestVO1.setName("Kingnraju");

mLstTestVO.add(mTestVO1);

BeanPropertyValueLikePredicate predicate = new BeanPropertyValueLikePredicate("name", "Kin");

mLstSearcgTestVO=(List)CollectionUtils.select( mLstTestVO, predicate );

for(int i=0;i<mLstSearcgTestVO.size();i++)

{

tempTestVO=(TestVO) mLstSearcgTestVO.get(i);

System.out.println("Size of the TestVO is getId :"+tempTestVO.getId());

System.out.println("Size of the TestVO is getCity :"+tempTestVO.getCity());

System.out.println("Size of the TestVO is getName :"+tempTestVO.getName());

}

predicate = new BeanPropertyValueLikePredicate("id", "I002");

mLstSearcgTestVO=(List)CollectionUtils.select( mLstSearcgTestVO, predicate );

for(int i=0;i<mLstSearcgTestVO.size();i++)

{

tempTestVO=(TestVO) mLstSearcgTestVO.get(i);

System.out.println("Size of the TestVO is getId 1:"+tempTestVO.getId());

System.out.println("Size of the TestVO is getCity 1:"+tempTestVO.getCity());

System.out.println("Size of the TestVO is getName 1:"+tempTestVO.getName());

}

}

}

package test.programs;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.PropertyUtils;

import org.apache.commons.collections.Predicate;

public class BeanPropertyValueLikePredicate implements Predicate {

//private final Log log;

private String propertyName;

private Object propertyValue;

private boolean ignoreNull;

public BeanPropertyValueLikePredicate(String propertyName, Object propertyValue) {

this(propertyName, propertyValue, false);

}

public BeanPropertyValueLikePredicate(String propertyName, Object propertyValue, boolean ignoreNull) {

if(propertyName != null && propertyName.length() >0)

{

this.propertyName = propertyName;

this.propertyValue = propertyValue;

this.ignoreNull = ignoreNull;

}else

{

throw new IllegalArgumentException(" propertyName cannot be null or empty ");

}

}

public BeanPropertyValueLikePredicate() {

// TODO Auto-generated constructor stub

}

public boolean evaluate(Object object) {

boolean evaluation = false;

try

{

evaluation = evaluateValue(propertyValue, PropertyUtils.getProperty(object, propertyName));

}

catch(IllegalArgumentException e)

{

String errorMsg = "Problem during evaluation. Null value encountered in property path...";

if(ignoreNull)

{

System.out.println("WARNING: Problem during evaluation. Null value encountered in property path..."+e);

} else

{

System.out.println("ERROR: Problem during evaluation. Null value encountered in property path..."+e);

throw e;

}

}

catch(IllegalAccessException e)

{

String errorMsg = "Unable to access the property provided.";

System.out.println("Unable to access the property provided."+e);

throw new IllegalArgumentException("Unable to access the property provided.");

}

catch(InvocationTargetException e)

{

String errorMsg = "Exception occurred in property's getter";

System.out.println("Exception occurred in property's getter"+e);

throw new IllegalArgumentException("Exception occurred in property's getter");

}

catch(NoSuchMethodException e)

{

String errorMsg = "Property not found.";

System.out.println("Property not found."+e);

throw new IllegalArgumentException("Property not found.");

}

return evaluation;

}

private boolean evaluateValue(Object expected, Object actual)

{

return expected == actual || (expected != null && expected.equals(actual))||(comparePredicate(expected,actual));

}

public String getPropertyName()

{

return propertyName;

}

public Object getPropertyValue()

{

return propertyValue;

}

public boolean isIgnoreNull()

{

return ignoreNull;

}

private boolean comparePredicate(Object expected, Object actual)

{

String strExpected = expected.toString();

String strActual= actual.toString();

return strActual.contains(strExpected);

}

}

thanks you very much for your valuable help.

Nagaraju.

Collectionsa at 2007-7-12 2:55:41 > top of Java-index,Core,Core APIs...
# 6
Hi,sorry for delay I was on vacation.I can see You catched the idea :)One thing for future, please use [code] tag for code citing.
_Dima_a at 2007-7-12 2:55:41 > top of Java-index,Core,Core APIs...