From 474a4c944d7d0afda2b5c9469b7484fdade7d990 Mon Sep 17 00:00:00 2001 From: ndc-dxc <162444006+ndc-dxc@users.noreply.github.com> Date: Thu, 4 Apr 2024 18:33:08 +0200 Subject: [PATCH] small fixes (#103) --- .../ndc/harvester/HarvesterJob.java | 2 +- .../harvester/service/RepositoryService.java | 35 +++++++++++-------- .../ndc/model/harvester/Repository.java | 4 +++ 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/main/java/it/gov/innovazione/ndc/harvester/HarvesterJob.java b/src/main/java/it/gov/innovazione/ndc/harvester/HarvesterJob.java index 58a52326..a642ed7b 100644 --- a/src/main/java/it/gov/innovazione/ndc/harvester/HarvesterJob.java +++ b/src/main/java/it/gov/innovazione/ndc/harvester/HarvesterJob.java @@ -53,7 +53,7 @@ private JobExecutionResponse harvest(Repository repository, String correlationId } public JobExecutionResponse harvest(String repositoryId, String revision, Boolean force) { - Repository repository = repositoryService.findRepoById(repositoryId) + Repository repository = repositoryService.findActiveRepoById(repositoryId) .orElseThrow(() -> new HarvestJobException(String.format("Repository %s not found", repositoryId))); String correlationId = UUID.randomUUID().toString(); return harvest(repository, correlationId, revision, force); diff --git a/src/main/java/it/gov/innovazione/ndc/harvester/service/RepositoryService.java b/src/main/java/it/gov/innovazione/ndc/harvester/service/RepositoryService.java index fee61e78..85aa7e29 100644 --- a/src/main/java/it/gov/innovazione/ndc/harvester/service/RepositoryService.java +++ b/src/main/java/it/gov/innovazione/ndc/harvester/service/RepositoryService.java @@ -56,12 +56,12 @@ public class RepositoryService { private final String repositories; public List getActiveRepos() { - return getAllRepos().stream() + return getAllReposIncludingInactive().stream() .filter(Repository::getActive) .collect(toList()); } - private List getAllRepos() { + private List getAllReposIncludingInactive() { List allRepos = jdbcTemplate.query( QUERY_GET_ALL, (rs, rowNum) -> @@ -81,10 +81,10 @@ private List getAllRepos() { .build()); if (!allRepos.isEmpty()) { - allRepos.forEach(repo -> log.info("Repository: " + repo.toString())); - return allRepos.stream() - .filter(Repository::getActive) - .collect(toList()); + log.info("Found {} repositories in the database", allRepos.size()); + log.debug("Repositories: " + + allRepos.stream().map(Repository::forLogging).collect(Collectors.joining(", "))); + return allRepos; } log.warn("No repositories found in the database. Using the default repositories from configuration"); @@ -116,6 +116,7 @@ private void saveDefaultRepositories(List defaultRepositories) { } private void save(Repository repo) { + log.info("Saving repository {}", repo); String query = "INSERT INTO REPOSITORY (" + "ID, " + "URL, " @@ -142,23 +143,23 @@ private void save(Repository repo) { repo.getMaxFileSizeBytes()); } - public Optional findRepoById(String id) { - return getAllRepos().stream() + public Optional findActiveRepoById(String id) { + return getActiveRepos().stream() .filter(repo -> repo.getId().equals(id)) - .filter(Repository::getActive) .findFirst(); } @SneakyThrows public void createRepo(String url, String name, String description, Long maxFileSizeBytes, Principal principal) { if (repoAlreadyExists(url)) { + log.info("Repository {} already exists, reactivating", url); reactivate(url, name, description, maxFileSizeBytes, principal); return; } // does not exist but repo to create is a substring of an existing repo, // or existing repo is a substring of the repo to create - boolean isDuplicate = getAllRepos().stream() + boolean isDuplicate = getAllReposIncludingInactive().stream() .anyMatch(repo -> startsWithIgnoreCase( repo.getUrl(), @@ -171,6 +172,8 @@ public void createRepo(String url, String name, String description, Long maxFile throw new IllegalArgumentException("Duplicate repository " + url); } + log.info("Creating repository {}", url); + String query = "INSERT INTO REPOSITORY (" + "ID, " + "URL, " @@ -199,13 +202,14 @@ public void createRepo(String url, String name, String description, Long maxFile } private boolean repoAlreadyExists(String url) { - return getAllRepos().stream() + return getAllReposIncludingInactive().stream() .anyMatch(repo -> repo.getUrl().equals(url)); } public int updateRepo(String id, RepositoryController.CreateRepository loadedRepo, Principal principal) { + log.info("Updating repository {} using name={}, description={}, maxFileSizeBytes={}", + id, loadedRepo.getName(), loadedRepo.getDescription(), loadedRepo.getMaxFileSizeBytes()); String query = "UPDATE REPOSITORY SET " - + "URL = ?, " + "NAME = ?, " + "DESCRIPTION = ?, " + "UPDATED = ?, " @@ -213,7 +217,6 @@ public int updateRepo(String id, RepositoryController.CreateRepository loadedRep + "MAX_FILE_SIZE_BYTES = ? " + "WHERE ID = ?"; return jdbcTemplate.update(query, - loadedRepo.getUrl(), loadedRepo.getName(), loadedRepo.getDescription(), java.sql.Timestamp.from(java.time.Instant.now()), @@ -223,6 +226,7 @@ public int updateRepo(String id, RepositoryController.CreateRepository loadedRep } public int delete(String id, Principal principal) { + log.info("Deleting repository {}", id); String query = "UPDATE REPOSITORY SET " + "ACTIVE = ?, " + "UPDATED = ?, " @@ -236,6 +240,7 @@ public int delete(String id, Principal principal) { } public int reactivate(String url, String name, String description, Long maxFileSizeBytes, Principal principal) { + log.info("Reactivating repository {}", url); String query = "UPDATE REPOSITORY SET " + "ACTIVE = ?, " + "NAME = ?, " @@ -256,7 +261,7 @@ public int reactivate(String url, String name, String description, Long maxFileS @SneakyThrows public void storeRightsHolders(Repository repository, Map> rightsHolders) { - log.info("Storing rights holders for repository {}", repository); + log.info("Storing {} rights holders for repository {}", rightsHolders.keySet().size(), repository); String query = "UPDATE REPOSITORY SET " + "RIGHTS_HOLDER = ? " + "WHERE ID = ?"; @@ -266,7 +271,7 @@ public void storeRightsHolders(Repository repository, Map getRightsHolders() { - return getAllRepos().stream() + return getActiveRepos().stream() .map(Repository::getRightsHolders) .map(Map::entrySet) .flatMap(Collection::stream) diff --git a/src/main/java/it/gov/innovazione/ndc/model/harvester/Repository.java b/src/main/java/it/gov/innovazione/ndc/model/harvester/Repository.java index 23d34568..d2619b6d 100644 --- a/src/main/java/it/gov/innovazione/ndc/model/harvester/Repository.java +++ b/src/main/java/it/gov/innovazione/ndc/model/harvester/Repository.java @@ -29,4 +29,8 @@ public class Repository { private Long maxFileSizeBytes; @JsonIgnore private Map> rightsHolders; + + public String forLogging() { + return String.format("[%s] %s (%s) %s", id, name, url, active ? "active" : "inactive"); + } }