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)
053    {
054        _setTranslations(copiedPrograms);
055        _setTranslations(copiedContainers);
056        _setTranslations(copiedSubPrograms);
057        _setTranslations(copiedCourseLists);
058        _setTranslations(copiedCourses);
059    }
060
061    /**
062     * Set the translations
063     * @param copiedContents Contents to update
064     */
065    protected void _setTranslations(Map<String, String> copiedContents)
066    {
067        for (String initialContentId : copiedContents.keySet())
068        {
069            // Get the translations of initial content
070            Content initialContent = _resolver.resolveById(initialContentId);
071            Map<String, String> translations = TranslationHelper.getTranslations(initialContent);
072            
073            if (!translations.isEmpty())
074            {
075                Map<String, String> updatedTranslations = new HashMap<>();
076                
077                for (String lang : translations.keySet())
078                {
079                    // For each translation, get its copy in the new catalog
080                    String translatedContentId = translations.get(lang);
081                    if (copiedContents.containsKey(translatedContentId))
082                    {
083                        updatedTranslations.put(lang, copiedContents.get(translatedContentId));
084                    }
085                }
086                
087                // Update the translations for created content
088                ModifiableContent createdContent = _resolver.resolveById(copiedContents.get(initialContentId));
089                TranslationHelper.setTranslations(createdContent, updatedTranslations);
090            }
091        }
092    }
093}