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 org.apache.avalon.framework.logger.AbstractLogEnabled;
019import org.apache.avalon.framework.service.ServiceException;
020import org.apache.avalon.framework.service.ServiceManager;
021import org.apache.avalon.framework.service.Serviceable;
022import org.apache.commons.lang.ArrayUtils;
023
024import org.ametys.cms.CmsConstants;
025import org.ametys.cms.repository.Content;
026import org.ametys.plugins.repository.version.VersionAwareAmetysObject;
027import org.ametys.web.repository.content.jcr.DefaultSharedContent;
028import org.ametys.web.repository.page.Page;
029import org.ametys.web.repository.site.Site;
030import org.ametys.web.site.CopyUpdater;
031
032/**
033 * Updates copied shared contents, validating it if their initial content is live.
034 */
035public class SharedContentCopyUpdater extends AbstractLogEnabled implements CopyUpdater, Serviceable
036{
037    
038    /** The shared content manager. */
039    protected SharedContentManager _sharedContentManager;
040    
041    @Override
042    public void service(ServiceManager serviceManager) throws ServiceException
043    {
044        _sharedContentManager = (SharedContentManager) serviceManager.lookup(SharedContentManager.ROLE);
045    }
046
047    @Override
048    public void updateContent(Site initialSite, Site createdSite, Content initialContent, Content createdContent)
049    {
050        if (createdContent instanceof DefaultSharedContent)
051        {
052            DefaultSharedContent sharedContent = (DefaultSharedContent) createdContent;
053            
054            // Create a live version for the shared content if the initial content has one.
055            Content referenceContent = sharedContent.getInitialContent();
056            if (referenceContent != null && referenceContent instanceof VersionAwareAmetysObject)
057            {
058                VersionAwareAmetysObject vaContent = (VersionAwareAmetysObject) referenceContent;
059                
060                if (ArrayUtils.contains(vaContent.getAllLabels(), CmsConstants.LIVE_LABEL))
061                {
062                    _sharedContentManager.validateContent(sharedContent);
063                }
064            }
065        }
066    }
067
068    @Override
069    public void updatePage(Site initialSite, Site createdSite, Page page)
070    {
071        // Nothing to do here.
072    }
073    
074    @Override
075    public void updateSite(Site initialSite, Site createdSite)
076    {
077        // Nothing to do here.
078    }
079
080}