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 javax.jcr.Node;
021import javax.jcr.PropertyIterator;
022import javax.jcr.RepositoryException;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026
027import org.ametys.cms.ObservationConstants;
028import org.ametys.cms.repository.Content;
029import org.ametys.core.observation.Event;
030import org.ametys.plugins.repository.AmetysObject;
031import org.ametys.plugins.repository.AmetysObjectResolver;
032import org.ametys.plugins.repository.jcr.JCRAmetysObject;
033import org.ametys.web.repository.content.WebContent;
034import org.ametys.web.repository.page.ZoneItem;
035
036/**
037 * Abstract class for observer related to the management of the ZoneItems cache for content.
038 */
039public abstract class AbstractZoneItemCacheOnContentObserver extends AbstractZoneItemCacheObserver
040{
041    /** The Ametys object resolver */
042    protected AmetysObjectResolver _resolver;
043    
044    @Override
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        super.service(manager);
048        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
049    }
050    
051    @Override
052    public void observe(Event event, Map<String, Object> transientVars) throws Exception
053    {
054        Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
055        
056        if (content instanceof JCRAmetysObject && content instanceof WebContent)
057        {
058            try
059            {
060                String site = ((WebContent) content).getSiteName();
061                        
062                PropertyIterator it = ((JCRAmetysObject) content).getNode().getReferences();
063                while (it.hasNext())
064                {
065                    Node refererNode = it.nextProperty().getParent();
066                    
067                    AmetysObject ao = _resolver.resolve(refererNode, true);
068                    
069                    if (ao != null && ao instanceof ZoneItem)
070                    {
071                        _zoneItemCache.removeItem(getWorkspace(), site, "CONTENT", ao.getId());
072                    }
073                }
074            }
075            catch (RepositoryException e)
076            {
077                getLogger().error("Unable to get referencing ZoneItems for content '" + content.getId() + "' while processing event " + event, e);
078            }
079        }
080    }
081    
082    /**
083     * Get the target JCR workspace on which the zone item cache will be deleted.
084     * @return the target JCR workspace : "live", "default" or null for all workspaces
085     */
086    protected String getWorkspace()
087    {
088        // all workspaces
089        return null;
090    }
091}