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 java.util.Map;
019
020import javax.jcr.Node;
021import javax.jcr.RepositoryException;
022import javax.jcr.Session;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026
027import org.ametys.core.observation.Event;
028import org.ametys.core.observation.Observer;
029import org.ametys.web.ObservationConstants;
030import org.ametys.web.repository.page.Page;
031import org.ametys.web.repository.page.PagesContainer;
032import org.ametys.web.repository.sitemap.Sitemap;
033import org.ametys.web.skin.Skin;
034import org.ametys.web.skin.SkinsManager;
035
036/**
037 * {@link Observer} for observing page deletion in order to synchronize live workspace.
038 */
039public class SynchronizePageDeletionObserver extends AbstractSynchronizeObserver
040{
041    private SkinsManager _skinsManager;
042    
043    @Override
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        super.service(manager);
047        _skinsManager = (SkinsManager) manager.lookup(SkinsManager.ROLE);
048    }
049    
050    @Override
051    public boolean supports(Event event)
052    {
053        return event.getId().equals(ObservationConstants.EVENT_PAGE_DELETED);
054    }
055    
056    @Override
057    protected void _internalObserve(Event event, Session liveSession) throws RepositoryException
058    {
059        Map<String, Object> arguments = event.getArguments();
060        Sitemap sitemap = (Sitemap) arguments.get(ObservationConstants.ARGS_SITEMAP);
061        
062        String pagePathInSitemap = (String) arguments.get(ObservationConstants.ARGS_PAGE_PATH);
063        String pagePathInJcr = sitemap.getNode().getPath().substring(1) + "/" + pagePathInSitemap;
064        Node rootNode = liveSession.getRootNode();
065        
066        if (rootNode.hasNode(pagePathInJcr))
067        {
068            rootNode.getNode(pagePathInJcr).remove();
069            
070            PagesContainer parent = (PagesContainer) arguments.get(ObservationConstants.ARGS_PAGE_PARENT);
071            if (parent != null && parent instanceof Page)
072            {
073                Page parentPage = (Page) parent;
074                Skin skin = _skinsManager.getSkin(parentPage.getSite().getSkinId());
075                if (!_synchronizeComponent.isPageValid(parentPage, skin))
076                {
077                    _synchronizeComponent.invalidateHierarchy(parentPage, skin, liveSession);
078                }
079            }
080            
081            liveSession.save();
082        }
083    }
084}