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