001/*
002 *  Copyright 2026 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.web.synchronization;
017
018import java.util.Map;
019
020import javax.jcr.Node;
021import javax.jcr.Repository;
022import javax.jcr.RepositoryException;
023import javax.jcr.Session;
024
025import org.apache.avalon.framework.logger.AbstractLogEnabled;
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028import org.apache.avalon.framework.service.Serviceable;
029
030import org.ametys.cms.content.archive.ArchiveConstants;
031import org.ametys.core.observation.Event;
032import org.ametys.core.observation.Observer;
033import org.ametys.web.ObservationConstants;
034import org.ametys.web.repository.site.Site;
035
036/**
037 * {@link Observer} for observing site move in order to synchronize archives workspace.
038 */
039public class SynchronizeArchivesSiteMoveObserver extends AbstractLogEnabled implements Observer, Serviceable
040{
041    /** JCR repository. */
042    protected Repository _repository;
043    /** The {@link SynchronizeComponent} */
044    protected SynchronizeComponent _synchronizeComponent;
045
046    @Override
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        _repository = (Repository) manager.lookup(Repository.class.getName());
050        _synchronizeComponent = (SynchronizeComponent) manager.lookup(SynchronizeComponent.ROLE);
051    }
052    
053    @Override
054    public boolean supports(Event event)
055    {
056        return event.getId().equals(ObservationConstants.EVENT_SITE_MOVED);
057    }
058
059    @Override
060    public int getPriority()
061    {
062        return MAX_PRIORITY + 1000;
063    }
064
065    public void observe(Event event, Map<String, Object> transientVars) throws Exception
066    {
067        Session archiveSession = null;
068        
069        try
070        {
071            // Open a session to the workspace archives
072            archiveSession = _repository.login(ArchiveConstants.ARCHIVE_WORKSPACE);
073            Node archiveRootNode = archiveSession.getRootNode();
074            
075            Map<String, Object> arguments = event.getArguments();
076            String oldPath = (String) arguments.get(ObservationConstants.ARGS_SITE_OLD_PATH);
077
078            if (archiveRootNode.hasNode(oldPath))
079            {
080                Site site = (Site) arguments.get(ObservationConstants.ARGS_SITE);
081                Node siteNode = site.getNode();
082                _synchronizeComponent.cloneAncestorsAndPreserveUUID(siteNode, archiveSession);
083                
084                Node oldSiteNode = archiveRootNode.getNode(oldPath);
085                archiveSession.move(oldSiteNode.getPath(), siteNode.getPath());
086                
087                archiveSession.save();
088            }
089        }
090        catch (RepositoryException e)
091        {
092            getLogger().error("Unable to synchronize archives workspace with event: " + event, e);
093        }
094        finally
095        {
096            if (archiveSession != null)
097            {
098                archiveSession.logout();
099            }
100        }
101        
102    }
103
104}