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.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.repository.comment.Comment;
031import org.ametys.core.observation.Event;
032import org.ametys.plugins.repository.AmetysObjectResolver;
033import org.ametys.plugins.repository.AmetysRepositoryException;
034import org.ametys.plugins.repository.jcr.JCRAmetysObject;
035import org.ametys.plugins.repository.metadata.jcr.JCRCompositeMetadata;
036
037/**
038 * When a comment (validated) is modifier, synchronize it
039 */
040public class SynchronizeContentUncommentedObserver extends AbstractSynchronizeObserver
041{
042    /** Ametys object resolver. */
043    protected AmetysObjectResolver _ametysObjectResolver;
044
045    @Override
046    public void service(ServiceManager manager) throws ServiceException
047    {
048        super.service(manager);
049        _ametysObjectResolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
050    }
051    
052    @Override
053    public boolean supports(Event event)
054    {
055        return event.getId().equals(ObservationConstants.EVENT_CONTENT_COMMENT_UNVALIDATED);
056    }
057    
058    @Override
059    protected void _internalObserve(Event event, Session liveSession) throws RepositoryException
060    {
061        Map<String, Object> arguments = event.getArguments();
062        Content content = (Content) arguments.get(ObservationConstants.ARGS_CONTENT);
063        Comment comment = (Comment) arguments.get(ObservationConstants.ARGS_COMMENT);
064
065        if (!(content instanceof JCRAmetysObject))
066        {
067            return;
068        }
069        
070        Node node = ((JCRAmetysObject) content).getNode();
071
072        try
073        {
074            Node liveContentNode = liveSession.getNodeByIdentifier(node.getIdentifier());
075            
076            Node unversionnedMetadata = liveContentNode.getNode("ametys-internal:unversioned");
077            Node commentsNode = unversionnedMetadata.getNode(JCRCompositeMetadata.METADATA_PREFIX + Comment.METADATA_COMMENTS);
078            Node commentNode = commentsNode.getNode(JCRCompositeMetadata.METADATA_PREFIX + comment.getId());
079            
080            commentNode.remove();
081
082            liveSession.save();
083        }
084        catch (AmetysRepositoryException e)
085        {
086            // content is not validated.. nothing to do
087        }
088    }
089}