Skip to content

OKMRelation

In most methods you’ll see a parameter named “nodeId”. The value of this parameter can be a valid document, folder, mail, or record UUID.

Also, in all methods you’ll see a parameter named “token”. Because Cron tasks are executed in the background without authentication, the methods used in this scenario might use the token parameter. From the default application execution context you must use the value “null”, which indicates that the application should use the “user session”.

Description:

Method Return values Description
getRelationTypes(String token, String type) List Retrieves a list of all relations defined for a type.
  • Available types:

Example:

package com.openkm;
import com.openkm.api.OKMRelation;
import com.openkm.bean.RelationType;
import com.openkm.db.bean.NodeRelationType;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMRelation okmRelation = ContextWrapper.getContext().getBean(OKMRelation.class);
for (RelationType rt : okmRelation.getRelationTypes(null, NodeRelationType.BIDIRECTIONAL)) {
System.out.println(rt);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
add(String token, String nodeAId, String nodeBId, long relationType) void Sets a relation between two nodes.

Example:

package com.openkm;
import com.openkm.api.OKMRelation;
import com.openkm.bean.RelationType;
import com.openkm.db.bean.NodeRelationType;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMRelation okmRelation = ContextWrapper.getContext().getBean(OKMRelation.class);
for (RelationType rt : okmRelation.getRelationTypes(null, NodeRelationType.BIDIRECTIONAL)) {
// looking for a relation named invoice
if (rt.getTitle().equals("invoice")) {
okmRelation.add(null, "d168924d-e801-426d-a222-00124a1461bd", "1f8ee9b8-0357-447b-95d0-c7b0adf887ec", rt.getId());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
delete(String token, long relationId) void Deletes a relation.

Example:

package com.openkm;
import com.openkm.api.OKMRelation;
import com.openkm.bean.Relation;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMRelation okmRelation = ContextWrapper.getContext().getBean(OKMRelation.class);
for (Relation relation : okmRelation.getRelations(null, "d168924d-e801-426d-a222-00124a1461bd")) {
if (relation.getRelationTitle().equals("invoice")) {
okmRelation.delete(null, relation.getId());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
getRelations(String token, String nodeId) List Retrieves a list of all relations for a node.

Example:

package com.openkm;
import com.openkm.api.OKMRelation;
import com.openkm.bean.Relation;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMRelation okmRelation = ContextWrapper.getContext().getBean(OKMRelation.class);
for (Relation relation : okmRelation.getRelations(null, "d168924d-e801-426d-a222-00124a1461bd")) {
System.out.println(relation);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
getRelationGroups(String token, String nodeId) List Retrieves a list of all relation groups for a node.

Example:

package com.openkm;
import com.openkm.api.OKMRelation;
import com.openkm.bean.RelationGroup;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMRelation okmRelation = ContextWrapper.getContext().getBean(OKMRelation.class);
for (RelationGroup rg : okmRelation.getRelationGroups(null, "d168924d-e801-426d-a222-00124a1461bd")) {
System.out.println(rg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
getRelationGroups(String token, long relationTypeId, String filter, int offset, int limit) RelationGroupResultSet Retrieves a paginated list of relation groups filtered by relation type.
  • The parameter relationTypeId is the ID of the relation type to filter by.
  • The parameter filter is an optional text filter applied to group names.
  • The parameters offset and limit control pagination.

Example:

package com.openkm;
import com.openkm.api.OKMRelation;
import com.openkm.bean.RelationGroupResultSet;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMRelation okmRelation = ContextWrapper.getContext().getBean(OKMRelation.class);
RelationGroupResultSet resultSet = okmRelation.getRelationGroups(null, 1, "", 0, 10);
System.out.println(resultSet);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
addGroup(String token, String nodeId, String groupName, long type) void Adds a relation group to a node.
  • It only makes sense to apply the relation type NodeRelationType.MANY_TO_MANY to a relation group.

Example:

package com.openkm;
import com.openkm.api.OKMRelation;
import com.openkm.bean.RelationType;
import com.openkm.db.bean.NodeRelationType;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMRelation okmRelation = ContextWrapper.getContext().getBean(OKMRelation.class);
for (RelationType rt : okmRelation.getRelationTypes(null, NodeRelationType.MANY_TO_MANY)) {
if (rt.getTitle().equals("staple")) {
okmRelation.addGroup(null, "d168924d-e801-426d-a222-00124a1461bd", "staple group", rt.getId());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
addNodeToGroup(String token, String nodeId, long groupId) void Adds a node to an existing relation group.
  • It only makes sense to apply the relation type NodeRelationType.MANY_TO_MANY to a relation group.

Example:

package com.openkm;
import com.openkm.api.OKMRelation;
import com.openkm.bean.RelationGroup;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMRelation okmRelation = ContextWrapper.getContext().getBean(OKMRelation.class);
for (RelationGroup rg : okmRelation.getRelationGroups(null, "d168924d-e801-426d-a222-00124a1461bd")) {
if (rg.getRelationTitle().equals("staple")) {
okmRelation.addNodeToGroup(null, "1f8ee9b8-0357-447b-95d0-c7b0adf887ec", rg.getId());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
deleteGroup(String token, String nodeId, long groupId) void Removes a node from a relation group.

Example:

package com.openkm;
import com.openkm.api.OKMRelation;
import com.openkm.bean.RelationGroup;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMRelation okmRelation = ContextWrapper.getContext().getBean(OKMRelation.class);
for (RelationGroup rg : okmRelation.getRelationGroups(null, "8f101a85-88e7-4abe-8175-c5fea3e17d8b")) {
if (rg.getName().equals("staple group")) {
okmRelation.deleteGroup(null, "46762f90-82c6-4886-8d21-ad3017dd78a7", rg.getId());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
getGroup(String token, long groupId) RelationGroup Finds a relation group by ID.

Example:

package com.openkm;
import com.openkm.api.OKMRelation;
import com.openkm.bean.RelationGroup;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMRelation okmRelation = ContextWrapper.getContext().getBean(OKMRelation.class);
long groupId = 1;
RelationGroup rg = okmRelation.getGroup(null, groupId);
System.out.println(rg);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
setGroupName(String token, long groupId, String groupName) void Changes the relation group name.

Example:

package com.openkm;
import com.openkm.api.OKMRelation;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMRelation okmRelation = ContextWrapper.getContext().getBean(OKMRelation.class);
long groupId = 1;
okmRelation.setGroupName(null, groupId, "new name");
} catch (Exception e) {
e.printStackTrace();
}
}
}