@Override
public Boolean addRelation(String actorId, String targetId,
RelationshipKind kind) {
if (kind == null) {
throw new ClientRuntimeException("Type cannot be null");
}
DirectoryService directoryService = Framework.getLocalService(DirectoryService.class);
Session relationshipsDirectory = null;
try {
relationshipsDirectory = directoryService.open(RELATIONSHIP_DIRECTORY_NAME);
// try to get an existing entry
Map<String, Serializable> relationship = new HashMap<String, Serializable>();
relationship.put(RELATIONSHIP_FIELD_ACTOR, actorId);
relationship.put(RELATIONSHIP_FIELD_TARGET, targetId);
relationship.put(RELATIONSHIP_FIELD_KIND, kind.toString());
DocumentModelList relationships = relationshipsDirectory.query(relationship);
if (relationships.isEmpty()) {
relationshipsDirectory.createEntry(new HashMap<String, Object>(
relationship));
return true;
} else {
return false;
}
} catch (ClientException e) {
throw new ClientRuntimeException("Unable to create a new relation",
e);
} finally {
if (relationshipsDirectory != null) {
try {
relationshipsDirectory.close();