Skip to content

Commit

Permalink
MODBATPRNT-1 Deletion of multiple jobs (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanisSaldabols committed Dec 21, 2023
1 parent 103e99e commit 30e4fb1
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 53 deletions.
76 changes: 44 additions & 32 deletions descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
],
"pathPattern": "/print/entries",
"permissionsRequired": [
"mod-batch-print.entries.item.post"
"batch-print.entries.item.post"
],
"permissionsDesired": [
"mod-batch-print.print.write"
"batch-print.print.write"
]
},
{
Expand All @@ -44,10 +44,22 @@
],
"pathPattern": "/print/entries",
"permissionsRequired": [
"mod-batch-print.entries.collection.get"
"batch-print.entries.collection.get"
],
"permissionsDesired": [
"mod-batch-print.print.read"
"batch-print.print.read"
]
},
{
"methods": [
"DELETE"
],
"pathPattern": "/print/entries",
"permissionsRequired": [
"batch-print.entries.item.delete"
],
"permissionsDesired": [
"batch-print.print.write"
]
},
{
Expand All @@ -56,10 +68,10 @@
],
"pathPattern": "/print/entries/{id}",
"permissionsRequired": [
"mod-batch-print.entries.item.get"
"batch-print.entries.item.get"
],
"permissionsDesired": [
"mod-batch-print.print.read"
"batch-print.print.read"
]
},
{
Expand All @@ -68,10 +80,10 @@
],
"pathPattern": "/print/entries/{id}",
"permissionsRequired": [
"mod-batch-print.entries.item.put"
"batch-print.entries.item.put"
],
"permissionsDesired": [
"mod-batch-print.print.write"
"batch-print.print.write"
]
},
{
Expand All @@ -80,10 +92,10 @@
],
"pathPattern": "/print/entries/{id}",
"permissionsRequired": [
"mod-batch-print.entries.item.delete"
"batch-print.entries.item.delete"
],
"permissionsDesired": [
"mod-batch-print.print.write"
"batch-print.print.write"
]
},
{
Expand All @@ -92,10 +104,10 @@
],
"pathPattern": "/mail",
"permissionsRequired": [
"mod-batch-print.entries.mail.post"
"batch-print.entries.mail.post"
],
"permissionsDesired": [
"mod-batch-print.print.write"
"batch-print.print.write"
]
}
]
Expand All @@ -111,11 +123,11 @@
],
"pathPattern": "/print/batch-creation",
"modulePermissions": [
"mod-batch-print.print.write",
"mod-batch-print.print.read"
"batch-print.print.write",
"batch-print.print.read"
],
"schedule": {
"cron": "1 7 * * *",
"cron": "1 6 * * *",
"zone": "CET"
}
}
Expand All @@ -125,60 +137,60 @@
"requires": [],
"permissionSets": [
{
"permissionName": "mod-batch-print.print.write",
"permissionName": "batch-print.print.write",
"displayName": "batch print - write print entries",
"description": "Write print entries",
"visible": false
},
{
"permissionName": "mod-batch-print.print.read",
"permissionName": "batch-print.print.read",
"displayName": "batch print - read print entries",
"description": "Read print entries",
"visible": false
},
{
"permissionName": "mod-batch-print.entries.mail.post",
"permissionName": "batch-print.entries.mail.post",
"displayName": "batch print - send mail",
"description": "Send mail to print"
},
{
"permissionName": "mod-batch-print.entries.item.post",
"permissionName": "batch-print.entries.item.post",
"displayName": "batch print - create print entry",
"description": "Create print entry"
},
{
"permissionName": "mod-batch-print.entries.item.put",
"permissionName": "batch-print.entries.item.put",
"displayName": "batch print - update print entry",
"description": "Update print entry"
},
{
"permissionName": "mod-batch-print.entries.collection.get",
"permissionName": "batch-print.entries.collection.get",
"displayName": "batch print - get print entries",
"description": "Get batch print"
},
{
"permissionName": "mod-batch-print.entries.item.get",
"permissionName": "batch-print.entries.item.get",
"displayName": "batch print - get print entry",
"description": "Get print entry"
},
{
"permissionName": "mod-batch-print.entries.item.delete",
"permissionName": "batch-print.entries.item.delete",
"displayName": "batch print - delete print entry",
"description": "Delete print entry"
},
{
"permissionName": "mod-batch-print.entries.all",
"permissionName": "batch-print.entries.all",
"displayName": "batch print - all batch print permissions",
"description": "All batch print permissions",
"subPermissions": [
"mod-batch-print.entries.item.post",
"mod-batch-print.entries.collection.get",
"mod-batch-print.entries.item.get",
"mod-batch-print.entries.item.put",
"mod-batch-print.entries.item.delete",
"mod-batch-print.entries.mail.post",
"mod-batch-print.print.write",
"mod-batch-print.print.read"
"batch-print.entries.item.post",
"batch-print.entries.collection.get",
"batch-print.entries.item.get",
"batch-print.entries.item.put",
"batch-print.entries.item.delete",
"batch-print.entries.mail.post",
"batch-print.print.write",
"batch-print.print.read"
]
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ private BatchCreationService() {
*/
public static void process(RoutingContext ctx) {
String tenant = ctx.request().getHeader(XOkapiHeaders.TENANT);
LOGGER.debug("process:: tenant {}", tenant);
PrintStorage printStorage = new PrintStorage(ctx.vertx(), tenant);
LocalDateTime localDateTime = LocalDateTime.now().minusDays(1).minusMinutes(5);
LOGGER.info("process:: tenant {}, from {}", tenant, localDateTime);

printStorage.getEntriesByQuery("type=\"SINGLE\" and created > " + localDateTime
+ " sortby sortingField created", 0, MAX_COUNT_IN_BATCH)
Expand All @@ -41,6 +41,7 @@ public static void process(RoutingContext ctx) {
}

private static void processListAndSaveResult(List<PrintEntry> entries, PrintStorage storage) {
LOGGER.info("processListAndSaveResult:: {} entries will be processed", entries.size());
if (!entries.isEmpty()) {
byte[] merged = PdfService.combinePdfFiles(entries);
PrintEntry batch = new PrintEntry();
Expand All @@ -49,6 +50,8 @@ private static void processListAndSaveResult(List<PrintEntry> entries, PrintStor
batch.setType(PrintEntryType.BATCH);
batch.setContent(Hex.getString(merged));
storage.createEntry(batch);
List<UUID> ids = entries.stream().map(PrintEntry::getId).toList();
storage.deleteEntries(ids);
}
}
}
38 changes: 37 additions & 1 deletion src/main/java/org/folio/print/server/service/PrintService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import io.vertx.ext.web.validation.ValidationHandler;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -76,7 +78,12 @@ private void handlers(RouterBuilder routerBuilder) {
.onFailure(cause -> commonError(ctx, cause))
)
.failureHandler(this::failureHandler);

routerBuilder
.operation("deletePrintEntries")
.handler(ctx -> deletePrintEntries(ctx)
.onFailure(cause -> commonError(ctx, cause))
)
.failureHandler(this::failureHandler);
routerBuilder
.operation("postPrintEntry")
.handler(ctx -> postPrintEntry(ctx)
Expand Down Expand Up @@ -128,10 +135,13 @@ public static PrintStorage create(RoutingContext ctx) {
}

Future<Void> postPrintEntry(RoutingContext ctx) {
log.info("postPrintEntry:: create entry");
PrintStorage storage = create(ctx);
RequestParameters params = ctx.get(ValidationHandler.REQUEST_CONTEXT_KEY);
RequestParameter body = params.body();
PrintEntry entry = body.getJsonObject().mapTo(PrintEntry.class);
log.info("postPrintEntry:: entry with type {}, sorting field{}",
entry.getType(), entry.getSortingField());
return storage.createEntry(entry)
.map(entity -> {
ctx.response().setStatusCode(204);
Expand All @@ -140,7 +150,26 @@ Future<Void> postPrintEntry(RoutingContext ctx) {
});
}

Future<Void> deletePrintEntries(RoutingContext ctx) {
PrintStorage storage = create(ctx);
RequestParameters params = ctx.get(ValidationHandler.REQUEST_CONTEXT_KEY);
RequestParameter idsParameter = params.queryParameter("ids");
String ids = idsParameter != null ? idsParameter.getString() : "";
log.info("deletePrintEntries:: delete entries by ids: {}", ids);
List<UUID> uuids = Arrays.stream(ids.split(","))
.filter(id -> id != null && !id.isBlank())
.map(UUID::fromString)
.toList();
return storage.deleteEntries(uuids)
.map(r -> {
ctx.response().setStatusCode(204);
ctx.response().end();
return null;
});
}

Future<Void> saveMail(RoutingContext ctx) {
log.info("saveMail:: create single entry");
final PrintStorage storage = create(ctx);
RequestParameters params = ctx.get(ValidationHandler.REQUEST_CONTEXT_KEY);
RequestParameter body = params.body();
Expand All @@ -151,6 +180,8 @@ Future<Void> saveMail(RoutingContext ctx) {
entry.setCreated(ZonedDateTime.now().withZoneSameInstant(ZoneOffset.UTC));
entry.setSortingField(message.getTo());
entry.setContent(Hex.getString(PdfService.createPdfFile(message.getBody())));
log.info("saveMail:: entry with type {}, sorting field{}",
entry.getType(), entry.getSortingField());
return storage.createEntry(entry)
.map(entity -> {
ctx.response().setStatusCode(HttpResponseStatus.OK.code());
Expand All @@ -163,6 +194,7 @@ Future<Void> getPrintEntry(RoutingContext ctx) {
PrintStorage storage = create(ctx);
RequestParameters params = ctx.get(ValidationHandler.REQUEST_CONTEXT_KEY);
String id = params.pathParameter("id").getString();
log.info("getPrintEntry:: get single entry by id: {}", id);
return storage.getEntry(UUID.fromString(id))
.map(entity -> {
HttpResponse.responseJson(ctx, 200)
Expand All @@ -175,6 +207,7 @@ Future<Void> deletePrintEntry(RoutingContext ctx) {
PrintStorage printStorage = create(ctx);
RequestParameters params = ctx.get(ValidationHandler.REQUEST_CONTEXT_KEY);
String id = params.pathParameter("id").getString();
log.info("deletePrintEntry:: delete single entry by id: {}", id);
return printStorage.deleteEntry(UUID.fromString(id))
.map(res -> {
ctx.response().setStatusCode(204);
Expand All @@ -189,6 +222,7 @@ Future<Void> updatePrintEntry(RoutingContext ctx) {
RequestParameter body = params.body();
PrintEntry entry = body.getJsonObject().mapTo(PrintEntry.class);
UUID id = UUID.fromString(params.pathParameter("id").getString());
log.info("updatePrintEntry:: update single entry by id: {}", id);
if (!id.equals(entry.getId())) {
return Future.failedFuture(new EntryException("id mismatch"));
}
Expand All @@ -207,6 +241,8 @@ Future<Void> getPrintEntries(RoutingContext ctx) {
String query = queryParameter != null ? queryParameter.getString() : null;
int limit = params.queryParameter("limit").getInteger();
int offset = params.queryParameter("offset").getInteger();
log.info("getPrintEntries:: get entries by query: {}, limit {}, offset {}",
query, limit, offset);
return storage.getEntries(ctx.response(), query, offset, limit);
}

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/org/folio/print/server/storage/PrintStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class PrintStorage {

private static final String CREATE_IF_NO_EXISTS = "CREATE TABLE IF NOT EXISTS ";
private static final String WHERE_BY_ID = " WHERE id = $1";
private static final String WHERE_BY_IDS = " WHERE id in (%s)";

private final TenantPgPool pool;

Expand Down Expand Up @@ -167,6 +168,29 @@ public Future<Void> deleteEntry(UUID id) {
});
}

/**
* Delete print entries by ID list.
*
* @param uuids entry identifiers
* @return async result; exception if not found or forbidden
*/
public Future<Void> deleteEntries(List<UUID> uuids) {
if (uuids.isEmpty()) {
return Future.succeededFuture();
}
Tuple tuple = Tuple.tuple();
StringBuilder replacement = new StringBuilder();
int counter = 1;
for (UUID id : uuids) {
tuple.addUUID(id);
replacement.append((replacement.isEmpty()) ? "$" + counter++ : ", $" + counter++);
}
return pool.preparedQuery(
"DELETE FROM " + printTable + String.format(WHERE_BY_IDS, replacement))
.execute(tuple)
.map(res -> null);
}

/**
* Update print entry.
*
Expand Down
Loading

0 comments on commit 30e4fb1

Please sign in to comment.