001/* 002 * Copyright 2017 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.plugins.odfweb.observation; 017 018import javax.jcr.ItemNotFoundException; 019import javax.jcr.Node; 020import javax.jcr.RepositoryException; 021import javax.jcr.Session; 022 023import org.apache.commons.collections.PredicateUtils; 024import org.ametys.cms.ObservationConstants; 025import org.ametys.cms.repository.Content; 026import org.ametys.core.observation.Event; 027import org.ametys.core.observation.Observer; 028import org.ametys.odf.observation.OdfObservationConstants; 029import org.ametys.plugins.repository.RepositoryConstants; 030import org.ametys.plugins.repository.jcr.JCRAmetysObject; 031import org.ametys.web.synchronization.AbstractSynchronizeObserver; 032 033 034/** 035 * {@link Observer} for observing content translation in order to synchronize live workspace. 036 */ 037public class SynchronizedContentTranslatedObserver extends AbstractSynchronizeObserver 038{ 039 @Override 040 public boolean supports(Event event) 041 { 042 return event.getId().equals(OdfObservationConstants.ODF_CONTENT_TRANSLATED); 043 } 044 045 @Override 046 protected void _internalObserve(Event event, Session liveSession) throws RepositoryException 047 { 048 Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT); 049 050 if (!(content instanceof JCRAmetysObject)) 051 { 052 return; 053 } 054 055 Node node = ((JCRAmetysObject) content).getNode(); 056 057 String translationNodeName = RepositoryConstants.NAMESPACE_PREFIX + ":translations"; 058 059 try 060 { 061 Node clonedNode = liveSession.getNodeByIdentifier(node.getIdentifier()); 062 Node translationsNode = node.getNode(translationNodeName); 063 064 Node liveTranslationsNode = null; 065 if (clonedNode.hasNode(translationNodeName)) 066 { 067 liveTranslationsNode = clonedNode.getNode(translationNodeName); 068 } 069 else 070 { 071 liveTranslationsNode = clonedNode.addNode(translationNodeName, "ametys:compositeMetadata"); 072 } 073 074 _synchronizeComponent.cloneNodeAndPreserveUUID(translationsNode, liveTranslationsNode, PredicateUtils.truePredicate(), PredicateUtils.falsePredicate()); 075 } 076 catch (ItemNotFoundException e) 077 { 078 // content does not exist in the live workspace 079 } 080 081 if (liveSession.hasPendingChanges()) 082 { 083 liveSession.save(); 084 } 085 086 } 087}