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.jcr;
017
018import javax.jcr.Node;
019
020import org.ametys.plugins.repository.AmetysObject;
021import org.ametys.plugins.repository.AmetysObjectIterable;
022import org.ametys.plugins.repository.AmetysRepositoryException;
023import org.ametys.plugins.repository.UnknownAmetysObjectException;
024
025/**
026 * Default implementation of an {@link JCRAmetysObjectFactory},
027 * handling {@link DefaultTraversableAmetysObject}.<br>
028 */
029public class DefaultTraversableAmetysObjectFactory extends SimpleAmetysObjectFactory
030{
031    @Override
032    @SuppressWarnings("unchecked")
033    public DefaultTraversableAmetysObject getAmetysObject(Node node, String parentPath) throws AmetysRepositoryException
034    {
035        return new DefaultTraversableAmetysObject(node, parentPath, this);
036    }
037    
038    /**
039     * Returns the {@link AmetysObject} at the given subPath,
040     * relative to the given {@link DefaultTraversableAmetysObject}.
041     * @param <A> the actual type of {@link AmetysObject}.
042     * @param object the context {@link DefaultTraversableAmetysObject}.
043     * @param path the sub path. Cannot be <code>null</code>, empty or absolute.
044     * @return the {@link AmetysObject} at the given subPath,
045     *         relative to the given {@link DefaultTraversableAmetysObject}.
046     * @throws AmetysRepositoryException if an error occurs.
047     * @throws UnknownAmetysObjectException if no such object exists.
048     */
049    public <A extends AmetysObject> A getChild(DefaultTraversableAmetysObject object, String path) throws AmetysRepositoryException, UnknownAmetysObjectException
050    {
051        return TraversableAmetysObjectHelper.<A>getChild(object, this, path, _resolver, getLogger());
052    }
053    
054    /**
055     * Returns all children of the given {@link DefaultTraversableAmetysObject}.
056     * @param <A> the actual type of {@link AmetysObject}s
057     * @param object a {@link DefaultTraversableAmetysObject}.
058     * @return a List containing all children object in the Ametys hierarchy.
059     * @throws AmetysRepositoryException if an error occurs.
060     */
061    public <A extends AmetysObject> AmetysObjectIterable<A> getChildren(DefaultTraversableAmetysObject object) throws AmetysRepositoryException
062    {
063        return TraversableAmetysObjectHelper.getChildren(object, this, _resolver, getLogger());
064    }
065    
066    /**
067     * Tests if a given object has a child with a given name.
068     * @param object the context object.
069     * @param name the name to test.
070     * @return <code>true</code> is the given object has a child with the given name,
071     *         <code>false</code> otherwise.
072     * @throws AmetysRepositoryException if an error occurs.
073     */
074    public boolean hasChild(DefaultTraversableAmetysObject object, String name) throws AmetysRepositoryException
075    {
076        return TraversableAmetysObjectHelper.hasChild(object, name, _ametysFactoryExtensionPoint, getLogger());
077    }
078    
079    /**
080     * Creates a child to the given object.
081     * @param <A> the actual type of {@link AmetysObject}.
082     * @param object the parent {@link AmetysObject}.
083     * @param name the new object's name.
084     * @param type the new object's type.
085     * @return the newly created {@link AmetysObject}.
086     * @throws AmetysRepositoryException if an error occurs.
087     */
088    public <A extends AmetysObject> A createChild(DefaultTraversableAmetysObject object, String name, String type) throws AmetysRepositoryException
089    {
090        return TraversableAmetysObjectHelper.<A>createChild(object, this, name, type, _ametysFactoryExtensionPoint, _resolver, getLogger());
091    }
092}