001/*
002 *  Copyright 2019 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 */
016package org.ametys.plugins.repository.jcr;
017
018import javax.jcr.Node;
019import javax.jcr.NodeIterator;
020import javax.jcr.RepositoryException;
021
022import org.ametys.plugins.repository.AmetysRepositoryException;
023
024/**
025 * Provides helper methods on nodes.
026 */
027public final class NodeHelper
028{
029    private NodeHelper()
030    {
031        // Hides the default constructor.
032    }
033
034    /**
035     * Rename the given {@link Node} with the given new name
036     * @param node the node to rename
037     * @param newName the new name of the node
038     * @throws AmetysRepositoryException if an error occurs.
039     */
040    public static void rename(Node node, String newName) throws AmetysRepositoryException
041    {
042        try
043        {
044            Node parentNode = node.getParent();
045            boolean order = parentNode.getPrimaryNodeType().hasOrderableChildNodes();
046            Node nextSibling = null;
047            
048            if (order)
049            {
050                // iterate over the siblings to find the following
051                NodeIterator siblings = parentNode.getNodes();
052                boolean iterate = true;
053                
054                while (siblings.hasNext() && iterate)
055                {
056                    Node sibling = siblings.nextNode();
057                    iterate = !sibling.getName().equals(node.getName());
058                }
059                
060                // iterator is currently on the node
061                while (siblings.hasNext() && nextSibling == null)
062                {
063                    Node sibling = siblings.nextNode();
064                    String path = sibling.getPath();
065                    if (node.getSession().itemExists(path))
066                    {
067                        nextSibling = sibling;
068                    }
069                }
070            }
071            
072            node.getSession().move(node.getPath(), node.getParent().getPath() + "/" + newName);
073            
074            if (order)
075            {
076                // nextSibling is either null meaning that the Node must be ordered last or is equals to the following sibling
077                if (nextSibling != null)
078                {
079                    parentNode.orderBefore(newName, nextSibling.getName());
080                }
081                else
082                {
083                    parentNode.orderBefore(newName, null);
084                }
085            }
086        }
087        catch (RepositoryException e)
088        {
089            throw new AmetysRepositoryException(e);
090        }
091    }
092}