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