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.File; 019import java.io.FileOutputStream; 020import java.io.IOException; 021import java.io.InputStream; 022import java.io.OutputStream; 023import java.util.Collections; 024import java.util.Map; 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.io.IOUtils; 030import org.apache.commons.lang.StringUtils; 031import org.apache.excalibur.source.Source; 032import org.apache.excalibur.source.SourceResolver; 033 034import org.ametys.cms.workflow.AbstractContentWorkflowComponent; 035import org.ametys.odf.program.Program; 036import org.ametys.runtime.config.Config; 037import org.ametys.runtime.i18n.I18nizableText; 038 039import com.opensymphony.module.propertyset.PropertySet; 040import com.opensymphony.workflow.FunctionProvider; 041import com.opensymphony.workflow.WorkflowException; 042 043/** 044 * Generate and put CDM-fr file in an output folder 045 * 046 */ 047public class DepositCDMFRFunction extends AbstractContentWorkflowComponent implements FunctionProvider, Initializable 048{ 049 private File _outputFolder; 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().getValueAsBoolean("odf.publish.cdm-fr.output.folder"); 064 String outputFolderPath = Config.getInstance().getValueAsString("odf.publish.cdm-fr.output.folder.path"); 065 066 if (StringUtils.isNotEmpty(outputFolderPath)) 067 { 068 _outputFolder = new File(outputFolderPath); 069 if (!_outputFolder.exists()) 070 { 071 _outputFolder.mkdirs(); 072 } 073 } 074 } 075 076 @Override 077 public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException 078 { 079 if (!_isActive || _outputFolder == null) 080 { 081 // Do nothing 082 return; 083 } 084 085 // Retrieve current content 086 Program program = (Program) getContent(transientVars); 087 088 try 089 { 090 // Generate CDM-FR file 091 Source cdmfrSource = _sourceResolver.resolveURI("cocoon://_plugins/odf/export-cdmfr.xml?id=" + program.getId()); 092 093 // Save file 094 try (InputStream is = cdmfrSource.getInputStream()) 095 { 096 String filename = program.getCDMId() + ".xml"; 097 098 // Delete existing file 099 File file = new File(_outputFolder, filename); 100 if (file.exists()) 101 { 102 file.delete(); 103 } 104 105 try (OutputStream os = new FileOutputStream(file)) 106 { 107 IOUtils.copy(is, os); 108 } 109 } 110 } 111 catch (IOException e) 112 { 113 addWorkflowWarning(transientVars, new I18nizableText("plugin.odf", "PLUGINS_ODF_PUBLISH_PROGRAM_CDMFR_ERROR", Collections.singletonList(program.getTitle()))); 114 _logger.error("Unable to generate and copy the CDM-fr file", e); 115 } 116 } 117}