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.cdmfr.ExportCDMfrManager;
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    private ExportCDMfrManager _exportCDMfrManager;
048    
049    @Override
050    public void service(ServiceManager manager) throws ServiceException
051    {
052        super.service(manager);
053        _rootOrgUnitProvider = (RootOrgUnitProvider) manager.lookup(RootOrgUnitProvider.ROLE);
054        _exportCDMfrManager = (ExportCDMfrManager) manager.lookup(ExportCDMfrManager.ROLE);
055    }
056    
057    @Override
058    public Course getAmetysObject(Node node, String parentPath) throws AmetysRepositoryException
059    {
060        return new Course(node, parentPath, this);
061    }
062
063    /**
064     * Creates a child to the given object.
065     * 
066     * @param object the parent {@link AmetysObject}.
067     * @param name the new object's name.
068     * @param type the new object's type.
069     * @return the newly created {@link AmetysObject}.
070     * @throws AmetysRepositoryException if an error occurs.
071     */
072    public CourseList createChild(JCRTraversableAmetysObject object, String name, String type) throws AmetysRepositoryException
073    {
074        return TraversableAmetysObjectHelper.createChild(object, this, name, type, _ametysFactoryExtensionPoint, _resolver, getLogger());
075    }
076    
077    CourseList getChild(String path, Course object) throws AmetysRepositoryException, UnknownAmetysObjectException
078    {
079        return TraversableAmetysObjectHelper.getChild((JCRTraversableAmetysObject) object, this, path, _resolver, getLogger());
080    }
081
082    boolean hasChild(String name, Course object) throws AmetysRepositoryException
083    {
084        return TraversableAmetysObjectHelper.hasChild(object, name, this._ametysFactoryExtensionPoint, getLogger());
085    }
086
087    AmetysObjectIterable<AmetysObject> getChildren(Course object) throws AmetysRepositoryException
088    {
089        return TraversableAmetysObjectHelper.getChildren((JCRTraversableAmetysObject) object, this, _resolver, getLogger());
090    }
091    
092    String _getRootOrgUnitRNE()
093    {
094        return _rootOrgUnitProvider.getRoot().getUAICode();
095    }
096    
097    ExportCDMfrManager _getExportCDMfrManager()
098    {
099        return _exportCDMfrManager;
100    }
101}