Skip to content

Add support for resource "views" #30

@talios

Description

@talios

Add support for simple resource views, which allow you to generate a sub-set of the resource in question.

public class ResourceView {

    private List<String> names;
    private List<String> matches;

    public ResourceView() {
        this.names = new ArrayList<String>();
        this.matches = new ArrayList<String>();
    }

    public ResourceView extract(String fieldName) {
        names.add(fieldName);
        return this;
    }

    public ResourceView extractMatching(String match) {
        matches.add(match);
        return this;
    }

    public ReadableResource fromResource(ReadableResource oReadableResource) {
        ResourceFactory resourceFactory = new ResourceFactory();
        Resource extractedResource = resourceFactory.newResource(oReadableResource.getResourceLink().getHref());
        for (String fieldName : names) {
            if (oReadableResource.get(fieldName).isPresent()) {
                extractedResource.withProperty(fieldName, oReadableResource.get(fieldName).get());
            }
        }
        for (String matcher : matches) {
            final Map<String,Object> properties = oReadableResource.getProperties();
            for (String name : properties.keySet()) {
                if (name.startsWith(matcher)) {
                    final String newKeyField = name.substring(matcher.length());
                    if (newKeyField == null || "".equals(newKeyField)) {
                        throw new IllegalArgumentException("Illegal match pattern of " + matcher + " generates empty key for " + name);
                    }
                    if (oReadableResource.get(name).isPresent()) {
                        extractedResource.withProperty(newKeyField, oReadableResource.get(name).get());
                    }
                }
            }
        }

        return extractedResource;
    }

}

This should probably also extract links/embedded resources as well.

Suggest adding this method on the {{ResourceFactory}} class.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions