cancel
Showing results for 
Search instead for 
Did you mean: 

How to retrieve the data from MDM hierarchy table using MDM Java API

Former Member
0 Kudos

Hi,

I had a hierarchy table in MDM. This table had some column say x. I want to retrieve the values of this x column and need to show them in a drop down using MDM Java API.

Can anyone help me to solve this?

Regards

Vallabhaneni

Accepted Solutions (0)

Answers (2)

Answers (2)

former_member206107
Active Participant
0 Kudos

Hi,

Here is your code...

TableId Hier_TId = repository_schema.getTableId(<hierarchy table id>);

java.util.List list = new ArrayList();

ResultDefinition Supporting_result_dfn = null;

FieldProperties[] Hier_Field_props =rep_schema.getTableSchema(Hier_TId).getFields();

LookupFieldProperties lookup_field = null;

TableSchema lookupTableSchema = null;

FieldId[] lookupFieldIDs = null;

for (int i = 0, j = Hier_Field_props.length; i < j; i++) {

if (Hier_Field_props<i>.isLookup()) {

lookup_field = (LookupFieldProperties) Hier_Field_props<i>;

lookupTableSchema =repository_schema.getTableSchema(lookup_field.getLookupTableId());

lookupFieldIDs = lookupTableSchema.getFieldIds();

Supporting_result_dfn = new ResultDefinition(lookup_field.getLookupTableId());

Supporting_result_dfn.setSelectFields(lookupFieldIDs);

list.add(Supporting_result_dfn);

}

}

com.sap.mdm.search.Search hier_search =new com.sap.mdm.search.Search(Hier_TId);

ResultDefinition Hier_Resultdfn = new ResultDefinition(Hier_TId);

Hier_Resultdfn.setSelectFields(rep_schema.getTableSchema(Hier_TId).getDisplayFieldIds());

ResultDefinition[] supportingResultDefinitions =

(ResultDefinition[])list.toArray(new ResultDefinition [ list.size() ]);

RetrieveLimitedHierTreeCommand retrieve_Hier_tree_cmd =

new RetrieveLimitedHierTreeCommand(conn_acc);

retrieve_Hier_tree_cmd.setResultDefinition(Hier_Resultdfn);

retrieve_Hier_tree_cmd.setSession(Auth_User_session_cmd.getSession());

retrieve_Hier_tree_cmd.setSearch(hier_search);

retrieve_Hier_tree_cmd.setSupportingResultDefinitions(supportingResultDefinitions);

try {

retrieve_Hier_tree_cmd.execute();

} catch (CommandException e5) {

// TODO Auto-generated catch block

e5.printStackTrace();

}

HierNode Hier_Node = retrieve_Hier_tree_cmd.getTree();

print(Hier_Node,1);

//method print()

static private void print(HierNode node, int level) {

if (!node.isRoot()) {

for (int i = 0, j = level; i < j; i++) {

System.out.print("\t");

}

System.out.println(node.getDisplayValue());

}

HierNode[] children = node.getChildren();

if (children != null) {

level++;

for (int i = 0, j = children.length; i < j; i++) {

print(children<i>, level);

}

}

}

//end method print()

Best regards,

Arun prabhu S

Edited by: Arun Prabhu Sivakumar on Jul 7, 2008 12:19 PM

Former Member
0 Kudos

Hi,

Please find my code below

Collection customerUnits = new ArrayList();

try{

MdmDataConnection connection = getConnection();

TableId hierTableId = connection.getRepositorySchema().getTableId("HIERARCHY");

FieldId cFieldId = connection.getRepositorySchema().getTableSchema(hierTableId).getFieldId("UNIT");

String[] iTypes = new String[] {"G", "M"};

String[] nonTypes = new String[] {"G", "M", "NIA", "NKA"};

Search search = new Search(hierTableId);

FieldSearchDimension fieldDim = new FieldSearchDimension(cFieldId);

SearchConstraint srchConstraint = null;

if (hierType.equals("ICM")) {

for (int i=0; i<iTypes.length; i++) {

srchConstraint = new TextSearchConstraint(iTypes<i>, TextSearchConstraint.EQUALS);

search.addSearchItem(fieldDim, srchConstraint);

}

} else if (hierType.equals("Group")) {

search.setComparisonOperator(SearchGroup.AND_OPERATOR);

for (int i=0; i<nonTypes.length; i++) {

srchConstraint = new TextSearchConstraint(nonTypes<i>, TextSearchConstraint.EXCLUDES);

search.addSearchItem(fieldDim, srchConstraint);

}

SearchConstraint nullConstraint = new NullSearchConstraint(false);

Search.addSearchItem(fieldDim, nullConstraint);

}

ResultDefinition rd = new ResultDefinition(hierTableId);

Record[] hierRecords = connection.retrieveLimitedRecords(rd, search);

for (int j=0; j<hierRecords.length; j++) {

customerUnits.add(hierRecords[j].getDisplayValue());

}

} catch (Throwable t){

try {

this.closeConnection();

} catch (Throwable tt) {

reportException(tt);

}

reportException(t);

}

In the above, the number of records retreiving is zero. I had records in the MDM table.

What can be the reason? I guess in search, there might be some problem. Please help me in solving this

Former Member
0 Kudos

Hi,

For retrieving the hierarchy using MDM Java 2 API, you need to first fetch the Record ID of the innermost leaf node. Once you have this record Id then you can retrieve the Parents by using RetrieveHierAncestorsCommand. Set the child node with the recordId that you retrived and then it will etrive all the ancestor records.

Hope this helps!!

Regards,

Arafat