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