OKMRelation
Basics
On most methods you'll see 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 on all methods you'll see parameter named "token". Because the Cron Task are executed in background without authentication, the methods used in this scenario might use the token parameter. From default application execution context you must use "null" value what indicates to the application must use the "user session".
On special cases you might be "promoted as Administrator" using the "administrator token".
String systemToken = DbSessionManager.getInstance().getSystemToken()
Methods
getRelationTypes
Description:
Method | Return values | Description |
---|---|---|
getRelationTypes(String token, String type) |
List<RelationType> |
Retrieves a list of all relations defined of a type. |
Available types values:
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:
Method | Return values | Description |
---|---|---|
add(String token, String nodeAId, String nodeBId, long relationType) |
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.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:
Method | Return values | Description |
---|---|---|
delete(String token, long relationId) |
void |
Deletes a relation. |
Only when the relation will not be used by any node is able to be deleted, 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:
Method | Return values | Description |
---|---|---|
getRelations(String token, String nodeId) |
List<Relation> |
Retrieves a list of all relations of 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:
Method | Return values | Description |
---|---|---|
getRelationGroups(String token, String nodeId) |
List<RelationGroup> |
Retrieves a list of all relation groups of 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:
Method | Return values | Description |
---|---|---|
addGroup(String token, String nodeId, String groupName, long type) |
void |
Adds a relation group at a node. |
On a relation group only has 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:
Method | Return values | Description |
---|---|---|
addNodeToGroup(String token, String nodeId, long groupId) |
void |
Adds a node to an existing relation group. |
On a relation group only has 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:
Method | Return values | Description |
---|---|---|
deleteGroup(String token, String nodeId, long groupId) |
void |
Remove 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:
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
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();
}
}
}