Software AG Products 10.5 | Administering Integration Server | Configuring Ehcache on Integration Server | Making a Cache Searchable | Defining Attributes | Extracting Attributes by Class | Example Extractor
 
Example Extractor
The following sample shows how to extract an attribute that has a document as the element value in the key/value pair:
package com.softwareag.cache.sample ;

import java.util.Properties;
import com.wm.data.IData ;
import com.wm.data.IDataCursor ;
import com.wm.data.IDataUtil ;

import net.sf.ehcache.Element;
import net.sf.ehcache.search.attribute.AttributeExtractor;
import net.sf.ehcache.search.attribute.AttributeExtractorException;
/**
* This attribute extractor is designed for a Doc Type of the following format
*
* PO
* String number
* Customer
* String name
* String address
*
* In our cache search settings we defined 2 search attributes:
* 1) PONumber - Correspo nds to the top level number field
* 2) CustomerName - Corresponds to the customer\name field
*
*/
public class IDataAttributeExtractor implements AttributeExtractor {
private Properties _prop = null;

/**
* @param prop Contains the values entered in the "Properties" field for the
search attribute.
*/
public IDataAttributeExtractor(Properties prop)
{
_prop = prop;
System.out.println(prop);
}
public Object attributeFor(final Element element, final String attributeName)
throws AttributeExtractorException
{

IDataCursor poCursor = null;
IDataCursor custCursor = null;

String value = null;
try
{
poCursor = ((IData)element.getObjectValue()).getCursor();

if (attributeName.equals("PONumber"))
{
// The search attribute PONumber corresponds to the top
// level number field in our stored document.
value = IDataUtil.getString (poCursor, "number");
}
else if (attributeName.equals("CustomerName"))
{
// The search attribute CustomerName corresponds to the
// customer\name field in our stored document.
IData custIData = IDataUtil.getIData(poCursor, "customer");
if(custIData == null)
{
//We could not find the customer field in our stored document
throw new AttributeExtractorException("Unable to find the customer" +
" field in our element for the " + attributeName + " search attribute.");

}
else
{
// return the customer->name value
custCursor = custIData.getCursor();
value = IDataUtil.getString(custCursor, attributeName);
}
}
else
{
// We do not recognize the search attribute
throw new AttributeExtractorException("Unknown search attribute: " +
attributeName);
}
}
finally
{
if(poCursor != null) poCursor.destroy();
if(custCursor != null) custCursor.destroy();
}

return value;
}
}