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