Skip to content

Commit

Permalink
Fix multi value property copy
Browse files Browse the repository at this point in the history
  • Loading branch information
eschleb committed May 7, 2024
1 parent 40978b8 commit 2a37ae2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import com.merkle.oss.magnolia.powernode.predicate.magnolia.IsMetaData;
Expand Down Expand Up @@ -124,12 +125,16 @@ public void copy(final Node src, final Node dest, final Predicate<Node> recursiv
});
}

private void copyProperties(final Node src, final Node dest, final Predicate<Property> propertyPredicate) {
final PropertyIterator properties = getOrThrow(src::getProperties);
private void copyProperties(final Node src, final Node dest, final Predicate<Property> propertyPredicate) throws RepositoryException {
final PropertyIterator properties = src.getProperties();
while (properties.hasNext()) {
final Property property = properties.nextProperty();
if(new IsMetaDataProperty().negate().and(propertyPredicate).test(property)) {
run(() -> dest.setProperty(property.getName(), property.getValue()));
if(property.isMultiple()) {
dest.setProperty(property.getName(), property.getValues());
}else {
dest.setProperty(property.getName(), property.getValue());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import info.magnolia.cms.i18n.I18nContentSupport;
import info.magnolia.jcr.util.NodeNameHelper;
import info.magnolia.jcr.util.NodeTypes;
import info.magnolia.jcr.util.PropertyUtil;
import info.magnolia.jcr.wrapper.I18nNodeWrapper;
import info.magnolia.test.ComponentsTestUtil;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -126,7 +127,7 @@ void move() throws RepositoryException {
void copy() throws RepositoryException {
final Node src = session.getRootNode().addNode("node1", "someNodeType");
src.setProperty("property1", 42);
src.setProperty("property2", "test");
src.setProperty("property2", new String[]{"test"});
src.setProperty(NodeTypes.Renderable.TEMPLATE, "someTemplateId");
src.setProperty("excludedProperty", "test42");
src.addNode("subNode1", "someNodeType").addNode("subNode2", "someOtherNodeType");
Expand All @@ -144,7 +145,7 @@ void copy() throws RepositoryException {
final Optional<Node> copy = nodeService.getByPath(session, "/node2/node1");
assertTrue(copy.isPresent());
assertEquals(42, copy.get().getProperty("property1").getLong());
assertEquals("test", copy.get().getProperty("property2").getString());
assertEquals(List.of("test"), PropertyUtil.getValuesStringList(copy.get().getProperty("property2").getValues()));
assertEquals("someTemplateId", copy.get().getProperty(NodeTypes.Renderable.TEMPLATE).getString());
assertFalse(copy.get().hasProperty("excludedProperty"));
assertTrue(copy.get().hasNode("subNode1"));
Expand Down

0 comments on commit 2a37ae2

Please sign in to comment.