001/*
002 *  Copyright 2017 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.inputdata;
017
018import java.util.Map;
019
020import org.ametys.cms.repository.Content;
021import org.ametys.odf.course.Course;
022import org.ametys.odf.program.AbstractProgram;
023import org.ametys.odf.translation.TranslationHelper;
024import org.ametys.plugins.odfweb.repository.CoursePage;
025import org.ametys.plugins.odfweb.repository.OdfPageResolver;
026import org.ametys.plugins.odfweb.repository.ProgramPage;
027import org.ametys.plugins.repository.AmetysObjectResolver;
028import org.ametys.plugins.repository.UnknownAmetysObjectException;
029import org.ametys.runtime.plugin.component.AbstractLogEnabled;
030import org.ametys.web.inputdata.InputData;
031import org.ametys.web.repository.page.Page;
032import org.ametys.web.repository.site.Site;
033import org.apache.avalon.framework.context.Context;
034import org.apache.avalon.framework.context.ContextException;
035import org.apache.avalon.framework.context.Contextualizable;
036import org.apache.avalon.framework.service.ServiceException;
037import org.apache.avalon.framework.service.ServiceManager;
038import org.apache.avalon.framework.service.Serviceable;
039import org.apache.cocoon.ProcessingException;
040import org.apache.cocoon.components.ContextHelper;
041import org.apache.cocoon.environment.Request;
042import org.apache.cocoon.xml.AttributesImpl;
043import org.apache.cocoon.xml.XMLUtils;
044import org.xml.sax.ContentHandler;
045import org.xml.sax.SAXException;
046
047/**
048 * {@link InputData} which generates the translations of program and course pages.
049 */
050public class TranslationsInputData extends AbstractLogEnabled implements InputData, Serviceable, Contextualizable
051{
052    /** Avalon context. */
053    protected Context _context;
054    
055    /** The ametys object resolver. */
056    protected AmetysObjectResolver _ametysResolver;
057    
058    /** The ODF page path resolver. */ 
059    protected OdfPageResolver _odfPageResolver;
060    
061    @Override
062    public void service(ServiceManager manager) throws ServiceException
063    {
064        _ametysResolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
065        _odfPageResolver = (OdfPageResolver) manager.lookup(OdfPageResolver.ROLE);
066    }
067    
068    @Override
069    public void contextualize(Context context) throws ContextException
070    {
071        _context = context;
072    }
073    
074    @Override
075    public boolean isCacheable(Site site, Page page)
076    {
077        return true;
078    }
079    
080    @Override
081    public void toSAX(ContentHandler handler) throws SAXException, ProcessingException
082    {
083        Request request = ContextHelper.getRequest(_context);
084        Page page = (Page) request.getAttribute(Page.class.getName());
085        
086        handler.startDocument();
087        XMLUtils.startElement(handler, "translations");
088        
089        if (page instanceof ProgramPage)
090        {
091            AbstractProgram program = ((ProgramPage) page).getProgram();
092            
093            Map<String, String> translations = TranslationHelper.getTranslations(program);
094            _saxTranslations (handler, translations);
095        }
096        else if (page instanceof CoursePage)
097        {
098            Course course = ((CoursePage) page).getCourse();
099            
100            Map<String, String> translations = TranslationHelper.getTranslations(course);
101            _saxTranslations (handler, translations);
102        }
103        
104        XMLUtils.endElement(handler, "translations");
105        handler.endDocument();
106    }
107    
108    private void _saxTranslations (ContentHandler contentHandler, Map<String, String> translations) throws SAXException
109    {
110        for (String lang : translations.keySet())
111        {
112            String contentId = translations.get(lang);
113            try
114            {
115                Content translatedProgram = _ametysResolver.resolveById(contentId);
116                
117                AttributesImpl attrs = new AttributesImpl();
118                attrs.addCDATAAttribute("language", lang);
119                attrs.addCDATAAttribute("id", translatedProgram.getId());
120                
121                XMLUtils.createElement(contentHandler, "translation", attrs);
122            }
123            catch (UnknownAmetysObjectException e)
124            {
125                // Ignore, the translation just doesn't exist in the live workspace.
126            }
127        }
128    }
129}