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.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.ObservationConstants;
029import org.ametys.cms.repository.Content;
030import org.ametys.cms.tag.jcr.TaggableAmetysObjectHelper;
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;
035
036/**
037 * {@link Observer} for observing content validation in order to synchronize live workspace.
038 */
039public class SynchronizeContentTaggedObserver extends AbstractSynchronizeObserver
040{
041    /** Ametys object resolver. */
042    protected AmetysObjectResolver _ametysObjectResolver;
043
044    @Override
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        super.service(manager);
048        _ametysObjectResolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
049    }
050    
051    @Override
052    public boolean supports(Event event)
053    {
054        return event.getId().equals(ObservationConstants.EVENT_CONTENT_TAGGED);
055    }
056    
057    @Override
058    protected void _internalObserve(Event event, Session liveSession) throws RepositoryException
059    {
060        Map<String, Object> arguments = event.getArguments();
061        Content content = (Content) arguments.get(ObservationConstants.ARGS_CONTENT);
062        
063        if (!(content instanceof JCRAmetysObject))
064        {
065            return;
066        }
067        
068        Node node = ((JCRAmetysObject) content).getNode();
069        
070        try
071        {
072            Node clonedNode = liveSession.getNodeByIdentifier(node.getIdentifier());
073            
074            String tagProperty = TaggableAmetysObjectHelper.DEFAULT_METADATA_TAGS;
075            
076            if (node.hasProperty(tagProperty))
077            {
078                clonedNode.setProperty(tagProperty, node.getProperty(tagProperty).getValues());
079            }
080        }
081        catch (ItemNotFoundException e)
082        {
083            // content does not exist in the live workspace
084        }
085        
086        if (liveSession.hasPendingChanges())
087        {
088            liveSession.save();
089        }
090    }
091}