class MetadataEntityMapper implements EntityMapper<MetadataEntity> {

    @Override
    public XContentBuilder map(final EntityContainer<MetadataEntity> container) {
        MetadataEntity entity = container.getEntity();
        try (XContentBuilder jsonBuilder = XContentFactory.jsonBuilder()) {
            jsonBuilder.startObject();
            jsonBuilder
                    .field(DOC_TYPE_FIELD, SearchDocumentType.METADATA_ENTITY.name())
                    .field("id", entity.getId())
                    .field("externalId", entity.getExternalId())
                    .field("parentId", Optional.ofNullable(entity.getParent())
                            .map(BaseEntity::getId).orElse(null))
                    .field("name", StringUtils.defaultString(entity.getName(), entity.getExternalId()));

            buildEntityClass(entity.getClassEntity(), jsonBuilder);
            buildPermissions(container.getPermissions(), jsonBuilder);

            buildMap(prepareEntityMetadata(entity.getData()), jsonBuilder, "fields");
            jsonBuilder.endObject();
            return jsonBuilder;
        } catch (IOException e) {
            throw new IllegalArgumentException("Failed to create elasticsearch document for metadata: ", e);
        }
    }

}