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 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.ProgramTranslationUpdater;
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, ProgramTranslationUpdater, 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 updateContents(String initialCatalogName, String newCatalogName, Map<Content, Content> copiedContents, Content targetParentContent)
053    {
054        for (Content initialContent : copiedContents.keySet())
055        {
056            Set<String> synchronizableCollectionIds = _sccHelper.getSynchronizableCollectionIds(initialContent);
057            if (!synchronizableCollectionIds.isEmpty())
058            {
059                Content createdContent = copiedContents.get(initialContent);
060                
061                if (createdContent instanceof ModifiableContent modifiableCreatedContent)
062                {
063                    for (String sccId : synchronizableCollectionIds)
064                    {
065                        try
066                        {
067                            _sccHelper.updateSCCProperty(modifiableCreatedContent, sccId);
068                        }
069                        catch (RepositoryException e)
070                        {
071                            getLogger().error("An error occurred setting the scc id for content '{}'", createdContent.getId(), e);
072                        }
073                    }
074                
075                    modifiableCreatedContent.saveChanges();
076                }
077            }
078        }
079    }
080}