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    
040    @Override
041    public void service(ServiceManager smanager) throws ServiceException
042    {
043        super.service(smanager);
044        _contentDAO = (ContentDAO) smanager.lookup(ContentDAO.ROLE);
045    }
046    
047    ContentDAO _getContentDAO()
048    {
049        return _contentDAO;
050    }
051    
052    AmetysObjectResolver _getAOResolver()
053    {
054        return _resolver;
055    }
056    
057    @Override
058    public DefaultContent getAmetysObject(Node node, String parentPath) throws AmetysRepositoryException
059    {
060        return new DefaultContent<>(node, parentPath, this);
061    }
062    
063    /**
064     * Returns the {@link AmetysObject} at the given subPath,
065     * relative to the given {@link DefaultTraversableAmetysObject}.
066     * @param <A> the actual type of {@link AmetysObject}.
067     * @param object the context {@link DefaultTraversableAmetysObject}.
068     * @param path the sub path. Cannot be <code>null</code>, empty or absolute.
069     * @return the {@link AmetysObject} at the given subPath,
070     *         relative to the given {@link DefaultTraversableAmetysObject}.
071     * @throws AmetysRepositoryException if an error occurs.
072     * @throws UnknownAmetysObjectException if no such object exists.
073     */
074    public <A extends AmetysObject> A getChild(DefaultContent object, String path) throws AmetysRepositoryException, UnknownAmetysObjectException
075    {
076        return TraversableAmetysObjectHelper.<A>getChild(object, this, path, _resolver, getLogger());
077    }
078    
079    /**
080     * Returns all children of the given {@link DefaultTraversableAmetysObject}.
081     * @param <A> the actual type of {@link AmetysObject}s
082     * @param object a {@link DefaultTraversableAmetysObject}.
083     * @return a List containing all children object in the Ametys hierarchy.
084     * @throws AmetysRepositoryException if an error occurs.
085     */
086    public <A extends AmetysObject> AmetysObjectIterable<A> getChildren(DefaultContent object) throws AmetysRepositoryException
087    {
088        return TraversableAmetysObjectHelper.getChildren(object, this, _resolver, getLogger());
089    }
090    
091    /**
092     * Tests if a given object has a child with a given name.
093     * @param object the context object.
094     * @param name the name to test.
095     * @return <code>true</code> is the given object has a child with the given name,
096     *         <code>false</code> otherwise.
097     * @throws AmetysRepositoryException if an error occurs.
098     */
099    public boolean hasChild(DefaultContent object, String name) throws AmetysRepositoryException
100    {
101        return TraversableAmetysObjectHelper.hasChild(object, name, _ametysFactoryExtensionPoint, getLogger());
102    }
103    
104    /**
105     * Creates a child to the given object.
106     * @param <A> the actual type of {@link AmetysObject}.
107     * @param object the parent {@link AmetysObject}.
108     * @param name the new object's name.
109     * @param type the new object's type.
110     * @return the newly created {@link AmetysObject}.
111     * @throws AmetysRepositoryException if an error occurs.
112     */
113    public <A extends AmetysObject> A createChild(DefaultContent object, String name, String type) throws AmetysRepositoryException
114    {
115        return TraversableAmetysObjectHelper.<A>createChild(object, this, name, type, _ametysFactoryExtensionPoint, _resolver, getLogger());
116    }
117}