001/*
002 *  Copyright 2019 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 * Abstract class for observer related to the management of the ZoneItems cache for zone item modification.
033 */
034public abstract class AbstractZoneItemCacheOnZoneItemObserver 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 void observe(Event event, Map<String, Object> transientVars) throws Exception
048    {
049        Map<String, Object> args = event.getArguments();
050        Page page = (Page) args.get(ObservationConstants.ARGS_PAGE);
051        String zoneItemId = (String) args.get(ObservationConstants.ARGS_ZONE_ITEM_ID);
052        
053        try
054        {
055            String siteName = page.getSiteName();
056            
057            ZoneItem zoneItem = _resolver.resolveById(zoneItemId);
058            
059            // Compute the page element type.
060            String pageElementType = zoneItem.getType().equals(ZoneType.CONTENT) ? "CONTENT" : "SERVICE:" + zoneItem.getServiceId();
061            
062            // Remove the item.
063            _zoneItemCache.removeItem(null, siteName, pageElementType, zoneItemId);
064        }
065        catch (AmetysRepositoryException e)
066        {
067            getLogger().error("Unable to get site name from page '" + page.getId() + "' while processing event " + event, e);
068        }
069        
070    }
071}