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.content.ContentHelper;
024import org.ametys.cms.content.ContentSaxer;
025import org.ametys.cms.data.ContentDataHelper;
026import org.ametys.cms.model.ModelLessBasicTypesExtensionPoint;
027import org.ametys.plugins.repository.AmetysObject;
028import org.ametys.plugins.repository.AmetysObjectFactory;
029import org.ametys.plugins.repository.AmetysObjectIterable;
030import org.ametys.plugins.repository.AmetysObjectResolver;
031import org.ametys.plugins.repository.AmetysRepositoryException;
032import org.ametys.plugins.repository.UnknownAmetysObjectException;
033import org.ametys.plugins.repository.jcr.DefaultAmetysObjectFactory;
034import org.ametys.plugins.repository.jcr.DefaultTraversableAmetysObject;
035import org.ametys.plugins.repository.jcr.TraversableAmetysObjectHelper;
036
037/**
038 * {@link AmetysObjectFactory} handling {@link DefaultContent}.
039 */
040public class ContentFactory extends DefaultAmetysObjectFactory
041{
042    private ServiceManager _serviceManager;
043    private ContentDAO _contentDAO;
044    private ContentSaxer _contentSaxer;
045    private ContentHelper _contentHelper;
046    private ModifiableContentHelper _modifiableContentHelper;
047    private ContentDataHelper _contentDataHelper;
048    private ModelLessBasicTypesExtensionPoint _modelLessBasicTypesExtensionPoint;
049    
050    @Override
051    public void service(ServiceManager smanager) throws ServiceException
052    {
053        super.service(smanager);
054        _serviceManager = smanager;
055        _contentHelper = (ContentHelper) smanager.lookup(ContentHelper.ROLE);
056        _modifiableContentHelper = (ModifiableContentHelper) smanager.lookup(ModifiableContentHelper.ROLE);
057        _contentSaxer = (ContentSaxer) smanager.lookup(ContentSaxer.CMS_CONTENT_SAXER_ROLE);
058        _contentDataHelper = (ContentDataHelper) smanager.lookup(ContentDataHelper.ROLE);
059        _modelLessBasicTypesExtensionPoint = (ModelLessBasicTypesExtensionPoint) smanager.lookup(ModelLessBasicTypesExtensionPoint.ROLE);
060    }
061    
062    synchronized ContentDAO _getContentDAO()
063    {
064        // lazy initialization to prevent chicken/egg scenario on startup
065        if (_contentDAO == null)
066        {
067            try
068            {
069                _contentDAO = (ContentDAO) _serviceManager.lookup(ContentDAO.ROLE);
070            }
071            catch (ServiceException e)
072            {
073                throw new AmetysRepositoryException("Caught an exception while retrieving ContentDAO", e);
074            }
075        }
076        return _contentDAO;
077    }
078    
079    AmetysObjectResolver _getAOResolver()
080    {
081        return _resolver;
082    }
083    
084    ModifiableContentHelper _getModifiableContentHelper()
085    {
086        return _modifiableContentHelper;
087    }
088    
089    /**
090     * Retrieves the content saxer
091     * @return the content saxer
092     */
093    public ContentSaxer getContentSaxer()
094    {
095        return _contentSaxer;
096    }
097    
098    /**
099     * Retrieves the content helper
100     * @return the content helper
101     */
102    public ContentHelper getContentHelper()
103    {
104        return _contentHelper;
105    }
106    
107    /**
108     * Retrieves the content data helper
109     * @return the content data helper
110     */
111    public ContentDataHelper getContentDataHelper()
112    {
113        return _contentDataHelper;
114    }
115    
116    /**
117     * Retrieves the extension point with available data types for internal data
118     * @return the extension point with available data types for internal data
119     */
120    public ModelLessBasicTypesExtensionPoint getInternalDataTypesExtensionPoint()
121    {
122        return _modelLessBasicTypesExtensionPoint;
123    }
124    
125    @Override
126    public DefaultContent getAmetysObject(Node node, String parentPath) throws AmetysRepositoryException
127    {
128        return new DefaultContent<>(node, parentPath, this);
129    }
130    
131    /**
132     * Returns the {@link AmetysObject} at the given subPath,
133     * relative to the given {@link DefaultTraversableAmetysObject}.
134     * @param <A> the actual type of {@link AmetysObject}.
135     * @param object the context {@link DefaultTraversableAmetysObject}.
136     * @param path the sub path. Cannot be <code>null</code>, empty or absolute.
137     * @return the {@link AmetysObject} at the given subPath,
138     *         relative to the given {@link DefaultTraversableAmetysObject}.
139     * @throws AmetysRepositoryException if an error occurs.
140     * @throws UnknownAmetysObjectException if no such object exists.
141     */
142    public <A extends AmetysObject> A getChild(DefaultContent object, String path) throws AmetysRepositoryException, UnknownAmetysObjectException
143    {
144        return TraversableAmetysObjectHelper.<A>getChild(object, this, path, _resolver, getLogger());
145    }
146    
147    /**
148     * Returns all children of the given {@link DefaultTraversableAmetysObject}.
149     * @param <A> the actual type of {@link AmetysObject}s
150     * @param object a {@link DefaultTraversableAmetysObject}.
151     * @return a List containing all children object in the Ametys hierarchy.
152     * @throws AmetysRepositoryException if an error occurs.
153     */
154    public <A extends AmetysObject> AmetysObjectIterable<A> getChildren(DefaultContent object) throws AmetysRepositoryException
155    {
156        return TraversableAmetysObjectHelper.getChildren(object, this, _resolver, getLogger());
157    }
158    
159    /**
160     * Tests if a given object has a child with a given name.
161     * @param object the context object.
162     * @param name the name to test.
163     * @return <code>true</code> is the given object has a child with the given name,
164     *         <code>false</code> otherwise.
165     * @throws AmetysRepositoryException if an error occurs.
166     */
167    public boolean hasChild(DefaultContent object, String name) throws AmetysRepositoryException
168    {
169        return TraversableAmetysObjectHelper.hasChild(object, name, _ametysFactoryExtensionPoint, getLogger());
170    }
171    
172    /**
173     * Creates a child to the given object.
174     * @param <A> the actual type of {@link AmetysObject}.
175     * @param object the parent {@link AmetysObject}.
176     * @param name the new object's name.
177     * @param type the new object's type.
178     * @return the newly created {@link AmetysObject}.
179     * @throws AmetysRepositoryException if an error occurs.
180     */
181    public <A extends AmetysObject> A createChild(DefaultContent object, String name, String type) throws AmetysRepositoryException
182    {
183        return TraversableAmetysObjectHelper.<A>createChild(object, this, name, type, _ametysFactoryExtensionPoint, _resolver, getLogger());
184    }
185}