From 4683a34cb990d65d998c84163a766e23de1eb638 Mon Sep 17 00:00:00 2001 From: ndc-dxc <162444006+ndc-dxc@users.noreply.github.com> Date: Tue, 26 Mar 2024 15:53:13 +0100 Subject: [PATCH] clean repo on delete (#99) --- .../ndc/controller/RepositoryController.java | 19 ++++++++++++ .../harvester/service/RepositoryService.java | 31 +++++++++++++++++++ .../ndc/model/harvester/Repository.java | 2 ++ 3 files changed, 52 insertions(+) diff --git a/src/main/java/it/gov/innovazione/ndc/controller/RepositoryController.java b/src/main/java/it/gov/innovazione/ndc/controller/RepositoryController.java index 89e946cc..86a15b5e 100644 --- a/src/main/java/it/gov/innovazione/ndc/controller/RepositoryController.java +++ b/src/main/java/it/gov/innovazione/ndc/controller/RepositoryController.java @@ -1,5 +1,6 @@ package it.gov.innovazione.ndc.controller; +import it.gov.innovazione.ndc.harvester.HarvesterService; import it.gov.innovazione.ndc.harvester.service.RepositoryService; import it.gov.innovazione.ndc.model.harvester.Repository; import lombok.Data; @@ -22,6 +23,7 @@ import java.net.URL; import java.security.Principal; import java.util.List; +import java.util.Optional; import static org.springframework.http.HttpStatus.CREATED; @@ -32,6 +34,7 @@ public class RepositoryController { private final RepositoryService repositoryService; + private final HarvesterService harvesterService; @GetMapping public List getAllRepositories() { @@ -86,11 +89,27 @@ public ResponseEntity updateRepository( public ResponseEntity deleteRepository( @PathVariable String id, Principal principal) { + Optional optionalRepository = repositoryService.getActiveRepos().stream() + .filter(repo -> repo.getId().equals(id)) + .findFirst(); + + if (optionalRepository.isEmpty()) { + log.warn("Repository {} not found or not active", id); + return ResponseEntity.notFound().build(); + } + + Repository repository = optionalRepository.get(); + + harvesterService.clear(repository.getUrl()); + int deleted = repositoryService.delete(id, principal); if (deleted == 0) { + log.warn("Repository {} not found", id); return ResponseEntity.notFound().build(); } + + log.info("Repository {} deleted", repository); return ResponseEntity.noContent().build(); } 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 f7e44e4d..bf4fc352 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 @@ -149,6 +149,13 @@ public Optional findRepoById(String id) { @SneakyThrows public void createRepo(String url, String name, String description, Long maxFileSizeBytes, Principal principal) { + if (repoAlreadyExists(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() .anyMatch(repo -> startsWithIgnoreCase( @@ -189,6 +196,11 @@ public void createRepo(String url, String name, String description, Long maxFile maxFileSizeBytes); } + private boolean repoAlreadyExists(String url) { + return getAllRepos().stream() + .anyMatch(repo -> repo.getUrl().equals(url)); + } + public int updateRepo(String id, RepositoryController.CreateRepository loadedRepo, Principal principal) { String query = "UPDATE REPOSITORY SET " + "URL = ?, " @@ -221,6 +233,25 @@ public int delete(String id, Principal principal) { id); } + public int reactivate(String url, String name, String description, Long maxFileSizeBytes, Principal principal) { + String query = "UPDATE REPOSITORY SET " + + "ACTIVE = ?, " + + "NAME = ?, " + + "DESCRIPTION = ?, " + + "MAX_FILE_SIZE_BYTES = ?, " + + "UPDATED = ?, " + + "UPDATED_BY = ? " + + "WHERE URL = ?"; + return jdbcTemplate.update(query, + true, + name, + description, + maxFileSizeBytes, + java.sql.Timestamp.from(java.time.Instant.now()), + principal.getName(), + url); + } + @SneakyThrows public void storeRightsHolders(Repository repository, Map> rightsHolders) { log.info("Storing rights holders for repository {}", repository); 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 d04ede65..23d34568 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 @@ -4,6 +4,7 @@ import lombok.Builder; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.ToString; import lombok.With; import java.time.Instant; @@ -12,6 +13,7 @@ @Data @Builder(toBuilder = true) @EqualsAndHashCode(exclude = {"id", "createdAt", "updatedAt"}) +@ToString(exclude = {"rightsHolders"}) public class Repository { private String id; @With