001/*
002 *  Copyright 2013 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.ItemNotFoundException;
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;
027
028import org.ametys.cms.repository.Content;
029import org.ametys.core.observation.Event;
030import org.ametys.core.observation.Observer;
031import org.ametys.plugins.repository.AmetysObjectResolver;
032import org.ametys.plugins.repository.jcr.JCRAmetysObject;
033import org.ametys.web.ObservationConstants;
034
035/**
036 * {@link Observer} monitoring changes on content privacy in order to synchronize live workspace.
037 */
038public class SynchronizeContentPrivacyChangeObserver extends AbstractSynchronizeObserver
039{
040    
041    private static final String _PRIVACY_PROPERTY = "ametys:privacy";
042    
043    /** Ametys object resolver. */
044    protected AmetysObjectResolver _resolver;
045    
046    @Override
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        super.service(manager);
050        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
051    }
052    
053    @Override
054    public boolean supports(Event event)
055    {
056        return event.getId().equals(ObservationConstants.EVENT_CONTENT_PRIVACY_CHANGED);
057    }
058    
059    @Override
060    protected void _internalObserve(Event event, Session liveSession) throws RepositoryException
061    {
062        Map<String, Object> arguments = event.getArguments();
063        Content content = (Content) arguments.get(org.ametys.cms.ObservationConstants.ARGS_CONTENT);
064        
065        if (!(content instanceof JCRAmetysObject))
066        {
067            return;
068        }
069        
070        Node node = ((JCRAmetysObject) content).getNode();
071        
072        try
073        {
074            Node clonedNode = liveSession.getNodeByIdentifier(node.getIdentifier());
075            
076            if (node.hasProperty(_PRIVACY_PROPERTY))
077            {
078                clonedNode.setProperty(_PRIVACY_PROPERTY, node.getProperty(_PRIVACY_PROPERTY).getValue());
079            }
080        }
081        catch (ItemNotFoundException e)
082        {
083            // The content does not exist in the live workspace.
084        }
085        
086        if (liveSession.hasPendingChanges())
087        {
088            liveSession.save();
089        }
090    }
091    
092}