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.RepositoryException;
021import javax.jcr.Session;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025
026import org.ametys.cms.ObservationConstants;
027import org.ametys.cms.repository.Content;
028import org.ametys.core.observation.Event;
029import org.ametys.core.observation.Observer;
030import org.ametys.plugins.repository.AmetysObjectResolver;
031import org.ametys.plugins.repository.jcr.JCRAmetysObject;
032import org.ametys.web.repository.content.WebContent;
033import org.ametys.web.repository.page.Page;
034import org.ametys.web.skin.Skin;
035import org.ametys.web.skin.SkinsManager;
036
037/**
038 * {@link Observer} for observing content validation in order to synchronize live workspace.
039 */
040public class SynchronizeContentValidationObserver extends AbstractSynchronizeObserver
041{
042    /** Ametys object resolver. */
043    protected AmetysObjectResolver _ametysObjectResolver;
044    private SkinsManager _skinsManager;
045
046    @Override
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        super.service(manager);
050        _ametysObjectResolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
051        _skinsManager = (SkinsManager) manager.lookup(SkinsManager.ROLE);
052    }
053    
054    @Override
055    public boolean supports(Event event)
056    {
057        return event.getId().equals(ObservationConstants.EVENT_CONTENT_VALIDATED);
058    }
059    
060    @Override
061    protected void _internalObserve(Event event, Session liveSession) throws RepositoryException
062    {
063        Map<String, Object> arguments = event.getArguments();
064        Content content = (Content) arguments.get(org.ametys.cms.ObservationConstants.ARGS_CONTENT);
065        
066        if (!(content instanceof JCRAmetysObject))
067        {
068            return;
069        }
070        
071        _synchronizeComponent.synchronizeContent(content, liveSession);
072        
073        // publish pages if necessary
074        if (content instanceof WebContent)
075        {
076            WebContent webContent = (WebContent) content;
077            for (Page page : webContent.getReferencingPages())
078            {
079                Skin skin = _skinsManager.getSkin(page.getSite().getSkinId());
080                
081                _synchronizeComponent.synchronizePage(page, skin, liveSession);
082            }
083        }
084        
085        if (liveSession.hasPendingChanges())
086        {
087            liveSession.save();
088        }
089    }
090}