Skip to content

Commit

Permalink
MODBATPRNT-1 Deletion of multiple jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
JanisSaldabols committed Dec 19, 2023
1 parent c6d3161 commit 0060194
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 20 deletions.
12 changes: 12 additions & 0 deletions descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@
"mod-batch-print.print.read"
]
},
{
"methods": [
"DELETE"
],
"pathPattern": "/print/entries",
"permissionsRequired": [
"mod-batch-print.entries.item.delete"
],
"permissionsDesired": [
"mod-batch-print.print.write"
]
},
{
"methods": [
"GET"
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 Down
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
33 changes: 24 additions & 9 deletions src/main/resources/openapi/batchPrint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ paths:
- $ref: headers/okapi-token.yaml
- $ref: headers/okapi-url.yaml
- $ref: headers/okapi-user.yaml
- $ref: parameters/limit.yaml
- $ref: parameters/offset.yaml
- $ref: parameters/query.yaml
get:
description: >
Get batch printing entries with optional CQL query.
X-Okapi-Permissions must include mod-batch-print.print.read
X-Okapi-Permissions must include mod-batch-print.entries.collection.get
parameters:
- $ref: parameters/limit.yaml
- $ref: parameters/offset.yaml
- $ref: parameters/query.yaml
operationId: getPrintEntries
responses:
"200":
Expand All @@ -31,10 +32,24 @@ paths:
$ref: "#/components/responses/trait_404"
"500":
$ref: "#/components/responses/trait_500"
delete:
description: >
Delete batch printing entries by comma separated IDs.
X-Okapi-Permissions must include mod-batch-print.entries.collection.delete
parameters:
- $ref: parameters/ids.yaml
operationId: deletePrintEntries
responses:
"204":
description: Print entries deleted
"400":
$ref: "#/components/responses/trait_400"
"500":
$ref: "#/components/responses/trait_500"
post:
description: >
Create print entry.
X-Okapi-Permissions must include mod-batch-print.print.write
X-Okapi-Permissions must include mod-batch-print.entries.item.post
operationId: postPrintEntry
requestBody:
content:
Expand Down Expand Up @@ -69,7 +84,7 @@ paths:
get:
description: >
Get print entry by id.
X-Okapi-Permissions must include mod-batch-print.print.read
X-Okapi-Permissions must include mod-batch-print.entries.item.get
operationId: getPrintEntry
responses:
"200":
Expand All @@ -89,7 +104,7 @@ paths:
delete:
description: >
Delete print entry.
X-Okapi-Permissions must include mod-batch-print.print.write
X-Okapi-Permissions must include mod-batch-print.entries.item.delete
operationId: deletePrintEntry
responses:
"204":
Expand All @@ -103,7 +118,7 @@ paths:
put:
description: >
Update print entry.
X-Okapi-Permissions must include mod-batch-print.print.write
X-Okapi-Permissions must include mod-batch-print.entries.item.put
operationId: updatePrintEntry
requestBody:
content:
Expand All @@ -129,7 +144,7 @@ paths:
post:
description: >
Send mail to create print entry.
X-Okapi-Permissions must include mod-batch-print.print.write
X-Okapi-Permissions must include mod-batch-print.entries.mail.post
operationId: saveMail
requestBody:
content:
Expand Down
20 changes: 20 additions & 0 deletions src/main/resources/openapi/examples/listResponse.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"items": [
{
"id": "47c62bf9-e225-4a4b-bb61-dc6fc11eed76",
"created": "2023-12-01T13:46:40.193455Z",
"type": "SINGLE",
"sortingField": "[email protected]"
},
{
"id": "80ceb8f2-eae4-4a82-8a7e-23ea9b971480",
"created": "2023-12-12T18:59:35.347757Z",
"type": "SINGLE",
"sortingField": "Saldabols,Janis,[email protected]"
}
],
"resultInfo": {
"totalRecords": 2,
"diagnostics": []
}
}
7 changes: 7 additions & 0 deletions src/main/resources/openapi/examples/printEntry.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"id": "47c62bf9-e225-4a4b-bb61-dc6fc11eed76",
"created": "2023-12-01T13:46:40.193455Z",
"type": "SINGLE",
"sortingField": "[email protected]",
"content": "AABB"
}
7 changes: 0 additions & 7 deletions src/main/resources/openapi/examples/tenantAttributes.sample

This file was deleted.

6 changes: 6 additions & 0 deletions src/main/resources/openapi/parameters/ids.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
in: query
name: ids
description: Comma seperated IDs of items
required: true
schema:
type: string
Loading

0 comments on commit 0060194

Please sign in to comment.