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