An Altova Mapforce Hack

by Jacob 17. October 2007 11:40

Mapforce Anyone who has been forced to deal with EDI X12 documents knows that they are a royal pain. Each document has tons of fields—enough for any reasonable use for any reasonable organization. Having so many defined fields is actually its biggest liability. It means that every organization that uses EDI X12 pretty much has to decide what fields are significant for them. That means, for example, that an 850 document is functionally different for Nordstroms than it is for, say, Wal-Mart.

As a result, there’s a significant market for people who can map EDI documents. Most of them are service vendors pushing both document translation and document delivery with the result (happy for them) of tying you tightly into their service—and revenue stream. Jerks.

Mapforce to the Rescue

Anyway, Altova has a different model because their business is still tied to producing software that makes it easy to manipulate XML documents. They’ve grown to the point where they do a lot more than simple XML and one of those areas of growth is the ability to understand EDI documents (both X12 and EDIFACT formats). This alone was worth it for us to spring for their professional "Mission Kit".

The cool thing about their Mapforce product is that once you’ve defined your mapping, it has an option to "Generate code" with one of the selections being C#.

Mapforce Mapping

This is extremely cool and actually works

A Minor Inconvenience

Now, this is pure magic as far as I’m concerned, so I’m certainly not going to gripe about the code generated by Altova. Much. There are a few things that niggle, though.

  1. The resulting project is created as a "Console" app, so it generates an .exe file.
  2. The worker class’s Run method accepts both source and destination parameters so you’ll hit the file system if you use it "as is". Since want to use this as a library .dll, I’d prefer not to hit the file system for the destination.
  3. If you customize the Run method, you’ll overwrite it if you have to make changes and regenerate the code.

As I said, minor. Still, it means that I have to manually edit code every time I (re)generate a project and every developer knows that if it isn’t automated, it’s a potential source for errors.

Making it Better

I don’t know about you, but little things like that sometimes bug me enough that I’ll try to do something about it. In this case, I found a work-around that is useful enough that I thought I’d share it (thus this post). Two things make this work-around possible.

  1. Mapforce doesn’t clear directories when it generates code.
  2. Mapforce uses templates to generate code.

So a little creative editing and this becomes a lot less error-prone. Here’s what I ended up doing.

  • The templates are located in \{mapforce install dir}\spl\cs\MapForce\
  • Edit the "Mapforce2005.csproj" file so that it’s a Library instead of Exe:
	<OutputType>Library</OutputType>
  • If you want to get fancy, add a compile reference for a partial class you’ll be adding (the highlighted Compile line):
    <ItemGroup>
        <Compile Include="AssemblyInfo.cs"/>
        <Compile Include="[=$application.Name]Console.cs"/>
[foreach $Mapping in $application.Mappings
    foreach $AlgorithmGroup in $Mapping.AlgorithmGroups
]        <Compile Include="[=$application.Name][=$AlgorithmGroup.Name].cs"/>
         <Compile Include="[=$application.Name][=$AlgorithmGroup.Name]Partial.cs"/>
[    next
    foreach $AlgorithmGroup in $Mapping.AllLocalFunctions
]        <Compile Include="[=$AlgorithmGroup.LocalFunctionNamespace]_[=$AlgorithmGroup.Name].cs"/>
[    next
next
]    </ItemGroup>
  • Edit the "AlgorithmGroup.cs" file so that the main generated class is partial:
public partial class [=$classname] 
  • After code is generated, you’ll need to open the solution and add the partial class file it is expecting. Once you’ve done this once, however, you shouldn’t need to redo this step if/when you make changes.

Since I only really want to override the "Run" method, I’ll copy that from the main class, change the return type to XmlDocument and replace the save logic. Since saving is in a try...catch bit at the end, this is easy to replace.

At that point, you can reference the generated .dll file in whatever project is having to read EDI files. Having Mapforce target a project-specific DataSet schema makes it easy to use the returned document, though you have to work the XmlDocument into the DataSet.

public EDI850Data ReadNordstrom850File(string FileName)
{
    NordstromX850MapToEDI850Data nordstromEDIProcessor = new NordstromX850MapToEDI850Data();
    XmlDocument targetDoc = nordstromEDIProcessor.Run(FileName);

    StringReader reader = new StringReader(targetDoc.OwnerDocument.OuterXml);
    EDI850Data ds = new EDI850Data();
    ds.ReadXml(reader);

    return ds;
}
 
Trying This at Home

This is my first pass at it, so bear that in mind if you plan on doing something similar. And I’d welcome any refinement or suggestions you might have. That said, initial indications for this solution are pretty good so by all means use this as a basis for your own experimentation.

Tags: , , , , ,

Programming | Software

Comments


October 18. 2007 22:05
Alexander Falk
Very nice writeup! I also appreciate the feedback on your preference for library vs. console app.
Let me just add one more thing: modifying the code-gen templates to suit your particular applicatin or environment is actually something that we encourage (so it's not really meant to be a hack). There is an entire chapter in the online manual devoted to describing the template language to make it easy for you to customize it as needed:
www.altova.com/.../...aytospl(spyprogrammingla.htm


October 30. 2007 14:06
TunnelRat
Without me knowing too much about either, if you had to pick a tool that had some heavy duty EDI and ATL stuff, would you use MapForce or BizTalk?  Or are the apples and lime beans?


October 30. 2007 16:54
Jacob
To an extent, they're apples and lima beans because BizTalk is a server and MapForce is a tool.  BizTalk can handle the actual transport and mapping whereas MapForce is only able to map the file once it arrives.

That said, we did investigate both. The main drawback to BizTalk (besides the expense and overhead of a server) is that it requires SQL Server 2000 to store its mappings. I find this almost literally insane, but its true. Yeah, they'll tell you that it can use SQL Server 2005 (also true), but that's as a destination or source, not as a store for its routing/whatever.

Honestly, if we hadn't shrunk from the SQL Server 2000 requirement, I doubt we'd have gotten MapForce. I'm doing a lot of architecture/service programming that I wouldn't have to do if we had BizTalk. That said, MapForce is easy to integrate and I don't regret going with it as my EDI tool.


 steveo1883 
November 1. 2007 21:11
steveo1883
I have been comparing this to other BI tools that contain a similar function to MapForce within their $200K software, and it seems to have comparable functions/capability but really cheap. Why?
Also, for a one time migration of less than a terabyte of data from Oracle DB to Oracle DB, possibly 120 tables totaling 1500 elements, is this a useful
tool?  


November 2. 2007 12:51
Jacob
@Steveo: The main difference is the company development--Altova started as a product company for developers producing developer tools for XML files. They kind of grew into the data arena and that shows in various ways.  Frankly, I wouldn't classify Mapforce as BI really.  It sounds like it's an excellent fit for your needs, though.  I'll warn you, though, that once you have Mapforce, it's easy to find uses for it in other areas...


 Jeff 
November 8. 2007 16:09
Jeff
I'm finding MapForce to be a very robust and effective tool. I have a question for anyone willing to answer Smile

Will I have problems with the console app if I am using VS2005? I gave this a shot, and the solution upgraded with a few warnings and no errors. I don't get an EXE though, just 3 DLL's. The Altova, Altova.DB, and my specific DLL. I am using MapForce 2008.

Nice article by the way, thank you.


 Jeff 
November 8. 2007 18:50
Jeff
I wanted to follow-up my post with my solution.

Step 1: Read the directions Smile
Step 2: Compile the Solution in the "Mapping" folder created by MapForce. Presto! Console app created!


 Vaibhav 
December 4. 2007 22:14
Vaibhav
Hi,

I have generated an XSLT using the Mapping tool.

But the XSLT doesn't seem to be robust(Will hit the performance).It would not be an optimal once i start using it for transformation(XML to XML) from the code. Is there any other tool that you can suggest.

Comments are closed

Information

    Recent Posts

    Calendar

    <<  September 2010  >>
    MoTuWeThFrSaSu
    303112345
    6789101112
    13141516171819
    20212223242526
    27282930123
    45678910

    View posts in large calendar
    Disclaimer
    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2010 Scruffy-looking Cat Herder