001/*
002 *  Copyright 2013 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.plugins.repository.AmetysObjectResolver;
034import org.ametys.web.ObservationConstants;
035import org.ametys.web.repository.site.Site;
036
037/**
038 * {@link Observer} for observing site or sitemap deletion in order to synchronize archives workspace.
039 */
040public class SynchronizeArchivesSiteDeletionObserver extends AbstractLogEnabled implements Observer, Serviceable
041{
042    
043    /** The JCR repository. */
044    protected Repository _repository;
045    
046    /** The ametys object resolver. */
047    protected AmetysObjectResolver _resolver;
048
049    @Override
050    public void service(ServiceManager manager) throws ServiceException
051    {
052        _repository = (Repository) manager.lookup(Repository.class.getName());
053        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.class.getName());
054    }
055    
056    @Override
057    public boolean supports(Event event)
058    {
059        return event.getId().equals(ObservationConstants.EVENT_SITE_DELETED);
060    }
061    
062    @Override
063    public int getPriority(Event event)
064    {
065        return MAX_PRIORITY + 1000;
066    }
067
068    @Override
069    public void observe(Event event, Map<String, Object> transientVars) throws Exception
070    {
071        Session archiveSession = null; 
072        
073        try
074        {
075            // Open a session to the archives workspace.
076            archiveSession = _repository.login(ArchiveConstants.ARCHIVE_WORKSPACE);
077            
078            Node archiveRootNode = archiveSession.getRootNode();
079            
080            Map<String, Object> arguments = event.getArguments();
081            String jcrPath = (String) arguments.get(ObservationConstants.ARGS_SITE_PATH);
082            
083            if (archiveRootNode.hasNode(jcrPath))
084            {
085                // Resolve the site in the archives workspace and remove it.
086                Node siteNode = archiveRootNode.getNode(jcrPath);
087                Site site = _resolver.resolve(siteNode, true);
088                
089                if (site != null)
090                {
091                    site.remove();
092                }
093            }
094            
095            archiveSession.save();
096        }
097        catch (RepositoryException e)
098        {
099            getLogger().error("Unable to synchronize archives workspace with event: " + event, e);
100        }
101        finally
102        {
103            if (archiveSession != null)
104            {
105                archiveSession.logout();
106            }
107        }
108    }
109    
110}