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 java.util.Map; 019 020import javax.jcr.ItemNotFoundException; 021import javax.jcr.Node; 022import javax.jcr.PropertyIterator; 023import javax.jcr.RepositoryException; 024import javax.jcr.Session; 025 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028 029import org.ametys.cms.ObservationConstants; 030import org.ametys.cms.repository.Content; 031import org.ametys.core.observation.Event; 032import org.ametys.core.observation.Observer; 033import org.ametys.plugins.repository.AmetysObjectResolver; 034import org.ametys.plugins.repository.jcr.JCRAmetysObject; 035import org.ametys.web.repository.page.Page; 036import org.ametys.web.repository.page.jcr.DefaultZone; 037import org.ametys.web.skin.Skin; 038import org.ametys.web.skin.SkinsManager; 039 040/** 041 * {@link Observer} for observing content validation in order to synchronize live workspace. 042 */ 043public class SynchronizeContentAdditionObserver extends AbstractSynchronizeObserver 044{ 045 /** Ametys object resolver. */ 046 protected AmetysObjectResolver _ametysObjectResolver; 047 private SkinsManager _skinsManager; 048 049 @Override 050 public void service(ServiceManager manager) throws ServiceException 051 { 052 super.service(manager); 053 _ametysObjectResolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 054 _skinsManager = (SkinsManager) manager.lookup(SkinsManager.ROLE); 055 } 056 057 @Override 058 public boolean supports(Event event) 059 { 060 return event.getId().equals(ObservationConstants.EVENT_CONTENT_ADDED); 061 } 062 063 @Override 064 protected void _internalObserve(Event event, Session liveSession) throws RepositoryException 065 { 066 Map<String, Object> arguments = event.getArguments(); 067 Content content = (Content) arguments.get(ObservationConstants.ARGS_CONTENT); 068 069 if (!(content instanceof JCRAmetysObject)) 070 { 071 return; 072 } 073 074 Node node = ((JCRAmetysObject) content).getNode(); 075 076 try 077 { 078 // this call will fail if the node does not exist 079 liveSession.getNodeByIdentifier(node.getIdentifier()); 080 081 // Fixme the page incriminated in the array of args 082 083 // Node is in the live workspace, need to be linked with referencing pages 084 PropertyIterator itReferences = ((JCRAmetysObject) content).getNode().getReferences(); 085 086 while (itReferences.hasNext()) 087 { 088 Node refererNode = itReferences.nextProperty().getParent(); 089 if (refererNode.getPrimaryNodeType().getName().equals(DefaultZone.ZONEITEM_NODE_NAME)) 090 { 091 Node pageNode = refererNode.getParent().getParent().getParent().getParent(); 092 Page page = _ametysObjectResolver.resolve(pageNode, false); 093 Skin skin = _skinsManager.getSkin(page.getSite().getSkinId()); 094 095 _synchronizeComponent.synchronizePage(page, skin, liveSession); 096 } 097 } 098 } 099 catch (ItemNotFoundException e) 100 { 101 // content does not exist in the live workspace 102 } 103 104 if (liveSession.hasPendingChanges()) 105 { 106 liveSession.save(); 107 } 108 } 109}