Skip to content

Commit

Permalink
small fixes (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndc-dxc authored Apr 4, 2024
1 parent bf9ca19 commit 474a4c9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public class RepositoryService {
private final String repositories;

public List<Repository> getActiveRepos() {
return getAllRepos().stream()
return getAllReposIncludingInactive().stream()
.filter(Repository::getActive)
.collect(toList());
}

private List<Repository> getAllRepos() {
private List<Repository> getAllReposIncludingInactive() {
List<Repository> allRepos = jdbcTemplate.query(
QUERY_GET_ALL,
(rs, rowNum) ->
Expand All @@ -81,10 +81,10 @@ private List<Repository> 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");
Expand Down Expand Up @@ -116,6 +116,7 @@ private void saveDefaultRepositories(List<Repository> defaultRepositories) {
}

private void save(Repository repo) {
log.info("Saving repository {}", repo);
String query = "INSERT INTO REPOSITORY ("
+ "ID, "
+ "URL, "
Expand All @@ -142,23 +143,23 @@ private void save(Repository repo) {
repo.getMaxFileSizeBytes());
}

public Optional<Repository> findRepoById(String id) {
return getAllRepos().stream()
public Optional<Repository> 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(),
Expand All @@ -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, "
Expand Down Expand Up @@ -199,21 +202,21 @@ 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 = ?, "
+ "UPDATED_BY = ?, "
+ "MAX_FILE_SIZE_BYTES = ? "
+ "WHERE ID = ?";
return jdbcTemplate.update(query,
loadedRepo.getUrl(),
loadedRepo.getName(),
loadedRepo.getDescription(),
java.sql.Timestamp.from(java.time.Instant.now()),
Expand All @@ -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 = ?, "
Expand All @@ -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 = ?, "
Expand All @@ -256,7 +261,7 @@ public int reactivate(String url, String name, String description, Long maxFileS

@SneakyThrows
public void storeRightsHolders(Repository repository, Map<String, Map<String, String>> 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 = ?";
Expand All @@ -266,7 +271,7 @@ public void storeRightsHolders(Repository repository, Map<String, Map<String, St
}

public List<RightsHolder> getRightsHolders() {
return getAllRepos().stream()
return getActiveRepos().stream()
.map(Repository::getRightsHolders)
.map(Map::entrySet)
.flatMap(Collection::stream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ public class Repository {
private Long maxFileSizeBytes;
@JsonIgnore
private Map<String, Map<String, String>> rightsHolders;

public String forLogging() {
return String.format("[%s] %s (%s) %s", id, name, url, active ? "active" : "inactive");
}
}

0 comments on commit 474a4c9

Please sign in to comment.