Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CIRC-2148: Reverted CIRC-2100 PR and Rework for PrintEventDetail Pagination, searching and sorting #1492

Merged
merged 14 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -2065,9 +2065,7 @@
"users.collection.get",
"addresstypes.collection.get",
"usergroups.collection.get",
"usergroups.item.get",
"print-events-storage.print-events-status.item.post",
"circulation-storage.circulation-settings.collection.get"
"usergroups.item.get"
],
"visible": false
},
Expand Down
8 changes: 5 additions & 3 deletions ramls/examples/request.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@
"pickupServicePointName": "Circ Desk 1"
},
"printDetails": {
"count": 4,
"lastPrintedDate": "2024-07-29T11:54:07.000Z",
"printCount": 32,
"requesterId": "21457ab5-4635-4e56-906a-908f05e9233b",
"isPrinted": true,
"printEventDate": "2024-09-13T06:34:16.035+00:00",
"lastPrintRequester": {
"lastName": "lastName",
"firstName": "firstName",
"lastName": "lastName",
"middleName": "middleName"
}
}
Expand Down
14 changes: 12 additions & 2 deletions ramls/request.json
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,22 @@
"type": "object",
"readonly": true,
"properties": {
"count": {
"printCount": {
"description": "Total no of times the request is printed",
"type": "integer",
"readOnly": true
},
"lastPrintedDate": {
"requesterId": {
"description": "User uuid of last print requester",
"type": "string",
"readOnly": true
},
"isPrinted": {
"description": "Whether the request is ever printed",
"type": "boolean",
"readOnly": true
},
"printEventDate": {
"description": "Recent printed time of the request",
"type": "string",
"format": "date-time",
Expand Down
48 changes: 0 additions & 48 deletions src/main/java/org/folio/circulation/domain/PrintEventDetail.java

This file was deleted.

13 changes: 11 additions & 2 deletions src/main/java/org/folio/circulation/domain/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ public class Request implements ItemRelatedRecord, UserRelatedRecord {
private boolean changedPosition;
private Integer previousPosition;
private boolean changedStatus;

@With
private PrintEventDetail printEventDetail;
private final User printDetailsRequester;

public static Request from(JsonObject representation) {
// TODO: make sure that operation and TLR settings don't matter for all processes calling
Expand Down Expand Up @@ -225,7 +226,7 @@ public Request withItem(Item newItem) {
cancellationReasonRepresentation, instance, instanceItems, instanceItemsRequestPolicies,
newItem, requester, proxy, addressType,
loan == null ? null : loan.withItem(newItem), pickupServicePoint, changedPosition,
previousPosition, changedStatus, printEventDetail);
previousPosition, changedStatus, null);
}

@Override
Expand All @@ -243,6 +244,10 @@ public User getUser() {
return getRequester();
}

public User getPrintDetailsRequester() {
return printDetailsRequester;
}

public String getfulfillmentPreferenceName() {
return requestRepresentation.getString("fulfillmentPreference");
}
Expand Down Expand Up @@ -291,6 +296,10 @@ public JsonObject getRequesterFromRepresentation() {
return requestRepresentation.getJsonObject("requester");
}

public JsonObject getPrintDetails() {
return requestRepresentation.getJsonObject("printDetails");
}

public String getRequesterBarcode() {
return getRequesterFromRepresentation().getString("barcode", EMPTY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,33 @@ public JsonObject extendedRepresentation(Request request) {
addAdditionalProxyProperties(requestRepresentation, request.getProxy());
addAdditionalServicePointProperties(requestRepresentation, request.getPickupServicePoint());
addDeliveryAddress(requestRepresentation, request, request.getRequester());
addPrintEventProperties(requestRepresentation, request.getPrintEventDetail());
addPrintDetailsProperties(request, requestRepresentation);

removeSearchIndexFields(requestRepresentation);

return requestRepresentation;
}

private void addPrintDetailsProperties(Request request, JsonObject requestRepresentation) {
JsonObject printDetails = requestRepresentation.getJsonObject("printDetails");
if (printDetails == null) {
if (log.isInfoEnabled()) {
log.info("addPrintEventProperties:: printDetails property is null for" +
" requestId {}", requestRepresentation.getString("id"));
}
return;
}

User printDetailsUser = request.getPrintDetailsRequester();
if (printDetailsUser != null) {
JsonObject lastPrintRequester = new JsonObject();
lastPrintRequester.put("firstName", printDetailsUser.getFirstName());
lastPrintRequester.put("lastName", printDetailsUser.getLastName());
lastPrintRequester.put("middleName", printDetailsUser.getMiddleName());
printDetails.put("lastPrintRequester", lastPrintRequester);
}
}

private static void addAdditionalRequesterProperties(JsonObject request, User requester) {
if (requester == null) {
String msg = "Unable to add requester properties to the request: {}, requester is null.";
Expand Down Expand Up @@ -258,29 +279,5 @@ private static JsonObject userSummary(User user) {
private static void removeSearchIndexFields(JsonObject request) {
request.remove("searchIndex");
}

private static void addPrintEventProperties(JsonObject request, PrintEventDetail printEventDetail) {
if (printEventDetail == null) {
if (log.isInfoEnabled()) {
log.info("addPrintEventProperties:: printEvent property is null for requestId {}", request.getString("id"));
}
return;
}

var printEvent = new JsonObject();
write(printEvent, "count", printEventDetail.getCount());
write(printEvent, "lastPrintedDate", printEventDetail.getPrintEventDate());

var user = printEventDetail.getUser();
if (user != null) {
var userSummary = new JsonObject();
write(userSummary, "lastName", user.getLastName());
write(userSummary, "firstName", user.getFirstName());
write(userSummary, "middleName", user.getMiddleName());
write(printEvent, "lastPrintRequester", userSummary);
}
write(request, "printDetails", printEvent);
}

}

Original file line number Diff line number Diff line change
@@ -1,46 +1,26 @@
package org.folio.circulation.infrastructure.storage;

import io.vertx.core.json.JsonObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.circulation.domain.MultipleRecords;
import org.folio.circulation.domain.PrintEventDetail;
import org.folio.circulation.domain.PrintEventRequest;
import org.folio.circulation.domain.Request;
import org.folio.circulation.support.Clients;
import org.folio.circulation.support.CollectionResourceClient;
import org.folio.circulation.support.http.client.ResponseInterpreter;
import org.folio.circulation.support.results.Result;

import java.lang.invoke.MethodHandles;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.folio.circulation.resources.PrintEventsResource.PRINT_EVENT_FLAG_PROPERTY_NAME;
import static org.folio.circulation.resources.PrintEventsResource.PRINT_EVENT_FLAG_QUERY;
import static org.folio.circulation.support.http.ResponseMapping.forwardOnFailure;
import static org.folio.circulation.support.json.JsonPropertyFetcher.getProperty;
import static org.folio.circulation.support.results.Result.of;
import static org.folio.circulation.support.results.Result.succeeded;
import static org.folio.circulation.support.results.ResultBinding.flatMapResult;
import static org.folio.circulation.support.utils.LogUtil.multipleRecordsAsString;

public class PrintEventsRepository {
private static final Logger log = LogManager.getLogger(MethodHandles.lookup().lookupClass());
private static final String RECORDS_PROPERTY_NAME = "printEventsStatusResponses";
private static final String REQUEST_IDS = "requestIds";

private final CollectionResourceClient printEventsStorageClient;
private final CollectionResourceClient printEventsStorageStatusClient;
private final CirculationSettingsRepository circulationSettingsRepository;

public PrintEventsRepository(Clients clients) {
this.printEventsStorageClient = clients.printEventsStorageClient();
this.printEventsStorageStatusClient = clients.printEventsStorageStatusClient();
this.circulationSettingsRepository = new CirculationSettingsRepository(clients);
printEventsStorageClient = clients.printEventsStorageClient();
}

public CompletableFuture<Result<Void>> create(PrintEventRequest printEventRequest) {
Expand All @@ -52,54 +32,4 @@ public CompletableFuture<Result<Void>> create(PrintEventRequest printEventReques
return printEventsStorageClient.post(storagePrintEventRequest).thenApply(interpreter::flatMap);
}

public CompletableFuture<Result<MultipleRecords<Request>>> findPrintEventDetails(
MultipleRecords<Request> multipleRequests) {
log.debug("findPrintEventDetails:: parameters multipleRequests: {}",
() -> multipleRecordsAsString(multipleRequests));
var requestIds = multipleRequests.toKeys(Request::getId);
if (requestIds.isEmpty()) {
log.info("fetchAndMapPrintEventDetails:: No request id found");
return completedFuture(succeeded(multipleRequests));
}
return validatePrintEventFeatureFlag()
.thenCompose(isEnabled -> Boolean.TRUE.equals(isEnabled)
? fetchAndMapPrintEventDetails(multipleRequests, requestIds)
: completedFuture(succeeded(multipleRequests)));
}

private CompletableFuture<Result<MultipleRecords<Request>>> fetchAndMapPrintEventDetails(
MultipleRecords<Request> multipleRequests, Set<String> requestIds) {
log.debug("fetchAndMapPrintEventDetails:: parameters multipleRequests: {}, requestIds {}",
() -> multipleRecordsAsString(multipleRequests), () -> requestIds);
return fetchPrintDetailsByRequestIds(requestIds)
.thenApply(printEventRecordsResult -> printEventRecordsResult
.next(printEventRecords -> mapPrintEventDetailsToRequest(printEventRecords, multipleRequests)));
}

private CompletableFuture<Boolean> validatePrintEventFeatureFlag() {
log.debug("validatePrintEventFeatureFlag:: Fetching and validating enablePrintLog flag from settings");
return circulationSettingsRepository.findBy(PRINT_EVENT_FLAG_QUERY)
.thenApply(res -> Optional.ofNullable(res.value())
.flatMap(records -> records.getRecords().stream().findFirst())
.map(setting -> Boolean.valueOf(getProperty(setting.getValue(), PRINT_EVENT_FLAG_PROPERTY_NAME)))
.orElse(false));
}

private CompletableFuture<Result<MultipleRecords<PrintEventDetail>>> fetchPrintDetailsByRequestIds
(Collection<String> requestIds) {
log.debug("fetchPrintDetailsByRequestIds:: fetching print event details for requestIds {}", requestIds);
return printEventsStorageStatusClient.post(new JsonObject().put(REQUEST_IDS, requestIds))
.thenApply(flatMapResult(response ->
MultipleRecords.from(response, PrintEventDetail::from, RECORDS_PROPERTY_NAME)));
}

private Result<MultipleRecords<Request>> mapPrintEventDetailsToRequest(
MultipleRecords<PrintEventDetail> printEventDetails, MultipleRecords<Request> requests) {
log.debug("mapPrintEventDetailsToRequest:: Mapping print event details {} with requests {}",
() -> multipleRecordsAsString(printEventDetails), () -> multipleRecordsAsString(requests));
Map<String, PrintEventDetail> printEventDetailMap = printEventDetails.toMap(PrintEventDetail::getRequestId);
return of(() ->
requests.mapRecords(request -> request
.withPrintEventDetail(printEventDetailMap.getOrDefault(request.getId(), null))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.folio.circulation.domain.ServicePoint;
import org.folio.circulation.domain.StoredRequestRepresentation;
import org.folio.circulation.domain.User;
import org.folio.circulation.infrastructure.storage.PrintEventsRepository;
import org.folio.circulation.infrastructure.storage.ServicePointRepository;
import org.folio.circulation.infrastructure.storage.inventory.InstanceRepository;
import org.folio.circulation.infrastructure.storage.inventory.ItemRepository;
Expand Down Expand Up @@ -67,7 +66,6 @@ public class RequestRepository {
private final ServicePointRepository servicePointRepository;
private final PatronGroupRepository patronGroupRepository;
private final InstanceRepository instanceRepository;
private final PrintEventsRepository printEventsRepository;

/**
* Public constructor to avoid creating repositories twice
Expand All @@ -78,8 +76,7 @@ public RequestRepository(org.folio.circulation.support.Clients clients,

this(new Clients(clients.requestsStorage(), clients.requestsBatchStorage(),
clients.cancellationReasonStorage()), itemRepository, userRepository,
loanRepository, servicePointRepository, patronGroupRepository, new InstanceRepository(clients),
new PrintEventsRepository(clients));
loanRepository, servicePointRepository, patronGroupRepository, new InstanceRepository(clients));
}

/**
Expand All @@ -90,10 +87,9 @@ public RequestRepository(org.folio.circulation.support.Clients clients) {
}

private RequestRepository(Clients clients, ItemRepository itemRepository,
UserRepository userRepository, LoanRepository loanRepository,
ServicePointRepository servicePointRepository,
PatronGroupRepository patronGroupRepository, InstanceRepository instanceRepository,
PrintEventsRepository printEventsRepository) {
UserRepository userRepository, LoanRepository loanRepository,
ServicePointRepository servicePointRepository,
PatronGroupRepository patronGroupRepository, InstanceRepository instanceRepository) {

this.requestsStorageClient = clients.getRequestsStorageClient();
this.requestsBatchStorageClient = clients.getRequestsBatchStorageClient();
Expand All @@ -104,7 +100,6 @@ private RequestRepository(Clients clients, ItemRepository itemRepository,
this.servicePointRepository = servicePointRepository;
this.patronGroupRepository = patronGroupRepository;
this.instanceRepository = instanceRepository;
this.printEventsRepository = printEventsRepository;
}

public static RequestRepository using(
Expand All @@ -118,14 +113,12 @@ public static RequestRepository using(
itemRepository, userRepository, loanRepository,
new ServicePointRepository(clients),
new PatronGroupRepository(clients),
new InstanceRepository(clients),
new PrintEventsRepository(clients));
new InstanceRepository(clients));
}

public CompletableFuture<Result<MultipleRecords<Request>>> findBy(String query) {
return requestsStorageClient.getManyWithRawQueryStringParameters(query)
.thenApply(flatMapResult(this::mapResponseToRequests))
.thenCompose(r -> r.after(this::fetchPrintEventDetails))
.thenCompose(r -> r.after(this::fetchAdditionalFields));
}

Expand All @@ -149,14 +142,6 @@ private CompletableFuture<Result<MultipleRecords<Request>>> fetchAdditionalField
.thenComposeAsync(result -> result.after(instanceRepository::findInstancesForRequests));
}

private CompletableFuture<Result<MultipleRecords<Request>>> fetchPrintEventDetails(
MultipleRecords<Request> requestRecords) {
log.debug("fetchPrintEventDetails:: Fetching print event details for requestRecords: {}",
()-> multipleRecordsAsString(requestRecords));
return ofAsync(() -> requestRecords)
.thenComposeAsync(result -> result.after(printEventsRepository::findPrintEventDetails));
}

CompletableFuture<Result<MultipleRecords<Request>>> findByWithoutItems(
CqlQuery query, PageLimit pageLimit) {

Expand Down
Loading