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