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