001/*
002 *  Copyright 2023 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.catalog;
017
018import java.util.Map;
019import java.util.Set;
020
021import javax.jcr.RepositoryException;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026
027import org.ametys.cms.repository.Content;
028import org.ametys.cms.repository.ModifiableContent;
029import org.ametys.odf.catalog.CopyCatalogUpdater;
030import org.ametys.odf.program.Program;
031import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionHelper;
032import org.ametys.plugins.repository.AmetysObjectResolver;
033import org.ametys.runtime.plugin.component.AbstractLogEnabled;
034
035/**
036 * The catalog copy updater to add the internal scc id if exists
037 */
038public class SCCCopyUpdater extends AbstractLogEnabled implements CopyCatalogUpdater, Serviceable
039{
040    /** The ametys object resolver */
041    protected AmetysObjectResolver _resolver;
042    
043    /** The SCC helper */
044    protected SynchronizableContentsCollectionHelper _sccHelper;
045    
046    public void service(ServiceManager manager) throws ServiceException
047    {
048        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
049        _sccHelper = (SynchronizableContentsCollectionHelper) manager.lookup(SynchronizableContentsCollectionHelper.ROLE);
050    }
051    
052    public void updateContent(String initialCatalogName, String newCatalogName, Program initalContent, Program createdContent)
053    {
054        // Nothing to do
055    }
056    
057    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)
058    {
059        _updateSCCId(copiedPrograms);
060        _updateSCCId(copiedContainers);
061        _updateSCCId(copiedSubPrograms);
062        _updateSCCId(copiedCourseLists);
063        _updateSCCId(copiedCourses);
064    }
065
066    /**
067     * Update the scc
068     * @param copiedContents contents to update
069     */
070    protected void _updateSCCId(Map<String, String> copiedContents)
071    {
072        for (String initialContentId : copiedContents.keySet())
073        {
074            Content initialContent = _resolver.resolveById(initialContentId);
075            Set<String> synchronizableCollectionIds = _sccHelper.getSynchronizableCollectionIds(initialContent);
076            if (!synchronizableCollectionIds.isEmpty())
077            {
078                ModifiableContent createdContent = _resolver.resolveById(copiedContents.get(initialContentId));
079                for (String sccId : synchronizableCollectionIds)
080                {
081                    try
082                    {
083                        _sccHelper.updateSCCProperty(createdContent, sccId);
084                    }
085                    catch (RepositoryException e)
086                    {
087                        getLogger().error("An error occurred setting the scc id for content '{}'", createdContent.getId(), e);
088                    }
089                }
090                
091                createdContent.saveChanges();
092            }
093        }
094    }
095}