Relation samples

Basics

Example of uuid:

  • Using UUID -> "f123a950-0329-4d62-8328-0ff500fd42db";

Suggested code sample

First, you must create the web service object:

OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

Then should log in using the method "login." You can access the "login" method from the web service object "ws" as is shown below:

ws.login(user, password);

Once logged in with the web services, the session is kept in the web service Object. Then you can use the other API method.

At this point, you can use all the Relation methods from the "relation" class as shown below:

ws.relation.getRelationTypes(RelationType.BIDIRECTIONAL);

Methods

getRelationTypes

Description:

MethodReturn valuesDescription

getRelationTypes(String type)

List<RelationType>

Retrieves a list of all relations defined of a type.

Available types values:

  • RelationType.BIDIRECTIONAL
  • RelationType.PARENT_CHILD
  • RelationType.MANY_TO_MANY

More information on Relation types.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.RelationType;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            for (RelationType type : ws.relation.getRelationTypes(RelationType.BIDIRECTIONAL)) {
                System.out.println(type);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

addRelation

Description:

MethodReturn valuesDescription

addRelation(String nodeAId, String nodeBId, long relTypeId)

void

Sets a relation between two nodes.

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

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.RelationType;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            for (RelationType type : ws.getRelationTypes(RelationType.BIDIRECTIONAL)) {
                // looking for a relation named invoice
                if (type.getTitle().equals("invoice")) {
                    // Relation invoice with budget
                    ws.relation.addRelation("46762f90-82c6-4886-8d21-ad3017dd78a7", "8cd1e072-8595-4dd3-b121-41d622c43f08", type.getId());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

deleteRelation

Description:

MethodReturn valuesDescription

deleteRelation(long relationId)

void

Deletes a relationship.

Only when a node will not use the relationship it can be deleted. Otherwise, you'll get an error.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.Relation;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            for (Relation relation : ws.getRelations("46762f90-82c6-4886-8d21-ad3017dd78a7")) {
                // looking for a relation named invoice
                if (relation.getRelationTitle().equals("invoice")) {
                    ws.relation.deleteRelation(relation.getId());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getRelations

Description:

MethodReturn valuesDescription

getRelations(String uuid)

List<Relation>

Retrieves a list of all relations of a node.

The parameter uuid should be any valid document, folder, mail, or record UUID.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.Relation;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            for (Relation relation : ws.relation.getRelations("46762f90-82c6-4886-8d21-ad3017dd78a7")) {
                System.out.println(relation);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getRelationGroups

Description:

MethodReturn valuesDescription

getRelationGroups(String uuid)

List<RelationGroup>

Retrieves a list of all related groups of a node.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.RelationGroup;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            for (RelationGroup rg : ws.relation.getRelationGroups("46762f90-82c6-4886-8d21-ad3017dd78a7")) {
                System.out.println(rg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

addRelationGroup

Description:

MethodReturn valuesDescription

addRelationGroup(String uuid, String groupName, long type)

void

Adds a relation group at a node.

A relation group only has the sense to apply a relation type of RelationType.MANY_TO_MANY.

More information on Relation types.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.RelationType;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            for (RelationType type : ws.relation.getRelationTypes(RelationType.MANY_TO_MANY)) {
                if (type.getTitle().equals("staple")) {
                    ws.addRelationGroup("46762f90-82c6-4886-8d21-ad3017dd78a7", "staple group", type.getId());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

addNodeToGroup

Description:

MethodReturn valuesDescription

addNodeToGroup(String uuid, long groupId)

void

Adds a node to an existing relation group.

On a relation group, it only has the sense to apply the type RelationType.MANY_TO_MANY.

More information on Relation types.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.RelationGroup;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            for (RelationGroup rg : ws.relation.getRelationGroups("46762f90-82c6-4886-8d21-ad3017dd78a7")) {
                if (rg.getName().equals("staple group")) {
                    ws.relation.addNodeToGroup("8f101a85-88e7-4abe-8175-c5fea3e17d8b", rg.getId());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

deleteRelationGroup

Description:

MethodReturn valuesDescription

deleteRelationGroup(long groupId)

void

Remove the relation group from a node

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.RelationGroup;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            for (RelationGroup rg : ws.relation.getRelationGroups("8f101a85-88e7-4abe-8175-c5fea3e17d8b")) {
                if (rg.getName().equals("staple group")) {
                    ws.relation.deleteRelationGroup(rg.getId());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

findRelationGroup

Description:

MethodReturn valuesDescription

findRelationGroup(long groupId)

RelationGroup

Finds a relation group by id.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            long groupId = 1;
            System.out.println(ws.relation.findRelationGroup(groupId));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

setRelationGroupName

Description:

MethodReturn valuesDescription

setRelationGroupName(long groupId, String groupName)

void

Changes the relation group name.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            long groupId = 1;
            ws.relation.setRelationGroupName(groupId, "new name");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getAllRelationGroups

Description:

MethodReturn valuesDescription

getAllRelationGroups(int relationTypeId, String filter, int offset, int limit)

RelationGroupResultSet

Retrieves a list of all related groups.

The parameter "limit" and "offset" allows you to retrieve just a portion of the results of a query.

  • The parameter "limit" limits 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

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.RelationGroup;
import com.openkm.sdk4j.bean.RelationGroupResultSet;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);

            int relationTypeId = 2;
            String filter = "";
            RelationGroupResultSet resultSet = ws.relation.getAllRelationGroups(relationTypeId, filter, 0, 10);
            System.out.println("Total: " + resultSet.getTotal());
            for (RelationGroup relationGroup : resultSet.getResults()) {
                System.out.println(relationGroup);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

deleteRelationGroupItem

Description:

MethodReturn valuesDescription

deleteRelationGroupItem(String uuid, long groupId)

void

Remove a node from a related group.

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.bean.RelationGroup;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            for (RelationGroup rg : ws.relation.getRelationGroups("930baee0-8712-41b0-85c0-81d21e55742f")) {
                if (rg.getName().equals("staple")) {
                    ws.relation.deleteRelationGroupItem("930baee0-8712-41b0-85c0-81d21e55742f", rg.getId());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}