001/*
002 *  Copyright 2012 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.cache.zoneitem;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.core.observation.Event;
024import org.ametys.plugins.repository.AmetysObjectResolver;
025import org.ametys.plugins.repository.AmetysRepositoryException;
026import org.ametys.web.ObservationConstants;
027import org.ametys.web.repository.page.Page;
028import org.ametys.web.repository.page.ZoneItem;
029import org.ametys.web.repository.page.ZoneItem.ZoneType;
030
031/**
032 * Invalidates a ZoneItem when it has been moved.
033 */
034public class InvalidateZoneItemCacheOnZoneItemMoveObserver extends AbstractZoneItemCacheObserver
035{
036    
037    private AmetysObjectResolver _resolver;
038    
039    @Override
040    public void service(ServiceManager manager) throws ServiceException
041    {
042        super.service(manager);
043        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
044    }
045    
046    @Override
047    public boolean supports(Event event)
048    {
049        return event.getId().equals(ObservationConstants.EVENT_ZONEITEM_MOVED);
050    }
051    
052    @Override
053    public void observe(Event event, Map<String, Object> transientVars) throws Exception
054    {
055        Map<String, Object> args = event.getArguments();
056        Page page = (Page) args.get(ObservationConstants.ARGS_PAGE);
057        String zoneItemId = (String) args.get(ObservationConstants.ARGS_ZONE_ITEM_ID);
058        
059        try
060        {
061            String siteName = page.getSiteName();
062            
063            ZoneItem zoneItem = _resolver.resolveById(zoneItemId);
064            
065            // Compute the page element type.
066            String pageElementType = zoneItem.getType().equals(ZoneType.CONTENT) ? "CONTENT" : "SERVICE:" + zoneItem.getServiceId();
067            
068            // Remove the item.
069            _zoneItemCache.removeItem(null, siteName, pageElementType, zoneItemId);
070        }
071        catch (AmetysRepositoryException e)
072        {
073            getLogger().error("Unable to get site name from page '" + page.getId() + "' while processing event " + event, e);
074        }
075        
076    }
077}