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.RepositoryException;
021import javax.jcr.Session;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025
026import org.ametys.core.observation.Event;
027import org.ametys.plugins.repository.jcr.JCRAmetysObject;
028import org.ametys.web.ObservationConstants;
029import org.ametys.web.repository.page.Page;
030import org.ametys.web.repository.page.ZoneItem;
031import org.ametys.web.skin.Skin;
032import org.ametys.web.skin.SkinsManager;
033
034/**
035 * {@link AbstractSynchronizeObserver} for observing a page event.
036 */
037public abstract class AbstractSynchronizePageObserver extends AbstractSynchronizeObserver
038{
039    /** the {@link SkinsManager} */
040    protected SkinsManager _skinsManager;
041    /** the {@link SynchronizeComponent} */
042    
043    @Override
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        super.service(manager);
047        _skinsManager = (SkinsManager) manager.lookup(SkinsManager.ROLE);
048    }
049    
050    @Override
051    public boolean supports(Event event)
052    {
053        Map<String, Object> arguments = event.getArguments();
054        Page page = (Page) arguments.get(ObservationConstants.ARGS_PAGE);
055        ZoneItem zoneItem = (ZoneItem) arguments.get(ObservationConstants.ARGS_ZONE_ITEM);
056        
057        if (page != null)
058        {
059            return _internalSupport(event, page);
060        }
061        else if (zoneItem != null)
062        {
063            return _internalSupport(event, zoneItem.getZone().getPage());
064        }
065
066        return false;
067    }
068
069    /**
070     * Checks if the page event is supported.
071     * @param event the event.
072     * @param target the page target.
073     * @return <code>true</code> for observing this event,
074     *         <code>false</code> otherwise.
075     */
076    protected abstract boolean _internalSupport(Event event, Page target);
077    
078    @Override
079    protected void _internalObserve(Event event, Session liveSession) throws RepositoryException
080    {
081        Map<String, Object> arguments = event.getArguments();
082        ZoneItem zoneItem = (ZoneItem) arguments.get(ObservationConstants.ARGS_ZONE_ITEM);
083        
084        Page page = null;
085        if (zoneItem != null)
086        {
087            page = zoneItem.getZone().getPage();
088        }
089        else
090        {
091            page = (Page) arguments.get(ObservationConstants.ARGS_PAGE);
092        }
093        
094        if (page instanceof JCRAmetysObject)
095        {
096            Skin skin = _skinsManager.getSkin(page.getSite().getSkinId());
097            _internalObservePage(event, page, skin, liveSession);
098        }
099    }
100
101    /**
102     * Observes the page event with access to the workspace live.
103     * @param event the event.
104     * @param target the page target.
105     * @param skin the skin of the page's site.
106     * @param liveSession the session to the workspace live.
107     * @throws RepositoryException if an error occurs.
108     */
109    protected abstract void _internalObservePage(Event event, Page target, Skin skin, Session liveSession) throws RepositoryException;
110}