refactoring

This commit is contained in:
Konstantinos Spyrou 2023-01-23 18:26:19 +02:00
parent 4231480cbc
commit 9e4817f2bd
2 changed files with 42 additions and 50 deletions

View File

@ -1,13 +1,12 @@
package eu.dnetlib.repo.manager.service; package eu.dnetlib.repo.manager.service;
import com.fasterxml.jackson.core.JsonProcessingException; import eu.dnetlib.api.functionality.ValidatorServiceException;
import eu.dnetlib.repo.manager.domain.*; import eu.dnetlib.repo.manager.domain.*;
import eu.dnetlib.repo.manager.exception.RepositoryServiceException; import eu.dnetlib.repo.manager.exception.RepositoryServiceException;
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException; import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
import org.json.JSONException; import org.json.JSONException;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -17,27 +16,25 @@ public interface RepositoryService {
// TODO: move this elsewhere // TODO: move this elsewhere
Country[] getCountries(); Country[] getCountries();
List<Repository> getRepositories(List<String> ids) throws JSONException; List<Repository> getRepositories(List<String> ids);
List<Repository> getRepositories(List<String> ids, int page, int size) throws JSONException; List<Repository> getRepositories(List<String> ids, int page, int size);
List<RepositorySnippet> getRepositoriesSnippets(List<String> ids) throws Exception; List<RepositorySnippet> getRepositoriesSnippets(List<String> ids) throws Exception;
List<RepositorySnippet> getRepositoriesSnippets(List<String> ids, int page, int size) throws Exception; List<RepositorySnippet> getRepositoriesSnippets(List<String> ids, int page, int size) throws Exception;
List<RepositorySnippet> getRepositoriesByCountry(String country, String mode, Boolean managed) throws JSONException, IOException; List<RepositorySnippet> getRepositoriesByCountry(String country, String mode, Boolean managed);
// TODO: remove? // TODO: remove?
List<Repository> getRepositoriesOfUser(String page, String size) throws JSONException, IOException; List<Repository> getRepositoriesOfUser(String page, String size);
// TODO: remove? // TODO: remove?
List<Repository> getRepositoriesOfUser(String userEmail, List<Repository> getRepositoriesOfUser(String userEmail, String page, String size);
String page,
String size) throws JSONException, IOException;
List<RepositorySnippet> getRepositoriesSnippetsOfUser(String page, String size) throws Exception; List<RepositorySnippet> getRepositoriesSnippetsOfUser(String page, String size);
List<RepositorySnippet> getRepositoriesSnippetsOfUser(String userEmail, String page, String size) throws Exception; List<RepositorySnippet> getRepositoriesSnippetsOfUser(String userEmail, String page, String size);
RepositorySnippet getRepositorySnippetById(String id) throws JSONException, ResourceNotFoundException; RepositorySnippet getRepositorySnippetById(String id) throws JSONException, ResourceNotFoundException;
@ -71,11 +68,9 @@ public interface RepositoryService {
List<Timezone> getTimezones(); List<Timezone> getTimezones();
Repository updateRepository(Repository repository, Authentication authentication) throws Exception; Repository updateRepository(Repository repository, Authentication authentication);
List<String> getUrlsOfUserRepos(String user_email, List<String> getUrlsOfUserRepos(String userEmail, String page, String size);
String page,
String size);
Map<String, String> getCompatibilityClasses(String mode); Map<String, String> getCompatibilityClasses(String mode);
@ -87,7 +82,7 @@ public interface RepositoryService {
Map<String, String> getListLatestUpdate(String mode); Map<String, String> getListLatestUpdate(String mode);
RepositoryInterface updateRepositoryInterface(String repoId, String comment, RepositoryInterface repositoryInterface, String desiredCompatibilityLevel) throws Exception; RepositoryInterface updateRepositoryInterface(String repoId, String comment, RepositoryInterface repositoryInterface, String desiredCompatibilityLevel) throws ResourceNotFoundException, ValidatorServiceException;
void updateInterfaceCompliance(String repositoryId, String repositoryInterfaceId, String compliance); void updateInterfaceCompliance(String repositoryId, String repositoryInterfaceId, String compliance);
} }

View File

@ -13,14 +13,12 @@ import eu.dnetlib.repo.manager.domain.dto.User;
import eu.dnetlib.repo.manager.exception.RepositoryServiceException; import eu.dnetlib.repo.manager.exception.RepositoryServiceException;
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException; import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
import eu.dnetlib.repo.manager.service.aai.registry.AaiRegistryService; import eu.dnetlib.repo.manager.service.aai.registry.AaiRegistryService;
import eu.dnetlib.repo.manager.service.security.AuthoritiesUpdater;
import eu.dnetlib.repo.manager.service.security.AuthorizationService; import eu.dnetlib.repo.manager.service.security.AuthorizationService;
import eu.dnetlib.repo.manager.service.security.RoleMappingService; import eu.dnetlib.repo.manager.service.security.RoleMappingService;
import eu.dnetlib.repo.manager.utils.Converter; import eu.dnetlib.repo.manager.utils.Converter;
import eu.dnetlib.repo.manager.utils.DateUtils; import eu.dnetlib.repo.manager.utils.DateUtils;
import gr.uoa.di.driver.enabling.vocabulary.VocabularyLoader; import gr.uoa.di.driver.enabling.vocabulary.VocabularyLoader;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import org.json.JSONException;
import org.mitre.openid.connect.model.OIDCAuthenticationToken; import org.mitre.openid.connect.model.OIDCAuthenticationToken;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -38,7 +36,6 @@ import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import java.io.IOException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -51,7 +48,6 @@ public class RepositoryServiceImpl implements RepositoryService {
private final AuthorizationService authorizationService; private final AuthorizationService authorizationService;
private final RoleMappingService roleMappingService; private final RoleMappingService roleMappingService;
private final AaiRegistryService registryCalls; private final AaiRegistryService registryCalls;
private final AuthoritiesUpdater authoritiesUpdater;
private final RestTemplate restTemplate; private final RestTemplate restTemplate;
private final ObjectMapper objectMapper; private final ObjectMapper objectMapper;
private final VocabularyLoader vocabularyLoader; private final VocabularyLoader vocabularyLoader;
@ -86,11 +82,9 @@ public class RepositoryServiceImpl implements RepositoryService {
public RepositoryServiceImpl(AuthorizationService authorizationService, public RepositoryServiceImpl(AuthorizationService authorizationService,
RoleMappingService roleMappingService, RoleMappingService roleMappingService,
AaiRegistryService registryCalls, AaiRegistryService registryCalls,
AuthoritiesUpdater authoritiesUpdater,
VocabularyLoader vocabularyLoader, VocabularyLoader vocabularyLoader,
RestTemplate restTemplate, RestTemplate restTemplate,
ObjectMapper objectMapper, ObjectMapper objectMapper,
// Converter converter,
@Lazy EmailUtils emailUtils, @Lazy EmailUtils emailUtils,
@Lazy ValidatorService validatorService, @Lazy ValidatorService validatorService,
@Lazy PiWikService piWikService, @Lazy PiWikService piWikService,
@ -98,7 +92,6 @@ public class RepositoryServiceImpl implements RepositoryService {
this.authorizationService = authorizationService; this.authorizationService = authorizationService;
this.roleMappingService = roleMappingService; this.roleMappingService = roleMappingService;
this.registryCalls = registryCalls; this.registryCalls = registryCalls;
this.authoritiesUpdater = authoritiesUpdater;
this.vocabularyLoader = vocabularyLoader; this.vocabularyLoader = vocabularyLoader;
this.piWikService = piWikService; this.piWikService = piWikService;
this.emailUtils = emailUtils; this.emailUtils = emailUtils;
@ -134,7 +127,7 @@ public class RepositoryServiceImpl implements RepositoryService {
} }
httpHeaders = new HttpHeaders(); httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); httpHeaders.setContentType(MediaType.APPLICATION_JSON);
for (String vocName : vocabularyNames) { for (String vocName : vocabularyNames) {
vocabularyMap.put(vocName, vocabularyLoader.getVocabulary(vocName, Locale.ENGLISH, Locale.ROOT)); vocabularyMap.put(vocName, vocabularyLoader.getVocabulary(vocName, Locale.ENGLISH, Locale.ROOT));
@ -159,7 +152,7 @@ public class RepositoryServiceImpl implements RepositoryService {
// and the "requestFilter.setId(repoId)" should return only one result at a time, thus, // and the "requestFilter.setId(repoId)" should return only one result at a time, thus,
// another way for paging must be implemented. // another way for paging must be implemented.
@Override @Override
public List<Repository> getRepositories(List<String> ids) throws JSONException { public List<Repository> getRepositories(List<String> ids) {
return getRepositories(ids, 0, 10); return getRepositories(ids, 0, 10);
} }
@ -188,7 +181,7 @@ public class RepositoryServiceImpl implements RepositoryService {
// and the "requestFilter.setId(repoId)" should return only one result at a time, thus, // and the "requestFilter.setId(repoId)" should return only one result at a time, thus,
// another way for paging must be implemented. // another way for paging must be implemented.
@Override @Override
public List<RepositorySnippet> getRepositoriesSnippets(List<String> ids) throws Exception { public List<RepositorySnippet> getRepositoriesSnippets(List<String> ids) {
return getRepositoriesSnippets(ids, 0, 10); return getRepositoriesSnippets(ids, 0, 10);
} }
@ -196,8 +189,8 @@ public class RepositoryServiceImpl implements RepositoryService {
// and the "requestFilter.setId(repoId)" should return only one result at a time, thus, // and the "requestFilter.setId(repoId)" should return only one result at a time, thus,
// another way for paging must be implemented. // another way for paging must be implemented.
@Override @Override
public List<RepositorySnippet> getRepositoriesSnippets(List<String> ids, int page, int size) throws Exception { public List<RepositorySnippet> getRepositoriesSnippets(List<String> ids, int page, int size) {
List<RepositorySnippet> resultSet; List<RepositorySnippet> resultSet = null;
List<DatasourceDetails> datasourceDetailsList = new ArrayList<>(); List<DatasourceDetails> datasourceDetailsList = new ArrayList<>();
// here page should be 0 // here page should be 0
@ -215,15 +208,19 @@ public class RepositoryServiceImpl implements RepositoryService {
} }
} }
resultSet = objectMapper.readValue(objectMapper.writeValueAsString(datasourceDetailsList), try {
objectMapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class)); resultSet = objectMapper.readValue(objectMapper.writeValueAsString(datasourceDetailsList),
objectMapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class));
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("resultSet: {}", objectMapper.writeValueAsString(resultSet)); logger.debug("resultSet: {}", objectMapper.writeValueAsString(resultSet));
}
resultSet.parallelStream().forEach(repositorySnippet -> {
repositorySnippet.setPiwikInfo(piWikService.getPiwikSiteForRepo(repositorySnippet.getId()));
});
} catch (JsonProcessingException e) {
logger.error("Error deserializing.", e);
} }
resultSet.parallelStream().forEach(repositorySnippet -> {
repositorySnippet.setPiwikInfo(piWikService.getPiwikSiteForRepo(repositorySnippet.getId()));
});
return resultSet; return resultSet;
} }
@ -231,7 +228,7 @@ public class RepositoryServiceImpl implements RepositoryService {
@Override @Override
public List<RepositorySnippet> getRepositoriesByCountry(String country, public List<RepositorySnippet> getRepositoriesByCountry(String country,
String mode, String mode,
Boolean managed) throws IOException { Boolean managed) {
logger.debug("Getting repositories by country!"); logger.debug("Getting repositories by country!");
int page = 0; int page = 0;
int size = 100; int size = 100;
@ -254,7 +251,7 @@ public class RepositoryServiceImpl implements RepositoryService {
} }
public List<RepositorySnippet> searchRegisteredRepositories(String country, String typology, String englishName, public List<RepositorySnippet> searchRegisteredRepositories(String country, String typology, String englishName,
String officialName, String requestSortBy, String order, int page, int pageSize) throws Exception { String officialName, String requestSortBy, String order, int page, int pageSize) {
logger.debug("Searching registered repositories"); logger.debug("Searching registered repositories");
@ -287,7 +284,7 @@ public class RepositoryServiceImpl implements RepositoryService {
} }
@Override @Override
public List<Repository> getRepositoriesOfUser(String page, String size) throws JSONException { public List<Repository> getRepositoriesOfUser(String page, String size) {
logger.debug("Retrieving repositories of authenticated user : {}", logger.debug("Retrieving repositories of authenticated user : {}",
((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail()); ((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail());
Collection<String> repoIds = roleMappingService.getRepoIdsByRoleIds(authorizationService.getUserRoles()); Collection<String> repoIds = roleMappingService.getRepoIdsByRoleIds(authorizationService.getUserRoles());
@ -295,19 +292,19 @@ public class RepositoryServiceImpl implements RepositoryService {
} }
@Override @Override
public List<Repository> getRepositoriesOfUser(String userEmail, String page, String size) throws JSONException { public List<Repository> getRepositoriesOfUser(String userEmail, String page, String size) {
logger.debug("Retrieving repositories of authenticated user : {}", userEmail); logger.debug("Retrieving repositories of authenticated user : {}", userEmail);
Collection<String> repoIds = roleMappingService.getRepoIdsByRoleIds(authorizationService.getUserRolesByEmail(userEmail)); Collection<String> repoIds = roleMappingService.getRepoIdsByRoleIds(authorizationService.getUserRolesByEmail(userEmail));
return getRepositories(new ArrayList<>(repoIds)); return getRepositories(new ArrayList<>(repoIds));
} }
@Override @Override
public List<RepositorySnippet> getRepositoriesSnippetsOfUser(String page, String size) throws Exception { public List<RepositorySnippet> getRepositoriesSnippetsOfUser(String page, String size) {
return getRepositoriesSnippetsOfUser(null, page, size); return getRepositoriesSnippetsOfUser(null, page, size);
} }
@Override @Override
public List<RepositorySnippet> getRepositoriesSnippetsOfUser(String userEmail, String page, String size) throws Exception { public List<RepositorySnippet> getRepositoriesSnippetsOfUser(String userEmail, String page, String size) {
int from = Integer.parseInt(page) * Integer.parseInt(size); int from = Integer.parseInt(page) * Integer.parseInt(size);
int to = from + Integer.parseInt(size); int to = from + Integer.parseInt(size);
List<String> repoIds = new ArrayList<>(); List<String> repoIds = new ArrayList<>();
@ -361,7 +358,7 @@ public class RepositoryServiceImpl implements RepositoryService {
@Override @Override
public List<Repository> getRepositoriesByName(String name, public List<Repository> getRepositoriesByName(String name,
String page, String page,
String size) throws JSONException { String size) {
logger.debug("Retrieving repositories with official name : {}", name); logger.debug("Retrieving repositories with official name : {}", name);
UriComponents uriComponents = searchDatasourceUri("0", "100"); UriComponents uriComponents = searchDatasourceUri("0", "100");
@ -400,7 +397,7 @@ public class RepositoryServiceImpl implements RepositoryService {
} }
@Override @Override
public Repository addRepository(String datatype, Repository repository) throws Exception { public Repository addRepository(String datatype, Repository repository) {
logger.debug("storing '{}' repository with id: {}", datatype, repository.getId()); logger.debug("storing '{}' repository with id: {}", datatype, repository.getId());
@ -459,7 +456,7 @@ public class RepositoryServiceImpl implements RepositoryService {
} }
@Override @Override
public Repository updateRepository(Repository repository, Authentication authentication) throws Exception { public Repository updateRepository(Repository repository, Authentication authentication) {
UriComponents uriComponents = UriComponentsBuilder UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(baseAddress + "/ds/update/") .fromHttpUrl(baseAddress + "/ds/update/")
.build() .build()
@ -480,7 +477,7 @@ public class RepositoryServiceImpl implements RepositoryService {
return repository; return repository;
} }
private void storeRepository(Repository repository, Authentication authentication) throws Exception { private void storeRepository(Repository repository, Authentication authentication) {
Date utilDate = new Date(); Date utilDate = new Date();
Timestamp date = new Timestamp(utilDate.getTime()); Timestamp date = new Timestamp(utilDate.getTime());
@ -554,7 +551,7 @@ public class RepositoryServiceImpl implements RepositoryService {
public RepositoryInterface updateRepositoryInterface(String repoId, public RepositoryInterface updateRepositoryInterface(String repoId,
String comment, String comment,
RepositoryInterface repositoryInterface, RepositoryInterface repositoryInterface,
String desiredCompatibilityLevel) throws Exception { String desiredCompatibilityLevel) throws ResourceNotFoundException, ValidatorServiceException {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Repository repository = this.getRepositoryById(repoId); Repository repository = this.getRepositoryById(repoId);
if (repositoryInterface.getId() != null) { if (repositoryInterface.getId() != null) {
@ -694,7 +691,7 @@ public class RepositoryServiceImpl implements RepositoryService {
public Map<String, String> getCompatibilityClasses(String mode) { public Map<String, String> getCompatibilityClasses(String mode) {
logger.debug("Getting compatibility classes for mode: {}", mode); logger.debug("Getting compatibility classes for mode: {}", mode);
Map<String, String> retMap = new HashMap<String, String>(); Map<String, String> retMap = new HashMap<>();
Map<String, String> compatibilityClasses = this.getVocabulary("dnet:compatibilityLevel").getAsMap(); Map<String, String> compatibilityClasses = this.getVocabulary("dnet:compatibilityLevel").getAsMap();
boolean foundData = false; boolean foundData = false;
@ -729,7 +726,7 @@ public class RepositoryServiceImpl implements RepositoryService {
logger.debug("Getting datasource classes for mode: {}", mode); logger.debug("Getting datasource classes for mode: {}", mode);
Map<String, String> retMap = new HashMap<String, String>(); Map<String, String> retMap = new HashMap<>();
// TODO: refactor (remove?) // TODO: refactor (remove?)
for (Map.Entry<String, String> entry : this.getVocabulary("dnet:datasource_typologies").getAsMap().entrySet()) { for (Map.Entry<String, String> entry : this.getVocabulary("dnet:datasource_typologies").getAsMap().entrySet()) {
@ -823,7 +820,7 @@ public class RepositoryServiceImpl implements RepositoryService {
return Collections.singletonMap("lastCollectionDate", DateUtils.toString(getRepositoryInterface("openaire____::" + mode).get(0).getLastCollectionDate())); return Collections.singletonMap("lastCollectionDate", DateUtils.toString(getRepositoryInterface("openaire____::" + mode).get(0).getLastCollectionDate()));
} }
private void updateValidationSet(String repositoryId, String repositoryInterfaceId, String validationSet) throws Exception { private void updateValidationSet(String repositoryId, String repositoryInterfaceId, String validationSet) {
UriComponents uriComponents = UriComponentsBuilder UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(baseAddress + "/ds/api/oaiset") .fromHttpUrl(baseAddress + "/ds/api/oaiset")
.queryParam("dsId", repositoryId) .queryParam("dsId", repositoryId)