2023-12-13 10:42:59 +01:00
|
|
|
package eu.eudat.service.deposit;
|
|
|
|
|
|
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
|
|
|
import eu.eudat.authorization.Permission;
|
2024-03-04 15:36:15 +01:00
|
|
|
import eu.eudat.commonmodels.models.FileEnvelopeModel;
|
2024-03-06 17:18:10 +01:00
|
|
|
import eu.eudat.commonmodels.models.dmp.DmpModel;
|
2023-12-13 10:42:59 +01:00
|
|
|
import eu.eudat.configurations.deposit.DepositProperties;
|
|
|
|
import eu.eudat.data.DmpEntity;
|
|
|
|
import eu.eudat.depositinterface.repository.DepositClient;
|
|
|
|
import eu.eudat.depositinterface.repository.DepositConfiguration;
|
|
|
|
import eu.eudat.model.EntityDoi;
|
2024-03-04 15:36:15 +01:00
|
|
|
import eu.eudat.model.builder.commonmodels.DepositConfigurationBuilder;
|
2024-03-06 17:18:10 +01:00
|
|
|
import eu.eudat.model.builder.commonmodels.dmp.DmpCommonModelBuilder;
|
2023-12-13 10:42:59 +01:00
|
|
|
import eu.eudat.model.persist.deposit.DepositAuthenticateRequest;
|
|
|
|
import eu.eudat.model.persist.deposit.DepositRequest;
|
|
|
|
import eu.eudat.model.persist.EntityDoiPersist;
|
|
|
|
import eu.eudat.query.DmpQuery;
|
|
|
|
import eu.eudat.service.entitydoi.EntityDoiService;
|
|
|
|
import gr.cite.commons.web.oidc.filter.webflux.TokenExchangeCacheService;
|
2024-03-04 15:36:15 +01:00
|
|
|
import gr.cite.commons.web.authz.service.AuthorizationService;
|
2023-12-13 10:42:59 +01:00
|
|
|
import gr.cite.commons.web.oidc.filter.webflux.TokenExchangeFilterFunction;
|
|
|
|
import gr.cite.commons.web.oidc.filter.webflux.TokenExchangeModel;
|
|
|
|
import gr.cite.tools.data.builder.BuilderFactory;
|
|
|
|
import gr.cite.tools.data.query.QueryFactory;
|
|
|
|
import gr.cite.tools.exception.MyNotFoundException;
|
|
|
|
import gr.cite.tools.fieldset.FieldSet;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.context.MessageSource;
|
|
|
|
import org.springframework.context.i18n.LocaleContextHolder;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.web.reactive.function.client.WebClient;
|
|
|
|
|
|
|
|
import java.net.URI;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
@Service
|
|
|
|
public class DepositServiceImpl implements DepositService {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(DepositServiceImpl.class);
|
|
|
|
|
|
|
|
private final DepositProperties depositProperties;
|
|
|
|
private final Map<String, DepositClient> clients;
|
|
|
|
private final TokenExchangeCacheService tokenExchangeCacheService;
|
|
|
|
private final AuthorizationService authorizationService;
|
|
|
|
private final WebClient.Builder webClientBuilder;
|
|
|
|
private final EntityDoiService doiService;
|
|
|
|
private final QueryFactory queryFactory;
|
|
|
|
private final MessageSource messageSource;
|
|
|
|
private final BuilderFactory builderFactory;
|
|
|
|
private final DepositConfigurationCacheService depositConfigurationCacheService;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
public DepositServiceImpl(DepositProperties depositProperties,
|
|
|
|
TokenExchangeCacheService tokenExchangeCacheService,
|
|
|
|
WebClient.Builder builder,
|
|
|
|
AuthorizationService authorizationService,
|
|
|
|
EntityDoiService doiService,
|
|
|
|
QueryFactory queryFactory,
|
|
|
|
MessageSource messageSource,
|
|
|
|
BuilderFactory builderFactory, DepositConfigurationCacheService depositConfigurationCacheService) {
|
|
|
|
this.depositProperties = depositProperties;
|
|
|
|
this.tokenExchangeCacheService = tokenExchangeCacheService;
|
|
|
|
this.authorizationService = authorizationService;
|
|
|
|
this.webClientBuilder = builder;
|
|
|
|
this.doiService = doiService;
|
|
|
|
this.queryFactory = queryFactory;
|
|
|
|
this.messageSource = messageSource;
|
|
|
|
this.builderFactory = builderFactory;
|
|
|
|
this.depositConfigurationCacheService = depositConfigurationCacheService;
|
|
|
|
this.clients = new HashMap<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
private DepositClient getDepositClient(String repositoryId) {
|
|
|
|
if (this.clients.containsKey(repositoryId)) return this.clients.get(repositoryId);
|
|
|
|
|
|
|
|
//GK: It's register time
|
|
|
|
DepositProperties.DepositSource source = depositProperties.getSources().stream().filter(depositSource -> depositSource.getRepositoryId().equals(repositoryId)).findFirst().orElse(null);
|
|
|
|
if (source != null) {
|
|
|
|
String host = URI.create(source.getUrl()).getHost();
|
|
|
|
TokenExchangeModel tokenExchangeModel = new TokenExchangeModel("deposit:" + source.getRepositoryId(), source.getIssuerUrl(), source.getClientId(), source.getClientSecret(), source.getScope());
|
|
|
|
TokenExchangeFilterFunction apiKeyExchangeFilterFunction = new TokenExchangeFilterFunction(this.tokenExchangeCacheService, tokenExchangeModel);
|
|
|
|
WebClient webClient = webClientBuilder.baseUrl(source.getUrl() + "/api/deposit").filters(exchangeFilterFunctions -> exchangeFilterFunctions.add(apiKeyExchangeFilterFunction)).build();
|
|
|
|
DepositClientImpl repository = new DepositClientImpl(webClient);
|
|
|
|
this.clients.put(source.getRepositoryId(), repository);
|
|
|
|
return repository;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<eu.eudat.model.deposit.DepositConfiguration> getAvailableConfigurations(FieldSet fieldSet) {
|
|
|
|
this.authorizationService.authorizeForce(Permission.BrowseDeposit);
|
|
|
|
|
|
|
|
List<eu.eudat.model.deposit.DepositConfiguration> configurations = new ArrayList<>();
|
|
|
|
|
|
|
|
for (DepositProperties.DepositSource depositSource : depositProperties.getSources()) {
|
|
|
|
DepositConfigurationCacheService.DepositConfigurationCacheValue cacheValue = this.depositConfigurationCacheService.lookup(this.depositConfigurationCacheService.buildKey(depositSource.getRepositoryId()));
|
|
|
|
if (cacheValue == null){
|
|
|
|
DepositClient depositClient = getDepositClient(depositSource.getRepositoryId());
|
|
|
|
if (depositClient == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{depositSource.getRepositoryId(), DepositClient.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
DepositConfiguration configuration = depositClient.getConfiguration();
|
|
|
|
cacheValue = new DepositConfigurationCacheService.DepositConfigurationCacheValue(depositSource.getRepositoryId(), configuration);
|
|
|
|
this.depositConfigurationCacheService.put(cacheValue);
|
|
|
|
}
|
|
|
|
eu.eudat.model.deposit.DepositConfiguration depositConfiguration = this.builderFactory.builder(DepositConfigurationBuilder.class).build(fieldSet, cacheValue.getConfiguration());
|
|
|
|
configurations.add(depositConfiguration);
|
|
|
|
}
|
|
|
|
|
|
|
|
return configurations;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EntityDoi deposit(DepositRequest dmpDepositModel) throws Exception {
|
|
|
|
this.authorizationService.authorizeForce(Permission.EditDeposit);
|
|
|
|
|
|
|
|
this.authorizationService.authorize(Permission.EditDmp);
|
|
|
|
//GK: First get the right client
|
|
|
|
DepositClient depositClient = getDepositClient(dmpDepositModel.getRepositoryId());
|
|
|
|
if (depositClient == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{dmpDepositModel.getRepositoryId(), DepositClient.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
//GK: Second get the Target Data Management Plan
|
|
|
|
DmpEntity dmpEntity = this.queryFactory.query(DmpQuery.class).ids(dmpDepositModel.getDmpId()).first();
|
|
|
|
if (dmpEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{dmpDepositModel.getDmpId(), DmpEntity.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
//GK: Forth make the required files to be uploaded with the deposit
|
|
|
|
//TODO: Properly create required files
|
2024-03-04 15:36:15 +01:00
|
|
|
FileEnvelopeModel docEnvelope = new FileEnvelopeModel();
|
|
|
|
FileEnvelopeModel pdfEnvelope = new FileEnvelopeModel();
|
|
|
|
FileEnvelopeModel jsonEnvelope = new FileEnvelopeModel();
|
|
|
|
FileEnvelopeModel zip = new FileEnvelopeModel();
|
2023-12-13 10:42:59 +01:00
|
|
|
//TODO: Create Files
|
|
|
|
// File zip = new File(environment.getProperty("temp.temp") + UUID.randomUUID() + ".zip");
|
|
|
|
// try {
|
|
|
|
// docEnvelope.setFilename("test.docx");
|
|
|
|
// docEnvelope.setFile(this.storageFileService.getH2020TemplateFile());
|
|
|
|
// File pdfFile = PDFUtils.convertToPDF(docEnvelope, environment);
|
|
|
|
// pdfEnvelope.setFilename("test.pdf");
|
|
|
|
// pdfEnvelope.setFile(pdfFile);
|
|
|
|
// File jsonFile = new File(this.environment.getProperty("temp.temp") + UUID.randomUUID() + ".json");
|
|
|
|
// jsonEnvelope.setFilename("test.json");
|
|
|
|
// jsonEnvelope.setFile(jsonFile);
|
|
|
|
//
|
|
|
|
// } catch (IOException e) {
|
|
|
|
// logger.error(e.getMessage(), e);
|
|
|
|
// }
|
|
|
|
|
|
|
|
//GK: Fifth Transform them to the DepositModel
|
2024-03-04 15:36:15 +01:00
|
|
|
DmpModel depositModel = this.builderFactory.builder(DmpCommonModelBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic)
|
2023-12-13 10:42:59 +01:00
|
|
|
.setRepositoryId(dmpDepositModel.getRepositoryId()).setPdfFile(pdfEnvelope).setRdaJsonFile(jsonEnvelope).setSupportingFilesZip(zip).build(dmpEntity);
|
|
|
|
|
|
|
|
//GK: Sixth Perform the deposit
|
|
|
|
String doi = depositClient.deposit(depositModel, dmpDepositModel.getAccessToken());
|
|
|
|
//GK: Something has gone wrong return null
|
|
|
|
if (doi.isEmpty()) return null;
|
|
|
|
//GK: doi is fine store it in database
|
|
|
|
EntityDoiPersist doiPersist = new EntityDoiPersist();
|
|
|
|
doiPersist.setRepositoryId(dmpDepositModel.getRepositoryId());
|
|
|
|
doiPersist.setDoi(doi);
|
|
|
|
doiPersist.setEntityId(dmpEntity.getId());
|
|
|
|
return doiService.persist(doiPersist, dmpDepositModel.getProject());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getLogo(String repositoryId) {
|
|
|
|
this.authorizationService.authorizeForce(Permission.BrowseDeposit);
|
|
|
|
|
|
|
|
DepositClient depositClient = getDepositClient(repositoryId);
|
|
|
|
if (depositClient == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{repositoryId, DepositClient.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
return depositClient.getLogo();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String authenticate(DepositAuthenticateRequest model) {
|
|
|
|
this.authorizationService.authorizeForce(Permission.BrowseDeposit);
|
|
|
|
|
|
|
|
DepositClient depositClient = getDepositClient(model.getRepositoryId());
|
|
|
|
if (depositClient == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getRepositoryId(), DepositClient.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
return depositClient.authenticate(model.getCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|