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