Working programmatically with relationship models

Hi,

I am trying to understand how to work programmatically with relationship models. So far I have found this article in the documentation: GetA12 Login as well as the java doc under com.mgmtp.a12.dataservices.relationship.*

As an example lets say we have a Main_Task_DM and Sub_Task_DM.
And an relationship model that models the following:
A Main_Task can have 0 to N Sub_Tasks and a Sub_Task is always connected to one Main_Task or none.

My questions are:

  1. How can I programmatically create the relationship connection between a Main_Task-Document and Sub_Task-Document that I have created?
  2. If I have the DocRef for a Main_Task-Document, how can I access the DocRefs of all (via the relationship connected) Sub-Tasks programmatically?

Many thanks,
Christoph

Adding a relationship programtically can be done via com.mgmtp.a12.dataservices.relationship.RelationshipLinkService:

private RelationshipLink linkDocuments(DocumentReference source, DocumentReference target, String relationshipModel,
		String sourceRole, String targetRole) {
	LinkDescriptor linkDescriptor = new LinkDescriptor();
	linkDescriptor.setRelationshipModel(relationshipModel);
	RelationshipRoleSpec sourceRoleSpec = new RelationshipRoleSpec(sourceRole, source);
	RelationshipRoleSpec targetRoleSpec = new RelationshipRoleSpec(targetRole, target);
	linkDescriptor.setEntities(Arrays.asList(sourceRoleSpec, targetRoleSpec));
	return relationshipLinkService.create(linkDescriptor);
}

And loading the relationships works like this:

private static final PageRequest ONE_ELEMENT = PageRequest.ofSize(1);

private Optional<DocumentReference> loadRelatedDocRefs(DocumentReference documentRef, String relationshipModelName,
		String relationshipRoleFrom, String relationshipRoleTo) {
	RelationshipLinkSpecification specification = new RelationshipLinkSpecification(relationshipModelName);
	specification.setSourceFilter(new RelationshipRoleSpecification(relationshipRoleFrom, documentRef));
	return relationshipLinkService.load(specification, ONE_ELEMENT)
			.get()
			.findFirst()
			.flatMap(link -> Optional.ofNullable(link.getRoles().get(relationshipRoleTo))
					.map(RelationshipRole::getDocRef));
}

Except that you need to replace ONE_ELEMENT with a pageable of your needs and adopt the processing of the load invocation appropriately.