001/*
002 *  Copyright 2011 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.plugins.newsletter;
017
018import java.util.Arrays;
019
020import org.apache.avalon.framework.logger.AbstractLogEnabled;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024import org.apache.avalon.framework.thread.ThreadSafe;
025
026import org.ametys.cms.repository.Content;
027import org.ametys.cms.repository.ModifiableContent;
028import org.ametys.plugins.repository.AmetysObjectResolver;
029import org.ametys.plugins.repository.AmetysRepositoryException;
030import org.ametys.plugins.repository.UnknownAmetysObjectException;
031import org.ametys.web.repository.page.Page;
032import org.ametys.web.repository.site.Site;
033import org.ametys.web.site.CopyUpdater;
034
035/**
036 * Copy updater for newsletters content
037 *
038 */
039public class NewsletterCopyUpdater extends AbstractLogEnabled implements CopyUpdater, ThreadSafe, Serviceable
040{
041    private AmetysObjectResolver _resolver;
042    
043    @Override
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
047    }
048
049    @Override
050    public void updateContent(Site initialSite, Site createdSite, Content initialContent, Content createdContent)
051    {
052        try
053        {
054            if (Arrays.asList(createdContent.getTypes()).contains("org.ametys.plugins.newsletter.Content.newsletter"))
055            {
056                String category = createdContent.getInternalDataHolder().getValue("category");
057                if (category.startsWith("category_page://"))
058                {
059                    String pageId = category.substring("category_".length());
060                    Page initialPage = _resolver.resolveById(pageId);
061                    
062                    try
063                    {
064                        // Find symetric copied page
065                        Page copiedPage = createdSite.getSitemap(initialPage.getSitemapName()).getChild(initialPage.getPathInSitemap());
066                        ((ModifiableContent) createdContent).getInternalDataHolder().setValue("category", "category_" + copiedPage.getId());
067                    }
068                    catch (UnknownAmetysObjectException e)
069                    {
070                        ((ModifiableContent) createdContent).getInternalDataHolder().removeValue("category");
071                    }
072                }
073            }
074        }
075        catch (AmetysRepositoryException e)
076        {
077            getLogger().warn("[Site copy] Unable to update newsletter category for content '" + createdContent.getId() + "'", e);
078        }
079        
080    }
081
082    @Override
083    public void updatePage(Site initialSite, Site createdSite, Page page)
084    {
085        // Nothing to do
086    }
087    
088    @Override
089    public void updateSite(Site initialSite, Site createdSite)
090    {
091        // Nothing to do
092    }
093
094}