Combining Mule with Altova MapForce

3 Jun

As promised in our previous blog about Altova Mapforce we will show in this post how to combine the result of the generated code of Altova Mapforce with a Mule application. As we have shown before, the Java code that is generated by MapForce is divided in two parts:

  • one is Altova generic, so will be reused for each mapping
  • one is mapping specific so is the actual part that changes with the mapping (this package name is configurable)

We first jarred the Altova generic code and added it to our Maven repository (Artifactory) so we can include this in our Mule project as dependency. We removed the generated mapping-specific Java code and used the included Ant build script to create the jar file. Here is a screenshot of how it looks in Artifactory:

Now if we want to use Mapforce generated code, we add the following dependency to the pom:

<dependency>
<groupId>com.altova</groupId>
<artifactId>mapforce-core</artifactId>
<version>2010R2</version>
</dependency>

The next step is to add the generated mapping-specific code to the project. We do this by copying the generated mapping specific code to our Mule project (in MapForce you can configure the package name to be used for the generated code so that makes it easier fitting in
with the rest of the code). Here is a screenshot of the code to copy in this case:

Then we created a Transformer class in the Mule project that calls the generated mapping code and returns the result.
Here is an example of a Transformer which uses the generated code:

package net.pascalalma.transformers;

import com.altova.io.StreamInput;
import com.altova.io.StringInput;
import com.altova.io.StringOutput;
import java.io.InputStream;
import nl.redstream.mapping.MappingMapTocustomer2;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractTransformer;

/**
 *
 * @author pascal
 */
public class CustomerCsvToXmlTransformer extends AbstractTransformer {

    public CustomerCsvToXmlTransformer() {
        registerSourceType(InputStream.class);
        registerSourceType(String.class);
        setReturnClass(String.class);
    }

    @Override
    protected Object doTransform(Object payload, String encoding)
            throws TransformerException {

        MappingMapTocustomer2 mappingToXml = new MappingMapTocustomer2();
        StringOutput output = new StringOutput();

        if (payload instanceof String) {
            StringInput input = new StringInput((String) payload);
            try {
                mappingToXml.run(input, output);
            } catch (Exception ex) {
                throw new TransformerException(this, ex);
            }

        } else if (payload instanceof InputStream) {
            StreamInput sInput = new StreamInput((InputStream) payload);
            try {
                mappingToXml.run(sInput, output);
            } catch (Exception ex) {
                throw new TransformerException(this, ex);
            }

        }
        return output.getString().toString();
    }
}

That’s it! Define the transformer as a ‘custom-transformer’ in your Mule config and your done. You can now create and maintain all your mappings visually with MapForce.
We are now looking into the capabilities that SPL (Spy Programming Language) offer, because it looks like we can generate the Mule Transformer straight from MapForce, that would even boost the producivity more!

Follow

Get every new post delivered to your Inbox.

Join 101 other followers

%d bloggers like this: