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 SynchronizeArchivesSiteChangeObserver 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_UPDATED);
058    }
059
060    @Override
061    public int getPriority(Event event)
062    {
063        return MAX_PRIORITY + 1000;
064    }
065
066    public void observe(Event event, Map<String, Object> transientVars) throws Exception
067    {
068        Session archiveSession = null; 
069        
070        try
071        {
072            // Open a session to the workspace archives
073            archiveSession = _repository.login(ArchiveConstants.ARCHIVE_WORKSPACE);
074            
075            Map<String, Object> arguments = event.getArguments();
076            Site site = (Site) arguments.get(ObservationConstants.ARGS_SITE);
077            
078            Node siteNode = site.getNode();
079            String siteNodePath = siteNode.getPath();
080            
081            if (archiveSession.itemExists(siteNodePath))
082            {
083                Node clonedSiteNode = (Node) archiveSession.getItem(siteNodePath);
084                
085                _synchronizeComponent.cloneNodeAndPreserveUUID(siteNode, clonedSiteNode, PredicateUtils.truePredicate(), PredicateUtils.falsePredicate());
086                
087                archiveSession.save();
088            }
089        }
090        catch (RepositoryException e)
091        {
092            getLogger().error("Unable to synchronize archives workspace with event: " + event, e);
093        }
094        finally
095        {
096            if (archiveSession != null)
097            {
098                archiveSession.logout();
099            }
100        }
101        
102    }
103
104}