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.commons.collections.Predicate;
023import org.apache.commons.collections.PredicateUtils;
024
025import org.ametys.cms.support.AmetysPredicateUtils;
026import org.ametys.core.observation.Event;
027import org.ametys.core.observation.Observer;
028import org.ametys.plugins.repository.jcr.JCRAmetysObject;
029import org.ametys.web.ObservationConstants;
030import org.ametys.web.repository.page.Page;
031import org.ametys.web.skin.Skin;
032
033/**
034 * {@link Observer} for observing page updating in order to synchronize live workspace.
035 */
036public class SynchronizePageDataUpdateObserver extends AbstractSynchronizePageObserver
037{
038    @Override
039    protected boolean _internalSupport(Event event, Page target)
040    {
041        return event.getId().equals(ObservationConstants.EVENT_PAGE_UPDATED);
042    }
043    
044    @Override
045    protected void _internalObservePage(Event event, Page page, Skin skin, Session liveSession) throws RepositoryException
046    {
047        JCRAmetysObject jcrPage = (JCRAmetysObject) page;
048        
049        if (_synchronizeComponent.isPageValid(page, skin))
050        {
051            Node pageNode = jcrPage.getNode();
052            String pagePathInJcr = pageNode.getPath().substring(1);
053            Node rootNode = liveSession.getRootNode();
054            
055            if (rootNode.hasNode(pagePathInJcr))
056            {
057                Node livePageNode = rootNode.getNode(pagePathInJcr);
058                
059                // Clone all but child pages and zones
060                Predicate childPredicate = PredicateUtils.andPredicate(PredicateUtils.notPredicate(AmetysPredicateUtils.nodeTypePredicate("ametys:page")), 
061                        PredicateUtils.notPredicate(AmetysPredicateUtils.nodeTypePredicate("ametys:zones")));
062
063                _synchronizeComponent.cloneNodeAndPreserveUUID(pageNode, livePageNode, PredicateUtils.truePredicate(), childPredicate);
064                
065                liveSession.save();
066            }
067        }
068    }
069}