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 */
016
017package org.ametys.web.live;
018
019import java.util.Collections;
020import java.util.List;
021
022import javax.jcr.Node;
023import javax.jcr.NodeIterator;
024import javax.jcr.Session;
025
026import org.apache.commons.collections.PredicateUtils;
027
028import org.ametys.plugins.repository.AmetysObjectResolver;
029
030/**
031 * {@link LivePopulator} for synchronizing '/ametys:root/ametys:plugins'.
032 */
033public class PluginsLivePopulator extends ContentsLivePopulator
034{
035    @Override
036    public List<String> populate(Session session, Session liveSession) throws Exception
037    {
038        Node pluginsNode = session.getRootNode().getNode("ametys:root/ametys:plugins");
039        
040        Node liveRootNode = liveSession.getRootNode().getNode(AmetysObjectResolver.ROOT_REPO);
041        
042        if (liveRootNode.hasNode("ametys:plugins"))
043        {
044            liveRootNode.getNode("ametys:plugins").remove();
045        }
046        
047        Node clonedParentNode = _synchronizeHelper.cloneAncestorsAndPreserveUUID(pluginsNode, liveSession);
048        Node clonedPluginsNode = _synchronizeHelper.addNodeWithUUID(pluginsNode, clonedParentNode, pluginsNode.getName());
049        
050        NodeIterator nodes = pluginsNode.getNodes();
051        
052        while (nodes.hasNext())
053        {
054            Node pluginNode = nodes.nextNode();
055            Node clonedPluginNode = _synchronizeHelper.addNodeWithUUID(pluginNode, clonedPluginsNode, pluginNode.getName());
056            
057            NodeIterator childNodes = pluginNode.getNodes();
058            while (childNodes.hasNext())
059            {
060                Node childNode = childNodes.nextNode();
061                
062                if (childNode.getName().equals("ametys:contents"))
063                {
064                    _cloneContents(childNode, liveSession);
065                }
066                else
067                {
068                    Node clonedChildNode = _synchronizeHelper.addNodeWithUUID(childNode, clonedPluginNode, childNode.getName());
069                    _synchronizeHelper.cloneNodeAndPreserveUUID(childNode, clonedChildNode, PredicateUtils.truePredicate(), PredicateUtils.truePredicate());
070                }
071            }
072            
073        }
074        
075        return Collections.EMPTY_LIST;
076    }
077}