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.content;
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.content.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            // if the original content has at least one form, we have to update the new content
060            boolean needProcess = !_formPropertiesManager.getForms(initialContent).isEmpty();
061            
062            // before anything, we remove all forms that might exist in the new content copied from initial content
063            List<Form> forms = _formPropertiesManager.getForms(createdContent);
064            for (Form form : forms)
065            {
066                _formPropertiesManager.remove(form, createdContent);
067                needProcess = true;
068            }
069            
070            if (needProcess)
071            {
072                // Save form properties and create tables
073                _formManager.processContentForms(createdContent);
074            }
075        }
076        catch (Exception e)
077        {
078            getLogger().warn("[Site copy] Unable to update forms for content '" + createdContent.getId() + "'", e);
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}