From 49f5836bebc55cbba31081c8f099ff9097a3924a Mon Sep 17 00:00:00 2001 From: eschleb Date: Thu, 6 Jun 2024 10:57:52 +0200 Subject: [PATCH] Implement singleTree option for linkSet --- .../linkset/BasicLinkSetDefinition.java | 16 ++++++---- .../BasicLinkSetDefinitionBuilder.java | 10 +++++-- .../linkset/LinkSetDefinitionBuilder.java | 7 +++++ .../definition/custom/linkset/README.md | 24 +++++++++++++++ .../linkset/model/LinkModelFactory.java | 30 ++++++++++++------- 5 files changed, 69 insertions(+), 18 deletions(-) diff --git a/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/BasicLinkSetDefinition.java b/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/BasicLinkSetDefinition.java index e043cb8..1369c03 100644 --- a/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/BasicLinkSetDefinition.java +++ b/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/BasicLinkSetDefinition.java @@ -15,6 +15,7 @@ public class BasicLinkSetDefinition> extends ConfiguredComplexPropertyDefinition implements SwitchableForm { private final L link; + private final boolean singleTree; @Nullable private TextFieldDefinition anchorId; @Nullable @@ -22,9 +23,10 @@ public class BasicLinkSetDefinition> exte @Nullable private CheckBoxFieldDefinition openInNewWindow; - public BasicLinkSetDefinition(final L link) { + public BasicLinkSetDefinition(final L link, final boolean singleTree) { this.link = link; - setImplementationClass((Class) FormView.class); + this.singleTree = singleTree; + setImplementationClass((Class) FormView.class); setItemProvider(new CurrentItemProviderDefinition<>()); } @@ -74,10 +76,12 @@ public FieldLayoutDefinition getLayout() { @Override public void setI18n(final boolean i18n) { super.setI18n(i18n); - link.setI18n(i18n); - getAnchorId().ifPresent(definition -> - definition.setI18n(i18n) - ); + if(!singleTree) { + link.setI18n(i18n); + getAnchorId().ifPresent(definition -> + definition.setI18n(i18n) + ); + } getLinkText().ifPresent(definition -> definition.setI18n(i18n) ); diff --git a/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/BasicLinkSetDefinitionBuilder.java b/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/BasicLinkSetDefinitionBuilder.java index 2930c87..60ae636 100644 --- a/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/BasicLinkSetDefinitionBuilder.java +++ b/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/BasicLinkSetDefinitionBuilder.java @@ -24,6 +24,12 @@ public class BasicLinkSetDefinitionBuilder validators; + private boolean singleTree; + + public BasicLinkSetDefinitionBuilder singleTree(final boolean singleTree) { + this.singleTree = singleTree; + return self(); + } public BasicLinkSetDefinitionBuilder anchorId(@Nullable final TextFieldDefinition anchorId) { this.anchorId = anchorId; @@ -71,14 +77,14 @@ public BasicLinkSetDefinitionBuilder validators(final List build(final String name, final L link) { - final BasicLinkSetDefinition definition = new BasicLinkSetDefinition<>(link); - super.populate(definition, name); + final BasicLinkSetDefinition definition = new BasicLinkSetDefinition<>(link, singleTree); Optional.ofNullable(anchorId).ifPresent(definition::setAnchorId); Optional.ofNullable(linkText).ifPresent(definition::setLinkText); Optional.ofNullable(openInNewWindow).ifPresent(definition::setOpenInNewWindow); Optional.ofNullable(readOnly).ifPresent(definition::setReadOnly); Optional.ofNullable(required).ifPresent(definition::setRequired); Stream.ofNullable(validators).flatMap(Collection::stream).forEach(definition.getValidators()::add); + super.populate(definition, name); return definition; } } diff --git a/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/LinkSetDefinitionBuilder.java b/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/LinkSetDefinitionBuilder.java index 0136963..7f9ec53 100644 --- a/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/LinkSetDefinitionBuilder.java +++ b/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/LinkSetDefinitionBuilder.java @@ -18,9 +18,15 @@ public class LinkSetDefinitionBuilder extends AbstractSwitchableLinkSetDefinitio public static final UnaryOperator OPEN_IN_NEW_TAB_PROPERTY_NAME_PROVIDER = name -> name + "_in_new_window"; protected static final String LABEL_PREFIX = "merkle.customDefinitions.linkSet."; protected static final String FIELD_LABEL_PREFIX = LABEL_PREFIX + "field."; + private final boolean singleTree; public LinkSetDefinitionBuilder() { + this(false); + } + + protected LinkSetDefinitionBuilder(final boolean singleTree) { super(LABEL_PREFIX); + this.singleTree = singleTree; } @Override @@ -41,6 +47,7 @@ protected FieldOption internal(final LinkType linkType) { return new FieldOption<>( linkType, name -> new BasicLinkSetDefinitionBuilder<>() + .singleTree(singleTree) .anchorId(anchor(name)) .linkText(linkText(name)) .openInNewWindow(openInNewTab(name)) diff --git a/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/README.md b/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/README.md index 17dad4d..41899ce 100644 --- a/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/README.md +++ b/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/README.md @@ -53,6 +53,30 @@ Link to an internal DAM asset.
jcr-structure +## SingleTree +1. Extend LinkSetDefinitionBuilder +```java + import com.merkle.oss.magnolia.definition.custom.linkset.LinkSetDefinitionBuilder; + + public class CustomLinkSetDefinitionBuilder extends LinkSetDefinitionBuilder { + public CustomLinkSetDefinitionBuilder() { + super(true, false); + } + } +``` +2. Extend LinkModelFactory +```java + import com.merkle.oss.magnolia.definition.custom.linkset.LinkTypes; + import com.merkle.oss.magnolia.definition.custom.linkset.model.LinkModelFactory; + + public class CustomLinkModelFactory extends LinkModelFactory { + @Override + protected boolean isSingleTree(final LinkType linkType) { + return LinkTypes.INTERNAL.equals(linkType); + } + } +``` + ## Custom link-types 1. Define custom link types ```java diff --git a/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/model/LinkModelFactory.java b/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/model/LinkModelFactory.java index 53d83db..5e934cd 100644 --- a/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/model/LinkModelFactory.java +++ b/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/linkset/model/LinkModelFactory.java @@ -1,18 +1,20 @@ package com.merkle.oss.magnolia.definition.custom.linkset.model; +import java.util.Locale; +import java.util.Optional; +import java.util.Set; +import java.util.function.Predicate; + +import javax.inject.Inject; + +import org.apache.commons.lang.NotImplementedException; + import com.merkle.oss.magnolia.definition.custom.configuration.LocaleProvider; import com.merkle.oss.magnolia.definition.custom.linkset.LinkSetDefinitionBuilder; import com.merkle.oss.magnolia.definition.custom.linkset.LinkType; import com.merkle.oss.magnolia.definition.custom.linkset.model.util.ExtendedLinkAnchorModifier; import com.merkle.oss.magnolia.powernode.PowerNode; import com.merkle.oss.magnolia.powernode.ValueConverter; -import org.apache.commons.lang.NotImplementedException; - -import javax.inject.Inject; -import java.util.Locale; -import java.util.Optional; -import java.util.Set; -import java.util.function.Predicate; public class LinkModelFactory { private final LocaleProvider localeProvider; @@ -47,13 +49,21 @@ public Optional create(final Locale locale, final Locale dialogLocale, fin .getChild(propertyName) .flatMap(linkNode -> linkNode.getProperty(LinkSetDefinitionBuilder.LINK_TYPE_PROPERTY, dialogLocale, ValueConverter::getString).flatMap(this::resolve).flatMap(linkType -> - getLinkFactory(linkType).create(locale, dialogLocale, linkNode, linkType.getValue()).map(extendedLink -> - wrapAnchor(extendedLink, dialogLocale, linkNode, linkType) - ) + create(locale, isSingleTree(linkType) ? localeProvider.getDefaultLocale(node) : dialogLocale, linkNode, linkType) ) ); } + private Optional create(final Locale locale, final Locale dialogLocale, final PowerNode linkNode, final LinkType linkType) { + return getLinkFactory(linkType).create(locale, dialogLocale, linkNode, linkType.getValue()).map(extendedLink -> + wrapAnchor(extendedLink, dialogLocale, linkNode, linkType) + ); + } + + protected boolean isSingleTree(final LinkType linkType) { + return false; + } + private Optional resolve(final String type) { return linkTypeResolvers.stream() .map(resolver -> resolver.resolve(type))