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.forms;
017
018import java.util.List;
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.plugins.forms.jcr.FormPropertiesManager;
028import org.ametys.web.repository.content.SharedContent;
029import org.ametys.web.repository.page.Page;
030import org.ametys.web.repository.site.Site;
031import org.ametys.web.site.CopyUpdater;
032
033/**
034 * Copy updater for contents containing forms
035 *
036 */
037public class FormsCopyUpdater extends AbstractLogEnabled implements CopyUpdater, ThreadSafe, Serviceable
038{
039    private FormManager _formManager;
040    private FormPropertiesManager _formPropertiesManager;
041    
042    @Override
043    public void service(ServiceManager smanager) throws ServiceException
044    {
045        _formManager = (FormManager) smanager.lookup(FormManager.ROLE);
046        _formPropertiesManager = (FormPropertiesManager) smanager.lookup(FormPropertiesManager.ROLE);
047    }
048
049    @Override
050    public void updateContent(Site initialSite, Site createdSite, Content initialContent, Content createdContent)
051    {
052        if (createdContent instanceof SharedContent)
053        {
054            return;
055        }
056        
057        try
058        {
059            boolean find = false;
060            
061            // Remove all forms copied from initial content
062            List<Form> forms = _formPropertiesManager.getForms(createdContent);
063            for (Form form : forms)
064            {
065                _formPropertiesManager.remove(form, createdContent);
066                find = true;
067            }
068            
069            if (find)
070            {
071                // Save form properties and create tables
072                _formManager.processContentForms(createdContent);
073            }
074        }
075        catch (Exception e)
076        {
077            getLogger().warn("[Site copy] Unable to update forms for content '" + createdContent.getId() + "'", e);
078        }
079    }
080
081    @Override
082    public void updatePage(Site initialSite, Site createdSite, Page page)
083    {
084        // Nothing to do
085    }
086    
087    @Override
088    public void updateSite(Site initialSite, Site createdSite)
089    {
090        // Nothing to do
091    }
092
093}