001/*
002 *  Copyright 2016 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.RepositoryException;
021import javax.jcr.Session;
022
023import org.ametys.core.ObservationConstants;
024import org.ametys.core.observation.Event;
025import org.ametys.plugins.repository.ACLAmetysObject;
026import org.ametys.plugins.repository.jcr.JCRAmetysObject;
027
028/**
029 * This observer synchronize the ACL for JCR objects to live
030 */
031public class SynchronizeACLObserver extends AbstractSynchronizeObserver
032{
033
034    public boolean supports(Event event)
035    {
036        return event.getId().equals(ObservationConstants.EVENT_ACL_UPDATED);
037    }
038    
039    @Override
040    protected void _internalObserve(Event event, Session liveSession) throws RepositoryException
041    {
042        Map<String, Object> arguments = event.getArguments();
043        Object aclContext = arguments.get(ObservationConstants.ARGS_ACL_CONTEXT);
044        if (aclContext instanceof JCRAmetysObject && aclContext instanceof ACLAmetysObject)
045        {
046            _synchronizeComponent.synchronizeACL(((JCRAmetysObject) aclContext).getNode(), liveSession);
047        }
048        
049        if (liveSession.hasPendingChanges())
050        {
051            liveSession.save();
052        }
053    }
054
055}