001/*
002 *  Copyright 2021 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.plugins.odfsync.cdmfr.extractor;
017
018import org.apache.avalon.framework.component.Component;
019import org.apache.avalon.framework.service.ServiceException;
020import org.apache.avalon.framework.service.ServiceManager;
021import org.apache.avalon.framework.service.Serviceable;
022import org.w3c.dom.Element;
023
024import org.ametys.cms.contenttype.ContentType;
025import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
026import org.ametys.cms.repository.Content;
027import org.ametys.odf.course.Course;
028import org.ametys.odf.courselist.CourseList;
029import org.ametys.odf.coursepart.CoursePart;
030import org.ametys.odf.enumeration.OdfReferenceTableHelper;
031import org.ametys.odf.program.TraversableProgramPart;
032import org.ametys.plugins.odfsync.cdmfr.ImportCDMFrContext;
033import org.ametys.plugins.odfsync.cdmfr.components.ImportCDMFrComponent;
034import org.ametys.plugins.odfsync.utils.ContentWorkflowDescription;
035import org.ametys.plugins.repository.AmetysObjectResolver;
036import org.ametys.plugins.repository.data.extractor.ModelAwareValuesExtractor;
037
038/**
039 * Factory for {@link ImportCDMFrValuesExtractor}
040 * Creates the right instance of {@link ImportCDMFrValuesExtractor} according to the imported content
041 * Provides components to the values extractor
042 */
043public class ImportCDMFrValuesExtractorFactory implements Component, Serviceable
044{
045    /** The component role. */
046    public static final String ROLE = ImportCDMFrValuesExtractorFactory.class.getName();
047    
048    /** The ametys object resolver */
049    protected AmetysObjectResolver _resolver;
050    
051    /** The content type extension point */
052    protected ContentTypeExtensionPoint _contentTypeExtensionPoint;
053    
054    /** The ODF TableRef Helper */
055    private OdfReferenceTableHelper _odfRefTableHelper;
056
057    public void service(ServiceManager manager) throws ServiceException
058    {
059        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
060        _contentTypeExtensionPoint = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE);
061        _odfRefTableHelper = (OdfReferenceTableHelper) manager.lookup(OdfReferenceTableHelper.ROLE);
062    }
063    
064    /**
065     * Retrieves the values extractor corresponding to the given content
066     * @param contentElement the DOM content element
067     * @param component The import CDM-fr component
068     * @param content The content to synchronize
069     * @param contentType The content type
070     * @param syncCode The synchronization code
071     * @param context the import context
072     * @return the values extractor corresponding to the given content
073     */
074    public ModelAwareValuesExtractor getValuesExtractor(Element contentElement, ImportCDMFrComponent component, Content content, ContentType contentType, String syncCode, ImportCDMFrContext context)
075    {
076        if (content instanceof TraversableProgramPart)
077        {
078            return new ImportTraversableProgramPartValuesExtractor(contentElement, this, component, syncCode, context, contentType);
079        }
080        else if (content instanceof CourseList)
081        {
082            return new ImportCourseListValuesExtractor(contentElement, this, component, context, contentType);
083        }
084        else if (content instanceof Course)
085        {
086            return new ImportCourseValuesExtractor(contentElement, this, component, syncCode, context, contentType);
087        }
088        else if (content instanceof CoursePart)
089        {
090            return new ImportCoursePartValuesExtractor(contentElement, this, component, context, contentType);
091        }
092        else
093        {
094            return new ImportCDMFrValuesExtractor(contentElement, this, component, context, contentType);
095        }
096    }
097    
098    /**
099     * Retrieves the values extractor for mentions
100     * @param contentElement the DOM content element
101     * @param component The import CDM-fr component
102     * @param context the import context
103     * @return the values extractor corresponding to the given content
104     */
105    public ModelAwareValuesExtractor getMentionValuesExtractor(Element contentElement, ImportCDMFrComponent component, ImportCDMFrContext context)
106    {
107        ContentType contentType = _contentTypeExtensionPoint.getExtension(ContentWorkflowDescription.PROGRAM_WF_DESCRIPTION.getContentType());
108        return new ImportCDMFrValuesExtractor(contentElement, this, component, context, contentType);
109    }
110    
111    /**
112     * Retrieves the values extractor for mentions
113     * @param contentElement the DOM content element
114     * @param component The import CDM-fr component
115     * @param context the import context
116     * @return the values extractor corresponding to the given content
117     */
118    public ModelAwareValuesExtractor getSharedSubProgramValuesExtractor(Element contentElement, ImportCDMFrComponent component, ImportCDMFrContext context)
119    {
120        ContentType contentType = _contentTypeExtensionPoint.getExtension(ContentWorkflowDescription.SUBPROGRAM_WF_DESCRIPTION.getContentType());
121        return new ImportCDMFrValuesExtractor(contentElement, this, component, context, contentType);
122    }
123    
124    /**
125     * Retrieves the ametys object resolver
126     * @return the ametys object resolver
127     */
128    public AmetysObjectResolver getAmetysObjectResolver()
129    {
130        return _resolver;
131    }
132    
133    /**
134     * Retrieves the content type extension point
135     * @return the content type extension point
136     */
137    public ContentTypeExtensionPoint getContentTypeExtensionPoint()
138    {
139        return _contentTypeExtensionPoint;
140    }
141
142    /**
143     * Retrieves the {@link OdfReferenceTableHelper}
144     * @return the {@link OdfReferenceTableHelper}
145     */
146    public OdfReferenceTableHelper getODFReferenceTableHelper()
147    {
148        return _odfRefTableHelper;
149    }
150}