This is an automated email from the ASF dual-hosted git repository. bdelacretaz pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git
commit 9529f2269285ae84296ed6e3dc3538652d8904be Author: Bertrand Delacretaz <bdelacre...@apache.org> AuthorDate: Thu May 6 17:15:45 2021 +0200 Tweak annotations --- .../documentmapper/api/Annotations.java | 27 +++++++++++++++++++ .../documentmapper/impl/ContentDocumentMapper.java | 18 ++++++++----- .../documentmapper/impl/PropertiesMapper.java | 3 +++ .../annotations/AnnotationsRegistryImpl.java | 31 +++++++++------------- .../features/feature-sample-graphql-api.json | 2 +- 5 files changed, 55 insertions(+), 26 deletions(-) diff --git a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/documentmapper/api/Annotations.java b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/documentmapper/api/Annotations.java index c460ac8..a0402a0 100644 --- a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/documentmapper/api/Annotations.java +++ b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/documentmapper/api/Annotations.java @@ -19,6 +19,7 @@ package org.apache.sling.remotecontent.documentmapper.api; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -42,6 +43,21 @@ public class Annotations { this.resourceType = resourceType; } + @Override + public String toString() { + return String.format( + "RT=%s N=%b VC=%b DR=%b VCRN=%s IP=%s EP=%s DR=%s", + resourceType, + navigable, + visitContent, + documentRoot, + visitContentResourceNamePattern, + includePropertyPattern, + excludePropertyPattern, + dereferenceByPathProperties + ); + } + // TODO equals + hashcode public String getResourceType() { @@ -63,6 +79,7 @@ public class Annotations { return visitContentResourceNamePattern == null ? true : visitContentResourceNamePattern.matcher(resourceName).matches(); } + // TODO should accept a parent Annotations - other methods as well? public boolean includeProperty(String name) { // include has priority over exclude boolean result = includePropertyPattern == null ? true : includePropertyPattern.matcher(name).matches(); @@ -120,6 +137,16 @@ public class Annotations { return this; } + public Builder withDereferenceByPathProperties(String ... names) { + if(target.dereferenceByPathProperties == null) { + target.dereferenceByPathProperties = new ArrayList<String>(); + } + for(String name : names) { + target.dereferenceByPathProperties.add(name); + } + return this; + } + public Annotations build() { return target; } diff --git a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/documentmapper/impl/ContentDocumentMapper.java b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/documentmapper/impl/ContentDocumentMapper.java index 47b95be..40cbfda 100644 --- a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/documentmapper/impl/ContentDocumentMapper.java +++ b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/documentmapper/impl/ContentDocumentMapper.java @@ -45,18 +45,20 @@ public class ContentDocumentMapper implements DocumentMapper { final String resourceType = r.getResourceType(); final Annotations annot = annotationsRegistry.getAnnotations(resourceType); dest.addValue("path", r.getPath()); + log.debug("Top level Resource map {} as {}: {}", r.getPath(), r.getResourceType(), annot); mapResource(r, dest, urlb, resourceType, annot, annot.isDocumentRoot()); } private void mapResource(@NotNull Resource r, @NotNull MappingTarget.TargetNode dest, - UrlBuilder urlb, String parentResourceType, Annotations annot, boolean recurse) { + UrlBuilder urlb, String documentResourceType, Annotations documentAnnot, boolean recurse) { - log.debug("Mapping Resource {} as {}", r.getPath(), r.getResourceType()); - propertiesMapper.mapProperties(dest, r, annot); + log.debug("Mapping Resource {} as {}: {}", r.getPath(), r.getResourceType(), documentAnnot); + propertiesMapper.mapProperties(dest, r, documentAnnot); + final Annotations thisAnnot = annotationsRegistry.getAnnotations(r.getResourceType()); // Dereference by path if specified // TODO detect cycles which might lead to infinite loops - annot.dereferenceByPathPropertyNames().forEach(derefPathPropertyName -> { + thisAnnot.dereferenceByPathPropertyNames().forEach(derefPathPropertyName -> { log.debug("Dereferencing {} on {}", r.getPath(), derefPathPropertyName); final ValueMap vm = r.adaptTo(ValueMap.class); final String derefPath = vm == null ? null : vm.get(derefPathPropertyName, String.class); @@ -64,7 +66,7 @@ public class ContentDocumentMapper implements DocumentMapper { final Resource dereferenced = r.getResourceResolver().getResource(derefPath); if(dereferenced != null) { final MappingTarget.TargetNode derefNode = dest.addChild("dereferenced_by_" + derefPathPropertyName); - mapResource(dereferenced, derefNode, urlb, parentResourceType, annot, recurse); + mapResource(dereferenced, derefNode, urlb, documentResourceType, documentAnnot, recurse); } } }); @@ -73,13 +75,15 @@ public class ContentDocumentMapper implements DocumentMapper { if(recurse) { log.debug("Recursing into {}", r.getPath()); for(Resource child : r.getChildren()) { - if(!annot.visitChildResource(child.getName())) { + final boolean visit = thisAnnot.visitChildResource(child.getName()); + log.debug("child resource {} visit decision {}", child.getName(), visit); + if(!visit) { continue; } final String childResourceType = child.getResourceType(); if(annotationsRegistry.getAnnotations(childResourceType).visitContent()) { final MappingTarget.TargetNode childDest = dest.addChild(child.getName()); - mapResource(child, childDest, urlb, childResourceType, annot, true); + mapResource(child, childDest, urlb, childResourceType, documentAnnot, true); } } } else if(log.isDebugEnabled()) { diff --git a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/documentmapper/impl/PropertiesMapper.java b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/documentmapper/impl/PropertiesMapper.java index 1190e6a..825c238 100644 --- a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/documentmapper/impl/PropertiesMapper.java +++ b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/documentmapper/impl/PropertiesMapper.java @@ -20,6 +20,7 @@ package org.apache.sling.remotecontent.documentmapper.impl; import java.util.Arrays; +import java.util.Calendar; import java.util.Map; import org.apache.sling.api.resource.Resource; @@ -38,6 +39,8 @@ class PropertiesMapper { final Object value = e.getValue(); if(value instanceof Object[]) { dest.addValue(e.getKey(), Arrays.asList((Object[])value)); + } else if(value instanceof Calendar) { + dest.addValue(e.getKey(), ((Calendar)value).getTime().toString()); } else { dest.addValue(e.getKey(), String.valueOf(value)); } diff --git a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/annotations/AnnotationsRegistryImpl.java b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/annotations/AnnotationsRegistryImpl.java index 5b10de4..1750928 100644 --- a/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/annotations/AnnotationsRegistryImpl.java +++ b/remote-content-api/sample-graphql-api/src/main/java/org/apache/sling/remotecontent/samples/graphql/annotations/AnnotationsRegistryImpl.java @@ -51,7 +51,7 @@ public class AnnotationsRegistryImpl implements AnnotationsRegistry { @Activate public void activate() { add( - Annotations.forResourceType("cg:page") + Annotations.forResourceType("cq:Page") .withDocumentRoot(true) .withNavigable(true) .withVisitContent(true) @@ -59,16 +59,20 @@ public class AnnotationsRegistryImpl implements AnnotationsRegistry { .withIncludePropertyPattern("sling:ResourceType|cq:tags") .withExcludePropertyPattern("jcr:.*|cq:.*") ); - /* add( - Builder.forResourceType("cq:Page") - .withAnnotation(DOCUMENT_ROOT, TRUE) - .withAnnotation(NAVIGABLE, TRUE) - .withAnnotation(VISIT_CONTENT, TRUE) - .withAnnotation(VISIT_CONTENT_RESOURCE_NAME_PATTERN, "jcr:content") - .withAnnotation(CONTENT_INCLUDE_PROPERTY_REGEXP, "sling:ResourceType|cq:tags") - .withAnnotation(CONTENT_EXCLUDE_PROPERTY_REGEXP, "jcr:.*|cq:.*") + Annotations.forResourceType("wknd/components/page") + // TODO shall we only have "visit content"? + .withDocumentRoot(true) + .withVisitContent(true) + .withIncludePropertyPattern("sling:ResourceType|jcr:description") + .withExcludePropertyPattern("jcr:.*|cq:.*") + ); + add( + Annotations.forResourceType("wknd/components/image") + .withVisitContent(true) + .withDereferenceByPathProperties("fileReference") ); + /* add( Builder.forResourceType("sling:Folder") .withAnnotation(NAVIGABLE, TRUE) @@ -82,15 +86,6 @@ public class AnnotationsRegistryImpl implements AnnotationsRegistry { .withAnnotation(NAVIGABLE, TRUE) ); add( - Builder.forResourceType("wknd/components/page") - .withAnnotation(VISIT_CONTENT, TRUE) - ); - add( - Builder.forResourceType("wknd/components/image") - .withAnnotation(VISIT_CONTENT, TRUE) - .withAnnotation(DEREFERENCE_BY_PATH, "fileReference") - ); - add( Builder.forResourceType("wknd/components/carousel") .withAnnotation(VISIT_CONTENT, TRUE) ); diff --git a/remote-content-api/sample-graphql-api/src/main/resources/features/feature-sample-graphql-api.json b/remote-content-api/sample-graphql-api/src/main/resources/features/feature-sample-graphql-api.json index ef1033c..3a3b62c 100644 --- a/remote-content-api/sample-graphql-api/src/main/resources/features/feature-sample-graphql-api.json +++ b/remote-content-api/sample-graphql-api/src/main/resources/features/feature-sample-graphql-api.json @@ -52,7 +52,7 @@ "org.apache.sling.commons.log.level": "DEBUG", "org.apache.sling.commons.log.names": [ - "org.apache.sling.documentmapper", + "org.apache.sling.remotecontent", "org.apache.sling.graphql" ], "org.apache.sling.commons.log.pattern": "%-5level [%-50logger{50}] %message ## %mdc{sling.InternalRequest} %n"