Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for resource "views" #30

Open
talios opened this issue May 24, 2012 · 0 comments
Open

Add support for resource "views" #30

talios opened this issue May 24, 2012 · 0 comments

Comments

@talios
Copy link
Owner

talios commented May 24, 2012

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant