001/*
002 *  Copyright 2010 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 javax.jcr.Node;
019import javax.jcr.RepositoryException;
020import javax.jcr.Session;
021
022import org.ametys.core.observation.Event;
023import org.ametys.core.observation.Observer;
024import org.ametys.web.ObservationConstants;
025import org.ametys.web.repository.site.Site;
026
027/**
028 * {@link Observer} for observing site or sitemap deletion in order to synchronize live workspace.
029 */
030public class SynchronizeSiteOrSitemapDeletionObserver extends AbstractSynchronizeObserver
031{
032    @Override
033    public boolean supports(Event event)
034    {
035        return event.getId().equals(ObservationConstants.EVENT_SITE_DELETED) || event.getId().equals(ObservationConstants.EVENT_SITEMAP_DELETED);
036    }
037    
038    @Override
039    protected void _internalObserve(Event event, Session liveSession) throws RepositoryException
040    {
041        Node liveRootNode = liveSession.getRootNode();
042        
043        String jcrPath = null;
044        if (event.getId().equals(ObservationConstants.EVENT_SITE_DELETED))
045        {
046            jcrPath = (String) event.getArguments().get(ObservationConstants.ARGS_SITE_PATH);
047        }
048        else if (event.getId().equals(ObservationConstants.EVENT_SITEMAP_DELETED))
049        {
050            Site site = (Site) event.getArguments().get(ObservationConstants.ARGS_SITE);
051            String sitemapName = (String) event.getArguments().get(ObservationConstants.ARGS_SITEMAP_NAME);
052            jcrPath = site.getNode().getPath().substring(1) + "/" + sitemapName;
053        }
054        
055        if (jcrPath != null && liveRootNode.hasNode(jcrPath))
056        {
057            liveRootNode.getNode(jcrPath).remove();
058        }
059        
060        liveSession.save();
061    }
062}