OKMRelation

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.

Example of nodeId:

  • Using UUID -> "d168924d-e801-426d-a222-00124a1461bd";

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".

In special cases you might be "promoted as Administrator" using the "administrator token".

String systemToken = DbSessionManager.getInstance().getSystemToken()

Methods

getRelationTypes

Description:

MethodReturn valuesDescription

getRelationTypes(String token, String type)

List<RelationType>

Retrieves a list of all relations defined for a type.

Available types values:

  • NodeRelationType.BIDIRECTIONAL
  • NodeRelationType.PARENT_CHILD
  • NodeRelationType.MANY_TO_MANY

More information at Relation 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();
        }
    }
}

add

Description:

MethodReturn valuesDescription

add(String token, String nodeAId, String nodeBId, long relationType)

void

Sets a relation between two nodes.

The parameters nodeAId and nodeBId should be valid document, folder, mail, or record UUIDs.

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

Description:

MethodReturn valuesDescription

delete(String token, long relationId)

void

Deletes a relation.

A relation can be deleted only when it is not used by any node; otherwise you'll get an error.

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

Description:

MethodReturn valuesDescription

getRelations(String token, String nodeId)

List<Relation>

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

Description:

MethodReturn valuesDescription

getRelationGroups(String token, String nodeId)

List<RelationGroup>

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

Description:

MethodReturn valuesDescription

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.

More information at Relation 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.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

Description:

MethodReturn valuesDescription

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.

More information at Relation types.

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

Description:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

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 parameters "limit" and "offset" allow you to retrieve just a portion of the results of a query.

  • The parameter "limit" is used to limit the number of results returned.
  • The parameter "offset" says to skip that many results before the beginning to return results.

For example, if your query has 1000 results, but you only want to return the first 10, you should use these values:

  • limit=10
  • offset=0

Now suppose you want to show the results from 11-20, you should use these values:

  • limit=10
  • offset=10

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();
        }
    }
}