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.PropertyIterator;
022import javax.jcr.RepositoryException;
023import javax.jcr.Session;
024
025import org.apache.avalon.framework.context.ContextException;
026import org.apache.avalon.framework.context.Contextualizable;
027import org.apache.avalon.framework.service.ServiceException;
028import org.apache.avalon.framework.service.ServiceManager;
029import org.apache.cocoon.components.ContextHelper;
030import org.apache.cocoon.environment.Request;
031
032import org.ametys.cms.ObservationConstants;
033import org.ametys.cms.repository.Content;
034import org.ametys.core.observation.Event;
035import org.ametys.core.observation.Observer;
036import org.ametys.plugins.repository.AmetysObjectResolver;
037import org.ametys.plugins.repository.UnknownAmetysObjectException;
038import org.ametys.plugins.repository.jcr.JCRAmetysObject;
039import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector;
040import org.ametys.web.WebConstants;
041import org.ametys.web.repository.page.Page;
042import org.ametys.web.repository.page.jcr.DefaultZone;
043import org.ametys.web.skin.Skin;
044import org.ametys.web.skin.SkinsManager;
045
046/**
047 * {@link Observer} for observing content deletion in order to synchronize live workspace.
048 */
049public class SynchronizeContentUnpublishObserver extends AbstractSynchronizeObserver implements Contextualizable
050{
051    /** Ametys object resolver. */
052    protected AmetysObjectResolver _resolver;
053    /** The context. */
054    protected org.apache.avalon.framework.context.Context _context;
055    private SkinsManager _skinsManager;
056    
057    @Override
058    public void service(ServiceManager manager) throws ServiceException
059    {
060        super.service(manager);
061        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
062        _skinsManager = (SkinsManager) manager.lookup(SkinsManager.ROLE);
063    }
064    
065    @Override
066    public void contextualize(org.apache.avalon.framework.context.Context context) throws ContextException
067    {
068        _context = context;
069    }
070    
071    @Override
072    public boolean supports(Event event)
073    {
074        return event.getId().equals(ObservationConstants.EVENT_CONTENT_UNTAG_LIVE);
075    }
076    
077    @Override
078    protected void _internalObserve(Event event, Session liveSession) throws RepositoryException
079    {
080        Map<String, Object> arguments = event.getArguments();
081        Content content = (Content) arguments.get(ObservationConstants.ARGS_CONTENT);
082        
083        if (!(content instanceof JCRAmetysObject))
084        {
085            return;
086        }
087        
088        // Retrieve current workspace
089        Request request = ContextHelper.getRequest(_context);
090        String currentWsp = RequestAttributeWorkspaceSelector.getForcedWorkspace(request);
091        
092        try
093        {
094            // Use live workspace
095            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, WebConstants.LIVE_WORKSPACE);
096            Content liveContent = null;
097            
098            try
099            {
100                // Retrieve content in the live workspace
101                liveContent = _resolver.resolveById(content.getId());
102            }
103            catch (UnknownAmetysObjectException e)
104            {
105                // Content is not synchronized
106            }
107            
108            if (liveContent != null && liveContent instanceof JCRAmetysObject)
109            {
110                Node node = ((JCRAmetysObject) content).getNode();
111                
112                PropertyIterator itReferences = node.getReferences();
113                while (itReferences.hasNext())
114                {
115                    Node refererNode = itReferences.nextProperty().getParent();
116                    if (refererNode.getPrimaryNodeType().getName().equals(DefaultZone.ZONEITEM_NODE_NAME))
117                    {
118                        Node pageNode = refererNode.getParent().getParent().getParent().getParent();
119                        Page page = _resolver.resolve(pageNode, false);
120                        Skin skin = _skinsManager.getSkin(page.getSite().getSkinId());
121                        
122                        _synchronizeComponent.synchronizePage(page, skin, liveSession);
123                        
124                        liveSession.save();
125                    }
126                }
127                
128                Node liveNode = ((JCRAmetysObject) liveContent).getNode();
129                if (!liveNode.getReferences().hasNext())
130                {
131                    ((JCRAmetysObject) liveContent).getNode().remove();
132                }
133                
134                if (liveSession.hasPendingChanges())
135                {
136                    liveSession.save();
137                }
138            }
139        }
140        finally
141        {
142            // Restore context
143            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWsp);
144        }
145    }
146}