Skip to content

Commit

Permalink
Merge pull request #912 from folio-org/CIRC-1178-back-port-to-2021-R2
Browse files Browse the repository at this point in the history
Back port CIRC-1178 to 2021 R2
  • Loading branch information
marcjohnson-kint authored Aug 3, 2021
2 parents cc090a6 + e48a2fc commit ccad9ba
Show file tree
Hide file tree
Showing 16 changed files with 279 additions and 242 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 22.0.2 2021-08-03

* Increases the number of loans to be checked for scheduled anonymization to 50 000 (CIRC-1178)

## 22.0.1 2021-08-03

* User asked to override patron block on renewing when no patron block exists on renewing (CIRC-1185)
Expand Down
10 changes: 6 additions & 4 deletions descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@
"pubsub.publish.post"
],
"unit": "minute",
"delay": "1"
"delay": "60"
},
{
"methods": [
Expand Down Expand Up @@ -2257,21 +2257,23 @@
],
"visible": false
}


],
"launchDescriptor": {
"dockerImage": "${artifactId}:${version}",
"dockerPull": false,
"dockerArgs": {
"HostConfig": {
"Memory": 715827882,
"Memory": 1073741824,
"PortBindings": { "9801/tcp": [ { "HostPort": "%p" } ] }
}
},
"env": [
{ "name": "JAVA_OPTIONS",
"value": "-XX:MaxRAMPercentage=66.0"
},
{
"name": "SCHEDULED_ANONYMIZATION_NUMBER_OF_LOANS_TO_CHECK",
"value": "50000"
}
]
}
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/org/folio/Environment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.folio;

import static java.lang.Integer.parseInt;
import static org.apache.commons.lang.StringUtils.isBlank;

import java.lang.invoke.MethodHandles;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Environment {
private static final Logger log = LogManager.getLogger(MethodHandles.lookup().lookupClass());

private Environment() { }

public static int getScheduledAnonymizationNumberOfLoansToCheck() {
return getVariable("SCHEDULED_ANONYMIZATION_NUMBER_OF_LOANS_TO_CHECK", 50000);
}

private static int getVariable(String key, int defaultValue) {
final var variable = System.getenv().get(key);

if (isBlank(variable)) {
return defaultValue;
}

try {
return parseInt(variable);
}
catch(Exception e) {
log.warn("Invalid value for '{}': '{}' ", key, variable);

return defaultValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,42 @@
import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.folio.circulation.domain.anonymization.LoanAnonymizationRecords.CAN_BE_ANONYMIZED_KEY;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

import org.folio.circulation.infrastructure.storage.loans.AnonymizeStorageLoansRepository;
import org.folio.circulation.domain.Loan;
import org.folio.circulation.domain.anonymization.service.AnonymizationCheckersService;
import org.folio.circulation.domain.anonymization.service.LoanAnonymizationFinderService;
import org.folio.circulation.infrastructure.storage.loans.AnonymizeStorageLoansRepository;
import org.folio.circulation.services.EventPublisher;
import org.folio.circulation.support.results.Result;

public class DefaultLoanAnonymizationService implements LoanAnonymizationService {
private final AnonymizeStorageLoansRepository anonymizeStorageLoansRepository;
private final AnonymizationCheckersService anonymizationCheckersService;
private final LoanAnonymizationFinderService loansFinder;
private final EventPublisher eventPublisher;

DefaultLoanAnonymizationService(AnonymizationCheckersService anonymizationCheckersService,
LoanAnonymizationFinderService loansFinderService,
AnonymizeStorageLoansRepository anonymizeStorageLoansRepository, EventPublisher eventPublisher) {
public DefaultLoanAnonymizationService(
AnonymizationCheckersService anonymizationCheckersService,
AnonymizeStorageLoansRepository anonymizeStorageLoansRepository,
EventPublisher eventPublisher) {

this.anonymizationCheckersService = anonymizationCheckersService;
this.loansFinder = loansFinderService;
this.anonymizeStorageLoansRepository = anonymizeStorageLoansRepository;
this.eventPublisher = eventPublisher;
}

@Override
public CompletableFuture<Result<LoanAnonymizationRecords>> anonymizeLoans() {
return loansFinder.findLoansToAnonymize()
public CompletableFuture<Result<LoanAnonymizationRecords>> anonymizeLoans(
Supplier<CompletableFuture<Result<Collection<Loan>>>> loansToCheck) {

if (anonymizationCheckersService.neverAnonymizeLoans()) {
return completedFuture(Result.of(LoanAnonymizationRecords::new));
}

return loansToCheck.get()
.thenApply(r -> r.map(new LoanAnonymizationRecords()::withLoansFound))
.thenCompose(this::segregateLoanRecords)
.thenCompose(r -> r.after(anonymizeStorageLoansRepository::postAnonymizeStorageLoans))
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.folio.circulation.domain.anonymization;

import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

import org.folio.circulation.domain.Loan;
import org.folio.circulation.support.results.Result;

public interface LoanAnonymizationService {
CompletableFuture<Result<LoanAnonymizationRecords>> anonymizeLoans();
CompletableFuture<Result<LoanAnonymizationRecords>> anonymizeLoans(
Supplier<CompletableFuture<Result<Collection<Loan>>>> loansToCheck);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.folio.circulation.domain.anonymization.checkers.NeverAnonymizeLoansChecker;
import org.folio.circulation.domain.anonymization.checkers.NeverAnonymizeLoansWithFeeFinesChecker;
import org.folio.circulation.domain.anonymization.checkers.NoAssociatedFeesAndFinesChecker;
import org.folio.circulation.domain.anonymization.config.ClosingType;
import org.folio.circulation.domain.anonymization.config.LoanAnonymizationConfiguration;

public class AnonymizationCheckersService {
Expand All @@ -26,7 +27,6 @@ public class AnonymizationCheckersService {
private AnonymizationChecker feesAndFinesCheckersFromLoanHistory;
private AnonymizationChecker closedLoansCheckersFromLoanHistory;


public AnonymizationCheckersService(LoanAnonymizationConfiguration config) {
this.config = config;
if ( config != null) {
Expand All @@ -40,6 +40,16 @@ public AnonymizationCheckersService() {
this(null);
}

public boolean neverAnonymizeLoans() {
// Without config, this cannot be determined
if (config == null) {
return false;
}

return config.getLoanClosingType() == ClosingType.NEVER &&
!config.treatLoansWithFeesAndFinesDifferently();
}

public Map<String, Set<String>> segregateLoans(Collection<Loan> loans) {
return loans.stream()
.collect(Collectors.groupingBy(applyCheckersForLoanAndLoanHistoryConfig(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.folio.circulation.infrastructure.storage.feesandfines.AccountRepository;
import org.folio.circulation.support.results.Result;

abstract class DefaultLoansFinder implements LoanAnonymizationFinderService {
abstract class DefaultLoansFinder {
private final AccountRepository accountRepository;

DefaultLoansFinder(AccountRepository accountRepository) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.folio.circulation.domain.anonymization.service;

import static org.folio.circulation.domain.anonymization.LoanAnonymization.FETCH_LOANS_PAGE_LIMIT;
import static org.folio.circulation.support.http.client.PageLimit.limit;

import java.util.Collection;
import java.util.concurrent.CompletableFuture;
Expand All @@ -22,9 +22,8 @@ public LoansForBorrowerFinder(String userId, LoanRepository loanRepository,
this.loanRepository = loanRepository;
}

@Override
public CompletableFuture<Result<Collection<Loan>>> findLoansToAnonymize() {
return loanRepository.findClosedLoans(userId, FETCH_LOANS_PAGE_LIMIT)
return loanRepository.findClosedLoans(userId, limit(5000))
.thenCompose(this::fetchAdditionalLoanInfo);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.folio.circulation.domain.anonymization.service;

import static org.folio.circulation.domain.anonymization.LoanAnonymization.FETCH_LOANS_PAGE_LIMIT;
import static org.folio.circulation.support.http.client.PageLimit.limit;

import java.util.Collection;
import java.util.concurrent.CompletableFuture;
Expand All @@ -12,17 +12,18 @@

public class LoansForTenantFinder extends DefaultLoansFinder {
private final LoanRepository loanRepository;
private final int numberOfLoansToCheck;

public LoansForTenantFinder(LoanRepository loanRepository,
AccountRepository accountRepository) {
AccountRepository accountRepository, int numberOfLoansToCheck) {

super(accountRepository);
this.loanRepository = loanRepository;
this.numberOfLoansToCheck = numberOfLoansToCheck;
}

@Override
public CompletableFuture<Result<Collection<Loan>>> findLoansToAnonymize() {
return loanRepository.findLoansToAnonymize(FETCH_LOANS_PAGE_LIMIT)
return loanRepository.findLoansToAnonymize(limit(numberOfLoansToCheck))
.thenCompose(this::fetchAdditionalLoanInfo);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package org.folio.circulation.resources;

import static java.util.concurrent.CompletableFuture.completedFuture;
import java.lang.invoke.MethodHandles;

import org.folio.circulation.domain.anonymization.LoanAnonymization;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.circulation.domain.anonymization.DefaultLoanAnonymizationService;
import org.folio.circulation.domain.anonymization.service.AnonymizationCheckersService;
import org.folio.circulation.domain.anonymization.service.LoansForBorrowerFinder;
import org.folio.circulation.domain.representations.anonymization.AnonymizeLoansRepresentation;
import org.folio.circulation.infrastructure.storage.feesandfines.AccountRepository;
import org.folio.circulation.infrastructure.storage.loans.AnonymizeStorageLoansRepository;
import org.folio.circulation.infrastructure.storage.loans.LoanRepository;
import org.folio.circulation.services.EventPublisher;
import org.folio.circulation.support.Clients;
import org.folio.circulation.support.RouteRegistration;
import org.folio.circulation.support.http.server.JsonHttpResponse;
Expand All @@ -16,6 +22,8 @@
import io.vertx.ext.web.RoutingContext;

public class LoanAnonymizationResource extends Resource {
private final Logger log = LogManager.getLogger(MethodHandles.lookup().lookupClass());

public LoanAnonymizationResource(HttpClient client) {
super(client);
}
Expand All @@ -32,12 +40,23 @@ private void anonymizeLoans(RoutingContext routingContext) {

String borrowerId = routingContext.request().getParam("userId");

final var loanAnonymization = new LoanAnonymization(clients, new LoanRepository(clients), new AccountRepository(clients));
final var loanRepository = new LoanRepository(clients);
final var accountRepository = new AccountRepository(clients);

final var loansFinder = new LoansForBorrowerFinder(borrowerId,
loanRepository, accountRepository);

final var anonymizeStorageLoansRepository = new AnonymizeStorageLoansRepository(clients);
final var eventPublisher = new EventPublisher(clients.pubSubPublishingService());

final var loanAnonymizationService = new DefaultLoanAnonymizationService(
new AnonymizationCheckersService(), anonymizeStorageLoansRepository, eventPublisher);

log.info("Initializing loan anonymization for borrower: {}", borrowerId);

completedFuture(loanAnonymization.byUserId(borrowerId)
.anonymizeLoans()
loanAnonymizationService.anonymizeLoans(loansFinder::findLoansToAnonymize)
.thenApply(AnonymizeLoansRepresentation::from)
.thenApply(r -> r.map(JsonHttpResponse::ok))
.thenAccept(context::writeResultToHttpResponse));
.thenAccept(context::writeResultToHttpResponse);
}
}
Loading

0 comments on commit ccad9ba

Please sign in to comment.