@SuppressWarnings({"unchecked"})
  @Override
  public Resource convert(Object source) {
    if (null == repositoryMetadata || null == source) {
      return new Resource<Object>(source);
    }

    Serializable id = (Serializable) repositoryMetadata.entityMetadata().idAttribute().get(source);
    URI selfUri = buildUri(config.getBaseUri(), repositoryMetadata.name(), id.toString());

    Set<Link> links = new HashSet<Link>();
    for (Object attrName : entityMetadata.linkedAttributes().keySet()) {
      URI uri = buildUri(selfUri, attrName.toString());
      String rel =
          repositoryMetadata.rel() + "." + source.getClass().getSimpleName() + "." + attrName;
      links.add(new Link(uri.toString(), rel));
    }
    links.add(new Link(selfUri.toString(), "self"));

    Map<String, Object> entityDto = new HashMap<String, Object>();
    for (Map.Entry<String, AttributeMetadata> attrMeta :
        ((Map<String, AttributeMetadata>) entityMetadata.embeddedAttributes()).entrySet()) {
      String name = attrMeta.getKey();
      Object val;
      if (null != (val = attrMeta.getValue().get(source))) {
        entityDto.put(name, val);
      }
    }

    return new EntityResource(entityDto, links);
  }
 public EntityToResourceConverter(
     RepositoryRestConfiguration config, RepositoryMetadata repositoryMetadata) {
   this.config = config;
   Assert.notNull(repositoryMetadata, "RepositoryMetadata cannot be null!");
   this.repositoryMetadata = repositoryMetadata;
   this.entityMetadata = repositoryMetadata.entityMetadata();
 }