001/*
002 *  Copyright 2010 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.root;
017
018import javax.jcr.Node;
019import javax.jcr.RepositoryException;
020
021import org.apache.commons.lang.StringUtils;
022
023import org.ametys.plugins.repository.AmetysObject;
024import org.ametys.plugins.repository.AmetysObjectFactory;
025import org.ametys.plugins.repository.AmetysObjectResolver;
026import org.ametys.plugins.repository.AmetysRepositoryException;
027import org.ametys.plugins.repository.jcr.DefaultTraversableAmetysObject;
028import org.ametys.plugins.repository.jcr.DefaultTraversableAmetysObjectFactory;
029
030/**
031 * Special {@link AmetysObjectFactory} handling only the root {@link AmetysObject}.
032 */
033public final class RootAmetysObjectFactory extends DefaultTraversableAmetysObjectFactory
034{
035    @Override
036    public DefaultTraversableAmetysObject getAmetysObject(Node node, String parentPath) throws AmetysRepositoryException
037    {
038        if (StringUtils.isNotEmpty(parentPath))
039        {
040            throw new AmetysRepositoryException("The root object can't have a not empty parent path");
041        }
042        
043        try
044        {
045            if (!AmetysObjectResolver.ROOT_TYPE.equals(node.getPrimaryNodeType().getName()) || !"/ametys:root".equals(node.getPath()))
046            {
047                throw new AmetysRepositoryException("The root object must be located at /ametys:root and be of type ametys:root");
048            }
049        }
050        catch (RepositoryException e) 
051        {
052            throw new AmetysRepositoryException("An error occured testing root node", e);
053        }
054        
055        return new RootAmetysObject(node, this);
056    }
057}