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.cms.repository;
017
018import javax.jcr.Node;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.plugins.repository.AmetysObject;
024import org.ametys.plugins.repository.AmetysObjectFactory;
025import org.ametys.plugins.repository.AmetysObjectIterable;
026import org.ametys.plugins.repository.AmetysObjectResolver;
027import org.ametys.plugins.repository.AmetysRepositoryException;
028import org.ametys.plugins.repository.UnknownAmetysObjectException;
029import org.ametys.plugins.repository.jcr.DefaultAmetysObjectFactory;
030import org.ametys.plugins.repository.jcr.DefaultTraversableAmetysObject;
031import org.ametys.plugins.repository.jcr.TraversableAmetysObjectHelper;
032
033/**
034 * {@link AmetysObjectFactory} handling {@link DefaultContent}.
035 */
036public class ContentFactory extends DefaultAmetysObjectFactory
037{
038    private ContentDAO _contentDAO;
039    private ModifiableContentHelper _modifiableContentHelper;
040    
041    @Override
042    public void service(ServiceManager smanager) throws ServiceException
043    {
044        super.service(smanager);
045        _contentDAO = (ContentDAO) smanager.lookup(ContentDAO.ROLE);
046        _modifiableContentHelper = (ModifiableContentHelper) smanager.lookup(ModifiableContentHelper.ROLE);
047    }
048    
049    ContentDAO _getContentDAO()
050    {
051        return _contentDAO;
052    }
053    
054    AmetysObjectResolver _getAOResolver()
055    {
056        return _resolver;
057    }
058    
059    ModifiableContentHelper _getModifiableContentHelper()
060    {
061        return _modifiableContentHelper;
062    }
063    
064    @Override
065    public DefaultContent getAmetysObject(Node node, String parentPath) throws AmetysRepositoryException
066    {
067        return new DefaultContent<>(node, parentPath, this);
068    }
069    
070    /**
071     * Returns the {@link AmetysObject} at the given subPath,
072     * relative to the given {@link DefaultTraversableAmetysObject}.
073     * @param <A> the actual type of {@link AmetysObject}.
074     * @param object the context {@link DefaultTraversableAmetysObject}.
075     * @param path the sub path. Cannot be <code>null</code>, empty or absolute.
076     * @return the {@link AmetysObject} at the given subPath,
077     *         relative to the given {@link DefaultTraversableAmetysObject}.
078     * @throws AmetysRepositoryException if an error occurs.
079     * @throws UnknownAmetysObjectException if no such object exists.
080     */
081    public <A extends AmetysObject> A getChild(DefaultContent object, String path) throws AmetysRepositoryException, UnknownAmetysObjectException
082    {
083        return TraversableAmetysObjectHelper.<A>getChild(object, this, path, _resolver, getLogger());
084    }
085    
086    /**
087     * Returns all children of the given {@link DefaultTraversableAmetysObject}.
088     * @param <A> the actual type of {@link AmetysObject}s
089     * @param object a {@link DefaultTraversableAmetysObject}.
090     * @return a List containing all children object in the Ametys hierarchy.
091     * @throws AmetysRepositoryException if an error occurs.
092     */
093    public <A extends AmetysObject> AmetysObjectIterable<A> getChildren(DefaultContent object) throws AmetysRepositoryException
094    {
095        return TraversableAmetysObjectHelper.getChildren(object, this, _resolver, getLogger());
096    }
097    
098    /**
099     * Tests if a given object has a child with a given name.
100     * @param object the context object.
101     * @param name the name to test.
102     * @return <code>true</code> is the given object has a child with the given name,
103     *         <code>false</code> otherwise.
104     * @throws AmetysRepositoryException if an error occurs.
105     */
106    public boolean hasChild(DefaultContent object, String name) throws AmetysRepositoryException
107    {
108        return TraversableAmetysObjectHelper.hasChild(object, name, _ametysFactoryExtensionPoint, getLogger());
109    }
110    
111    /**
112     * Creates a child to the given object.
113     * @param <A> the actual type of {@link AmetysObject}.
114     * @param object the parent {@link AmetysObject}.
115     * @param name the new object's name.
116     * @param type the new object's type.
117     * @return the newly created {@link AmetysObject}.
118     * @throws AmetysRepositoryException if an error occurs.
119     */
120    public <A extends AmetysObject> A createChild(DefaultContent object, String name, String type) throws AmetysRepositoryException
121    {
122        return TraversableAmetysObjectHelper.<A>createChild(object, this, name, type, _ametysFactoryExtensionPoint, _resolver, getLogger());
123    }
124}