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