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.odf.cdmfr;
017
018import java.io.IOException;
019import java.io.InputStream;
020import java.util.ArrayList;
021import java.util.Collections;
022import java.util.List;
023import java.util.Map;
024import java.util.Set;
025
026import org.apache.avalon.framework.activity.Initializable;
027import org.apache.avalon.framework.service.ServiceException;
028import org.apache.avalon.framework.service.ServiceManager;
029import org.apache.commons.collections.ListUtils;
030import org.apache.commons.lang.StringUtils;
031import org.apache.excalibur.source.Source;
032import org.apache.excalibur.source.SourceResolver;
033
034import org.ametys.cms.repository.WorkflowAwareContent;
035import org.ametys.cms.workflow.AbstractContentWorkflowComponent;
036import org.ametys.odf.CallWSHelper;
037import org.ametys.plugins.workflow.EnhancedFunction;
038import org.ametys.runtime.config.Config;
039import org.ametys.runtime.i18n.I18nizableText;
040
041import com.opensymphony.module.propertyset.PropertySet;
042import com.opensymphony.workflow.WorkflowException;
043
044/**
045 * Send CDM-fr to a distant server
046 *
047 */
048public class SendCDMFRFunction extends AbstractContentWorkflowComponent implements EnhancedFunction, Initializable
049{
050    private boolean _isActive;
051    private SourceResolver _sourceResolver;
052    
053    @Override
054    public void service(ServiceManager manager) throws ServiceException
055    {
056        super.service(manager);
057        _sourceResolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
058    }
059    
060    @Override
061    public void initialize() throws Exception
062    {
063        _isActive = Config.getInstance().getValue("odf.publish.server");
064    }
065
066    @Override
067    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
068    {
069        if (!_isActive)
070        {
071            return;
072        }
073        
074        // Retrieve current content
075        WorkflowAwareContent content = getContent(transientVars);
076        
077        try
078        {
079            // Generate CDM-fr file
080            Source cdmfrSource = _sourceResolver.resolveURI(getExportUri() + "?id=" + content.getId() + "&" + ExportCDMfrHelper.REQUEST_PARAM_VALID_LABEL + "=true&" + ExportCDMfrHelper.REQUEST_PARAM_EXPORT_FOR_AMETYS + "=true");
081
082            // Send to remote server
083            try (InputStream is = cdmfrSource.getInputStream())
084            {
085                Set<String> failedUrl = CallWSHelper.callWS("_odf-sync/upload-cdm", is, _logger);
086                if (failedUrl == null || failedUrl.size() > 0)
087                {
088                    List<String> params = new ArrayList<>();
089                    params.add(content.getTitle());
090                    params.add(StringUtils.join(failedUrl, ", "));
091                    addWorkflowWarning(transientVars, new I18nizableText("plugin.odf", "PLUGINS_ODF_PUBLISH_PROGRAM_PORTAL_ERROR", params));
092                    
093                    _logger.error("The program " + content.getId() + " can't be synchronized with portals" + (failedUrl != null ? " " + StringUtils.join(failedUrl, ", ") : ""));
094                }
095            }
096        }
097        catch (IOException e)
098        {
099            addWorkflowError(transientVars, new I18nizableText("plugin.odf", "PLUGINS_ODF_PUBLISH_PROGRAM_REMOTE_ERROR", Collections.singletonList(content.getTitle())));
100            _logger.error("Unable to publish CDM-fr on distant server", e);
101        }
102    }
103    
104    /**
105     * Get the URI to use to export CDMfr
106     * @return The uri location
107     */
108    protected String getExportUri ()
109    {
110        return "cocoon://_plugins/odf/export-cdmfr.xml";
111    }
112    
113    @Override
114    public List<FunctionArgument> getArguments()
115    {
116        return ListUtils.EMPTY_LIST;
117    }
118
119    @Override
120    public I18nizableText getDescription(Map<String, String> argumentsValues)
121    {
122        return new I18nizableText("plugin.odf", "PLUGINS_ODF_SEND_CDMFR_FUNCTION_DESCRIPTION");
123    }
124}