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());
}
}
}>
# 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.