001/*
002 *  Copyright 2023 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016
017package org.ametys.workspaces.repository.jcr;
018
019import java.util.NoSuchElementException;
020
021import javax.jcr.Node;
022import javax.jcr.NodeIterator;
023import javax.jcr.RepositoryException;
024
025/**
026 * Provides helper methods on nodes.
027 */
028public final class NodeHelper
029{
030    private NodeHelper()
031    {
032        // Hides the default constructor.
033    }
034
035    /**
036     * Rename the given {@link Node} with the given new name
037     * @param node the node to rename
038     * @param newName the new name of the node
039     * @throws RepositoryException if an error occurs.
040     */
041    public static void rename(Node node, String newName) throws RepositoryException
042    {
043        Node parentNode = node.getParent();
044        boolean order = parentNode.getPrimaryNodeType().hasOrderableChildNodes();
045        Node nextSibling = null;
046        
047        if (order)
048        {
049            // iterate over the siblings to find the following
050            NodeIterator siblings = parentNode.getNodes();
051            boolean iterate = true;
052            
053            while (siblings.hasNext() && iterate)
054            {
055                Node sibling = siblings.nextNode();
056                iterate = !sibling.getName().equals(node.getName());
057            }
058            
059            // iterator is currently on the node
060            while (siblings.hasNext() && nextSibling == null)
061            {
062                Node sibling = siblings.nextNode();
063                String path = sibling.getPath();
064                if (node.getSession().itemExists(path))
065                {
066                    nextSibling = sibling;
067                }
068            }
069        }
070        
071        node.getSession().move(node.getPath(), node.getParent().getPath() + "/" + newName);
072        
073        if (order)
074        {
075            // nextSibling is either null meaning that the Node must be ordered last or is equals to the following sibling
076            if (nextSibling != null)
077            {
078                parentNode.orderBefore(newName, nextSibling.getName());
079            }
080            else
081            {
082                parentNode.orderBefore(newName, null);
083            }
084        }
085    }
086    
087    /**
088     * Get the child node at the given index
089     * @param node The parent node
090     * @param index The position of the child node
091     * @return The child node
092     * @throws NoSuchElementException If no node exist
093     * @throws RepositoryException If an error occurred
094     */
095    public static Node getChildAt(Node node, int index) throws NoSuchElementException, RepositoryException
096    {
097        if (index < 0)
098        {
099            throw new NoSuchElementException("Child node index cannot be negative (" + index + ")");
100        }
101        
102        NodeIterator nodes = node.getNodes();
103        nodes.skip(index);
104        return nodes.nextNode();
105    }
106}