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.ArrayList;
020import java.util.List;
021
022import javax.jcr.Node;
023import javax.jcr.NodeIterator;
024import javax.jcr.RepositoryException;
025import javax.jcr.Session;
026
027import org.apache.avalon.framework.logger.AbstractLogEnabled;
028import org.apache.avalon.framework.service.ServiceException;
029import org.apache.avalon.framework.service.ServiceManager;
030import org.apache.avalon.framework.service.Serviceable;
031
032import org.ametys.plugins.repository.AmetysObjectIterable;
033import org.ametys.web.repository.site.Site;
034import org.ametys.web.repository.site.SiteManager;
035import org.ametys.web.skin.Skin;
036import org.ametys.web.skin.SkinsManager;
037
038/**
039 * {@link LivePopulator} for synchronizing sites.
040 */
041public class SitesLivePopulator extends AbstractLogEnabled implements LivePopulator, Serviceable
042{
043    private SitePopulator _sitePopulator;
044    private SkinsManager _skinsManager;
045    private SiteManager _siteManager;
046    
047    @Override
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        _sitePopulator = (SitePopulator) manager.lookup(SitePopulator.ROLE);
051        _skinsManager = (SkinsManager) manager.lookup(SkinsManager.ROLE);
052        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
053    }
054    
055    @Override
056    public List<String> populate(Session session, Session liveSession)
057    {
058        List<String> errors = new ArrayList<>(); 
059        
060        // First delete sites in live (to avoid that a deleted site in workspace default still remains in live workspace)
061        errors.addAll(_deleteLiveSites(liveSession));
062        
063        AmetysObjectIterable<Site> siteIterable = _siteManager.getSites();
064            
065        for (Site site : siteIterable)
066        {
067            try
068            {
069                if (getLogger().isInfoEnabled())
070                {
071                    getLogger().info("Populate live workspace for site " + site.getName());
072                }
073                
074                Skin skin = _skinsManager.getSkin(site.getSkinId());
075                
076                if (skin == null)
077                {
078                    throw new IllegalArgumentException("There's no skin for site " + site.getName() + ". Unable to rebuild live workspace.");
079                }
080                
081                _sitePopulator.populate(site, skin);
082            }
083            catch (Exception e)
084            {
085                errors.add("Unable to populate live workspace for site: " + site.getName() + "\n" + e.getMessage());
086                getLogger().error("Unable to populate live workspace for site: " + site.getName(), e);
087            }
088        }
089        
090        return errors;
091    }
092    
093    private List<String> _deleteLiveSites(Session liveSession)
094    {
095        List<String> errors = new ArrayList<>();
096
097        try
098        {
099            if (getLogger().isInfoEnabled())
100            {
101                getLogger().info("Remove all sites from live");
102            }
103            
104            Node liveRootNode = liveSession.getRootNode();
105            if (liveRootNode.hasNode("ametys:root" + SiteManager.ROOT_SITES_PATH))
106            {
107                NodeIterator childNodes = liveRootNode.getNode("ametys:root" + SiteManager.ROOT_SITES_PATH).getNodes();
108                while (childNodes.hasNext())
109                {
110                    Node siteNode = childNodes.nextNode();
111                    siteNode.remove();
112                }
113            }
114        }
115        catch (RepositoryException e)
116        {
117            errors.add("Failed to delete all sites from live workspace\n" + e.getMessage());
118            getLogger().error("Failed to delete all sites from live workspace", e);
119        }
120        
121        return errors;
122    }
123}