@Override
public Boolean removeRelation(String actorId, String targetId,
RelationshipKind kind) {
DirectoryService directoryService = Framework.getLocalService(DirectoryService.class);
Session relationshipDirectory = null;
try {
relationshipDirectory = directoryService.open(RELATIONSHIP_DIRECTORY_NAME);
Map<String, Serializable> filter = new HashMap<String, Serializable>();
filter.put(RELATIONSHIP_FIELD_ACTOR, actorId);
if (!StringUtils.isBlank(targetId)) {
filter.put(RELATIONSHIP_FIELD_TARGET, targetId);
}
if (!(kind == null || kind.isEmpty())) {
filter.put(RELATIONSHIP_FIELD_KIND, kind.toString());
}
DocumentModelList relations = relationshipDirectory.query(filter,
filter.keySet());
if (relations.isEmpty()) {
log.warn("Trying to delete a relationship that doesn't exists");
return false;
} else {
for (DocumentModel relation : relations) {
relationshipDirectory.deleteEntry(relation.getId());
}
return true;
}
} catch (ClientException e) {
throw new ClientRuntimeException("Unable to remove a relationship",
e);
} finally {
if (relationshipDirectory != null) {
try {
relationshipDirectory.close();
} catch (DirectoryException e) {
log.error("Error while trying to close relationships directory");
log.debug("Exception occurred", e);
}
}