001/*
002 *  Copyright 2011 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.synchronization;
017
018import java.util.Map;
019
020import javax.jcr.Node;
021import javax.jcr.Repository;
022import javax.jcr.RepositoryException;
023import javax.jcr.Session;
024
025import org.apache.avalon.framework.logger.AbstractLogEnabled;
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028import org.apache.avalon.framework.service.Serviceable;
029import org.apache.commons.collections.PredicateUtils;
030
031import org.ametys.cms.content.archive.ArchiveConstants;
032import org.ametys.core.observation.Event;
033import org.ametys.core.observation.Observer;
034import org.ametys.web.ObservationConstants;
035import org.ametys.web.repository.site.Site;
036
037/**
038 * {@link Observer} for observing site addition in order to synchronize archives workspace.
039 */
040public class SynchronizeArchivesSiteAdditionObserver extends AbstractLogEnabled implements Observer, Serviceable
041{
042    /** JCR repository. */
043    protected Repository _repository;
044    /** The {@link SynchronizeComponent} */
045    protected SynchronizeComponent _synchronizeComponent;
046
047    @Override
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        _repository = (Repository) manager.lookup(Repository.class.getName());
051        _synchronizeComponent = (SynchronizeComponent) manager.lookup(SynchronizeComponent.ROLE);
052    }
053    
054    @Override
055    public boolean supports(Event event)
056    {
057        return event.getId().equals(ObservationConstants.EVENT_SITE_ADDED);
058    }
059
060    @Override
061    public int getPriority(Event event)
062    {
063        return MAX_PRIORITY + 1000;
064    }
065    
066    @Override
067    public void observe(Event event, Map<String, Object> transientVars) throws Exception
068    {
069        Session archiveSession = null; 
070        
071        try
072        {
073            // Open a session to the workspace archives
074            archiveSession = _repository.login(ArchiveConstants.ARCHIVE_WORKSPACE);
075            
076            Map<String, Object> arguments = event.getArguments();
077            Site site = (Site) arguments.get(ObservationConstants.ARGS_SITE);
078            
079            Node siteNode = site.getNode();
080            String siteNodePath = siteNode.getPath().substring(1);
081            Node archivesRootNode = archiveSession.getRootNode();
082            
083            if (!archivesRootNode.hasNode(siteNodePath))
084            {
085                Node sitesNode = _synchronizeComponent.cloneAncestorsAndPreserveUUID(siteNode, archiveSession);
086                Node clonedSiteNode = _synchronizeComponent.addNodeWithUUID(siteNode, sitesNode, siteNode.getName());
087                
088                _synchronizeComponent.cloneNodeAndPreserveUUID(siteNode, clonedSiteNode, PredicateUtils.truePredicate(), PredicateUtils.falsePredicate());
089            }
090            
091            archiveSession.save();
092        }
093        catch (RepositoryException e)
094        {
095            getLogger().error("Unable to synchronize archives workspace with event: " + event, e);
096        }
097        finally
098        {
099            if (archiveSession != null)
100            {
101                archiveSession.logout();
102            }
103        }
104        
105    }
106
107}