001/* 002 * Copyright 2019 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.odfsync.cdmfr; 017 018import java.io.InputStream; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.configuration.Configuration; 024import org.apache.avalon.framework.configuration.ConfigurationException; 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.cocoon.ProcessingException; 028import org.slf4j.Logger; 029 030import org.ametys.cms.repository.ModifiableContent; 031import org.ametys.plugins.odfsync.cdmfr.actions.UploadCDMFrAction; 032import org.ametys.plugins.odfsync.cdmfr.components.ImportCDMFrComponent; 033import org.ametys.plugins.odfsync.cdmfr.components.impl.RemoteImportCDMFrComponent; 034 035/** 036 * Class to import remote CDMFr contents 037 */ 038public class RemoteCDMFrSynchronizableContentsCollection extends AbstractCDMFrSynchronizableContentsCollection 039{ 040 /** The name of the param to get the shared with type */ 041 public static final String PARAM_SHARED_WITH_TYPE = "shared.subprogram.type"; 042 043 /** The name of the param to get the role of the CDM-fr importer */ 044 public static final String PARAM_CDMFR_IMPORTER_ROLE = "remote.implementation"; 045 046 /** The name of the param to get the catalog */ 047 public static final String PARAM_CDMFR_CATALOG = "remote.catalog"; 048 049 /** The name of the param to validate after import */ 050 public static final String PARAM_CDMFR_VALIDATE_AFTER_IMPORT = "remote.validate.after.import"; 051 052 /** The service manager */ 053 protected ServiceManager _manager; 054 055 @Override 056 public void service(ServiceManager manager) throws ServiceException 057 { 058 super.service(manager); 059 _manager = manager; 060 } 061 062 @Override 063 protected void configureStaticParams(Configuration configuration) throws ConfigurationException 064 { 065 super.configureStaticParams(configuration); 066 try 067 { 068 _importCDMFrComponent = (ImportCDMFrComponent) _manager.lookup(getImportCDMFrRole()); 069 } 070 catch (ServiceException e) 071 { 072 throw new ConfigurationException("An error occurred getting the CDM-fr importer", e); 073 } 074 } 075 076 @Override 077 @SuppressWarnings("unchecked") 078 public List<ModifiableContent> importContent(String idValue, Map<String, Object> additionalParameters, Logger logger) throws Exception 079 { 080 if (additionalParameters.containsKey(UploadCDMFrAction.CDMFR_IMPUT_STREAM_KEY_NAME)) 081 { 082 InputStream is = (InputStream) additionalParameters.get(UploadCDMFrAction.CDMFR_IMPUT_STREAM_KEY_NAME); 083 try 084 { 085 Map<String, Object> parameters = new HashMap<>(); 086 parameters.put(PARAM_SHARED_WITH_TYPE, getSharedWithType()); 087 parameters.put(PARAM_CDMFR_CATALOG, getCatalog()); 088 parameters.put(PARAM_CDMFR_VALIDATE_AFTER_IMPORT, validateAfterImport()); 089 Map<String, Object> resultMap = _importCDMFrComponent.handleInputStream(is, parameters, this, logger); 090 091 return (List<ModifiableContent>) resultMap.get("importedPrograms"); 092 } 093 catch (ProcessingException e) 094 { 095 logger.error("An error occurred. Can't import remote program.", e); 096 } 097 } 098 else 099 { 100 logger.error("An error occurred because no input stream has been sent"); 101 } 102 103 return null; 104 } 105 106 /** 107 * Get the defined way to detect shared program 108 * @return The defined way to detect shared program 109 */ 110 protected String getSharedWithType() 111 { 112 return (String) getParameterValues().get(PARAM_SHARED_WITH_TYPE); 113 } 114 115 /** 116 * Get the role of the CDM-fr importer 117 * @return the role of the CDM-fr importer 118 */ 119 protected String getImportCDMFrRole() 120 { 121 return (String) getParameterValues().getOrDefault(PARAM_CDMFR_IMPORTER_ROLE, RemoteImportCDMFrComponent.ROLE); 122 } 123 124 /** 125 * Get the catalog 126 * @return the catalog 127 */ 128 protected String getCatalog() 129 { 130 return (String) getParameterValues().get(PARAM_CDMFR_CATALOG); 131 } 132}