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.synchronization;
017
018import java.util.Map;
019
020import javax.jcr.Node;
021import javax.jcr.RepositoryException;
022import javax.jcr.Session;
023
024import org.ametys.core.observation.Event;
025import org.ametys.core.observation.Observer;
026import org.ametys.plugins.repository.RepositoryConstants;
027import org.ametys.plugins.repository.jcr.JCRAmetysObject;
028import org.ametys.web.ObservationConstants;
029import org.ametys.web.repository.page.SitemapElement;
030import org.ametys.web.repository.page.jcr.DefaultPage;
031import org.ametys.web.repository.sitemap.Sitemap;
032
033/**
034 * {@link Observer} for observing structural page changes (page added, page type changed) in order to synchronize live workspace.
035 */
036public class SynchronizeRobotsChangeObserver extends AbstractSynchronizeObserver
037{
038    public boolean supports(Event event)
039    {
040        return event.getId().equals(ObservationConstants.EVENT_ROBOTS_CHANGED);
041    }
042    
043    @Override
044    protected void _internalObserve(Event event, Session liveSession) throws RepositoryException
045    {
046        Map<String, Object> arguments = event.getArguments();
047        
048        SitemapElement pageOrSitemap = (SitemapElement) arguments.get(ObservationConstants.ARGS_SITEMAP_ELEMENT);
049        if (pageOrSitemap == null)
050        {
051            pageOrSitemap = (Sitemap) arguments.get(ObservationConstants.ARGS_SITEMAP);
052        }
053        
054        if (pageOrSitemap instanceof JCRAmetysObject)
055        {
056            Node node = ((JCRAmetysObject) pageOrSitemap).getNode();
057            
058            Node liveNode = (Node) liveSession.getItem(node.getPath());
059            String propertyName = RepositoryConstants.NAMESPACE_PREFIX + ":" + DefaultPage.METADATA_ROBOTS_DISALLOW;
060            liveNode.setProperty(propertyName, node.getProperty(propertyName).getBoolean());
061            
062            if (liveSession.hasPendingChanges())
063            {
064                liveSession.save();
065            }
066        }
067    }
068    
069}