001/* 002 * Copyright 2024 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.Set; 021import java.util.stream.Stream; 022 023import org.apache.avalon.framework.activity.Initializable; 024import org.apache.avalon.framework.component.Component; 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.avalon.framework.service.Serviceable; 028import org.apache.excalibur.source.Source; 029import org.apache.excalibur.source.SourceResolver; 030 031import org.ametys.odf.ODFHelper; 032import org.ametys.odf.program.Program; 033import org.ametys.runtime.config.Config; 034import org.ametys.runtime.plugin.component.AbstractLogEnabled; 035 036/** 037 * Processor to process CDM-fr files. 038 * - Filter programs if need 039 * - Iterate on programs 040 * - Generate the CDM-fr file 041 * - Process on the CDM-fr file 042 */ 043public abstract class AbstractCDMfrProcessor extends AbstractLogEnabled implements Component, Serviceable, Initializable 044{ 045 /** The source resolver */ 046 protected SourceResolver _sourceResolver; 047 048 /** The ODF helper */ 049 protected ODFHelper _odfHelper; 050 051 private boolean _isActive; 052 053 public void service(ServiceManager manager) throws ServiceException 054 { 055 _sourceResolver = (SourceResolver) manager.lookup(SourceResolver.ROLE); 056 _odfHelper = (ODFHelper) manager.lookup(ODFHelper.ROLE); 057 } 058 059 public void initialize() throws Exception 060 { 061 _isActive = Config.getInstance().getValue(getConfigActiveParam()); 062 } 063 064 /** 065 * Get the configuration parameter name to determine if the process is active. 066 * @return The configuration parameter name 067 */ 068 protected abstract String getConfigActiveParam(); 069 070 /** 071 * <code>true</code> if the process is active 072 * @return <code>true</code> if the process is active 073 */ 074 public boolean isActive() 075 { 076 return _isActive; 077 } 078 079 /** 080 * Process the CDM-fr of the given programs. 081 * @param programs The programs to process 082 */ 083 public void processPrograms(Set<Program> programs) 084 { 085 // Check if the process is active 086 if (isActive()) 087 { 088 // Filter programs if needed and iterate on it 089 filterPrograms(programs.stream()).forEach( 090 program -> 091 { 092 // Get the CDM-fr content and process the program 093 try (InputStream is = _getCDMfrContent(program)) 094 { 095 processProgram(program, is); 096 } 097 catch (IOException e) 098 { 099 getLogger().error(getErrorMessage(), e); 100 } 101 } 102 ); 103 } 104 } 105 106 /** 107 * Process the program and its CDM-fr input stream. 108 * @param program The program 109 * @param cdmfrContent The CDM-fr content as {@link InputStream} 110 * @throws IOException if an exception occurs 111 */ 112 protected abstract void processProgram(Program program, InputStream cdmfrContent) throws IOException; 113 114 /** 115 * Get the error message if process fails. 116 * @return the error message 117 */ 118 protected abstract String getErrorMessage(); 119 120 /** 121 * Filter programs if needed. 122 * @param programs The programs to filter 123 * @return The filtered programs 124 */ 125 protected Stream<Program> filterPrograms(Stream<Program> programs) 126 { 127 // Default implementation does not filter anything 128 return programs; 129 } 130 131 /** 132 * Get the CDM-fr of the current program. 133 * @param program The program to export as CDM-fr 134 * @return The {@link InputStream} with the CDM-fr content 135 * @throws IOException if an exception occurs 136 */ 137 protected InputStream _getCDMfrContent(Program program) throws IOException 138 { 139 // Build the URI to export program as CDM-fr 140 StringBuilder sb = new StringBuilder("cocoon://_plugins/odf/export-cdmfr.xml"); 141 sb.append("?id=").append(program.getId()); 142 sb.append("&").append(ExportCDMfrHelper.REQUEST_PARAM_VALID_LABEL).append("=true"); 143 if (isCDMfrForAmetys()) 144 { 145 sb.append("&").append(ExportCDMfrHelper.REQUEST_PARAM_EXPORT_FOR_AMETYS).append("=true"); 146 } 147 148 // Generate CDM-fr file 149 Source cdmfrSource = _sourceResolver.resolveURI(sb.toString()); 150 151 // Send to remote server 152 return cdmfrSource.getInputStream(); 153 } 154 155 /** 156 * <code>true</code> to generate CDM-fr for another Ametys, <code>false</code> otherwise. 157 * @return <code>true</code> to generate CDM-fr for another Ametys instance 158 */ 159 protected abstract boolean isCDMfrForAmetys(); 160}