001/*
002 *  Copyright 2021 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.forms;
017
018import javax.jcr.ItemExistsException;
019import javax.jcr.Node;
020import javax.jcr.RepositoryException;
021import javax.jcr.Session;
022
023import org.ametys.plugins.forms.dao.FormDirectoryDAO;
024import org.ametys.plugins.forms.repository.Form;
025import org.ametys.plugins.forms.repository.FormDirectory;
026import org.ametys.plugins.repository.AmetysObject;
027import org.ametys.plugins.repository.AmetysRepositoryException;
028import org.ametys.plugins.repository.MovableAmetysObject;
029import org.ametys.plugins.repository.RepositoryIntegrityViolationException;
030import org.ametys.plugins.repository.jcr.NameHelper;
031import org.ametys.plugins.repository.jcr.NameHelper.NameComputationMode;
032import org.ametys.plugins.repository.jcr.SimpleAmetysObject;
033
034/**
035 * Common methods for {@link Form} and {@link FormDirectory}
036 */
037public final class FormAndDirectoryCommonMethods
038{
039    private FormAndDirectoryCommonMethods()
040    {
041    }
042    
043    /**
044     * Implementation of {@link MovableAmetysObject#canMoveTo(AmetysObject)}
045     * @param siteName Name of current site
046     * @param newParent See moveTo.
047     * @param formOrFormDirectory The object to move
048     * @param formDAO The Form DAO
049     * @return true if the move operation may succeed. If false is returned and you call moveTo anyway, you may encontered a RuntimeException (such as UnsupportedOperationException)
050     * @throws AmetysRepositoryException if an error occurs.
051     */
052    public static boolean canMoveTo(String siteName, AmetysObject newParent, SimpleAmetysObject formOrFormDirectory, FormDirectoryDAO formDAO) throws AmetysRepositoryException
053    {
054        return newParent instanceof FormDirectory
055                && !formOrFormDirectory.equals(formDAO.getFormDirectoriesRootNode(siteName));
056    }
057    
058    /**
059     * Implementation of {@link MovableAmetysObject#moveTo(AmetysObject, boolean)}
060     * @param newParent The new parent for the current object. Can not be null. Can not be a child of the current node. Must be a TraversableAmetyObject.
061     * @param renameIfExist true to rename moved page if a page with same name already exist
062     * @param formOrFormDirectory The object to move
063     * @throws AmetysRepositoryException if an error occurs.
064     * @throws RepositoryIntegrityViolationException if a page with the same name already exists.
065     */
066    public static void moveTo(AmetysObject newParent, boolean renameIfExist, SimpleAmetysObject formOrFormDirectory) throws AmetysRepositoryException, RepositoryIntegrityViolationException
067    {
068        try
069        {
070            if (formOrFormDirectory.getParent().equals(newParent))
071            {
072                // Do nothing
073            }
074            else
075            {
076                Node targetNode = ((FormDirectory) newParent).getNode(); // assume canMoveTo was called, so cast can be done
077                Node srcNode = formOrFormDirectory.getNode();
078                
079                String name = renameIfExist ? NameHelper.getUniqueAmetysObjectName((FormDirectory) newParent, srcNode.getName(), NameComputationMode.INCREMENTAL, true) : srcNode.getName();
080                String newPath = targetNode.getPath() + "/" + name;
081
082                Session session = srcNode.getSession();
083                try
084                {
085                    session.move(srcNode.getPath(), newPath);
086                }
087                catch (ItemExistsException e)
088                {
089                    throw new AmetysRepositoryException("A form already exists for new path '" + newPath + "'", e);
090                }
091                session.save();
092            }
093        }
094        catch (RepositoryException e)
095        {
096            throw new AmetysRepositoryException("Unable to move form '" + formOrFormDirectory + "' to node '" + newParent.getId() + "'", e);
097        }
098    }
099    
100    /**
101     * Implementation of {@link MovableAmetysObject#orderBefore(AmetysObject)}
102     * @param siblingNode The node that will be the next sibling node of the current node. Must have the same parent as the current node. Can be null to set the current node as the last node.
103     * @param formOrFormDirectory The object to move
104     * @throws AmetysRepositoryException if an error occurs.
105     */
106    public static void orderBefore(AmetysObject siblingNode, SimpleAmetysObject formOrFormDirectory) throws AmetysRepositoryException
107    {
108        throw new UnsupportedOperationException("Form ordering is not supported");
109    }
110}