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

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

package com.merkle.oss.magnolia.powernode;

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

import javax.annotation.Nullable;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;

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 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 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); } //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 Item) { return getOrThrow(wrappedNode -> wrappedNode.isSame((Item) other)); } return false; } @Override public int hashCode() { return getOrThrow(wrappedNode -> Objects.hash( wrappedNode.getIdentifier(), wrappedNode.getSession().getWorkspace().getName() )); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy