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