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