-
Notifications
You must be signed in to change notification settings - Fork 4
Open
Description
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
Labels
No labels