All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.merkle.oss.magnolia.powernode.AbstractPowerNode Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
package com.merkle.oss.magnolia.powernode;

import info.magnolia.jcr.decoration.ContentDecoratorNodeWrapper;
import info.magnolia.jcr.util.NodeTypes;

import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
import java.util.stream.Stream;

import javax.annotation.Nullable;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;

public abstract class AbstractPowerNode> extends ContentDecoratorNodeWrapper> {
	private final NodeService nodeService;

	protected AbstractPowerNode(
			final NodeService nodeService,
			final Node node,
			final AbstractPowerNodeDecorator decorator
	) {
		super(node, decorator);
		this.nodeService = nodeService;
	}

	public void run(final RepositoryConsumer consumer) {
		nodeService.run(() -> consumer.accept(getWrappedNode()));
	}

	public  T getOrThrow(final RepositoryFunction function) {
		return nodeService.getOrThrow(() -> function.apply(getWrappedNode()));
	}

	public  Optional get(final RepositoryFunction provider) {
		return nodeService.get(() -> provider.apply(getWrappedNode()));
	}

	public interface RepositoryFunction {
		T apply(Node node) throws RepositoryException;
	}

	public interface RepositoryConsumer {
		void accept(Node node) throws RepositoryException;
	}


	@Override
	protected N wrapNode(final Node node) {
		return getContentDecorator().wrapNode(node);
	}

	public Optional getTemplate() {
		return getProperty(NodeTypes.Renderable.TEMPLATE, ValueConverter::getString);
	}

	public void rename(final String newName) {
		nodeService.rename(getWrappedNode(), newName);
	}

	public void move(final Node newParent) {
		nodeService.move(getWrappedNode(), newParent);
	}

	public void copy(final Node dest) {
		nodeService.copy(getWrappedNode(), dest, ignored -> false, ignored -> true, (ignored, ignored2) -> {});
	}

	public void copyRecursive(final Node dest) {
		nodeService.copy(getWrappedNode(), dest, ignored -> true, ignored -> true, (ignored, ignored2) -> {});
	}

	public void copy(final Node dest, final Predicate recursiveChildNodePredicate) {
		nodeService.copy(getWrappedNode(), dest, node -> recursiveChildNodePredicate.test(wrapNode(node)), ignored -> true, (ignored, ignored2) -> {});
	}

	public void copy(final Node dest, final Predicate recursiveChildNodePredicate, final Predicate propertyPredicate, final BiConsumer copyConsumer) {
		nodeService.copy(getWrappedNode(), dest, node -> recursiveChildNodePredicate.test(wrapNode(node)), propertyPredicate, (src, copy) -> copyConsumer.accept(wrapNode(src), wrapNode(copy)));
	}

	public N getOrAddChild(final String relPath, final String primaryNodeTypeName) {
		return wrapNode(nodeService.getOrAddChild(getWrappedNode(), primaryNodeTypeName, relPath));
	}
	/**
	 * Creates child by localizing all relative path names.
* e.g. /node1/child_de/grandchild_de */ public N getOrAddChild(final String relPath, final String primaryNodeTypeName, final Locale locale) { return wrapNode(nodeService.getOrAddChild(getWrappedNode(), primaryNodeTypeName, relPath, locale)); } public Optional getChild(final String relativePath) { return nodeService.getChild(getWrappedNode(), relativePath).map(this::wrapNode); } /** * Gets child by localizing all relative path names.
* e.g. /node1/child_de/grandchild_de */ public Optional getChild(final String relativePath, final Locale locale) { return nodeService.getChild(getWrappedNode(), relativePath, locale).map(this::wrapNode); } public Stream streamChildren() { return nodeService.streamChildren(getWrappedNode()).map(this::wrapNode); } public Stream streamChildren(final Predicate predicate) { return nodeService.streamChildren(getWrappedNode(), node -> predicate.test(wrapNode(node))).map(this::wrapNode); } public Stream streamChildrenRecursive() { return nodeService.streamChildrenRecursive(getWrappedNode()).map(this::wrapNode); } /** * @param predicate has no impact on the traversed children (only filters) */ public Stream streamChildrenRecursive(final Predicate predicate) { return nodeService.streamChildrenRecursive(getWrappedNode(), node -> predicate.test(wrapNode(node))).map(this::wrapNode); } public Optional getParentOptional() { return nodeService.getParent(getWrappedNode()).map(this::wrapNode); } public Optional getAncestor(final Predicate predicate) { return nodeService.getAncestor(getWrappedNode(), node -> predicate.test(wrapNode(node))).map(this::wrapNode); } public Optional getAncestorOrSelf(final Predicate predicate) { return nodeService.getAncestorOrSelf(getWrappedNode(), node -> predicate.test(wrapNode(node))).map(this::wrapNode); } //Override due to link uuid conversion (see ValueConverter) @Override public Property setProperty(final String propertyName, @Nullable final String value) { return setProperty(propertyName, value, ValueConverter::toValue); } @Override public Property setProperty(final String propertyName, final String[] values) { return setMultiProperty(propertyName, List.of(values), ValueConverter::toValue); } public boolean hasProperty(final String propertyName) { return nodeService.hasProperty(getWrappedNode(), propertyName); } public boolean hasProperty(final String propertyName, final Locale locale) { return nodeService.hasProperty(getWrappedNode(), propertyName, locale); } public Property setProperty(final String propertyName, @Nullable final T value, final PropertyService.ValueFactory factory) { return nodeService.setProperty(getWrappedNode(), propertyName, value, factory); } public Property setProperty(final String propertyName, final Locale locale, @Nullable final T value, final PropertyService.ValueFactory factory) { return nodeService.setProperty(getWrappedNode(), propertyName, locale, value, factory); } public Property setMultiProperty(final String propertyName, final Iterable values, final PropertyService.ValueFactory factory) { return nodeService.setMultiProperty(getWrappedNode(), propertyName, values, factory); } public Property setMultiProperty(final String propertyName, final Locale locale, final Iterable values, final PropertyService.ValueFactory factory) { return nodeService.setMultiProperty(getWrappedNode(), propertyName, locale, values, factory); } public Optional getProperty(final String propertyName, final PropertyService.PropertyProvider provider) { return nodeService.getProperty(getWrappedNode(), propertyName, provider); } public Optional getProperty(final String propertyName, final Locale locale, final PropertyService.PropertyProvider provider) { return nodeService.getProperty(getWrappedNode(), propertyName, locale, provider); } public Stream streamMultiProperty(final String propertyName, final PropertyService.PropertyProvider provider) { return nodeService.streamMultiProperty(getWrappedNode(), propertyName, provider); } public Stream streamMultiProperty(final String propertyName, final Locale locale, final PropertyService.PropertyProvider provider) { return nodeService.streamMultiProperty(getWrappedNode(), propertyName, locale, provider); } public Optional removeProperty(final String propertyName) { return nodeService.removeProperty(getWrappedNode(), propertyName); } public Optional removeProperty(final String propertyName, final Locale locale) { return nodeService.removeProperty(getWrappedNode(), propertyName, locale); } @Override public boolean equals(final Object other) { if (other instanceof AbstractPowerNode) { final AbstractPowerNode otherPowerNode = (AbstractPowerNode) other; return getOrThrow(wrappedNode -> Objects.equals(wrappedNode.getIdentifier(), otherPowerNode.getIdentifier()) && Objects.equals(getWorkspaceName(), otherPowerNode.getWorkspaceName()) ); } return false; } @Override public int hashCode() { return getOrThrow(wrappedNode -> Objects.hash( wrappedNode.getIdentifier(), getWorkspaceName() )); } private String getWorkspaceName() throws RepositoryException{ return getWrappedNode().getSession().getWorkspace().getName(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy