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.live;
017
018import java.util.Collections;
019import java.util.List;
020
021import javax.jcr.Node;
022import javax.jcr.RepositoryException;
023import javax.jcr.Session;
024
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028import org.apache.commons.lang3.StringUtils;
029import org.slf4j.Logger;
030
031import org.ametys.cms.repository.Content;
032import org.ametys.core.schedule.progression.ContainerProgressionTracker;
033import org.ametys.core.schedule.progression.SimpleProgressionTracker;
034import org.ametys.core.util.I18nUtils;
035import org.ametys.plugins.repository.AmetysObjectIterable;
036import org.ametys.plugins.repository.AmetysObjectResolver;
037import org.ametys.plugins.repository.RepositoryConstants;
038import org.ametys.plugins.repository.collection.AmetysObjectCollection;
039import org.ametys.runtime.i18n.I18nizableText;
040import org.ametys.runtime.plugin.component.AbstractLogEnabled;
041import org.ametys.web.synchronization.SynchronizeComponent;
042
043/**
044 * {@link LivePopulator} for synchronizing '/ametys:root/ametys:contents'.
045 */
046public class ContentsLivePopulator extends AbstractLogEnabled implements LivePopulator, Serviceable
047{
048    private static final I18nizableText __LABEL = new I18nizableText("plugin.web", "PLUGINS_WEB_SCHEDULABLE_REBUILD_LIVE_CONTENTSLIVEPOPULATOR");
049    
050    /** The synchronize helper */
051    protected SynchronizeComponent _synchronizeHelper;
052    /** The Ametys resolver */
053    protected AmetysObjectResolver _resolver;
054    /** The utils for i18n */
055    protected I18nUtils _i18nUtils;
056    
057    @Override
058    public void service(ServiceManager smanager) throws ServiceException
059    {
060        _synchronizeHelper = (SynchronizeComponent) smanager.lookup(SynchronizeComponent.ROLE);
061        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
062        _i18nUtils = (I18nUtils) smanager.lookup(I18nUtils.ROLE);
063    }
064    
065    public I18nizableText getLabel()
066    {
067        return __LABEL;
068    }
069    
070    @Override
071    public Logger getProgressionTrackerLogger()
072    {
073        return getLogger();
074    }
075    
076    @Override
077    public List<String> populate(Session session, Session liveSession, ContainerProgressionTracker progressionTracker) throws Exception
078    {
079        SimpleProgressionTracker simpleStep = progressionTracker.addSimpleStep("contents", new I18nizableText("plugin.web", "PLUGINS_WEB_SCHEDULABLE_REBUILD_LIVE_CLONE_CONTENTS_STEP_LABEL"));
080        
081        Node contentsNode = session.getRootNode().getNode("ametys:root/ametys:contents");
082        
083        Node liveRootNode = liveSession.getRootNode().getNode(AmetysObjectResolver.ROOT_REPO);
084        
085        if (liveRootNode.hasNode("ametys:contents"))
086        {
087            liveRootNode.getNode("ametys:contents").remove();
088        }
089        
090        Node clonedParentNode = _synchronizeHelper.cloneAncestorsAndPreserveUUID(contentsNode, liveSession);
091        
092        _synchronizeHelper.addNodeWithUUID(contentsNode, clonedParentNode, contentsNode.getName());
093        
094        _cloneContents(contentsNode, liveSession, simpleStep);
095        
096        return Collections.EMPTY_LIST;
097    }
098    
099    /**
100     * Clone contents in live workspace
101     * @param contentsNode The root contents node
102     * @param liveSession The live session
103     * @param progressionTracker The progression tracker
104     * @throws RepositoryException if failed to clone contents in live
105     */
106    protected void _cloneContents(Node contentsNode, Session liveSession, SimpleProgressionTracker progressionTracker) throws RepositoryException
107    {
108        try (AmetysObjectIterable<Content> contents = _getContents(contentsNode))
109        {
110            long nbOfContents = contents.getSize();
111
112            _synchronizeHelper.synchronizeACL(contentsNode, liveSession);
113            
114            getLogger().info("Synchronizing {} contents at path {}", nbOfContents, contentsNode.getPath());
115            
116            _synchronizeHelper.synchronizeContents(contents, liveSession, progressionTracker);
117        }
118    }
119
120    /**
121     * Get the contents of root contents node
122     * @param contentsNode The root contents node
123     * @return The child contents
124     * @throws RepositoryException if failed to get the child contents
125     */
126    protected AmetysObjectIterable<Content> _getContents(Node contentsNode) throws RepositoryException
127    {
128        String contentsPath = StringUtils.substringAfter(contentsNode.getPath(), "/" + RepositoryConstants.NAMESPACE_PREFIX + ":root");
129        
130        AmetysObjectCollection rootContents = _resolver.resolveByPath(contentsPath);
131        return rootContents.getChildren();
132    }
133
134}