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 you should log in using the method "login." You can access the "login" method from the web service object "ws" as shown below:

ws.login(user, password);

Once logged in to the web services, the session is kept in the web service object. Then you can use the other API methods.

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 for a type.

Available types:

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

add

Description:

MethodReturn valuesDescription

add(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.add("46762f90-82c6-4886-8d21-ad3017dd78a7", "8cd1e072-8595-4dd3-b121-41d622c43f08", type.getId());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

delete

Description:

MethodReturn valuesDescription

delete(long relationId)

void

Deletes a relationship.

A relationship can only be deleted when no node uses it. 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.delete(relation.getId());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getRelations

Description:

MethodReturn valuesDescription

getRelations(String uuid)

List<Relation>

Retrieves a list of all relations for 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 relation groups for 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();
        }
    }
}

addGroup

Description:

MethodReturn valuesDescription

addGroup(String uuid, String groupName, long type)

void

Adds a relation group to a node.

A relation group only makes sense for the relation type 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.addGroup("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.

A relation group is only applicable to the relation 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();
        }
    }
}

deleteGroup

Description:

MethodReturn valuesDescription

deleteGroup(long groupId)

void

Removes 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.getGroups("8f101a85-88e7-4abe-8175-c5fea3e17d8b")) {
                if (rg.getName().equals("staple group")) {
                    ws.relation.deleteRelationGroup(rg.getId());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getGroup

Description:

MethodReturn valuesDescription

getGroup(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.getGroup(groupId));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

setGroupName

Description:

MethodReturn valuesDescription

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

  • The parameter "limit" limits the number of results returned.
  • The parameter "offset" specifies how many results to skip before starting 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 to 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();
        }
    }
}

deleteItemFromGroup

Description:

MethodReturn valuesDescription

deleteItemFromGroup(String uuid, long groupId)

void

Removes a node from a relation 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.deleteItemFromGroup("930baee0-8712-41b0-85c0-81d21e55742f", rg.getId());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}