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