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.commons.collections.Predicate;
025import org.apache.commons.collections.PredicateUtils;
026
027import org.ametys.cms.support.AmetysPredicateUtils;
028import org.ametys.core.observation.Event;
029import org.ametys.core.observation.Observer;
030import org.ametys.web.ObservationConstants;
031import org.ametys.web.repository.site.Site;
032
033/**
034 * {@link Observer} for observing site addition in order to synchronize live workspace.
035 */
036public class SynchronizeSiteChangeObserver extends AbstractSynchronizeObserver
037{
038    @Override
039    public boolean supports(Event event)
040    {
041        return event.getId().equals(ObservationConstants.EVENT_SITE_UPDATED);
042    }
043    
044    @Override
045    protected void _internalObserve(Event event, Session liveSession) throws RepositoryException
046    {
047        Map<String, Object> arguments = event.getArguments();
048        Site site = (Site)  arguments.get(ObservationConstants.ARGS_SITE);
049        
050        Node siteNode = site.getNode();
051        String siteNodePath = siteNode.getPath();
052        
053        if (liveSession.itemExists(siteNodePath))
054        {
055            Node clonedSiteNode = (Node) liveSession.getItem(siteNodePath);
056            
057            // Clone all but known nodes
058            Predicate childNodes = PredicateUtils.allPredicate(new Predicate[] {
059                PredicateUtils.notPredicate(AmetysPredicateUtils.nodeNamePredicate("ametys-internal:contents")),
060                PredicateUtils.notPredicate(AmetysPredicateUtils.nodeNamePredicate("ametys-internal:sites")),
061                PredicateUtils.notPredicate(AmetysPredicateUtils.nodeNamePredicate("ametys-internal:sitemaps")),
062                PredicateUtils.notPredicate(AmetysPredicateUtils.nodeNamePredicate("ametys-internal:resources")),
063                PredicateUtils.notPredicate(AmetysPredicateUtils.nodeNamePredicate("ametys-internal:plugins"))
064            });
065            
066            _synchronizeComponent.cloneNodeAndPreserveUUID(siteNode, clonedSiteNode, PredicateUtils.truePredicate(), childNodes);
067            
068            liveSession.save();
069        }
070    }
071}