001/*
002 *  Copyright 2012 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.repository.content.shared;
017
018import java.util.Map;
019import java.util.Set;
020
021import org.apache.avalon.framework.logger.AbstractLogEnabled;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025
026import org.ametys.cms.ObservationConstants;
027import org.ametys.cms.repository.Content;
028import org.ametys.cms.repository.DefaultContent;
029import org.ametys.core.observation.Event;
030import org.ametys.core.observation.Observer;
031import org.ametys.plugins.repository.AmetysObjectResolver;
032import org.ametys.web.repository.content.SharedContent;
033import org.ametys.web.repository.content.jcr.DefaultSharedContent;
034
035/**
036 * Observes contents validation and depublication to modify shared contents accordingly.
037 */
038public class SharedContentValidationObserver extends AbstractLogEnabled implements Observer, Serviceable
039{
040    
041    /** The ametys object resolver. */
042    protected AmetysObjectResolver _resolver;
043    
044    /** The shared content helper. */
045    protected SharedContentManager _sharedContentComponent;
046    
047    @Override
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
051        _sharedContentComponent = (SharedContentManager) manager.lookup(SharedContentManager.ROLE);
052    }
053    
054    @Override
055    public boolean supports(Event event)
056    {
057        return event.getId().equals(ObservationConstants.EVENT_CONTENT_VALIDATED)
058            || event.getId().equals(ObservationConstants.EVENT_CONTENT_UNTAG_LIVE);
059    }
060    
061    @Override
062    public int getPriority(Event event)
063    {
064        return MAX_PRIORITY + 4000;
065    }
066    
067    @Override
068    public void observe(Event event, Map<String, Object> transientVars) throws Exception
069    {
070        Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
071        
072        if (content instanceof DefaultContent && !(content instanceof SharedContent))
073        {
074            // Resolve the content to switch its revision without impacting the original object.
075            DefaultContent originalContent = (DefaultContent) content;
076            
077            Set<SharedContent> sharedContents = _sharedContentComponent.getSharedContents(originalContent);
078            for (SharedContent sharedContent : sharedContents)
079            {
080                if (sharedContent instanceof DefaultSharedContent)
081                {
082                    DefaultSharedContent defaultSharedContent = (DefaultSharedContent) sharedContent;
083                    if (event.getId().equals(ObservationConstants.EVENT_CONTENT_VALIDATED))
084                    {
085                        _sharedContentComponent.copyContentData(originalContent, defaultSharedContent);
086                        _sharedContentComponent.validateContent(defaultSharedContent);
087                    }
088                    else if (event.getId().equals(ObservationConstants.EVENT_CONTENT_UNTAG_LIVE))
089                    {
090                        _sharedContentComponent.invalidateSharedContent(defaultSharedContent);
091                    }
092                }
093            }
094        }
095    }
096
097}