001/* 002 * Copyright 2013 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.RepositoryException; 023import javax.jcr.Session; 024 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027 028import org.ametys.cms.repository.Content; 029import org.ametys.core.observation.Event; 030import org.ametys.core.observation.Observer; 031import org.ametys.plugins.repository.AmetysObjectResolver; 032import org.ametys.plugins.repository.RepositoryConstants; 033import org.ametys.plugins.repository.jcr.JCRAmetysObject; 034import org.ametys.web.ObservationConstants; 035 036/** 037 * {@link Observer} monitoring changes on content privacy in order to synchronize live workspace. 038 */ 039public class SynchronizeContentPrivacyChangeObserver extends AbstractSynchronizeObserver 040{ 041 042 private static final String _PRIVACY_PROPERTY = RepositoryConstants.NAMESPACE_PREFIX + ":privacy"; 043 private static final String _UNVERSIONED_NODE = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":unversioned"; 044 045 /** Ametys object resolver. */ 046 protected AmetysObjectResolver _resolver; 047 048 @Override 049 public void service(ServiceManager manager) throws ServiceException 050 { 051 super.service(manager); 052 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 053 } 054 055 @Override 056 public boolean supports(Event event) 057 { 058 return event.getId().equals(ObservationConstants.EVENT_CONTENT_PRIVACY_CHANGED); 059 } 060 061 @Override 062 protected void _internalObserve(Event event, Session liveSession) throws RepositoryException 063 { 064 Map<String, Object> arguments = event.getArguments(); 065 Content content = (Content) arguments.get(org.ametys.cms.ObservationConstants.ARGS_CONTENT); 066 067 if (!(content instanceof JCRAmetysObject jcrContent)) 068 { 069 return; 070 } 071 072 Node node = jcrContent.getNode(); 073 Node unversionedNode = node.getNode(_UNVERSIONED_NODE); 074 075 try 076 { 077 Node clonedNode = liveSession.getNodeByIdentifier(node.getIdentifier()); 078 Node clonedUnversionedNode = clonedNode.getNode(_UNVERSIONED_NODE); 079 080 if (unversionedNode.hasProperty(_PRIVACY_PROPERTY)) 081 { 082 clonedUnversionedNode.setProperty(_PRIVACY_PROPERTY, unversionedNode.getProperty(_PRIVACY_PROPERTY).getValue()); 083 } 084 } 085 catch (ItemNotFoundException e) 086 { 087 // The content does not exist in the live workspace. 088 } 089 090 if (liveSession.hasPendingChanges()) 091 { 092 liveSession.save(); 093 } 094 } 095 096}