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 */
016
017package org.ametys.web.synchronization;
018
019import java.util.Map;
020
021import javax.jcr.ItemNotFoundException;
022import javax.jcr.Node;
023import javax.jcr.PathNotFoundException;
024import javax.jcr.RepositoryException;
025import javax.jcr.Session;
026
027import org.apache.avalon.framework.service.ServiceException;
028import org.apache.avalon.framework.service.ServiceManager;
029import org.apache.commons.collections.PredicateUtils;
030
031import org.ametys.cms.ObservationConstants;
032import org.ametys.cms.repository.Content;
033import org.ametys.cms.repository.comment.Comment;
034import org.ametys.core.observation.Event;
035import org.ametys.plugins.repository.AmetysObjectResolver;
036import org.ametys.plugins.repository.jcr.JCRAmetysObject;
037import org.ametys.plugins.repository.metadata.ModifiableCompositeMetadata;
038import org.ametys.plugins.repository.metadata.jcr.JCRCompositeMetadata;
039
040/**
041 * When a comment (validated) is modifier, synchronize it
042 */
043public class SynchronizeContentCommentedObserver extends AbstractSynchronizeObserver
044{
045    /** Ametys object resolver. */
046    protected AmetysObjectResolver _ametysObjectResolver;
047
048    @Override
049    public void service(ServiceManager manager) throws ServiceException
050    {
051        super.service(manager);
052        _ametysObjectResolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
053    }
054    
055    @Override
056    public boolean supports(Event event)
057    {
058        return event.getId().equals(ObservationConstants.EVENT_CONTENT_COMMENT_VALIDATED);
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(ObservationConstants.ARGS_CONTENT);
066        Comment comment = (Comment) arguments.get(ObservationConstants.ARGS_COMMENT);
067        
068        ModifiableCompositeMetadata metadata = comment.getMetadata();
069        
070        if (!(metadata instanceof JCRCompositeMetadata))
071        {
072            return;
073        }
074        
075        Node node = ((JCRAmetysObject) content).getNode();
076        Node initialCommentNode = ((JCRCompositeMetadata) metadata).getNode();
077        
078        try
079        {
080            // let's check that content is in live
081            Node liveContentNode = liveSession.getNodeByIdentifier(node.getIdentifier());
082            
083            try
084            {
085                Node unversionnedMetadata = liveContentNode.getNode("ametys-internal:unversioned");
086                Node commentsNode = unversionnedMetadata.getNode(JCRCompositeMetadata.METADATA_PREFIX + Comment.METADATA_COMMENTS);
087                Node commentNode = commentsNode.getNode(JCRCompositeMetadata.METADATA_PREFIX + comment.getId());
088                
089                commentNode.remove();
090                
091                Node clonedNode = _synchronizeComponent.addNodeWithUUID(initialCommentNode, commentsNode, initialCommentNode.getName());
092                _synchronizeComponent.cloneNodeAndPreserveUUID(initialCommentNode, clonedNode, PredicateUtils.truePredicate(), PredicateUtils.truePredicate());
093            }
094            catch (PathNotFoundException e)
095            {
096                // comment was not already existing
097                Node clonedParentNode = _synchronizeComponent.cloneAncestorsAndPreserveUUID(initialCommentNode, liveSession);
098                Node clonedNode = _synchronizeComponent.addNodeWithUUID(initialCommentNode, clonedParentNode, initialCommentNode.getName());
099                _synchronizeComponent.cloneNodeAndPreserveUUID(initialCommentNode, clonedNode, PredicateUtils.truePredicate(), PredicateUtils.truePredicate());
100            }
101        }
102        catch (ItemNotFoundException e)
103        {
104            // content does not exist in the live workspace
105        }
106        
107        if (liveSession.hasPendingChanges())
108        {
109            liveSession.save();
110        }
111    }
112}