OKMRelation
Basics
Section titled “Basics”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 “null” value, which indicates that the application must use the “user session”.
Methods
Section titled “Methods”getRelationTypes
Section titled “getRelationTypes”Description:
| Method | Return values | Description |
|---|---|---|
| getRelationTypes(String token, String type) | List |
Retrieves a list of all relations defined for a type. |
- Available types values:
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(); } }}delete
Section titled “delete”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(); } }}getRelations
Section titled “getRelations”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(); } }}getRelationGroups
Section titled “getRelationGroups”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(); } }}addGroup
Section titled “addGroup”Description:
| Method | Return values | Description |
|---|---|---|
| addGroup(String token, String nodeId, String groupName, long type) | void | Adds a relation group to a node. |
- For a relation group it only makes sense to apply a relation type of NodeRelationType.MANY_TO_MANY.
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(); } }}addNodeToGroup
Section titled “addNodeToGroup”Description:
| Method | Return values | Description |
|---|---|---|
| addNodeToGroup(String token, String nodeId, long groupId) | void | Adds a node to an existing relation group. |
- For a relation group it only makes sense to apply a relation type of NodeRelationType.MANY_TO_MANY.
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.deleteGroup(null, "d168924d-e801-426d-a222-00124a1461bd", rg.getId()); } } } catch (Exception e) { e.printStackTrace(); } }}deleteGroup
Section titled “deleteGroup”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(); } }}getGroup
Section titled “getGroup”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(); } }}setGroupName
Section titled “setGroupName”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(); } }}getRelationGroups
Section titled “getRelationGroups”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 and name. |
- The parameter “filter” is used to search for relation groups whose name contains the given text.
- The parameter “relationTypeId” specifies which type of relation to filter by.
Example:
package com.openkm;
import com.openkm.api.OKMRelation;import com.openkm.bean.RelationGroup;import com.openkm.util.ContextWrapper;import com.openkm.bean.RelationGroupResultSet;
public class Test {
public static void main(String[] args) { try { OKMRelation okmRelation = ContextWrapper.getContext().getBean(OKMRelation.class); // Get relation groups of type 1, filtering by name containing "invoice", with pagination long relationTypeId = 1; String filter = "invoice"; int offset = 0; int limit = 10; RelationGroupResultSet resultSet = okmRelation.getRelationGroups(null, relationTypeId, filter, offset, limit); System.out.println("Total results: " + resultSet.getTotal()); for (RelationGroup group : resultSet.getResults()) { System.out.println(group); } } catch (Exception e) { e.printStackTrace(); } }}