Skip to Content

There are some cases, when working with Master Data Management, where it’s necessary to export a hierarchy structure. This blog show you a solution how you can do this with the MDM JavaAPI.  

 

Mainly I would post for you the code how to do this. The code is quite simple and uses just the ‘RetrieveLimitedHierTreeCommand’ to retrieve the whole hierarchy out of a defined mdm-table and JDOM (an open source Java-based document object model for XML) to build the xml file.

 

 

Prerequisites to execute the code: 

 

I hope you enjoy the code and the especially the result, feel free to improve it.

 

 

 Code:

 

 

 

package com.sap.mdm.examples;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import com.sap.mdm.commands.CommandException;
import com.sap.mdm.data.HierNode;
import com.sap.mdm.data.ResultDefinition;
import com.sap.mdm.data.commands.RetrieveLimitedHierTreeCommand;
import com.sap.mdm.ids.FieldId;
import com.sap.mdm.net.ConnectionException;

public class GenerateHierarchy {

public Repository repository;

public static void main(String[] args) throws ConnectionException, CommandException {

String filePath = “c:/”;
String fileName = “hierarchy.xml”;

// Create new repository instance and initialize with default values
Repository repository = new Repository();
repository.initialize();

// Create the result definition for the search table
ResultDefinition rd = new ResultDefinition(repository.getTableId(“Categories”));
rd.addSelectField(repository.getFieldId(“Categories”, “Name”));

// Instantiate command
RetrieveLimitedHierTreeCommand retrieveLimitedHierTreeCommand;

try {

retrieveLimitedHierTreeCommand = new RetrieveLimitedHierTreeCommand(repository.getConnection());
retrieveLimitedHierTreeCommand.setSession(repository.getAuthenticatedUserSession().getSession());
retrieveLimitedHierTreeCommand.setResultDefinition(rd);

retrieveLimitedHierTreeCommand.execute();

} catch (CommandException e1) {
System.out.println(“Exception executing the hiererchy-command”);
e1.printStackTrace();
return;
}

// retrieve the tree
HierNode rootNode = retrieveLimitedHierTreeCommand.getTree();

// Instantiate the xml document and the root element
Document doc = new Document();
Element xmlStructure = new Element(“NODES”);

// Instantiate the generation class
GenerateHierarchy app = new GenerateHierarchy(repository);

// build the xml structure recursively
xmlStructure = app.buildTree(rootNode, xmlStructure);

doc.setRootElement(xmlStructure);

XMLOutputter outputter = new XMLOutputter();

// specify the file path
File file = new File(filePath, fileName);

try {
// creates the file
file.createNewFile();
FileWriter out = new FileWriter(file);
outputter.output(doc, out);
out.close();

} catch (IOException e) {
e.printStackTrace();
System.out.println(“Error writing file”);
}

}

public GenerateHierarchy(Repository repository) {

this.repository = repository;

}

/**
* Saves the mdm-node data in a corresponding xmlnode The methods works from
* the root-node to all children nodes
*
* @param node
* actual node in the mdm tree
* @param xmlNode
* actual node in the xml structure
* @return the xmlNode currently working on
* @throws ConnectionException
* @throws CommandException
*/
private Element buildTree(HierNode node, Element xmlNode) throws ConnectionException, CommandException {

// Do this just with the child-nodes
if (!node.isRoot()) {

// print the record ID
xmlNode.setAttribute(“ID”, node.getId().toString());

// get the field ID
FieldId fieldId = repository.getFieldId(“Categories”, “Name”);

// check that its not null
if (node.getFieldValue(fieldId) != null) {
// print the name-attribute
xmlNode.setAttribute(“Name”, node.getFieldValue(fieldId).toString());

}
}

// Process the child nodes
HierNode[] children = node.getChildren();

if (children != null) {

for (int i = 0; i<children.length; i++) {
// work on the children recursively
xmlNode.addContent(buildTree(children[i], new Element(“Node”)));

}

}

return xmlNode;

}

}

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply