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