001/*
002 *  Copyright 2016 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.odfweb.cdmfr;
017
018import java.util.Set;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.avalon.framework.service.Serviceable;
023import org.apache.cocoon.xml.XMLUtils;
024import org.apache.commons.lang.StringUtils;
025import org.xml.sax.ContentHandler;
026import org.xml.sax.SAXException;
027
028import org.ametys.odf.cdmfr.CDMFRTagsConstants;
029import org.ametys.odf.cdmfr.CDMfrExtension;
030import org.ametys.odf.course.Course;
031import org.ametys.odf.orgunit.OrgUnit;
032import org.ametys.odf.person.Person;
033import org.ametys.odf.program.AbstractProgram;
034import org.ametys.odf.program.Container;
035import org.ametys.odf.program.Program;
036import org.ametys.odf.program.ProgramFactory;
037import org.ametys.odf.program.SubProgram;
038import org.ametys.plugins.odfweb.repository.OdfPageResolver;
039import org.ametys.plugins.odfweb.repository.ProgramPage;
040import org.ametys.web.repository.site.SiteManager;
041
042/**
043 * Web CDMfr extension for Ametys
044 */
045public class WebAmetysCDMfrExtension implements CDMfrExtension, Serviceable
046{
047    private OdfPageResolver _odfPageResolver;
048    private SiteManager _siteManager;
049    
050    @Override
051    public void service(ServiceManager manager) throws ServiceException
052    {
053        _odfPageResolver = (OdfPageResolver) manager.lookup(OdfPageResolver.ROLE);
054        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
055    }
056    
057    @Override
058    public void abstractProgram2CDM(ContentHandler contentHandler, AbstractProgram<? extends ProgramFactory> program, Set<String> persons, Set<String> orgUnits) throws SAXException
059    {
060        // Empty
061    }
062
063    public void program2CDM(ContentHandler contentHandler, Program program, Set<String> persons, Set<String> orgUnits) throws SAXException
064    {
065        ProgramPage page = _odfPageResolver.getProgramPage(program);
066        
067        if (page != null)
068        {
069            String pagePath = page.getSitemap().getName() + "/" + page.getPathInSitemap() + ".html";
070            String siteName = page.getSiteName();
071            
072            String url = _siteManager.getSite(siteName).getUrl();
073            if (!StringUtils.endsWith("/", url))
074            {
075                url += "/";
076            }
077            url += pagePath;
078            XMLUtils.createElement(contentHandler,  CDMFRTagsConstants.NAMESPACE_AMETYS_CDM + "webLink", url);
079        }
080    }
081    
082    public void subProgram2CDM(ContentHandler contentHandler, SubProgram subProgram, Set<String> persons, Set<String> orgUnits) throws SAXException
083    {
084        // Empty
085    }
086
087    public void course2CDM(ContentHandler contentHandler, Course course, Set<String> persons, Set<String> orgUnits) throws SAXException
088    {
089        // Empty
090    }
091
092    public void orgunit2CDM(ContentHandler contentHandler, OrgUnit orgunit) throws SAXException
093    {
094        // Empty
095    }
096
097    public void person2CDM(ContentHandler contentHandler, Person person) throws SAXException
098    {
099        // Empty
100    }
101
102    public void container2CDM(ContentHandler contentHandler, Container container, Set<String> persons, Set<String> orgUnits) throws SAXException
103    {
104        // Empty
105    }
106}