X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.jcr%2Fsrc%2Forg%2Fargeo%2Fjcr%2FJcrUtils.java;h=9dfbf305a89024df372d1c08ce81307f867e20e6;hb=69f56f697e9d92da1798f2f9427457dadced4145;hp=e304649e23ac386c718895b532b0b276e52e92ca;hpb=8ece43b656257244bb01e7ad081d04c21cc41d20;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.jcr/src/org/argeo/jcr/JcrUtils.java b/org.argeo.jcr/src/org/argeo/jcr/JcrUtils.java index e304649e2..9dfbf305a 100644 --- a/org.argeo.jcr/src/org/argeo/jcr/JcrUtils.java +++ b/org.argeo.jcr/src/org/argeo/jcr/JcrUtils.java @@ -305,20 +305,46 @@ public class JcrUtils { } } +// /** +// * Routine that get the child with this name, adding it if it does not already +// * exist +// */ +// public static Node getOrAdd(Node parent, String name, String primaryNodeType) throws RepositoryException { +// return parent.hasNode(name) ? parent.getNode(name) : parent.addNode(name, primaryNodeType); +// } + /** - * Routine that get the child with this name, adding id it does not already + * Routine that get the child with this name, adding it if it does not already * exist */ - public static Node getOrAdd(Node parent, String childName, String childPrimaryNodeType) throws RepositoryException { - return parent.hasNode(childName) ? parent.getNode(childName) : parent.addNode(childName, childPrimaryNodeType); + public static Node getOrAdd(Node parent, String name, String primaryNodeType, String... mixinNodeTypes) + throws RepositoryException { + Node node; + if (parent.hasNode(name)) { + node = parent.getNode(name); + if (primaryNodeType != null && !node.isNodeType(primaryNodeType)) + throw new IllegalArgumentException("Node " + node + " exists but is of primary node type " + + node.getPrimaryNodeType().getName() + ", not " + primaryNodeType); + for (String mixin : mixinNodeTypes) { + if (!node.isNodeType(mixin)) + node.addMixin(mixin); + } + return node; + } else { + node = primaryNodeType != null ? parent.addNode(name, primaryNodeType) : parent.addNode(name); + for (String mixin : mixinNodeTypes) { + node.addMixin(mixin); + } + return node; + } } /** - * Routine that get the child with this name, adding id it does not already + * Routine that get the child with this name, adding it if it does not already * exist */ - public static Node getOrAdd(Node parent, String childName) throws RepositoryException { - return parent.hasNode(childName) ? parent.getNode(childName) : parent.addNode(childName); + public static Node getOrAdd(Node parent, String name) throws RepositoryException { + return parent.hasNode(name) ? parent.getNode(name) : parent.addNode(name); } /** Convert a {@link NodeIterator} to a list of {@link Node} */