
net.sf.javaprinciples.data.transformer.ComplexMapper Maven / Gradle / Ivy
The newest version!
package net.sf.javaprinciples.data.transformer;
import java.util.Collection;
/**
* Maps an input object to an output object. Delegates attribute mapping
* to its list of mappers.
*
* @author Kay Chevalier
*/
public class ComplexMapper extends ModelElementMapper implements Mapper
{
@Override
public void map(T input, U output)
{
input = retrieveInput(getSourceModelElement(), input);
if (input == null)
{
throw new AttributeNotFoundException("Source Model Element input class is null - no attributes to set");
}
performPreProcessingMapping(input, output);
if (input instanceof Collection>)
{
boolean attributeSet = false;
for (Object inputObject : (Collection)input)
{
try
{
mapObject((T)inputObject, output);
attributeSet = true;
}
catch (AttributeNotFoundException e)
{
}
}
if (!attributeSet)
{
throw new AttributeNotFoundException(String.format("No attributes mapped from input %s to output %s",
input.getClass().getName(), output.getClass().getName()));
}
}
else
{
mapObject(input, output);
}
performPostProcessingMapping(input, output);
}
private void mapObject(T input, U output)
{
U destinationOutput = null;
String attributeName = null;
Collection outputCollection = null;
if(getDestinationModelElement() != null)
{
attributeName = ModelElementMapperHelper.getPropertyNameFromElement(getDestinationModelElement());
destinationOutput = getAttributeFromObject(output, attributeName);
if (destinationOutput instanceof Collection>)
{
outputCollection = (Collection)destinationOutput;
destinationOutput = ModelElementMapperHelper.instantiateObjectFromModel(objectTypeMapper,
getDestinationModelElement(), store, output, attributeName);
}
if (destinationOutput == null)
destinationOutput = instantiateOutputFromAttributeName(output, attributeName);
}
boolean attributeSet = processMapperAssociations(input,
destinationOutput == null ? output : destinationOutput);
String sourceModelElementName = ModelElementMapperHelper.getPropertyNameFromElement(getSourceModelElement());
String outputClassName = destinationOutput == null ? output.getClass().getName() : destinationOutput.getClass().getName();
logAttributeMapping(input.getClass().getName(), sourceModelElementName, outputClassName, attributeName,
destinationOutput == null ? output : destinationOutput);
if (attributeSet)
{
if (destinationOutput != null)
{
if (outputCollection != null)
{
outputCollection.add(destinationOutput);
}
else
{
assignAttributeToObject(destinationOutput, output, attributeName);
}
}
}
else
{
throw new AttributeNotFoundException(String.format("No attributes mapped from input %s to output %s",
input.getClass().getName(), output.getClass().getName()));
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy