}
@Override
@SuppressWarnings("unchecked")
public <T extends PropertyContainer> T putIfAbsent(T entity, RestIndex index, String key, Object value) {
final RestEntity restEntity = (RestEntity) entity;
restEntity.flush();
String uri = restEntity.getUri();
if (value instanceof ValueContext) {
value = ((ValueContext)value).getCorrectValue();
}
final Map<String, Object> data = map("key", key, "value", value, "uri", uri);
final RequestResult result = getRestRequest().post(uniqueIndexPath(index), data);
if (result.statusIs(Response.Status.CREATED)) {
if (index.getEntityType().equals(Node.class)) return (T)createRestNode(result);
if (index.getEntityType().equals(Relationship.class)) return (T)createRestRelationship(result,restEntity);
}
if (result.statusIs(Response.Status.OK)) {
return (T) getEntityExtractor().convertFromRepresentation(result);
}
throw new RuntimeException(String.format("Error adding element %d %s %s to index %s", restEntity.getId(), key, value, index.getIndexName()));
}