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.apache.avalon.framework.context.ContextException;
023import org.apache.avalon.framework.context.Contextualizable;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.cocoon.components.ContextHelper;
027import org.apache.cocoon.environment.Request;
028
029import org.ametys.cms.ObservationConstants;
030import org.ametys.cms.repository.Content;
031import org.ametys.cms.repository.RequestAttributeWorkspaceSelector;
032import org.ametys.core.observation.Event;
033import org.ametys.core.observation.Observer;
034import org.ametys.plugins.repository.AmetysObjectResolver;
035import org.ametys.plugins.repository.UnknownAmetysObjectException;
036import org.ametys.plugins.repository.jcr.JCRAmetysObject;
037import org.ametys.web.WebConstants;
038
039/**
040 * {@link Observer} for observing content deletion in order to synchronize live workspace.
041 */
042public class SynchronizeContentDeletionObserver extends AbstractSynchronizeObserver implements Contextualizable
043{
044    /** Ametys object resolver. */
045    protected AmetysObjectResolver _resolver;
046    /** The context. */
047    protected org.apache.avalon.framework.context.Context _context;
048    
049    @Override
050    public void service(ServiceManager manager) throws ServiceException
051    {
052        super.service(manager);
053        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
054    }
055    
056    @Override
057    public void contextualize(org.apache.avalon.framework.context.Context context) throws ContextException
058    {
059        _context = context;
060    }
061    
062    @Override
063    public boolean supports(Event event)
064    {
065        return event.getId().equals(ObservationConstants.EVENT_CONTENT_DELETED);
066    }
067    
068    @Override
069    protected void _internalObserve(Event event, Session liveSession) throws RepositoryException
070    {
071        Request request = ContextHelper.getRequest(_context);
072        
073        // Retrieve current workspace
074        String currentWsp = RequestAttributeWorkspaceSelector.getForcedWorkspace(request);
075        
076        try
077        {
078            // Use live workspace
079            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, WebConstants.LIVE_WORKSPACE);
080            Content liveContent = null;
081            
082            try
083            {
084                // Retrieve content in the live workspace
085                String contentId = (String) event.getArguments().get(ObservationConstants.ARGS_CONTENT_ID);
086                liveContent = _resolver.resolveById(contentId);
087            }
088            catch (UnknownAmetysObjectException e)
089            {
090                // Content is not synchronized
091            }
092            
093            if (liveContent != null && liveContent instanceof JCRAmetysObject)
094            {
095                Node node = ((JCRAmetysObject) liveContent).getNode();
096                Session session = node.getSession();
097                
098                if (!node.getReferences().hasNext())
099                {
100                    ((JCRAmetysObject) liveContent).getNode().remove();
101                }
102                
103                if (session.hasPendingChanges())
104                {
105                    session.save();
106                }
107            }
108        }
109        finally
110        {
111            // Restore context
112            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWsp);
113        }
114    }
115}