Skip to content

Commit

Permalink
MODAUD-31 - Requests log actions - improvements (#58)
Browse files Browse the repository at this point in the history
* MODAUD-31

* MODAUD-31

* MODAUD-31

* Update mod-audit-server/src/main/java/org/folio/builder/service/LogRecordBuilder.java

Co-authored-by: siarhei-charniak <[email protected]>

Co-authored-by: siarhei-charniak <[email protected]>
(cherry picked from commit df04c83)
  • Loading branch information
khandramai authored and siarhei-charniak committed Nov 12, 2020
1 parent c9f0b2c commit 20719a0
Show file tree
Hide file tree
Showing 13 changed files with 271 additions and 115 deletions.
7 changes: 6 additions & 1 deletion descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
{
"id": "holdings-storage",
"version": "4.4"
},
{
"id": "cancellation-reason-storage",
"version": "1.1"
}
],
"provides": [
Expand Down Expand Up @@ -117,7 +121,8 @@
"users.collection.get",
"templates.item.get",
"inventory.items.item.get",
"inventory-storage.holdings.item.get"
"inventory-storage.holdings.item.get",
"circulation-storage.cancellation-reasons.collection.get"
]
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import static org.folio.util.LogEventPayloadField.REQUEST_PICKUP_SERVICE_POINT;
import static org.folio.util.LogEventPayloadField.REQUEST_POSITION;
import static org.folio.util.LogEventPayloadField.REQUEST_PREVIOUS_POSITION;
import static org.folio.util.LogEventPayloadField.REQUEST_REASON_FOR_CANCELLATION;
import static org.folio.util.LogEventPayloadField.REQUEST_SERVICE_POINT;
import static org.folio.util.LogEventPayloadField.REQUEST_TYPE;

Expand Down Expand Up @@ -107,14 +106,13 @@ public String buildEditedDescription(JsonObject original, JsonObject updated) {
.trim();
}

public String buildCancelledDescription(JsonObject original, JsonObject updated) {
public String buildCancelledDescription(JsonObject original, String reasonForCancellation) {

StringBuilder description = buildBaseDescription(original);

Optional.ofNullable(getProperty(updated, REQUEST_REASON_FOR_CANCELLATION))
.ifPresent(reason -> description.append("Reason for cancellation: ")
.append(reason)
.append("."));
description.append("Reason for cancellation: ")
.append(reasonForCancellation)
.append(".");

return description.toString()
.trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static java.util.stream.Collectors.toList;
import static org.apache.commons.lang.StringUtils.EMPTY;
import static org.apache.commons.lang3.StringUtils.isEmpty;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import static org.folio.rest.RestVerticle.OKAPI_HEADER_TENANT;
import static org.folio.util.Constants.HOLDINGS_URL;
import static org.folio.util.Constants.ITEMS_URL;
Expand Down Expand Up @@ -34,7 +35,6 @@
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -66,10 +66,13 @@ public abstract class LogRecordBuilder {
public static final String SEARCH_PARAMS = "?limit=%s&offset=%s%s";
public static final String ID = "id";
public static final String USERS = "users";
public static final String CANCELLATION_REASONS = "cancellationReasons";

protected final Map<String, String> okapiHeaders;
protected final Context vertxContext;

public abstract CompletableFuture<List<LogRecord>> buildLogRecord(JsonObject payload);

public LogRecordBuilder(Map<String, String> okapiHeaders, Context vertxContext) {
this.okapiHeaders = okapiHeaders;
this.vertxContext = vertxContext;
Expand Down Expand Up @@ -115,9 +118,9 @@ private CompletableFuture<JsonObject> handleGetRequest(String endpoint) {
* @param ids List of item id's
* @return future with list of item records
*/
public CompletableFuture<List<JsonObject>> getEntitiesByIds(List<String> ids, String key) {
public CompletableFuture<List<JsonObject>> getEntitiesByIds(String url, String key, int limit, int offset, String... ids) {
String query = convertIdsToCqlQuery(ids);
String endpoint = String.format(USERS_URL + SEARCH_PARAMS, 2, 0, buildQuery(query));
String endpoint = String.format(url + SEARCH_PARAMS, limit, offset, buildQuery(query));
return handleGetRequest(endpoint).thenApply(response -> extractEntities(response, key));
}

Expand All @@ -140,9 +143,9 @@ public CompletableFuture<JsonObject> fetchUserDetails(JsonObject payload, String
ofNullable(getProperty(userJson, BARCODE)).ifPresent(barcode -> payload.put(USER_BARCODE.value(), barcode));
}
JsonObject personal = getObjectProperty(userJson, PERSONAL);
if (nonNull(personal)) {
if (nonNull(personal) && nonNull(buildPersonalName(getProperty(personal, FIRST_NAME), getProperty(personal, LAST_NAME)))) {
payload.put(PERSONAL_NAME.value(),
String.format(PERSONAL_NAME_PATTERN, getProperty(personal, LAST_NAME), getProperty(personal, FIRST_NAME)));
buildPersonalName(getProperty(personal, FIRST_NAME), getProperty(personal, LAST_NAME)));
}
}
return CompletableFuture.completedFuture(payload);
Expand Down Expand Up @@ -194,8 +197,8 @@ private String encodeQuery(String query) {
* @param ids list of id's
* @return String representing CQL query to get records by id's
*/
private String convertIdsToCqlQuery(Collection<String> ids) {
return convertIdsToCqlQuery(ids, ID, true);
private String convertIdsToCqlQuery(String... ids) {
return convertIdsToCqlQuery(ID, true, ids);
}

/**
Expand All @@ -206,7 +209,7 @@ private String convertIdsToCqlQuery(Collection<String> ids) {
* @param strictMatch indicates whether strict match mode (i.e. ==) should be used or not (i.e. =)
* @return String representing CQL query to get records by some property values
*/
private String convertIdsToCqlQuery(Collection<String> values, String fieldName, boolean strictMatch) {
private String convertIdsToCqlQuery(String fieldName, boolean strictMatch, String... values) {
String prefix = fieldName + (strictMatch ? "==(" : "=(");
return StreamEx.of(values)
.joining(" or ", prefix, ")");
Expand Down Expand Up @@ -244,13 +247,23 @@ private static JsonObject verifyAndExtractBody(Response response) {
return response.getBody();
}

public abstract CompletableFuture<List<LogRecord>> buildLogRecord(JsonObject payload);

protected LogRecord.Action resolveAction(String actionString) {
try {
return LogRecord.Action.fromValue(actionString);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Builder isn't implemented yet for: " + actionString);
}
}

String buildPersonalName(String firstName, String lastName) {
if (isNotEmpty(firstName) && isNotEmpty(lastName)) {
return lastName + ", " + firstName;
} else if (isEmpty(firstName) && isNotEmpty(lastName)) {
return lastName;
} else if (isNotEmpty(firstName) && isEmpty(lastName)) {
return firstName;
} else {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package org.folio.builder.service;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toMap;
import static org.apache.commons.lang3.StringUtils.isEmpty;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import static org.folio.builder.LogRecordBuilderResolver.MANUAL_BLOCK_CREATED;
import static org.folio.builder.LogRecordBuilderResolver.MANUAL_BLOCK_DELETED;
import static org.folio.builder.LogRecordBuilderResolver.MANUAL_BLOCK_MODIFIED;
import static org.folio.util.Constants.USERS_URL;
import static org.folio.util.JsonPropertyFetcher.getNestedStringProperty;
import static org.folio.util.JsonPropertyFetcher.getObjectProperty;
import static org.folio.util.JsonPropertyFetcher.getProperty;
Expand All @@ -27,14 +25,14 @@
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

import one.util.streamex.StreamEx;
import org.folio.builder.description.ManualBlockDescriptionBuilder;
import org.folio.rest.jaxrs.model.LinkToIds;
import org.folio.rest.jaxrs.model.LogRecord;
import org.folio.util.LogEventPayloadField;

import io.vertx.core.Context;
import io.vertx.core.json.JsonObject;
import one.util.streamex.StreamEx;

public class ManualBlockRecordBuilder extends LogRecordBuilder {
public ManualBlockRecordBuilder(Map<String, String> okapiHeaders, Context vertxContext) {
Expand All @@ -50,7 +48,7 @@ public CompletableFuture<List<LogRecord>> buildLogRecord(JsonObject event) {
String userId = getProperty(payload, USER_ID);
String sourceId = getNestedStringProperty(payload, METADATA, UPDATED_BY_USER_ID);

return getEntitiesByIds(asList(userId, sourceId), USERS).thenCompose(users -> {
return getEntitiesByIds(USERS_URL, USERS, 2, 0, userId, sourceId).thenCompose(users -> {
Map<String, JsonObject> usersGroupedById = StreamEx.of(users)
.collect(toMap(u -> getProperty(u, LogEventPayloadField.ID), Function.identity()));
LogRecord manualBlockLogRecord = buildManualBlockLogRecord(payload, logEventType, userId, sourceId, usersGroupedById);
Expand All @@ -59,7 +57,7 @@ public CompletableFuture<List<LogRecord>> buildLogRecord(JsonObject event) {
}

private LogRecord buildManualBlockLogRecord(JsonObject payload, String logEventType, String userId, String sourceId,
Map<String, JsonObject> usersGroupedById) {
Map<String, JsonObject> usersGroupedById) {
return new LogRecord().withObject(LogRecord.Object.MANUAL_BLOCK)
.withUserBarcode(getProperty(usersGroupedById.get(userId), BARCODE))
.withSource(getSource(logEventType, sourceId, usersGroupedById))
Expand All @@ -76,18 +74,6 @@ private String getSource(String logEventType, String sourceId, Map<String, JsonO
getNestedStringProperty(sourceJson, PERSONAL, LAST_NAME));
}

private String buildPersonalName(String firstName, String lastName) {
if (isNotEmpty(firstName) && isNotEmpty(lastName)) {
return lastName + ", " + firstName;
} else if (isEmpty(firstName) && isNotEmpty(lastName)) {
return lastName;
} else if (isNotEmpty(firstName) && isEmpty(lastName)) {
return firstName;
} else {
return null;
}
}

private LogRecord.Action resolveLogRecordAction(String logEventType) {
if (MANUAL_BLOCK_CREATED.equals(logEventType)) {
return LogRecord.Action.CREATED;
Expand Down
Loading

0 comments on commit 20719a0

Please sign in to comment.