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.odf.course;
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.repository.ModifiableContentFactory;
024import org.ametys.odf.ODFHelper;
025import org.ametys.odf.courselist.CourseList;
026import org.ametys.odf.orgunit.RootOrgUnitProvider;
027import org.ametys.plugins.repository.AmetysObject;
028import org.ametys.plugins.repository.AmetysObjectIterable;
029import org.ametys.plugins.repository.AmetysRepositoryException;
030import org.ametys.plugins.repository.RepositoryConstants;
031import org.ametys.plugins.repository.UnknownAmetysObjectException;
032import org.ametys.plugins.repository.jcr.JCRTraversableAmetysObject;
033import org.ametys.plugins.repository.jcr.TraversableAmetysObjectHelper;
034
035/**
036 * {@link CourseFactory} for handling {@link Course}
037 */
038public class CourseFactory extends ModifiableContentFactory
039{
040    /** {@link Course} nodetype for resources collection */
041    public static final String COURSE_NODETYPE = RepositoryConstants.NAMESPACE_PREFIX + ":courseContent";
042
043    /** {@link Course} content type */
044    public static final String COURSE_CONTENT_TYPE = "org.ametys.plugins.odf.Content.course";
045
046    private RootOrgUnitProvider _rootOrgUnitProvider;
047
048    private ODFHelper _odfHelper;
049    
050    @Override
051    public void service(ServiceManager manager) throws ServiceException
052    {
053        super.service(manager);
054        _rootOrgUnitProvider = (RootOrgUnitProvider) manager.lookup(RootOrgUnitProvider.ROLE);
055        _odfHelper = (ODFHelper) manager.lookup(ODFHelper.ROLE);
056    }
057    
058    @Override
059    public Course getAmetysObject(Node node, String parentPath) throws AmetysRepositoryException
060    {
061        return new Course(node, parentPath, this);
062    }
063
064    /**
065     * Creates a child to the given object.
066     * 
067     * @param object the parent {@link AmetysObject}.
068     * @param name the new object's name.
069     * @param type the new object's type.
070     * @return the newly created {@link AmetysObject}.
071     * @throws AmetysRepositoryException if an error occurs.
072     */
073    public CourseList createChild(JCRTraversableAmetysObject object, String name, String type) throws AmetysRepositoryException
074    {
075        return TraversableAmetysObjectHelper.createChild(object, this, name, type, _ametysFactoryExtensionPoint, _resolver, getLogger());
076    }
077    
078    CourseList getChild(String path, Course object) throws AmetysRepositoryException, UnknownAmetysObjectException
079    {
080        return TraversableAmetysObjectHelper.getChild((JCRTraversableAmetysObject) object, this, path, _resolver, getLogger());
081    }
082
083    boolean hasChild(String name, Course object) throws AmetysRepositoryException
084    {
085        return TraversableAmetysObjectHelper.hasChild(object, name, this._ametysFactoryExtensionPoint, getLogger());
086    }
087
088    AmetysObjectIterable<AmetysObject> getChildren(Course object) throws AmetysRepositoryException
089    {
090        return TraversableAmetysObjectHelper.getChildren((JCRTraversableAmetysObject) object, this, _resolver, getLogger());
091    }
092    
093    String _getRootOrgUnitRNE()
094    {
095        return _rootOrgUnitProvider.getRoot().getUAICode();
096    }
097    
098    ODFHelper _getODFHelper()
099    {
100        return _odfHelper;
101    }
102}