001/* 002 * Copyright 2025 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.container; 017 018import java.util.List; 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.data.ContentValue; 026import org.ametys.cms.repository.Content; 027import org.ametys.cms.workflow.ContentWorkflowHelper; 028import org.ametys.odf.ODFHelper; 029import org.ametys.odf.content.CopyODFContentUpdater; 030import org.ametys.odf.program.Container; 031import org.ametys.runtime.plugin.component.AbstractLogEnabled; 032 033import com.opensymphony.workflow.WorkflowException; 034 035/** 036 * Copy updater to remove the 'previousYears' and 'nexYears' attributes on {@link Container}. 037 */ 038public class ContainerCopyUpdater extends AbstractLogEnabled implements CopyODFContentUpdater, Serviceable 039{ 040 private ODFHelper _odfHelper; 041 private ContentWorkflowHelper _contentWorkflowHelper; 042 043 public void service(ServiceManager smanager) throws ServiceException 044 { 045 _odfHelper = (ODFHelper) smanager.lookup(ODFHelper.ROLE); 046 _contentWorkflowHelper = (ContentWorkflowHelper) smanager.lookup(ContentWorkflowHelper.ROLE); 047 } 048 049 @Override 050 public void updateContents(String initialCatalogName, String newCatalogName, Map<Content, Content> copiedContents, Content targetParentContent) 051 { 052 for (Content copiedContent : copiedContents.values()) 053 { 054 if (copiedContent instanceof Container copiedContainer && _odfHelper.isContainerOfTypeYear(copiedContainer)) 055 { 056 try 057 { 058 // Remove previousYears and nextYears references (invert relations should be updated too) 059 ContentValue[] refPreviousYears = copiedContainer.getValue("previousYears"); 060 ContentValue[] refNextYears = copiedContainer.getValue("nextYears"); 061 062 if (refPreviousYears != null && refPreviousYears.length > 0 || refNextYears != null && refNextYears.length > 0) 063 { 064 _contentWorkflowHelper.editContent(copiedContainer, Map.of("nextYears", List.of(), "previousYears", List.of()), 2); 065 } 066 } 067 catch (WorkflowException e) 068 { 069 getLogger().error("Unable to remove 'previousYears' and 'nextYears' references from copied year container ''{0}''.", e, copiedContainer.getName()); 070 } 071 } 072 } 073 } 074}