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;
021import java.util.Map;
022
023import javax.jcr.Node;
024import javax.jcr.NodeIterator;
025import javax.jcr.Session;
026
027import org.apache.commons.collections.PredicateUtils;
028
029import org.ametys.core.schedule.progression.ContainerProgressionTracker;
030import org.ametys.core.schedule.progression.SimpleProgressionTracker;
031import org.ametys.plugins.repository.AmetysObjectResolver;
032import org.ametys.runtime.i18n.I18nizableText;
033
034/**
035 * {@link LivePopulator} for synchronizing '/ametys:root/ametys:plugins'.
036 */
037public class PluginsLivePopulator extends ContentsLivePopulator
038{
039    private static final I18nizableText __LABEL = new I18nizableText("plugin.web", "PLUGINS_WEB_SCHEDULABLE_REBUILD_LIVE_PLUGINSCONTENTSLIVEPOPULATOR");
040
041    @Override
042    public I18nizableText getLabel()
043    {
044        return __LABEL;
045    }
046    
047    private void _createProgressionTrackerStepsForPopulate(ContainerProgressionTracker progressionTracker, Node pluginsNode) throws Exception
048    {
049        NodeIterator nodes = pluginsNode.getNodes();
050        while (nodes.hasNext())
051        {
052            String pluginName = nodes.nextNode().getName();
053            progressionTracker.addContainerStep(pluginName, new I18nizableText("plugin.web", "PLUGINS_WEB_SCHEDULABLE_REBUILD_LIVE_POPULATE_PLUGIN_STEP_LABEL", Map.of("0", new I18nizableText(pluginName))));
054        }
055    }
056    
057    @Override
058    public List<String> populate(Session session, Session liveSession, ContainerProgressionTracker progressionTracker) throws Exception
059    {
060        Node pluginsNode = session.getRootNode().getNode("ametys:root/ametys:plugins");
061        
062        _createProgressionTrackerStepsForPopulate(progressionTracker, pluginsNode);
063        
064        NodeIterator nodes = pluginsNode.getNodes();
065        
066        Node liveRootNode = liveSession.getRootNode().getNode(AmetysObjectResolver.ROOT_REPO);
067        
068        if (liveRootNode.hasNode("ametys:plugins"))
069        {
070            liveRootNode.getNode("ametys:plugins").remove();
071        }
072
073        Node clonedParentNode = _synchronizeHelper.cloneAncestorsAndPreserveUUID(pluginsNode, liveSession);
074        Node clonedPluginsNode = _synchronizeHelper.addNodeWithUUID(pluginsNode, clonedParentNode, pluginsNode.getName());
075        
076        nodes = pluginsNode.getNodes();
077        while (nodes.hasNext())
078        {
079            Node pluginNode = nodes.nextNode();
080            _populateSubNodes(pluginNode, clonedPluginsNode, liveSession, progressionTracker.getStep(pluginNode.getName()));
081        }
082        
083        return Collections.EMPTY_LIST;
084    }
085    
086    private void _createProgressionTrackerStepsForPopulateSubNodes(ContainerProgressionTracker progressionTracker, Node pluginNode) throws Exception
087    {
088        int otherNodes = 0;
089        
090        NodeIterator childNodes = pluginNode.getNodes();
091        while (childNodes.hasNext())
092        {
093            String nodeName = childNodes.nextNode().getName();
094            if (nodeName.equals("ametys:contents"))
095            {
096                progressionTracker.addSimpleStep("contents", new I18nizableText("plugin.web", "PLUGINS_WEB_SCHEDULABLE_REBUILD_LIVE_CONTENTS_SYNC_STEP_LABEL"));
097            }
098            else
099            {
100                if (otherNodes == 0)
101                {
102                    progressionTracker.addSimpleStep("others", new I18nizableText("plugin.web", "PLUGINS_WEB_SCHEDULABLE_REBUILD_LIVE_NODES_STEP_LABEL"));
103                }
104                otherNodes++;
105            }
106        }
107        
108        if (otherNodes > 0)
109        {
110            ((SimpleProgressionTracker) progressionTracker.getStep("others")).setSize(otherNodes);            
111        }
112    }
113    
114    private void _populateSubNodes(Node pluginNode, Node clonedPluginsNode, Session liveSession, ContainerProgressionTracker progressionTracker) throws Exception
115    {
116        _createProgressionTrackerStepsForPopulateSubNodes(progressionTracker, pluginNode);
117        
118        Node clonedPluginNode = _synchronizeHelper.addNodeWithUUID(pluginNode, clonedPluginsNode, pluginNode.getName());
119        
120        NodeIterator childNodes = pluginNode.getNodes();
121        
122        while (childNodes.hasNext())
123        {
124            Node childNode = childNodes.nextNode();
125            
126            
127            if (childNode.getName().equals("ametys:contents"))
128            {
129                SimpleProgressionTracker step = progressionTracker.getStep("contents");
130                _cloneContents(childNode, liveSession, step);
131            }
132            else
133            {
134                Node clonedChildNode = _synchronizeHelper.addNodeWithUUID(childNode, clonedPluginNode, childNode.getName());
135                _synchronizeHelper.cloneNodeAndPreserveUUID(childNode, clonedChildNode, PredicateUtils.truePredicate(), PredicateUtils.truePredicate());
136                SimpleProgressionTracker step = progressionTracker.getStep("others");
137                step.increment();
138            }
139        }
140    }
141}