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 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 * Invalidates the ZoneItem containing a Content which has just been commented. 038 */ 039public class InvalidateZoneItemCacheOnContentCommentedObserver extends AbstractZoneItemCacheObserver 040{ 041 private AmetysObjectResolver _resolver; 042 043 @Override 044 public void service(ServiceManager manager) throws ServiceException 045 { 046 super.service(manager); 047 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 048 } 049 050 @Override 051 public boolean supports(Event event) 052 { 053 return event.getId().equals(ObservationConstants.EVENT_CONTENT_COMMENT_VALIDATED) 054 || event.getId().equals(ObservationConstants.EVENT_CONTENT_COMMENT_UNVALIDATED); 055 } 056 057 @Override 058 public void observe(Event event, Map<String, Object> transientVars) throws Exception 059 { 060 Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT); 061 062 if (content instanceof JCRAmetysObject && content instanceof WebContent) 063 { 064 try 065 { 066 String site = ((WebContent) content).getSiteName(); 067 068 PropertyIterator it = ((JCRAmetysObject) content).getNode().getReferences(); 069 while (it.hasNext()) 070 { 071 Node refererNode = it.nextProperty().getParent(); 072 073 AmetysObject ao = _resolver.resolve(refererNode, true); 074 075 if (ao != null && ao instanceof ZoneItem) 076 { 077 _zoneItemCache.removeItem(null, site, "CONTENT", ao.getId()); 078 } 079 } 080 } 081 catch (RepositoryException e) 082 { 083 getLogger().error("Unable to get referencing ZoneItems for content '" + content.getId() + "' while processing event " + event, e); 084 } 085 } 086 087 } 088}