cancel
Showing results for 
Search instead for 
Did you mean: 

How to retrieve "only " the immediate child nodes of a node in hierarchy

Former Member
0 Kudos

Hi Everybody

How to retrieve "only " the immediate child nodes of a node in hierarchy using MDM API 2?

Problem:-

Using the code below i am able to retrieve only 300 immediate child nodes but with their childs.

Also looks like there is a constraint of 1000 records when we fetch using getChildren().

So, currently i m getting 1000 child nodes which include the children of the 300 first level nodes.

Requirement :

My requirement is to fetch all the immediate childs of the rootnode....and so forth.

I have tried looking into the following commands:-

RetrieveLimitedHierChildrenCommand

RetrieveLimitedHierTreeCommand

RetrieveHierAncestorsCommand

to no avail....

Please suggest or provide solutions.

Command used currently is RetrieveLimitedHierTreeCommand.

	rootNode = retrieveLimHierTreeCommand.getTree();
		if(!rootNode.isLeaf() && rootNode.getChildren() != null)
		{
			
			childNodes = rootNode.getChildren();
			
			childNodesLen = childNodes .length;
					for(int i = 0; i < childNodesLen ;i++)
			{
				System.out.println("Childs at Level 0"  + childNodes <i>.getDisplayValue());
						
			}
		}

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hello.

I used a RetrieveLimitedRecordsCommand to get records. Within this I was able to set also the page size to higher than 1000 and with a [RetrieveRelationshipsCommand|https://help.sap.com/javadocs/MDM/current/com/sap/mdm/data/commands/RetrieveRelationshipsCommand.html] I was able to get the direct childs using the methods setAnchorRecord(parent) and setMemberResultDefinition(child). Also there have several things to be set correctly to fulfill this command.

Also I used a GetRelationshipListCommand to retrieve the relationships before starting with a recursive call that I used to go down the hierarchy on each.

Maybe a bit unsorted, because I cannot remember exactly the order in which I did it.

Best regards

Dominik

Former Member
0 Kudos

Thanks for the response but could you please elaborate as to how you used the RetrieveRelationshipCommand as i am not able to figure out, how the setter methods which u have mentioned would provide me the intended results.

I think this would not hold true for a tree with level 3 where in rootnode has 500 immediate child and each immediate child has multiple(say 10 ) children.

please clarify.

Former Member
0 Kudos

OK, I found the class:

This was the method I had written: List<Record> getRelatingRecords(Record AnchorRecord, RelationshipId RelationID, ResultDefinition rdAnchor, ResultDefinition rdMembers, ConnectionAccessor mdmcon, String sessionId)

// Get the possible values out of relationship on attribute record
RetrieveRelationshipsCommand retrieveRelationshipsCommand = new RetrieveRelationshipsCommand(mdmcon); // mdmcon = a connection accessor
// Set the source record
retrieveRelationshipsCommand.setAnchorRecord(AnchorRecord); // AnchorRecord = the parent record / node
// Set the source result definition
retrieveRelationshipsCommand.setAnchorResultDefinition(rdAnchor); // rdAnchor = A ResultDefinition from a tableId. I knew which table the records should be taken from
// Set the Load Records option to load all relating records
retrieveRelationshipsCommand.setLoadRecords(true); // The relationship records are also loaded
// Set the result definition of children
retrieveRelationshipsCommand.setMemberResultDefinition(rdMembers); // rdMembers = the same result definition as above, because it is the same table again
// Set the session
retrieveRelationshipsCommand.setSession(sessionId); // The session
// Set the RelationshipId
retrieveRelationshipsCommand.setRelationshipId(RelationID); // This ID we found out by looking at all relationship IDs in Eclipse's debug mode 

With

RelationshipProperties[] relationshipProperties = getRelationshipListCommand.getRelationships();

and the GetRelationshipListCommand the RelationID can be retrieved, either using a System.out to everything in the array or using a debugger.

After executing the RetrieveRelationshipsCommand it is possible to retrieve a RelationshipGroup using .getRelationshipGroup().

Saving the members of this RelationshipGroup in an Relationship array and adding them to a List<Record>, I gave this back to my recursive method in which I called for every List-Member the recursion again. A base case for the recursion is not really needed, because there are limited numbers of child of each parent. At the beginning of each recursion step I saved the information I was interested in an array. E.g.:

- The actual number of the record, using a counter in a class variable. Mainly used for the writing position to the array

- The record itself

- The actual hierarchy level // For this one I did not used a class variable, but just passing the value!

Later on I went through that array and wrote everything to string and then to a CSV file.

One thing I should add and that is maybe helpful:

There were 3 top level records and for all of them their names were known. Also they were somewhat unchangeable. I added their corresponding records to an array and did a loop over that array, calling the recursion.

And yes, this could take a while. My program finished after 6-7 minutes with a total number of some 3000 records over all.

Yet, I do not know your desired output, but I think it is quite similar, a list of all records with their levels and maybe parents. I guess you have to find your own algorithm to get the problem solved.

Former Member
0 Kudos

Hi Domink

I am not able to figure just one thing which is relationship id and what is it?

the GetRelationshipListCommand is not getting executed though i have set all the required methods...hope the flow of my code below is okay....

Please suggest...

TableId mainTableId = repository.getTableId("table1");
	
	FieldId[] fields = new FieldId[1];
	
	fields[0] = repository.getFieldId("table1", "field");
	
	// specify the result definition (Which fields to retrieve)
	ResultDefinition rd = new ResultDefinition(mainTableId);
	rd.setSelectFields(fields);
		relationListCom = new GetRelationshipListCommand(repository.getConnection());
	relationListCom.setSession(repository.getAuthenticatedUserSession().getSession());

                relationListCom.execute(); --exception is handled
	*System.out.println("Command Complete:?? " + relationListCom.isCommandComplete());* ############### *returning false - any idea why?*

	relationshipProperties = (RelationshipProperties[])relationListCom.getRelationships();
	for(int j=0;j< relationshipProperties.length;j++)
	{
			
System.out.println("Relationship Id"  + relationshipProperties[j].getId());
			System.out.println("Relationship Id"  + relationshipProperties[j].getName().toString());

	}

	RetrieveRelationshipsCommand retrieveRelationshipsCommand = new RetrieveRelationshipsCommand(repository.getConnection()); // mdmcon = a connection accessor
	retrieveRelationshipsCommand.setAnchorRecordId(retrieveLimHierTreeCommand.getRootNode()); // AnchorRecord = the parent record / node
	retrieveRelationshipsCommand.setAnchorResultDefinition(rd); // rdAnchor = A ResultDefinition from a tableId. I knew which table the records should be taken from
	retrieveRelationshipsCommand.setLoadRecords(true); // The relationship records are also loaded
	retrieveRelationshipsCommand.setMemberResultDefinition(rd); // rdMembers = the same result definition as above, because it is the same table again
	retrieveRelationshipsCommand.setSession(repository.getAuthenticatedUserSession().getSession()); // The session
	retrieveRelationshipsCommand.setRelationshipId(relId); // This ID we found out by looking at all relationship IDs in Eclipse's debug mode

Edited by: Navneet Giria on Mar 27, 2008 11:41 AM

Former Member
0 Kudos
relationListCom.setSession(repository.getAuthenticatedUserSession().getSession());

I guess this is wrong. I used a repository session instead of a user session.

We faced this problem later on, using a repository session where a user session should have been used.

If then all relationship IDs are printed out, you can pick the one which shall be used. After this test it can be set once and used everytime.

E.g.: RelationshipId actualRelationshipID = relationshipProperties[14].getId();

Luckily we had not to detect which one it was, my project manager told it, because there were 15 different types of relationships.

This list can also be found somewhere in the Data Manager. Go to Record mode and to the according table where the hierarchy is in. Select the tab "Record details" and look at the field "Relationships" (Well, only the table has to be chosen, because the Data Manager is in Record mode and tab "Record details" as default).

Among these entries the right one has to be chosen. Or better said, that is not the complete list, somehow. There are only some entries of the list, that the command retrieves.

Former Member
0 Kudos

Thanks for all the knowledge shared by you.

I have tried with repository session but the GetRelationshipListCommand is not getting executed as the isCommandComplete() is returning false. absolutely no idea ...why?

Also if i know the relationship Id,will this

RelationshipId relID = new RelationshipId(9); (9 being the id) be enough to execute

RetrieveRelationshipsCommand ??

Cheers

Navneet

Former Member
0 Kudos

>

> Thanks for all the knowledge shared by you.

No problem.

>

> I have tried with repository session but the GetRelationshipListCommand is not getting executed as the isCommandComplete() is returning false. absolutely no idea ...why?

As alot or even every Subclass of AbstractCommandBase only inherit the isCommandComplete() and not overwrite it, the method of every Command-Class ALWAYS returns false as it is implemented in the abstract class. AbstractCommandBase. Maybe within the next Releases this will be fixed.

>

> Also if i know the relationship Id,will this

> RelationshipId relID = new RelationshipId(9); (9 being the id) be enough to execute

> RetrieveRelationshipsCommand ??

If the 9 is the "correct" Id for your needs, yes. The code itself is correct.

Best regards

Dominik