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.scc;
017
018import java.util.Map;
019import java.util.Set;
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.catalog.CopyCatalogUpdater;
028import org.ametys.odf.program.ProgramTranslationUpdater;
029import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionHelper;
030import org.ametys.plugins.repository.AmetysObjectResolver;
031import org.ametys.runtime.plugin.component.AbstractLogEnabled;
032
033/**
034 * The catalog copy updater to add the internal scc id if exists
035 */
036public class SCCCopyUpdater extends AbstractLogEnabled implements CopyCatalogUpdater, ProgramTranslationUpdater, Serviceable
037{
038    /** The ametys object resolver */
039    protected AmetysObjectResolver _resolver;
040    
041    /** The SCC helper */
042    protected SynchronizableContentsCollectionHelper _sccHelper;
043    
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
047        _sccHelper = (SynchronizableContentsCollectionHelper) manager.lookup(SynchronizableContentsCollectionHelper.ROLE);
048    }
049    
050    public void updateContents(String initialCatalogName, String newCatalogName, Map<Content, Content> copiedContents, Content targetParentContent)
051    {
052        for (Content initialContent : copiedContents.keySet())
053        {
054            Set<String> synchronizableCollectionIds = _sccHelper.getSynchronizableCollectionIds(initialContent);
055            if (!synchronizableCollectionIds.isEmpty())
056            {
057                Content createdContent = copiedContents.get(initialContent);
058                
059                if (createdContent instanceof ModifiableContent modifiableCreatedContent)
060                {
061                    for (String sccId : synchronizableCollectionIds)
062                    {
063                        _sccHelper.updateSCCProperty(modifiableCreatedContent, sccId);
064                    }
065                
066                    modifiableCreatedContent.saveChanges();
067                }
068            }
069        }
070    }
071}