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.odf.catalog;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024
025import org.ametys.cms.repository.Content;
026import org.ametys.cms.repository.ModifiableContent;
027import org.ametys.odf.program.Program;
028import org.ametys.odf.translation.TranslationHelper;
029import org.ametys.plugins.repository.AmetysObjectResolver;
030
031/**
032 * This updater updates the translations of created contents after the full catalog copy
033 *
034 */
035public class TranslationUpdater implements CopyCatalogUpdater, Serviceable
036{
037    private AmetysObjectResolver _resolver;
038    
039    @Override
040    public void service(ServiceManager manager) throws ServiceException
041    {
042        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
043    }
044
045    @Override
046    public void updateContent(String initialCatalogName, String newCatalogName, Program initalContent, Program createdContent)
047    {
048        // Nothing to do
049    }
050    
051    @Override
052    public void updateContents(String initialCatalogName, String newCatalogName, Map<String, String> copiedPrograms, Map<String, String> copiedSubPrograms, Map<String, String> copiedContainers, Map<String, String> copiedCourseLists, Map<String, String> copiedCourses, Map<String, String> copiedCourseParts)
053    {
054        _setTranslations(copiedPrograms);
055        _setTranslations(copiedContainers);
056        _setTranslations(copiedSubPrograms);
057        _setTranslations(copiedCourseLists);
058        _setTranslations(copiedCourses);
059        _setTranslations(copiedCourseParts);
060    }
061
062    /**
063     * Set the translations
064     * @param copiedContents Contents to update
065     */
066    protected void _setTranslations(Map<String, String> copiedContents)
067    {
068        for (String initialContentId : copiedContents.keySet())
069        {
070            // Get the translations of initial content
071            Content initialContent = _resolver.resolveById(initialContentId);
072            Map<String, String> translations = TranslationHelper.getTranslations(initialContent);
073            
074            if (!translations.isEmpty())
075            {
076                Map<String, String> updatedTranslations = new HashMap<>();
077                
078                for (String lang : translations.keySet())
079                {
080                    // For each translation, get its copy in the new catalog
081                    String translatedContentId = translations.get(lang);
082                    if (copiedContents.containsKey(translatedContentId))
083                    {
084                        updatedTranslations.put(lang, copiedContents.get(translatedContentId));
085                    }
086                }
087                
088                // Update the translations for created content
089                ModifiableContent createdContent = _resolver.resolveById(copiedContents.get(initialContentId));
090                TranslationHelper.setTranslations(createdContent, updatedTranslations);
091            }
092        }
093    }
094}