Skip to content

Instantly share code, notes, and snippets.

@aheusingfeld
Created February 9, 2012 20:52
Show Gist options
  • Save aheusingfeld/1782994 to your computer and use it in GitHub Desktop.
Save aheusingfeld/1782994 to your computer and use it in GitHub Desktop.
Class to be used as a Transformer in a Spring Integration workflow
package de.fittransporte.fitdb.util.integration;
import de.fittransporte.fitdb.util.ExpressionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.expression.EvaluationException;
import org.springframework.integration.Message;
import org.springframework.integration.transformer.Transformer;
import org.springframework.util.Assert;
import java.util.Map;
/**
* Sets properties of the <em>targetObject</em> using the specified <em>propertyMapping</em> with the incoming message.
* If targetObject is not specified the <em>message.getPayload()</em> is set as <em>targetObject</em>
*
* Configuration sample:
* <pre>
&lt;int:transformer method="transform"&gt;
&lt;bean id="mapToSalesOrderPalletsTransformer" class="de.fittransporte.fitdb.util.integration.BeanPropertySettingTransformer"&gt;
&lt;property name="propertyMapping"&gt;
&lt;util:map&gt;
&lt;entry key="salesOrder" value="%{headers['salesorder']}"/&gt;
&lt;entry key="palletCount" value="%{headers['pallet']['ANZAHL_PALETTEN']}"/&gt;
&lt;entry key="palletType" value="%{headers['pallet']['PALETTENTYP']}"/&gt;
&lt;entry key="palletTypeString" value="%{headers['pallet']['PALETTENTYP']}"/&gt;
&lt;/util:map&gt;
&lt;/property&gt;
&lt;/bean&gt;
&lt;/int:transformer&gt;
* </pre>
*/
public class BeanPropertySettingTransformer implements Transformer
{
private static final Logger LOG = LoggerFactory.getLogger(BeanPropertySettingTransformer.class);
private ConversionService conversionService;
private Map<String, Object> propertyMapping;
private Object targetObject;
@Override
public Message<?> transform(final Message<?> message)
{
Assert.notNull(propertyMapping, "'propertyMapping' must be set beforehand!");
targetObject = message.getPayload();
for (String property : propertyMapping.keySet())
{
Object propValue = propertyMapping.get(property);
final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(targetObject);
final Class targetType = wrapper.getPropertyType(property);
if (propValue instanceof String)
{
try {
propValue = ExpressionUtil.parse((String) propValue, message, conversionService, targetType);
} catch (EvaluationException evex)
{
LOG.error("Error evaluating the specified expression", evex);
propValue = null;
}
}
wrapper.setPropertyValue(property, propValue);
}
return message;
}
public void setPropertyMapping(Map<String, Object> propertyMapping)
{
this.propertyMapping = propertyMapping;
}
public Map<String, Object> getPropertyMapping()
{
return propertyMapping;
}
public Object getTargetObject()
{
return targetObject;
}
public void setTargetObject(final Object targetObject)
{
this.targetObject = targetObject;
}
public ConversionService getConversionService()
{
return conversionService;
}
@Autowired
public void setConversionService(final ConversionService conversionService)
{
this.conversionService = conversionService;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment