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                // TODO NEWATTRIBUTEAPI_CONTENT: There is no attribute category on newsletter content. See how to deal with this metadata
057                String category = createdContent.getMetadataHolder().getString("category");
058                if (category.startsWith("category_page://"))
059                {
060                    String pageId = category.substring("category_".length());
061                    Page initialPage = _resolver.resolveById(pageId);
062                    
063                    try
064                    {
065                        // Find symetric copied page
066                        Page copiedPage = createdSite.getSitemap(initialPage.getSitemapName()).getChild(initialPage.getPathInSitemap());
067                        // TODO NEWATTRIBUTEAPI_CONTENT: There is no attribute category on newsletter content. See how to deal with this metadata
068                        ((ModifiableContent) createdContent).getMetadataHolder().setMetadata("category", "category_" + copiedPage.getId());
069                    }
070                    catch (UnknownAmetysObjectException e)
071                    {
072                        // TODO NEWATTRIBUTEAPI_CONTENT: There is no attribute category on newsletter content. See how to deal with this metadata
073                        ((ModifiableContent) createdContent).getMetadataHolder().removeMetadata("category");
074                    }
075                }
076            }
077        }
078        catch (AmetysRepositoryException e)
079        {
080            getLogger().warn("[Site copy] Unable to update newsletter category for content '" + createdContent.getId() + "'", e);
081        }
082        
083    }
084
085    @Override
086    public void updatePage(Site initialSite, Site createdSite, Page page)
087    {
088        // Nothing to do
089    }
090    
091    @Override
092    public void updateSite(Site initialSite, Site createdSite)
093    {
094        // Nothing to do
095    }
096
097}