private String calculateResourcePath(Resource resource, String desiredPath) throws EndpointNotFoundException { if (resource.isRoot()) { throw new IllegalStateException("Cannot get path for root resource"); } Resource parent = this.resourceRepository.find(resource.parentId(), Resource.class); if (parent == null) { throw new IllegalStateException( "Could not load resource '" + resource.parentId() + "' from the repository"); } String result = parent instanceof Container ? generatePathForMember(resource, (Container) parent, desiredPath) : null; if (result == null) { result = generatePathForAttachment(resource, parent); if (result == null) { throw new IllegalStateException( "Could not determine path for resource '" + resource.id() + "' with parent '" + parent.id() + "'"); } } return result; }